quiz about symbolic manipulation
Michele Simionato
mis6 at pitt.edu
Fri Dec 13 08:48:22 EST 2002
bokr at oz.net (Bengt Richter) wrote in message news:<atasp6$loc$0 at 216.39.172.122>...
> >
> >In[1]:= square[square[x+y]+z]+square[x+w]/.square[x_] -> x^2
> >
> > 2 2
> >Out[1]= (w + x) + (z + square[x + y])
> ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
> | |
> +-----------+ |
> | |
> vvvvvvvv |
> [2] "((x+y)**2+z)**2+(x+w)**2" |
> ^^^^^^^^^^^^^^^ |
> | |
> +-------------------+
>
> What am I missing?
The "/." substitution operator of Mathematica does not work
recursively:
you see that the result still contain the expression square[x+y] and
that
you should apply "/." again to get rid of this second square.
Of course, as Carl pointed out, you could use the "//." operator, but
this
is not the point: I would like to do this in Python. My example
(perhaps
not too happily chosen) simply wanted to point out the need for a
recursive
algorithm. Still I think the solution should be not too difficult
using the
compiler module, but I have problems in understanding this module
since the documentation is still rather poor and not so clear (to me
at least).
Bye,
Michele
More information about the Python-list
mailing list