Ask Your Question

Revision history [back]

How can I easily create and handle polynomials with symbolic coefficients?

I would like to create polynomial functions with symbolic (yet unknown) coefficients, like:

p(x)=a*x^5 + b*x^4 + c*x^3 + d*x^2+e*x+f

Then I would like to experiment with the degree and thus the number of coefficients, for example:

p(x)=a*x^7 + b*x^6 + c*x^5 + d*x^4+e*x^3+f*x^2+g*x+h

The problem is I always have to explicitely declare the coefficients, as variables, like:

var("a b c d e f g h")

So I should always have to enumerate them by hand.

Isn't exist some automatism to do this? For example:

Polynomial(x,degree=7)

and it would return

c0*x^7 + c1*x^6 + c2*x^5 + c3*x^4+c4*x^3+c5*x^2+c6*x+c7

Sometimes I also would like to query the coefficients, to solve an equation system, which contains those coefficients:

p(x)=a*x^7 + b*x^6 + c*x^5 + d*x^4+e*x^3+f*x^2+g*x+h
eq=[
derivative(p(x),x).subs(x=0)==0,
derivative(p(x),x,2).subs(x=0)==0,
p(x=0)==0,
p(x=x0)==1,
p(x=1)==1+z,
derivative(p(x),x).subs(x=1)==0,
derivative(p(x),x,2).subs(x=1)==0,
derivative(p(x),x).subs(x=x0)==1
]
s=solve(eq,a,b,c,d,e,f,g,h)

Again, when calling solve(), I have to explicitly enumerate the coefficients again, instead of some shortcut, like:

s=solve(eq,p(x).coefficients())

When I try to experiment with the degree of my polynomial p(x), to try out a higher or lower degree, and with more or less equations in the system, I always have to declare my coefficients as vars again and again, which is very annoying.

Furthermore when I tried to query coefficients by, eg.:

p.coefficient(x)

or

p.coefficient(x^3)

The first returns "g" as the coefficient, which is good. But how can I access or query the "free" coefficient, "h" which isn't multiplied by x?

p.coefficient(0)

Doesn't return anything. If it would return the free coeff, then I could get the coefficients by calling a map() with lambda function.