# substitution not working properly This post is a wiki. Anyone with karma >750 is welcome to improve it.

I have the following code and I want to substitute all B^2=17.

B,x,y= var('B x y')
eq=81*x^16*B^6 + 40662*x^15*B^3 + 14353281*x^14*B^2;eq
eq1=eq.subs({B^2: (17)});eq1


and I get

81*B^6*x^16 + 40662*B^3*x^15 + 244005777*x^14


I am not sure why the substitution didn't bother to simplify B^6=17^3 and B^3=17*B

I also tried using this code which was suggested earlier :

B,x,y= var('B x y')
eq=81*x^16*B^6 + 40662*x^15*B^3 + 14353281*x^14*B^2;eq
eq1=eq.subs({B: sqrt(17)});eq1


which yields

397953*x^16 + 691254*sqrt(17)*x^15 + 244005777*x^14


and I have this sqrt(17) terms which is not written as B. I need it to be written as B because later on I need to collect the coefficient of B. Perhaps I should use something like while{B^2 : 17} but no idea how to do that.

edit retag close merge delete

Sort by » oldest newest most voted

Not satisfactory, but you can do:

sage: eq.subs({B: sqrt(17)}).subs({sqrt(17):B})
691254*B*x^15 + 397953*x^16 + 244005777*x^14


That said, since your expressions are polynomials (they do not involve things like log, sin,...), you can work in a well defined polynomial ring:

sage: R.<B,x,y> = QQ[]
sage: R
Multivariate Polynomial Ring in B, x, y over Rational Field
sage: eq.mod(B^2-17)
691254*B*x^15 + 397953*x^16 + 244005777*x^14

more