qfall_math/integer_mod_q/mat_polynomial_ring_zq/arithmetic/
neg.rs1use super::super::MatPolynomialRingZq;
12use flint3_sys::fmpz_poly_mat_neg;
13use std::ops::Neg;
14
15impl Neg for MatPolynomialRingZq {
16 type Output = MatPolynomialRingZq;
17
18 fn neg(mut self) -> Self::Output {
39 unsafe { fmpz_poly_mat_neg(&mut self.matrix.matrix, &self.matrix.matrix) };
40 self.reduce();
41 self
42 }
43}
44
45impl Neg for &MatPolynomialRingZq {
46 type Output = MatPolynomialRingZq;
47
48 fn neg(self) -> Self::Output {
50 let mut out = MatPolynomialRingZq {
51 matrix: -&self.matrix,
52 modulus: self.modulus.clone(),
53 };
54 out.reduce();
55 out
56 }
57}
58
59#[cfg(test)]
60mod test_neg {
61 use super::MatPolynomialRingZq;
62 use crate::integer::MatPolyOverZ;
63 use crate::integer_mod_q::ModulusPolynomialRingZq;
64 use std::str::FromStr;
65
66 #[test]
68 fn correct_small() {
69 let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
70 let mat_a = MatPolyOverZ::from_str("[[2 1 -2, 0],[1 -3, 3 1 0 4]]").unwrap();
71 let mat_b = MatPolyOverZ::from_str("[[2 -1 2, 0],[1 3, 3 -1 0 -4]]").unwrap();
72 let a = MatPolynomialRingZq::from((&mat_a, &modulus));
73 let b = MatPolynomialRingZq::from((&mat_b, &modulus));
74 let c = MatPolynomialRingZq::new(2, 2, &modulus);
75
76 assert_eq!(a, -(-&a));
77 assert_eq!(a, -&b);
78 assert_eq!(b, -&a);
79 assert_eq!(a, -b.clone());
80 assert_eq!(b, -a);
81 assert_eq!(c, -&c);
82 assert_eq!(c.clone(), -c);
83 }
84
85 #[test]
87 fn correct_large() {
88 let modulus =
89 ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
90 let mat_a = MatPolyOverZ::from_str(&format!("[[2 {} 1],[0]]", i64::MAX)).unwrap();
91 let mat_b = MatPolyOverZ::from_str(&format!("[[2 -{} -1],[0]]", i64::MAX)).unwrap();
92 let a = MatPolynomialRingZq::from((&mat_a, &modulus));
93 let b = MatPolynomialRingZq::from((&mat_b, &modulus));
94
95 assert_eq!(a, -(-&a));
96 assert_eq!(b, -&a);
97 assert_eq!(b, -a);
98 }
99
100 #[test]
103 fn reduced() {
104 let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
105 let mat_a = MatPolyOverZ::from_str("[[2 1 16, 0],[1 8, 3 1 0 4]]").unwrap();
106 let a = MatPolynomialRingZq::from((&mat_a, &modulus));
107 let cmp = MatPolyOverZ::from_str("[[2 16 1, 0],[1 9, 3 16 0 13]]").unwrap();
108
109 let neg_a = -&a;
110
111 assert_eq!(cmp, neg_a.get_representative_least_nonnegative_residue());
112 assert_eq!(cmp, (-a).get_representative_least_nonnegative_residue());
113 assert_eq!(modulus, neg_a.get_mod());
114 }
115}