ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 21 Oct 2015 06:17:13 +0200substitution not working properlyhttps://ask.sagemath.org/question/30099/substitution-not-working-properly/ 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.
Mon, 19 Oct 2015 01:32:21 +0200https://ask.sagemath.org/question/30099/substitution-not-working-properly/Answer by tmonteil for <p>I have the following code and I want to substitute all B^2=17.</p>
<pre><code>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
</code></pre>
<p>and I get</p>
<pre><code>81*B^6*x^16 + 40662*B^3*x^15 + 244005777*x^14
</code></pre>
<p>I am not sure why the substitution didn't bother to simplify <code>B^6=17^3</code> and <code>B^3=17*B</code></p>
<p>I also tried using this code which was suggested earlier :</p>
<pre><code>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
</code></pre>
<p>which yields</p>
<pre><code>397953*x^16 + 691254*sqrt(17)*x^15 + 244005777*x^14
</code></pre>
<p>and I have this <code>sqrt(17)</code> 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.</p>
https://ask.sagemath.org/question/30099/substitution-not-working-properly/?answer=30107#post-id-30107Not 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
Mon, 19 Oct 2015 04:13:40 +0200https://ask.sagemath.org/question/30099/substitution-not-working-properly/?answer=30107#post-id-30107Comment by Sha for <p>Not satisfactory, but you can do:</p>
<pre><code>sage: eq.subs({B: sqrt(17)}).subs({sqrt(17):B})
691254*B*x^15 + 397953*x^16 + 244005777*x^14
</code></pre>
<p>That said, since your expressions are polynomials (they do not involve things like <code>log</code>, <code>sin</code>,...), you can work in a well defined polynomial ring:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/30099/substitution-not-working-properly/?comment=30167#post-id-30167Thank you @tmonteil. This solved my problem.Wed, 21 Oct 2015 06:17:13 +0200https://ask.sagemath.org/question/30099/substitution-not-working-properly/?comment=30167#post-id-30167