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.Sat, 28 Jan 2012 18:30:07 +0100Rewriting an expression in terms of other expressions or functionshttps://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/I'd like to know if there is a way to ask Sage to write a given expression in terms of other expressions or functions. The user could have an insight about the latter while the CAS would be stuck. What I want is something like
write(a^2 - b^2, a-b)
write(cos(x+y), (cos(x), sin(x), cos(y), sin(y)))
write(gamma(p+1), gamma(p)) # or any other recurrence relation the user wants to check
Something like directed simplification, but not wholesale.
Sat, 28 Jan 2012 15:42:32 +0100https://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/Answer by Shashank for <p>I'd like to know if there is a way to ask Sage to write a given expression in terms of other expressions or functions. The user could have an insight about the latter while the CAS would be stuck. What I want is something like</p>
<pre><code> write(a^2 - b^2, a-b)
write(cos(x+y), (cos(x), sin(x), cos(y), sin(y)))
write(gamma(p+1), gamma(p)) # or any other recurrence relation the user wants to check
</code></pre>
<p>Something like directed simplification, but not wholesale. </p>
https://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/?answer=13206#post-id-13206I don't know of a method that will work for all expressions, but there is a way of doing it depending on the expression. I don't think this is what you are looking for, but may help you in finding the solution to your problem
from sympy import *
from sympy.abc import *
print cse([(a-b),factor(a^2-b^2)])
print cse([sin(x),cos(x),sin(y),cos(y),expand(cos(x+y),trig=True)])
The output is
([(x0, a - b)], [x0, x0*(a + b)])
([(x0, cos(x)), (x1, cos(y)), (x2, sin(x)), (x3, sin(y))], [x2, x0, x3,
x1, x0*x1 - x2*x3])
The first element are the substitutions and the second element is the original expression in terms of these substitutions. You still have to get the expression explicitly in terms of the subexpressions you want.
I know in Mathematica there is a function called "Eliminate", which does what you are trying to do, but I am not aware of an equivalent statement in sage.
Sorry for an incomplete answer. Sat, 28 Jan 2012 17:37:20 +0100https://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/?answer=13206#post-id-13206Comment by Shashank for <p>I don't know of a method that will work for all expressions, but there is a way of doing it depending on the expression. I don't think this is what you are looking for, but may help you in finding the solution to your problem</p>
<pre><code>from sympy import *
from sympy.abc import *
print cse([(a-b),factor(a^2-b^2)])
print cse([sin(x),cos(x),sin(y),cos(y),expand(cos(x+y),trig=True)])
</code></pre>
<p>The output is</p>
<pre><code>([(x0, a - b)], [x0, x0*(a + b)])
([(x0, cos(x)), (x1, cos(y)), (x2, sin(x)), (x3, sin(y))], [x2, x0, x3,
x1, x0*x1 - x2*x3])
</code></pre>
<p>The first element are the substitutions and the second element is the original expression in terms of these substitutions. You still have to get the expression explicitly in terms of the subexpressions you want. </p>
<p>I know in Mathematica there is a function called "Eliminate", which does what you are trying to do, but I am not aware of an equivalent statement in sage.</p>
<p>Sorry for an incomplete answer. </p>
https://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/?comment=20394#post-id-20394cse stands for common subexpression elimination. Sat, 28 Jan 2012 18:30:07 +0100https://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/?comment=20394#post-id-20394Comment by Green diod for <p>I don't know of a method that will work for all expressions, but there is a way of doing it depending on the expression. I don't think this is what you are looking for, but may help you in finding the solution to your problem</p>
<pre><code>from sympy import *
from sympy.abc import *
print cse([(a-b),factor(a^2-b^2)])
print cse([sin(x),cos(x),sin(y),cos(y),expand(cos(x+y),trig=True)])
</code></pre>
<p>The output is</p>
<pre><code>([(x0, a - b)], [x0, x0*(a + b)])
([(x0, cos(x)), (x1, cos(y)), (x2, sin(x)), (x3, sin(y))], [x2, x0, x3,
x1, x0*x1 - x2*x3])
</code></pre>
<p>The first element are the substitutions and the second element is the original expression in terms of these substitutions. You still have to get the expression explicitly in terms of the subexpressions you want. </p>
<p>I know in Mathematica there is a function called "Eliminate", which does what you are trying to do, but I am not aware of an equivalent statement in sage.</p>
<p>Sorry for an incomplete answer. </p>
https://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/?comment=20395#post-id-20395Please don't be sorry. It's already helpful. What cse does stand for? In fact, those examples were rather 'simple' and as you guessed, I was looking for a rather general way to provide the function guesses to Sage. In fact, one more relevant use case is the one in [this integral thread](http://ask.sagemath.org/question/1077/symbolic-expectations-and-double-integrals) where I suspect a possible recurrence relation in p.Sat, 28 Jan 2012 18:21:44 +0100https://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/?comment=20395#post-id-20395