qfall_math/integer_mod_q/polynomial_ring_zq/arithmetic/
neg.rs1use super::super::PolynomialRingZq;
12use flint3_sys::fmpz_poly_neg;
13use std::ops::Neg;
14
15impl Neg for PolynomialRingZq {
16 type Output = PolynomialRingZq;
17
18 fn neg(mut self) -> Self::Output {
39 unsafe { fmpz_poly_neg(&mut self.poly.poly, &self.poly.poly) };
40 self.reduce();
41 self
42 }
43}
44
45impl Neg for &PolynomialRingZq {
46 type Output = PolynomialRingZq;
47
48 fn neg(self) -> Self::Output {
50 let mut out = PolynomialRingZq {
51 poly: -&self.poly,
52 modulus: self.modulus.clone(),
53 };
54 out.reduce();
55 out
56 }
57}
58
59#[cfg(test)]
60mod test_neg {
61 use super::PolynomialRingZq;
62 use crate::integer::PolyOverZ;
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 a = PolynomialRingZq::from((&PolyOverZ::from_str("3 1 -2 3").unwrap(), &modulus));
71 let b = PolynomialRingZq::from((&PolyOverZ::from_str("3 -1 2 -3").unwrap(), &modulus));
72 let c = PolynomialRingZq::from((&PolyOverZ::default(), &modulus));
73
74 assert_eq!(a, -(-&a));
75 assert_eq!(a, -&b);
76 assert_eq!(b, -&a);
77 assert_eq!(a, -b.clone());
78 assert_eq!(b, -a);
79 assert_eq!(c, -&c);
80 assert_eq!(c.clone(), -c);
81 }
82
83 #[test]
85 fn correct_large() {
86 let modulus =
87 ModulusPolynomialRingZq::from_str(&format!("4 1 0 0 1 mod {}", u64::MAX)).unwrap();
88 let a = PolynomialRingZq::from((
89 &PolyOverZ::from_str(&format!("3 {} 0 1", i64::MAX)).unwrap(),
90 &modulus,
91 ));
92 let b = PolynomialRingZq::from((
93 &PolyOverZ::from_str(&format!("3 -{} 0 -1", i64::MAX)).unwrap(),
94 &modulus,
95 ));
96
97 assert_eq!(a, -(-&a));
98 assert_eq!(b, -&a);
99 assert_eq!(b, -a);
100 }
101
102 #[test]
105 fn reduced() {
106 let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 17").unwrap();
107 let a = PolynomialRingZq::from((&PolyOverZ::from_str("3 1 16 8").unwrap(), &modulus));
108 let cmp = PolyOverZ::from_str("3 16 1 9").unwrap();
109
110 let neg_a = -&a;
111
112 assert_eq!(cmp, neg_a.get_representative_least_nonnegative_residue());
113 assert_eq!(cmp, (-a).get_representative_least_nonnegative_residue());
114 assert_eq!(modulus, neg_a.get_mod());
115 }
116}