Ask Your Question

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

edit retag close merge delete

## 1 Answer

Sort by ยป oldest newest most voted

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.

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.

( 2012-01-28 18:21:44 +0200 )edit

cse stands for common subexpression elimination.

( 2012-01-28 18:30:07 +0200 )edit

## Your Answer

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

Add Answer

## Stats

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

Seen: 1,610 times

Last updated: Jan 28 '12