quiz about symbolic manipulation
Carl Banks
imbosol at vt.edu
Thu Dec 12 19:17:13 EST 2002
Michele Simionato wrote:
> In order to be specific I will pick up an example. Consider for instance
> the expression
>
> e="square(square(x+y)+z)+square(x+w)"
>
> I would like to define a function
>
> def substitute(math_expr,**subs):
> #.... something here
> result result
>
> such that when I call
>
> print substitute(e, square="x -> x**2")
>
> I obtain
>
> "((x+y)**2+z)**2+(x+w)**2"
>
> The difficult part is that e can contains arbitrarily nested invocations
> of square: notice for instance that Mathematica is unable to solve this:
>
> $ math
> Mathematica 4.1 for Linux
> Copyright 1988-2000 Wolfram Research, Inc.
> -- Motif graphics initialized --
>
> In[1]:= square[square[x+y]+z]+square[x+w]/.square[x_] -> x^2
>
> 2 2
> Out[1]= (w + x) + (z + square[x + y])
>
> ________________________________________________________________________
>
> If somebody is idle with nothing to do, he/she could think a little
> about that ;-)
Ok. Try this:
square[square[x+y]+z]+square[x+w] //. square[x_]->x^2
Notice the //. instead of /.
--
CARL BANKS
More information about the Python-list
mailing list