use super::MatZq;
use crate::{
error::MathError,
integer::{MatZ, Z},
integer_mod_q::Zq,
macros::compare_base::{compare_base_default, compare_base_get_mod, compare_base_impl},
traits::CompareBase,
};
use flint_sys::{fmpz::fmpz_equal, fmpz_mat::fmpz_mat_equal};
impl PartialEq for MatZq {
fn eq(&self, other: &Self) -> bool {
unsafe {
fmpz_equal(&self.matrix.mod_[0], &other.matrix.mod_[0]) != 0
&& fmpz_mat_equal(&self.matrix.mat[0], &other.matrix.mat[0]) != 0
}
}
}
compare_base_get_mod!(MatZq for MatZq Zq);
compare_base_default!(MatZq for MatZ);
impl<Integer: Into<Z>> CompareBase<Integer> for MatZq {}
impl Eq for MatZq {}
#[cfg(test)]
mod test_partial_eq {
use super::MatZq;
use crate::traits::MatrixSetEntry;
use std::str::FromStr;
#[test]
fn equality_between_instantiations() {
let a = MatZq::from_str("[[0, 1],[0, 0]] mod 4").unwrap();
let mut b = MatZq::new(2, 2, 4);
b.set_entry(0, 1, 1).unwrap();
assert_eq!(a, b);
}
#[test]
fn equality_for_large_and_small_entries() {
let mat_str_1 = format!(
"[[{}, {}, 1],[-10, 10, 0],[0, 1, -10]] mod {}",
i64::MAX - 1,
i64::MAX,
u64::MAX
);
let mat_str_2 = format!(
"[[{}, {}, 1],[-10, 10, 0],[{}, 1, -10]] mod {}",
i64::MAX - 1,
i64::MAX,
u64::MAX,
u64::MAX
);
let a = MatZq::from_str(&mat_str_1).unwrap();
let b = MatZq::from_str(&mat_str_1).unwrap();
let c = MatZq::from_str(&mat_str_2).unwrap();
assert_eq!(&a, &b);
assert_eq!(&a, &c);
}
#[test]
fn not_equal_same_modulus() {
let a =
MatZq::from_str(&format!("[[{}, {}],[-10, 10]] mod 42", i64::MIN, i64::MAX)).unwrap();
let b = MatZq::from_str(&format!("[[0, {}],[-10, 10]] mod 42", i64::MAX)).unwrap();
let c = MatZq::from_str(&format!(
"[[{}, {}],[-10, 10],[0, 0]] mod 42",
i64::MIN,
i64::MAX
))
.unwrap();
let d = MatZq::from_str(&format!("[[{}, {}]] mod 42", i64::MIN, i64::MAX)).unwrap();
let e = MatZq::from_str("[[0]] mod 42").unwrap();
assert_ne!(&a, &b);
assert_ne!(&a, &c);
assert_ne!(&a, &d);
assert_ne!(&a, &e);
assert_ne!(&b, &c);
assert_ne!(&b, &d);
assert_ne!(&b, &e);
assert_ne!(&c, &d);
assert_ne!(&c, &e);
assert_ne!(&d, &e);
}
#[test]
fn not_equal_different_modulus() {
let a = MatZq::from_str("[[0, 1],[0, 0]] mod 4").unwrap();
let b = MatZq::from_str("[[0, 1],[0, 0]] mod 8").unwrap();
let c = MatZq::from_str(&format!("[[0]] mod {}", u64::MAX)).unwrap();
let d = MatZq::from_str(&format!("[[0]] mod {}", u64::MAX - 1)).unwrap();
let e = MatZq::from_str(&format!("[[0]] mod {}", c.matrix.mod_[0].0 as u64)).unwrap();
assert_ne!(a, b);
assert_ne!(c, d);
assert_ne!(c, e);
}
}
#[cfg(test)]
mod test_compare_base {
use crate::{
integer::{MatZ, Z},
integer_mod_q::{MatZq, Zq},
traits::CompareBase,
};
#[test]
fn availability_without_comparisons() {
let one_1 = MatZq::new(3, 4, 17);
assert!(one_1.compare_base(&MatZ::new(1, 1)));
assert!(one_1.compare_base(&Z::ONE));
assert!(one_1.compare_base(&0_i8));
assert!(one_1.compare_base(&0_i16));
assert!(one_1.compare_base(&0_i32));
assert!(one_1.compare_base(&0_i64));
assert!(one_1.compare_base(&0_u8));
assert!(one_1.compare_base(&0_u16));
assert!(one_1.compare_base(&0_u32));
assert!(one_1.compare_base(&0_u64));
assert!(one_1.call_compare_base_error(&MatZ::new(1, 1)).is_none());
assert!(one_1.call_compare_base_error(&Z::ONE).is_none());
assert!(one_1.call_compare_base_error(&0_i8).is_none());
assert!(one_1.call_compare_base_error(&0_i16).is_none());
assert!(one_1.call_compare_base_error(&0_i32).is_none());
assert!(one_1.call_compare_base_error(&0_i64).is_none());
assert!(one_1.call_compare_base_error(&0_u8).is_none());
assert!(one_1.call_compare_base_error(&0_u16).is_none());
assert!(one_1.call_compare_base_error(&0_u32).is_none());
assert!(one_1.call_compare_base_error(&0_u64).is_none());
}
#[test]
fn availability_with_comparisons() {
let one_1 = MatZq::new(3, 4, 17);
assert!(one_1.compare_base(&one_1));
assert!(one_1.compare_base(&Zq::from((3, 17))));
assert!(!one_1.compare_base(&Zq::from((3, 18))));
assert!(one_1.compare_base(&MatZq::new(1, 1, 17)));
assert!(!one_1.compare_base(&MatZq::new(1, 1, 18)));
assert!(one_1.call_compare_base_error(&Zq::from((3, 18))).is_some());
assert!(
one_1
.call_compare_base_error(&MatZq::new(1, 1, 18))
.is_some()
);
}
}