Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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