def_incr_k(m,k):returntuple(list(m[:k])+[m[k]+1]+list(m[k+1:]))def_identity_matrix(n,domain):M=[[domain.zero]*nfor_inxrange(n)]foriinxrange(n):M[i][i]=domain.onereturnMdef_matrix_mul(M,v):return[sum([row[i]*v[i]foriinxrange(len(v))])forrowinM]def_update(s,_lambda,P):""" Update ``P`` such that for the updated `P'` `P' v = e_{s}`. """k=min([jforjinxrange(s,len(_lambda))if_lambda[j]!=0])forrinxrange(len(_lambda)):ifr!=k:P[r]=[P[r][j]-(P[k][j]*_lambda[r])/_lambda[k]forjinxrange(len(P[r]))]P[k]=[P[k][j]/_lambda[k]forjinxrange(len(P[k]))]P[k],P[s]=P[s],P[k]returnPdef_representing_matrices(basis,G,ring):""" Compute the matrices corresponding to the linear maps `m \mapsto x_i m` for all variables `x_i`. """domain=ring.domainu=ring.ngens-1defvar(i):returntuple([0]*i+[1]+[0]*(u-i))defrepresenting_matrix(m):M=[[domain.zero]*len(basis)for_inxrange(len(basis))]fori,vinenumerate(basis):r=ring.term_new(monomial_mul(m,v),domain.one).rem(G)formonom,coeffinr.terms():j=basis.index(monom)M[j][i]=coeffreturnMreturn[representing_matrix(var(i))foriinxrange(u+1)]def_basis(G,ring):""" Computes a list of monomials which are not divisible by the leading monomials wrt to ``O`` of ``G``. These monomials are a basis of `K[X_1, \ldots, X_n]/(G)`. """order=ring.orderleading_monomials=[g.LMforginG]candidates=[ring.zero_monom]basis=[]whilecandidates:t=candidates.pop()basis.append(t)new_candidates=[_incr_k(t,k)forkinxrange(ring.ngens)ifall(monomial_div(_incr_k(t,k),lmg)isNoneforlmginleading_monomials)]candidates.extend(new_candidates)candidates.sort(key=lambdam:order(m),reverse=True)basis=list(set(basis))returnsorted(basis,key=lambdam:order(m))