Ask Your Question

Rewriting an expression in terms of other expressions or functions

asked 2012-01-28 08:42:32 -0500

Green diod gravatar image

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-01-28 10:37:20 -0500

Shashank gravatar image

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

from sympy import *
from 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.

edit flag offensive delete link more


Please 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]( where I suspect a possible recurrence relation in p.

Green diod gravatar imageGreen diod ( 2012-01-28 11:21:44 -0500 )edit

cse stands for common subexpression elimination.

Shashank gravatar imageShashank ( 2012-01-28 11:30:07 -0500 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2012-01-28 08:42:32 -0500

Seen: 1,125 times

Last updated: Jan 28 '12