# how to solve equations for polynomials coefficients?

I am new to SAGE so sorry if this question is trivial. I have this code

  R2.<a0,a1,a2,a3, a4,b0,b1,b2,b3, b4,z,w,l,E,E1,v,t> = PolynomialRing(ZZ,17)
R.<x> = PowerSeriesRing(R2)
p = a1*x -a1*z*x**2+ a3*x**3+a4*x**4 +O(x**12)
k = b1*x+b2*x**2+ b3*x**3+b4*x**4 +O(x**12)
p1 = p.derivative()
p2 = p1.derivative()
y =2/x*(z-v*x)-E
x=2/x*a1*w*x**2
g=x*(p2 +y*p-x-E1*k)
v1=g.coefficients()
solve(v1,a3)


I am getting this error a3 is not a valid variable.

My question is how to solve equations for polynomials coefficients?

edit retag close merge delete

1

solve is supposed to have (a listof) symbolic expressions (or symbolic expressions) as first argument, and (al list of) symbolic variables as second argument. in the call solve(v1, a3), both arguments are polynomials :

sage: v1.parent()
Multivariate Polynomial Ring in a0, a1, a2, a3, a4, b0, b1, b2, b3, b4, z, w, l, E, E1, v, t over Integer Ring
sage: a3.parent()
Multivariate Polynomial Ring in a0, a1, a2, a3, a4, b0, b1, b2, b3, b4, z, w, l, E, E1, v, t over Integer Ring


A possible workaround is :

sage: WA=SR(v1).solve(SR(a3)); WA
a3 == 1/3*a1*z^2 + 1/6*E*a1 + 1/6*E1*b1 + 1/3*a1*v + 1/3*a1*w


but note that

sage: WA.rhs().parent()
Symbolic Ring


I do not understand what

 x=2/x*a1*w*x**2


is supposed to mean...

By x = 2/x * a1 * w * x**2 do you mean that 2*a1*w should be considered equal to 1?

Sort by » oldest newest most voted

Maybe the following is what you want.

Having defined:

sage: R2.<a0, a1, a2, a3, a4, b0, b1, b2, b3, b4, z, w, l, E, E1, v, t> = QQ[]
sage: R.<x> = PowerSeriesRing(R2)

sage: p = a1 * x - a1 * z * x^2 + a3 * x^3 + a4 * x^4 + O(x^12)
sage: p1 = p.derivative()
sage: p2 = p1.derivative()

sage: k = b1 * x + b2 * x^2 + b3 * x^3 + b4 * x^4 + O(x^12)
sage: y = 2/x * (z - v * x) - E
sage: g = x * (p2 + y * p - x - E1 * k)


we get:

sage: g
(-2*a1*z^2 - a1*E - b1*E1 - 2*a1*v + 6*a3 - 1)*x^2
+ (a1*z*E + 2*a1*z*v + 2*a3*z - b2*E1 + 12*a4)*x^3
+ (2*a4*z - a3*E - b3*E1 - 2*a3*v)*x^4
+ (-a4*E - b4*E1 - 2*a4*v)*x^5
+ O(x^11)


Let's look at the coefficients of g, and specifically the constant one:

sage: cc = g.coefficients()
sage: cc
[-2*a1*z^2 - a1*E - b1*E1 - 2*a1*v + 6*a3 - 1,
a1*z*E + 2*a1*z*v + 2*a3*z - b2*E1 + 12*a4,
2*a4*z - a3*E - b3*E1 - 2*a3*v,
-a4*E - b4*E1 - 2*a4*v]

sage: c = cc
sage: c
-2*a1*z^2 - a1*E - b1*E1 - 2*a1*v + 6*a3 - 1


Dirty trick: to solve, move to the symbolic ring via strings:

sage: eq = SR(str(c))
sage: eq
-2*a1*z^2 - E*a1 - E1*b1 - 2*a1*v + 6*a3 - 1

sage: symbolic_a3 = SR('a3')

sage: sol = solve([eq], symbolic_a3, solution_dict=True)
sage: sol
[{a3: 1/3*a1*z^2 + 1/6*E*a1 + 1/6*E1*b1 + 1/3*a1*v + 1/6}]


Extract the value and take it back to the polynomial ring:

sage: s = sol
sage: s
{a3: 1/3*a1*z^2 + 1/6*E*a1 + 1/6*E1*b1 + 1/3*a1*v + 1/6}

sage: my_symbolic_a3 = s[symbolic_a3]
sage: my_symbolic_a3
1/3*a1*z^2 + 1/6*E*a1 + 1/6*E1*b1 + 1/3*a1*v + 1/6

sage: my_a3 = R2(my_symbolic_a3)
sage: my_a3
1/3*a1*z^2 + 1/6*E*a1 + 1/6*E1*b1 + 1/3*a1*v + 1/6

more