Thie programs is written in Sympy 1.9 and it should find a polynomial
g of degree
dim for a given polynomial
f such that
f o g = g o f as described here, where I already posted program written in Maxima.
This is my first program in SymPy so I am interested in your feedback on this program except on the algorithm itself.
from sympy import symbols, pprint, expand, Poly, IndexedBase, Idx,Eq,solve from sympy.abc import x,f,g,i fg,gf,d=symbols('fg gf d') f=x**3+3*x # a polynomial g exists #f=x**3+4*x # a polynomial g does not exist dim=5 a=IndexedBase('a') i=Idx('i',dim) # create a polynomial of degree dim # with unknown coefficients, # but the coefficient of the highest power is 1 # an the lowest power is 1 g=a+x**dim for i in range(1,dim): g+=x**i*a[i] # calculate fg = f o g # and gf = g o f fg=f gf=g fg=expand(f.subs(x,g)) gf=expand(g.subs(x,f)) # calculate the difference d=(f o g) - (g o f) # and check how to choose the coefficients of g # such that it will become 0 difference=(fg-gf).as_poly(x) candidates=solve(difference.all_coeffs()[:dim],[a[i] for i in range(dim)]) replacements=[(a[i],candidates[i]) for i in range(dim)] if (difference.subs(replacements)==0): pprint(g.subs(replacements)) else: print("no solution exists")