add S-polynomials

This commit is contained in:
2026-04-22 22:14:49 +02:00
parent 79bf205762
commit 1730ac2fac
3 changed files with 115 additions and 0 deletions

View File

@@ -145,6 +145,61 @@ fn test_mono_div() {
assert_eq!(a.div(&b), Mono { term: vec![] });
}
#[test]
fn test_mono_lcm() {
// lcm(x², xy) = x²y
let a: Mono<StaticVar> = [("x", 2)].into();
let b: Mono<StaticVar> = [("x", 1), ("y", 1)].into();
assert_eq!(a.lcm(&b), Mono::from([("x", 2), ("y", 1)]));
// lcm(x, y) = xy (disjoint)
let a: Mono<StaticVar> = [("x", 1)].into();
let b: Mono<StaticVar> = [("y", 1)].into();
assert_eq!(a.lcm(&b), Mono::from([("x", 1), ("y", 1)]));
// lcm(x², x²) = x² (identical)
let a: Mono<StaticVar> = [("x", 2)].into();
assert_eq!(a.lcm(&a.clone()), a);
}
#[test]
fn test_s_poly() {
// S(x², xy) = 0: S-poly of two monomials always vanishes
let f: Poly<StaticVar> = [(1, [("x", 2)])].into();
let g: Poly<StaticVar> = [(1, [("x", 1), ("y", 1)])].into();
assert!(f.s_poly(&g).is_zero());
// f = x² + y, g = xy + z (lex: LM(f)=x², LM(g)=xy)
// L = x²y, t_f = y, t_g = x, d = gcd(1,1) = 1
// S = y*(x²+y) - x*(xy+z) = x²y + y² - x²y - xz = y² - xz
let f: Poly<StaticVar> = [
(1i32, Mono::from([("x", 2u32)])),
(1i32, Mono::from([("y", 1u32)])),
].into_iter().collect();
let g: Poly<StaticVar> = [
(1i32, Mono::from([("x", 1u32), ("y", 1u32)])),
(1i32, Mono::from([("z", 1u32)])),
].into_iter().collect();
let expected: Poly<StaticVar> = [
(1i32, Mono::from([("y", 2u32)])),
(-1i32, Mono::from([("x", 1u32), ("z", 1u32)])),
].into_iter().collect();
assert_eq!(f.s_poly(&g), expected);
// f = 2x + y, g = 3x + z (same LM=x, d=gcd(2,3)=1)
// S = (3/1)*f - (2/1)*g = 3(2x+y) - 2(3x+z) = 3y - 2z
let f: Poly<StaticVar> = [(2, [("x", 1)]), (1, [("y", 1)])].into();
let g: Poly<StaticVar> = [(3, [("x", 1)]), (1, [("z", 1)])].into();
let expected: Poly<StaticVar> = [(3, [("y", 1)]), (-2, [("z", 1)])].into();
assert_eq!(f.s_poly(&g), expected);
// f = 4x, g = 6x (d = gcd(4,6) = 2)
// S = (6/2)*x*4x - (4/2)*x*6x = 3*4x - 2*6x = 12x - 12x = 0
let f: Poly<StaticVar> = [(4, [("x", 1)])].into();
let g: Poly<StaticVar> = [(6, [("x", 1)])].into();
assert!(f.s_poly(&g).is_zero());
}
fn make_const_poly(c: i32) -> Poly<StaticVar> {
Poly { mono: [(Mono { term: vec![] }, c)].into_iter().collect() }
}