add buchberger algorithm

This commit is contained in:
2026-04-22 22:33:30 +02:00
parent 1730ac2fac
commit e22a45926a
5 changed files with 190 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
use super::flat::{lex_cmp, Mono, Poly};
use super::buchberger::{groebner_basis, is_groebner_basis};
use super::flat::{Mono, Poly, lex_cmp};
use super::var::StaticVar;
#[test]
@@ -175,15 +176,21 @@ fn test_s_poly() {
let f: Poly<StaticVar> = [
(1i32, Mono::from([("x", 2u32)])),
(1i32, Mono::from([("y", 1u32)])),
].into_iter().collect();
]
.into_iter()
.collect();
let g: Poly<StaticVar> = [
(1i32, Mono::from([("x", 1u32), ("y", 1u32)])),
(1i32, Mono::from([("z", 1u32)])),
].into_iter().collect();
]
.into_iter()
.collect();
let expected: Poly<StaticVar> = [
(1i32, Mono::from([("y", 2u32)])),
(-1i32, Mono::from([("x", 1u32), ("z", 1u32)])),
].into_iter().collect();
]
.into_iter()
.collect();
assert_eq!(f.s_poly(&g), expected);
// f = 2x + y, g = 3x + z (same LM=x, d=gcd(2,3)=1)
@@ -201,10 +208,18 @@ fn test_s_poly() {
}
fn make_const_poly(c: i32) -> Poly<StaticVar> {
Poly { mono: [(Mono { term: vec![] }, c)].into_iter().collect() }
Poly {
mono: [(Mono { term: vec![] }, c)].into_iter().collect(),
}
}
fn verify_div_rem(f: Poly<StaticVar>, g: &Poly<StaticVar>, d: u32, q: Poly<StaticVar>, r: Poly<StaticVar>) {
fn verify_div_rem(
f: Poly<StaticVar>,
g: &Poly<StaticVar>,
d: u32,
q: Poly<StaticVar>,
r: Poly<StaticVar>,
) {
// lc(g)^d * f == q * g + r
let (_, lc_g) = g.leading_term_lex().unwrap();
let lhs = lc_g.pow(d) * f;
@@ -228,7 +243,9 @@ fn test_div_rem() {
let f: Poly<StaticVar> = [
(1i32, Mono::from([("x", 3u32)])),
(1i32, Mono::from([("x", 2u32), ("y", 1u32)])),
].into_iter().collect();
]
.into_iter()
.collect();
let g: Poly<StaticVar> = [(1, [("x", 2)])].into();
let expected_q: Poly<StaticVar> = [(1, [("x", 1)]), (1, [("y", 1)])].into();
let (d, q, r) = f.clone().div_rem(&g);
@@ -273,7 +290,9 @@ fn test_div_rem() {
let f: Poly<StaticVar> = [
(1i32, Mono::from([("x", 2u32)])),
(1i32, Mono::from([("x", 1u32), ("y", 1u32)])),
].into_iter().collect();
]
.into_iter()
.collect();
let g: Poly<StaticVar> = [(1, [("x", 1)]), (1, [("y", 1)])].into();
let expected_q: Poly<StaticVar> = [(1, [("x", 1)])].into();
let (d, q, r) = f.clone().div_rem(&g);
@@ -281,3 +300,80 @@ fn test_div_rem() {
assert!(r.is_zero());
verify_div_rem(f, &g, d, q, r);
}
#[test]
fn test_groebner() {
// Ideal (x²) — already a Gröbner basis
let f: Poly<StaticVar> = [(1, [("x", 2)])].into();
let _basis = groebner_basis(vec![f]);
// Ideal (x³ - x², x² - x): gcd is x² - x
let f: Poly<StaticVar> = [
(1i32, Mono::from([("x", 3u32)])),
(-1i32, Mono::from([("x", 2u32)])),
]
.into_iter()
.collect();
let g: Poly<StaticVar> = [
(1i32, Mono::from([("x", 2u32)])),
(-1i32, Mono::from([("x", 1u32)])),
]
.into_iter()
.collect();
let basis = groebner_basis(vec![f, g]);
assert!(is_groebner_basis(&basis));
// Classic example: I = (x²y - x, xy² - y)
let f: Poly<StaticVar> = [
(1i32, Mono::from([("x", 2u32), ("y", 1u32)])),
(-1i32, Mono::from([("x", 1u32)])),
]
.into_iter()
.collect();
let g: Poly<StaticVar> = [
(1i32, Mono::from([("x", 1u32), ("y", 2u32)])),
(-1i32, Mono::from([("y", 1u32)])),
]
.into_iter()
.collect();
let basis = groebner_basis(vec![f, g]);
assert!(is_groebner_basis(&basis));
}
#[test]
fn test_is_groebner_basis() {
// {x} is a GB: only one element, no pairs to check.
let f: Poly<StaticVar> = [(1, [("x", 1)])].into();
assert!(is_groebner_basis(&[f]));
// {x, y} is a GB: S(x, y) = y*x - x*y = 0.
let x: Poly<StaticVar> = [(1, [("x", 1)])].into();
let y: Poly<StaticVar> = [(1, [("y", 1)])].into();
assert!(is_groebner_basis(&[x, y]));
// {x² + y, xy} is NOT a GB:
// S(x²+y, xy) = y*(x²+y) - x*(xy) = y² ≠ 0 mod the set.
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)]))]
.into_iter()
.collect();
assert!(!is_groebner_basis(&[f, g]));
// After running groebner_basis, the result must always pass.
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)]))]
.into_iter()
.collect();
let basis = groebner_basis(vec![f, g]);
assert!(is_groebner_basis(&basis));
}