check reduced grobner basis
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use super::buchberger::{groebner_basis, is_groebner_basis};
|
||||
use super::flat::{Mono, Poly, lex_cmp};
|
||||
use super::ideal::{Generators, GroebnerBasis, Ideal};
|
||||
use super::var::StaticVar;
|
||||
|
||||
#[test]
|
||||
@@ -377,3 +378,87 @@ fn test_is_groebner_basis() {
|
||||
let basis = groebner_basis(vec![f, g]);
|
||||
assert!(is_groebner_basis(&basis));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ideal() {
|
||||
// Construction from Vec and iterator
|
||||
let f: Poly<StaticVar> = [(1, [("x", 2)])].into();
|
||||
let g: Poly<StaticVar> = [(1, [("y", 1)])].into();
|
||||
let ideal = Ideal::new(vec![f.clone(), g.clone()]);
|
||||
assert_eq!(ideal.generators().len(), 2);
|
||||
let ideal: Ideal<StaticVar, Generators> = [f.clone(), g.clone()].into_iter().collect();
|
||||
assert_eq!(ideal.generators().len(), 2);
|
||||
|
||||
// Construction via From / .into()
|
||||
let ideal: Ideal<StaticVar, Generators> = vec![f.clone(), g.clone()].into();
|
||||
assert_eq!(ideal.generators().len(), 2);
|
||||
|
||||
// Display: <x², y>
|
||||
let ideal = Ideal::new(vec![f, g]);
|
||||
assert_eq!(ideal.to_string(), "<x\u{00B2}, y>");
|
||||
|
||||
// groebner_basis transitions state and result satisfies the criterion
|
||||
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 ideal: Ideal<StaticVar, Generators> = [f, g].into_iter().collect();
|
||||
let gb: Ideal<StaticVar, GroebnerBasis> = ideal.groebner_basis();
|
||||
assert!(is_groebner_basis(gb.generators()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_groebner_sagemath() {
|
||||
// I = (x³ - 2xy, x²y - 2y² + x) ⊆ k[x, y]
|
||||
// grobner basis: {4y³, x - 2y²}
|
||||
|
||||
// f1 = x³ - 2xy
|
||||
let f1: Poly<StaticVar> = [
|
||||
(1i32, Mono::from([("x", 3u32)])),
|
||||
(-2i32, Mono::from([("x", 1u32), ("y", 1u32)])),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
// f2 = x²y - 2y² + x
|
||||
let f2: Poly<StaticVar> = [
|
||||
(1i32, Mono::from([("x", 2u32), ("y", 1u32)])),
|
||||
(-2i32, Mono::from([("y", 2u32)])),
|
||||
(1i32, Mono::from([("x", 1u32)])),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
let gb = Ideal::new(vec![f1.clone(), f2.clone()]).groebner_basis();
|
||||
|
||||
assert!(is_groebner_basis(gb.generators()));
|
||||
assert_eq!(gb.generators().len(), 2);
|
||||
|
||||
// -x + 2y²
|
||||
let neg_x_plus_2y2: Poly<StaticVar> = [
|
||||
(-1i32, Mono::from([("x", 1u32)])),
|
||||
(2i32, Mono::from([("y", 2u32)])),
|
||||
]
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
// -4y³
|
||||
let neg_4y3: Poly<StaticVar> = [(-4i32, Mono::from([("y", 3u32)]))].into_iter().collect();
|
||||
|
||||
let expected = [neg_x_plus_2y2, neg_4y3];
|
||||
for e in &expected {
|
||||
assert!(
|
||||
gb.generators()
|
||||
.iter()
|
||||
.any(|a| *a == *e || *a == -1i32 * e.clone())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user