qfall_math/rational/mat_q/arithmetic/
neg.rs1use super::super::MatQ;
12use crate::traits::MatrixDimensions;
13use flint3_sys::fmpq_mat_neg;
14use std::ops::Neg;
15
16impl Neg for MatQ {
17 type Output = MatQ;
18
19 fn neg(mut self) -> Self::Output {
37 unsafe { fmpq_mat_neg(&mut self.matrix, &self.matrix) };
38 self
39 }
40}
41
42impl Neg for &MatQ {
43 type Output = MatQ;
44
45 fn neg(self) -> Self::Output {
47 let mut out = MatQ::new(self.get_num_rows(), self.get_num_columns());
48 unsafe { fmpq_mat_neg(&mut out.matrix, &self.matrix) };
49 out
50 }
51}
52
53#[cfg(test)]
54mod test_neg {
55 use super::MatQ;
56 use std::str::FromStr;
57
58 #[test]
60 fn correct_small() {
61 let a = MatQ::from_str("[[1/2, -2, 0],[3, 4/7, -5]]").unwrap();
62 let b = MatQ::from_str("[[-1/2, 2, 0],[-3, -4/7, 5]]").unwrap();
63 let c = MatQ::new(2, 3);
64
65 assert_eq!(a, -(-&a));
66 assert_eq!(a, -&b);
67 assert_eq!(b, -&a);
68 assert_eq!(a, -b.clone());
69 assert_eq!(b, -a);
70 assert_eq!(c, -&c);
71 assert_eq!(c.clone(), -c);
72 }
73
74 #[test]
76 fn correct_large() {
77 let a = MatQ::from_str(&format!("[[{}, 1/{}],[1, 2]]", u64::MAX, i64::MAX)).unwrap();
78 let b = MatQ::from_str(&format!("[[-{}, -1/{}],[-1, -2]]", u64::MAX, i64::MAX)).unwrap();
79
80 assert_eq!(a, -(-&a));
81 assert_eq!(b, -&a);
82 assert_eq!(b, -a);
83 }
84}