# complex substitution

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

edit retag close merge delete

When I use the .subs_expr it doesn't give me back anything

( 2014-08-26 23:00:15 +0100 )edit

It 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 ?

( 2014-08-26 23:13:09 +0100 )edit

I 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?

( 2014-08-27 15:28:19 +0100 )edit

Since this example works well, how can we understand your problem. Could you please provide the exact formulas that lead to an actual problem ?

( 2014-08-27 18:22:23 +0100 )edit

I 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/solve-for-variable-but-variable-is-still-in-answer/

( 2014-08-29 16:58:18 +0100 )edit

Sort by ยป oldest newest most voted

I 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()
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 a-b is like sum([a,mul[b,-1]]), which you can vizualize as a tree:

  a
/
-
\
b


versus

  a
/
+   b
\ /
*
\
-1


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 ...
more

Ah okay I think I get it! Great thanks for the elaborate answer! What is the difference between .subs and .subs_expr?

( 2014-08-28 16:18:53 +0100 )edit

Excellent and thorough explanation! +1

( 2014-08-29 21:19:06 +0100 )edit

Please start posting anonymously - your entry will be published after you log in or create a new account.