Ask Your Question
0

Rewriting an expression in terms of other expressions or functions

asked 2012-01-28 15:42:32 +0100

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
1

answered 2012-01-28 17:37:20 +0100

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

edit flag offensive delete link more

Comments

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](http://ask.sagemath.org/question/1077/symbolic-expectations-and-double-integrals) where I suspect a possible recurrence relation in p.

Green diod gravatar imageGreen diod ( 2012-01-28 18:21:44 +0100 )edit

cse stands for common subexpression elimination.

Shashank gravatar imageShashank ( 2012-01-28 18:30:07 +0100 )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

Stats

Asked: 2012-01-28 15:42:32 +0100

Seen: 1,923 times

Last updated: Jan 28 '12