0

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.

kcrisman
  • 4,374
  • 20
  • 41

2 Answers2

3

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.

kcrisman
  • 4,374
  • 20
  • 41
0

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

Mihai8
  • 3,113
  • 1
  • 21
  • 31
  • Yes, sure, but my question is how can I simplify expression (i.e. `b + c + d`) using definition of `a` and gain `a + d`. It is just simple example. – ElectricHedgehog Mar 12 '13 at 12:28
  • Use simplify as you see in http://www.sagemath.org/doc/reference/sage/symbolic/expression.html – Mihai8 Mar 12 '13 at 12:30
  • Seems it does not work: `sage: var("a b c d") (a, b, c, d) sage: a = var("a");b+c b + c sage: (b + c + d).simplify() b + c + d` – ElectricHedgehog Mar 12 '13 at 12:38