basic/
basic.rs

1extern crate groebner;
2use groebner::{groebner_basis, Monomial, MonomialOrder, Polynomial, Term};
3use num_rational::BigRational;
4
5fn main() {
6    // Example: Compute a Groebner basis for the ideal (x^2 + y^2 - 1, x - y)
7    // Variables: x = 0, y = 1
8    let nvars = 2;
9    let order = MonomialOrder::Lex;
10
11    // x^2 + y^2 - 1
12    let f = Polynomial::new(
13        vec![
14            Term::new(
15                BigRational::new(1.into(), 1.into()),
16                Monomial::new(vec![2, 0]),
17            ), // x^2
18            Term::new(
19                BigRational::new(1.into(), 1.into()),
20                Monomial::new(vec![0, 2]),
21            ), // y^2
22            Term::new(
23                BigRational::new((-1).into(), 1.into()),
24                Monomial::new(vec![0, 0]),
25            ), // -1
26        ],
27        nvars,
28        order,
29    );
30
31    // x - y
32    let g = Polynomial::new(
33        vec![
34            Term::new(
35                BigRational::new(1.into(), 1.into()),
36                Monomial::new(vec![1, 0]),
37            ), // x
38            Term::new(
39                BigRational::new((-1).into(), 1.into()),
40                Monomial::new(vec![0, 1]),
41            ), // -y
42        ],
43        nvars,
44        order,
45    );
46
47    // Compute the Groebner basis
48    match groebner_basis(vec![f, g], order, true) {
49        Ok(basis) => {
50            println!("Groebner basis:");
51            for (i, poly) in basis.iter().enumerate() {
52                println!("g{}: {}", i + 1, poly);
53            }
54        }
55        Err(e) => {
56            println!("Error computing Groebner basis: {e}");
57        }
58    }
59}