Is there way to express mathematical expression through variable defined earlier in SAGE?
For example if I have variable a = b + c
, I want SAGE rewrite expression b + c + d
as a + d
.
Thank you.
Is there way to express mathematical expression through variable defined earlier in SAGE?
For example if I have variable a = b + c
, I want SAGE rewrite expression b + c + d
as a + d
.
Thank you.
In fact, substituting such expressions is a nontrivial thing if you don't know what part of the expression tree you want. See Richard Fateman's comments here.
The core of the problem is that even the command that would do what you want is not about strings, but expressions.
sage: var("a b c d")
(a, b, c, d)
sage: (a+d).subs({a:b+c})
b + c + d
sage: (b+c+d).subs({b+c:a})
b + c + d
So you will have to use a "wildcard".
sage: w0 = SR.wild(0)
sage: (b+c+d).subs({b+c+w0:a+w0})
a + d
For more information, see
sage: x.match?
sage: SR.wild?
in the interactive shell or notebook.
As you can see in calculus, you can express d as variable with
a = var('a'); b+c
or like a function of b and c variable