use crate::{Algebra, DivisionAlgebra, Field, Group, Ring};
pub fn assert_iso_from_round_trip<S, T>(s: S, t: T)
where
S: From<T> + Clone + PartialEq + core::fmt::Debug,
T: From<S> + Clone + PartialEq + core::fmt::Debug,
{
let t_from_s: T = T::from(s.clone());
let s_back: S = S::from(t_from_s);
assert_eq!(
s, s_back,
"From round-trip S -> T -> S failed: original {:?} differs from S::from(T::from(original))",
s
);
let s_from_t: S = S::from(t.clone());
let t_back: T = T::from(s_from_t);
assert_eq!(
t, t_back,
"From round-trip T -> S -> T failed: original {:?} differs from T::from(S::from(original))",
t
);
}
pub fn assert_group_iso_from_law<S, T>(a: S, b: S)
where
S: Group + From<T> + Clone + PartialEq + core::fmt::Debug,
T: Group + From<S> + Clone + PartialEq + core::fmt::Debug,
{
let lhs: T = T::from(a.clone() + b.clone());
let rhs: T = T::from(a) + T::from(b);
assert_eq!(
lhs, rhs,
"GroupIso homomorphism failed: T::from(a + b) != T::from(a) + T::from(b)"
);
}
pub fn assert_ring_iso_from_laws<S, T>(a: S, b: S)
where
S: Ring + From<T> + Clone + PartialEq + core::fmt::Debug,
T: Ring + From<S> + Clone + PartialEq + core::fmt::Debug,
{
let lhs_add: T = T::from(a.clone() + b.clone());
let rhs_add: T = T::from(a.clone()) + T::from(b.clone());
assert_eq!(
lhs_add, rhs_add,
"RingIso addition homomorphism failed: T::from(a + b) != T::from(a) + T::from(b)"
);
let lhs_mul: T = T::from(a.clone() * b.clone());
let rhs_mul: T = T::from(a) * T::from(b);
assert_eq!(
lhs_mul, rhs_mul,
"RingIso multiplication homomorphism failed: T::from(a * b) != T::from(a) * T::from(b)"
);
}
pub fn assert_field_iso_from_laws<S, T>(a: S)
where
S: Field + From<T> + Clone + PartialEq + core::fmt::Debug,
T: Field + From<S> + Clone + PartialEq + core::fmt::Debug,
{
let inv_s: S = a.clone().inverse();
let from_inv: T = T::from(inv_s);
let inv_from: T = T::from(a).inverse();
assert_eq!(
from_inv, inv_from,
"FieldIso multiplicative-inverse homomorphism failed: T::from(a.inverse()) != T::from(a).inverse()"
);
}
pub fn assert_algebra_iso_from_law<S, T, R>(a: S, r: R)
where
R: Ring + Clone,
S: Algebra<R> + From<T> + Clone + PartialEq + core::fmt::Debug,
T: Algebra<R> + From<S> + Clone + PartialEq + core::fmt::Debug,
{
let scaled_s: S = a.clone().scale(r.clone());
let from_scaled: T = T::from(scaled_s);
let scale_from: T = T::from(a).scale(r);
assert_eq!(
from_scaled, scale_from,
"AlgebraIso scalar-multiplication homomorphism failed: T::from(a.scale(r)) != T::from(a).scale(r)"
);
}
pub fn assert_division_algebra_iso_from_law<S, T, R>(a: S)
where
R: Field + Clone,
S: DivisionAlgebra<R> + From<T> + Clone + PartialEq + core::fmt::Debug,
T: DivisionAlgebra<R> + From<S> + Clone + PartialEq + core::fmt::Debug,
{
let conj_s: S = a.clone().conjugate();
let from_conj: T = T::from(conj_s);
let conj_from: T = T::from(a).conjugate();
assert_eq!(
from_conj, conj_from,
"DivisionAlgebraIso conjugation homomorphism failed: T::from(a.conjugate()) != T::from(a).conjugate()"
);
}