Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ mod z;
pub use mat_poly_over_z::MatPolyOverZ;
pub use mat_z::MatZ;
pub use poly_over_z::PolyOverZ;
pub(crate) use poly_over_z::fmpz_poly_helpers;
pub use z::Z;
pub(crate) use z::fmpz_helpers;
4 changes: 2 additions & 2 deletions src/integer/mat_poly_over_z/arithmetic/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Mul<&MatPolynomialRingZq> for &MatPolyOverZ {
/// use std::str::FromStr;
///
/// let mat_1 = MatPolyOverZ::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]]").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 3 mod 17").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 1 mod 17").unwrap();
///
/// let mat_3 = &mat_1 * &mat_2;
/// ```
Expand Down Expand Up @@ -151,7 +151,7 @@ impl MatPolyOverZ {
/// use std::str::FromStr;
///
/// let mat_1 = MatPolyOverZ::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]]").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 3 mod 17").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 1 mod 17").unwrap();
///
/// let mat_3 = &mat_1.mul_mat_poly_ring_zq_safe(&mat_2).unwrap();
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/integer/mat_poly_over_z/arithmetic/mul_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ mod test_mul_poly_ring_zq {
/// Checks if scalar multiplication reduction works.
#[test]
fn reduction_correct() {
let modulus = ModulusPolynomialRingZq::from_str("4 2 0 0 2 mod 17").unwrap();
let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
let poly_mat1 = MatPolyOverZ::from_str("[[0, 1 10],[0, 2 1 2]]").unwrap();
let poly = PolyOverZ::from(2);
let poly_ring = PolynomialRingZq::from((&poly, &modulus));
Expand Down
4 changes: 2 additions & 2 deletions src/integer/mat_poly_over_z/arithmetic/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Sub<&MatPolynomialRingZq> for &MatPolyOverZ {
/// use std::str::FromStr;
///
/// let mat_1 = MatPolyOverZ::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]]").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 3 mod 17").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 1 mod 17").unwrap();
///
/// let mat_3 = &mat_1 - &mat_2;
/// ```
Expand Down Expand Up @@ -192,7 +192,7 @@ impl MatPolyOverZ {
/// use std::str::FromStr;
///
/// let mat_1 = MatPolyOverZ::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]]").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 3 mod 17").unwrap();
/// let mat_2 = MatPolynomialRingZq::from_str("[[2 1 42, 1 17],[1 8, 2 5 6]] / 3 1 2 1 mod 17").unwrap();
///
/// let mat_3 = &mat_1.sub_mat_poly_ring_zq_safe(&mat_2).unwrap();
/// ```
Expand Down
13 changes: 12 additions & 1 deletion src/integer/mat_poly_over_z/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ impl MatPolyOverZ {
for j in 0..self.get_num_columns() {
unsafe {
let entry = fmpz_poly_mat_entry(&self.matrix, i, j);
reduce_fmpz_poly_by_poly_over_z(entry, modulus);
if (*entry).length > modulus.get_degree() {
reduce_fmpz_poly_by_poly_over_z(&mut *entry, modulus);
}
}
}
}
Expand Down Expand Up @@ -94,4 +96,13 @@ mod test_reduce_by_poly {
let cmp_mat = MatPolyOverZ::from_str("[[1 1, 1 1],[1 -1, 0]]").unwrap();
assert_eq!(cmp_mat, a);
}

/// Ensures that the zero polynomial does not cause problems.
#[test]
fn zero_polynomial() {
let mut a = MatPolyOverZ::from_str("[[0, 0]]").unwrap();
let modulus = PolyOverZ::from_str(&format!("2 {} 1", u64::MAX)).unwrap();

a.reduce_by_poly(&modulus);
}
}
18 changes: 5 additions & 13 deletions src/integer/poly_over_z/arithmetic/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::{
};
use flint_sys::{
fmpq_poly::fmpq_poly_sub, fmpz_mod_poly::fmpz_mod_poly_sub, fmpz_poly::fmpz_poly_sub,
fq::fq_sub,
};
use std::ops::{Sub, SubAssign};

Expand Down Expand Up @@ -148,15 +147,8 @@ impl Sub<&PolynomialRingZq> for &PolyOverZ {
/// let c: PolynomialRingZq = &b - &a;
/// ```
fn sub(self, other: &PolynomialRingZq) -> Self::Output {
let mut out = PolynomialRingZq::from((&PolyOverZ::default(), &other.modulus));
unsafe {
fq_sub(
&mut out.poly.poly,
&self.poly,
&other.poly.poly,
other.modulus.get_fq_ctx(),
);
}
let mut out = PolynomialRingZq::from((self, &other.modulus));
out -= other;
out
}
}
Expand Down Expand Up @@ -352,10 +344,10 @@ mod test_sub_poly_ring_zq {
#[test]
fn borrowed_correctness() {
let poly_1 =
PolynomialRingZq::from_str(&format!("2 2 {} / 4 1 2 3 4 mod {}", i64::MAX, u64::MAX))
PolynomialRingZq::from_str(&format!("2 2 {} / 4 1 2 3 1 mod {}", i64::MAX, u64::MAX))
.unwrap();
let poly_2 = PolynomialRingZq::from_str(&format!(
"2 -1 -{} / 4 1 2 3 4 mod {}",
"2 -1 -{} / 4 1 2 3 1 mod {}",
i64::MAX as u64 - 2,
u64::MAX
))
Expand All @@ -370,7 +362,7 @@ mod test_sub_poly_ring_zq {
/// Checks if subtraction works fine for different types
#[test]
fn availability() {
let poly = PolynomialRingZq::from_str("3 1 2 3 / 4 1 2 3 4 mod 17").unwrap();
let poly = PolynomialRingZq::from_str("3 1 2 3 / 4 1 2 3 1 mod 17").unwrap();
let z = PolyOverZ::from(2);

_ = z.clone() - poly.clone();
Expand Down
Loading
Loading