Ask Your Question
2

Is there any way I can substitute a combination of variables.

asked 2010-11-11 13:28:57 +0200

Shashank gravatar image

updated 2011-04-28 15:56:35 +0200

Kelvin Li gravatar image

I know this is a long short but I was wondering whether it is possible to substitute a combination of variables by something else. Let me explain it with an example.

Let $f(x,y,z) = f(x) \sqrt{x^2+y^2+z^2} + f(y) \sqrt{x^2+y^2+z^2} + f(z) \sqrt{x^2+y^2+z^2}$

As we can see the combination $\sqrt{x^2+y^2+z^2}$ occurs very often. Is there a way I call this combination some other variable, say, w. I cannot use substitute in this case as I want to leave $x$,$y$ and $z$ alone if they dont come in this specific combination $\sqrt{x^2+y^2+z^2}$. If it were a string I could have used find and replace. But I want to use the expression as a input later on in the notebook.

Edit: I came across a sympy command which is very close to what I want to do but not quite. There is a command cse in the module simplify. However, it identifies the subexpresssion by itself. As far as I could see it does not offer the flexibility for the users to identify the subexpression. Also for some reason sage expressions cannot be converted to sympy using f._sympy_() if there are "i"'s in the expression.

Thanks in advance.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2010-11-11 22:19:11 +0200

DSM gravatar image

It's occasionally a little flaky, but I think substitute_expression (subs_expr) can do what you want:

sage: var("x y z")
(x, y, z)
sage: var("w")
w
sage: f = function("f")
sage: s = sqrt(x*x+y*y+z*z)
sage: eq = f(x,y,z) == f(x) * s + f(y) * s + f(z) * s
sage: eq
f(x, y, z) == sqrt(x^2 + y^2 + z^2)*f(x) + sqrt(x^2 + y^2 + z^2)*f(y) + sqrt(x^2 + y^2 + z^2)*f(z)
sage: eq.subs_expr(sqrt(x*x+y*y+z*z)== w)
f(x, y, z) == w*f(x) + w*f(y) + w*f(z)
 

Also, if you want to force a replace if subs_expr is being disobedient, you could treat it as a string and then evaluate it back. It's ugly, but there are times you might want to do something like this:


sage: # ugly and dangerous! do not use!
sage: tmp = str(eq)
sage: tmp2 = tmp.replace(str(s), 'w')
sage: sage_eval(tmp2,locals=vars())
f(x, y, z) == w*f(x) + w*f(y) + w*f(z)

but this can lead to problems when the string you're replacing is a substring of something else.

edit flag offensive delete link more
2

answered 2010-11-11 21:27:27 +0200

Shashank gravatar image

updated 2012-04-15 20:42:32 +0200

I figured out that the "subs" command in sympy is far more powerful than substitute in sage. So I converted the sage expression into sympy expression using f=f._sympy_() then did the substitution I wanted to do and after the substitution converted to sage again using f=f._sage_(). The problem is that sympy gives a problem because it cannot recognize i. So I declared a variable name i and restored it in the end. Here is an example script. It would be awesome though if the sage substitute command worked like the sympy subs command.

from sympy import Symbol, cos, sympify, pprint 
from sympy.abc import x
x=var('x')
z=var('z')
i=var('i')
f=x*x+x*i+1
view(f)
f=f._sympy_()
view(f)
f=f.subs(x*x,z)
f=f._sage_()
view(f)
restore('i')
edit flag offensive delete link more

Comments

would someone please format the code?

d3banjan gravatar imaged3banjan ( 2012-04-15 10:46:15 +0200 )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: 2010-11-11 13:28:57 +0200

Seen: 7,496 times

Last updated: Apr 15 '12