Nchimunya Muyuni
Nchimunya Muyuni

Reputation: 1

Implementing Buchberger's algorithm for Gröbner basis in Python using Sympy

Hello community I keep getting the error 'Results check error: During the evaluation of 'groebner_basis' the next error occured: 'int' object has no attribute 'is commutative'.

I am using the sympy library to determine whether two functions form a groebner basis using the sympy library in python. I cannot see where the mistake is and would like some help

from sympy import symbols, div, LT, poly, lcm

def S_polynomial(f1, f2, mo):
    lcm_lt = lcm(f1.LM(order=mo), f2.LM(order=mo))
    term1 = lcm_lt // f1.LM(order=mo)
    term2 = lcm_lt // f2.LM(order=mo)
    # Ensure that term1 and term2 are symbolic expressions
    term1 = term1 * f1.as_expr()
    term2 = term2 * f2.as_expr()
    return poly(term1 - term2, *f1.gens)

def reduce_with_division(f, G, mo):
    while True:
        division_occurred = False
        for g in G:
            ltg = LT(g, order=mo)
            ltf = LT(f, order=mo)
            # Check if ltg is a Poly and divides ltf
            if isinstance(ltg, poly) and ltg.divides(ltf):
                quotient, _ = div(ltf, ltg)
                f = poly(f.as_expr() - quotient * g.as_expr(), *f.gens)
                division_occurred = True
                break
        if not division_occurred:
            return f

def groebner_basis(F, mo):
    G = [poly(f.as_expr(), *f.gens) for f in F]
    pairs = [(i, j) for i in range(len(G)) for j in range(i + 1, len(G))]
    
    while pairs:
        i, j = pairs.pop(0)
        sp = S_polynomial(G[i], G[j], mo)
        sp_reduced = reduce_with_division(sp, G, mo)
        
        if not sp_reduced.is_zero:
            for k in range(len(G)):
                pairs.append((k, len(G)))
            G.append(sp_reduced)

    return G

I tried ensuring all terms are a polynomial by using the poly function

Upvotes: 0

Views: 229

Answers (0)

Related Questions