ASKSAGE: Sage Q&A Forum  Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 29 Aug 2014 14:19:06 0500complex substitutionhttp://ask.sagemath.org/question/23945/complexsubstitution/Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists.
Case 1 (Works):
I have a formula let say
sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)
Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3
Tue, 26 Aug 2014 14:42:31 0500http://ask.sagemath.org/question/23945/complexsubstitution/Comment by wzawzdb for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23982#postid23982That's not entirely true since the edit here was about another substitution, the other one was about solving for a variable which gave me an answer where the variable is not isolated.Fri, 29 Aug 2014 10:12:33 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23982#postid23982Comment by wzawzdb for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23986#postid23986Yeah, I understand, but I wanted to avoid making 1000 threadsFri, 29 Aug 2014 10:50:42 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23986#postid23986Comment by tmonteil for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23953#postid23953Since this example works well, how can we understand your problem. Could you please provide the exact formulas that lead to an actual problem ?Wed, 27 Aug 2014 11:22:23 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23953#postid23953Comment by wzawzdb for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23951#postid23951I expected that result, but if i try this with my own formula it gives me back (a*m+f)/g
And im doing it the exact same way except that there are more variables involved. Does sage see that if something is squared or multiplied with something else it can be taken apart?Wed, 27 Aug 2014 08:28:19 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23951#postid23951Comment by tmonteil for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23981#postid23981I reverted your question to the state it was before you added new questions, since those additional questions are asked in http://ask.sagemath.org/question/23967/solveforvariablebutvariableisstillinanswer/Fri, 29 Aug 2014 09:58:18 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23981#postid23981Comment by wzawzdb for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23947#postid23947When I use the .subs_expr it doesn't give me back anythingTue, 26 Aug 2014 16:00:15 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23947#postid23947Comment by tmonteil for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23948#postid23948It is not clear to me which expression do you want to substitute in which. It seems to work for me, for ``eq3`` i got : ``d/g == ((b + c)*k + f)/g``. Which result did you expect ?Tue, 26 Aug 2014 16:13:09 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23948#postid23948Comment by tmonteil for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23987#postid23987OK, so about the substitution question only (since it is the same topic), perhaps make an edit after the current one and add an *EDIT:* statement.Fri, 29 Aug 2014 11:24:47 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23987#postid23987Comment by tmonteil for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23983#postid23983Ok, it was not clear to me. Postediting a question once it is answered creates a mess. Indeed, some other people could benefit from your question but may get lost in reading answers not connected to the newly edited question. If another case of the same question appears, perhaps could you add it after the current one, starting by an *EDIT* statement, so that it is clear that it is a new case, this will help readers to understand what is the current status of the question.Fri, 29 Aug 2014 10:39:36 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23983#postid23983Comment by tmonteil for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23985#postid23985Or perhaps write "case 1" (the first one that actually worked), "case 2" (which i answered), and then "case 3" (your new substitution problem), with an *EDIT* between each stage. The thing is that your question is of interest to any one falling into such problem in the future and come into this page after searching on the web, ask.sagemath.org is not only about answering questions to individuals, but also maintaining a kind of FAQ. This helps avoiding duplicate questions.Fri, 29 Aug 2014 10:43:15 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23985#postid23985Answer by tmonteil for <p>Im working on a loooooooooooooong formula. I want to replace (substitute?) certain combination of variables for another simpeler combination of variables (like for example F=m*a), anywhere in the formula where this combination exists. </p>
<p>Case 1 (Works):
I have a formula let say </p>
<pre><code>sage: eq1=m*a==(b+c)*k
sage: eq2=d==a*m+f
sage: eq3=eq2.subs(eq1)/g
</code></pre>
<p>(doesnt work with the multiplication? just gives me the same answer as i had without the substitution)</p>
<p>Edit:
Case 2 (solved):
so it does some kind of operation on equation 2 but I want to replace the a in equation 2 for equation 1
my code:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
</code></pre>
<p>The last one i want to turn the a3r3u3 into the longer one, but it just gives me back a3r3u3</p>
http://ask.sagemath.org/question/23945/complexsubstitution/?answer=23958#postid23958I first thought you found a bug, but here is the reason of your problem, and a workaround:
Your problem summarizes as follows:
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
While you expect (as we got with ``d/g == ((b + c)*k + f)/g`` in your smaller example):
P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  (A2*u2*r2 + A1*r1*u1)*u3)/A3
The fun thing is that, if we reverse two signs, it works as expected:
sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + (A1*r1*u1 + A2*r2*u2)*u3)/A3
The problem is not in the product, but in the subtraction, or more precisely the interplay between both. Let us understand this:
When you write:
sage: a, b = var('a b')
sage: y = a  b
You do *not* have the operator ``sub`` with operands ``a`` and ``b``:
sage: y.operator()
<function operator.add>
sage: y.operands()
[a, b]
Also:
sage: (b).operator()
<function operator.mul>
sage: (b).operands()
[b, 1]
As you can see, the symbolic ring understands ``a  b`` as the sum of ``a`` and ``b`` and ``b`` as the product of ``b`` and ``1``. So ``ab`` is like ``sum([a,mul[b,1]])``, which you can vizualize as a tree:
a
/

\
b
versus
a
/
+ b
\ /
*
\
1
In your problem:
sage: (A3*r3*u3^2).operator()
<function operator.mul>
sage: (A3*r3*u3^2).operands()
[A3, r3, u3^2, 1]
If you imagine symbolic expressions as trees, it is now easy to understand why Sage is not able to do your substitution, you want to substitute ``mul([A3, r3, u3^2])`` which is not a subtree of your expression (while ``mul([A3, r3, u3^2, 1])`` is).
So, the workaround is now easy guess: you should not substitute ``A3*r3*u3^2`` in your expression, but ``A3*r3*u3^2``:
sage: sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  (A1*r1*u1 + A2*r2*u2)*u3)/A3
As you wanted.
That said, you should know that in general the symbolic ring is very stupid. If you only use products and sums (i mean no ``sin``, ``log`` or stuff like that), it could be worth trying to work with multivariate polynomial or fractions rings and their quotients.
Wed, 27 Aug 2014 16:49:52 0500http://ask.sagemath.org/question/23945/complexsubstitution/?answer=23958#postid23958Comment by wzawzdb for <div class="snippet"><p>I first thought you found a bug, but here is the reason of your problem, and a workaround:</p>
<p>Your problem summarizes as follows:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
</code></pre>
<p>While you expect (as we got with <code>d/g == ((b + c)*k + f)/g</code> in your smaller example):</p>
<pre><code>P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  (A2*u2*r2 + A1*r1*u1)*u3)/A3
</code></pre>
<p>The fun thing is that, if we reverse two signs, it works as expected:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + (A1*r1*u1 + A2*r2*u2)*u3)/A3
</code></pre>
<p>The problem is not in the product, but in the subtraction, or more precisely the interplay between both. Let us understand this:</p>
<p>When you write:</p>
<pre><code>sage: a, b = var('a b')
sage: y = a  b
</code></pre>
<p>You do <em>not</em> have the operator <code>sub</code> with operands <code>a</code> and <code>b</code>:</p>
<pre><code>sage: y.operator()
<function operator.add>
sage: y.operands()
[a, b]
</code></pre>
<p>Also:</p>
<pre><code>sage: (b).operator()
<function operator.mul>
sage: (b).operands()
[b, 1]
</code></pre>
<p>As you can see, the symbolic ring understands <code>a  b</code> as the sum of <code>a</code> and <code>b</code> and <code>b</code> as the product of <code>b</code> and <code>1</code>. So <code>ab</code> is like <code>sum([a,mul[b,1]])</code>, which you can vizualize as a tree:</p>
<pre><code> a
/

\
b
</code></pre>
<p>versus</p>
<pre><code> a
/
+ b
\ /
*
\
1
</code></pre>
<p>In your problem:</p>
<pre><code>sage: (A3*r3*u3^2).operator()
<function operator.mul>
sage: (A3*r3*u3^2).operands()
[A3, r3, u3^2, 1]
</code></pre>
<p>If you imagine symbolic expressions as trees, it is now easy to understand why Sage is not able to do your substitution, you want to substitute <code>mul([A3, r3, u3^2])</code> which is not a subtree of your expression (while <code>mul([A3, r3, u3^2, 1])</code> is).</p>
<p>So, the workaround is now easy guess: you should not substitute <code>A3*r3*u3^2</code> in your expression, but <code>A3*r3*u3^2</code>:</p>
<pre><code>sage: sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  (A1*r1*u1 + A2*r2 ...</code></pre><span class="expander"> <a>(more)</a></span></div>http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23964#postid23964Ah okay I think I get it! Great thanks for the elaborate answer! What is the difference between .subs and .subs_expr?Thu, 28 Aug 2014 09:18:53 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23964#postid23964Comment by dazedANDconfused for <div class="snippet"><p>I first thought you found a bug, but here is the reason of your problem, and a workaround:</p>
<p>Your problem summarizes as follows:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
</code></pre>
<p>While you expect (as we got with <code>d/g == ((b + c)*k + f)/g</code> in your smaller example):</p>
<pre><code>P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  (A2*u2*r2 + A1*r1*u1)*u3)/A3
</code></pre>
<p>The fun thing is that, if we reverse two signs, it works as expected:</p>
<pre><code>sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: eq7a = P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + A3*r3*u3^2)/A3
sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2  A2*r2*u2^2 + (A1*r1*u1 + A2*r2*u2)*u3)/A3
</code></pre>
<p>The problem is not in the product, but in the subtraction, or more precisely the interplay between both. Let us understand this:</p>
<p>When you write:</p>
<pre><code>sage: a, b = var('a b')
sage: y = a  b
</code></pre>
<p>You do <em>not</em> have the operator <code>sub</code> with operands <code>a</code> and <code>b</code>:</p>
<pre><code>sage: y.operator()
<function operator.add>
sage: y.operands()
[a, b]
</code></pre>
<p>Also:</p>
<pre><code>sage: (b).operator()
<function operator.mul>
sage: (b).operands()
[b, 1]
</code></pre>
<p>As you can see, the symbolic ring understands <code>a  b</code> as the sum of <code>a</code> and <code>b</code> and <code>b</code> as the product of <code>b</code> and <code>1</code>. So <code>ab</code> is like <code>sum([a,mul[b,1]])</code>, which you can vizualize as a tree:</p>
<pre><code> a
/

\
b
</code></pre>
<p>versus</p>
<pre><code> a
/
+ b
\ /
*
\
1
</code></pre>
<p>In your problem:</p>
<pre><code>sage: (A3*r3*u3^2).operator()
<function operator.mul>
sage: (A3*r3*u3^2).operands()
[A3, r3, u3^2, 1]
</code></pre>
<p>If you imagine symbolic expressions as trees, it is now easy to understand why Sage is not able to do your substitution, you want to substitute <code>mul([A3, r3, u3^2])</code> which is not a subtree of your expression (while <code>mul([A3, r3, u3^2, 1])</code> is).</p>
<p>So, the workaround is now easy guess: you should not substitute <code>A3*r3*u3^2</code> in your expression, but <code>A3*r3*u3^2</code>:</p>
<pre><code>sage: sage: A1, A2, A3, P1, P3, u1, u2, u3, r1, r2, r3 = var('A1 A2 A3 P1 P3 u1 u2 u3 r1 r2 r3')
sage: sage: eq7a = P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  A3*r3*u3^2)/A3
sage: sage: eq7a.subs_expr(A3*r3*u3^2 == (A2*u2*r2 + A1*r1*u1)*u3)
P1  P3 == (A1*r1*u1^2 + A2*r2*u2^2  (A1*r1*u1 + A2*r2 ...</code></pre><span class="expander"> <a>(more)</a></span></div>http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23996#postid23996Excellent and thorough explanation! +1Fri, 29 Aug 2014 14:19:06 0500http://ask.sagemath.org/question/23945/complexsubstitution/?comment=23996#postid23996