quiz about symbolic manipulation
Bengt Richter
bokr at oz.net
Thu Dec 12 15:49:42 EST 2002
On 12 Dec 2002 07:48:48 -0800, mis6 at pitt.edu (Michele Simionato) wrote:
>I saw a recent posting about symbolic manipulation in Python and I have
>understood that it is much better to use Mathematica or Maple.Of course,
>this is not surprising at all, still I am not completely happy with both
>Mathematica and Maple, essentially due to the fact that they do not scale
>well with large projects (at least in my experience) and I would welcome a more
>programming oriented replacement of them (I think the approach of GiNaC is
>promising but I have never used it).
>Nevertheless, for sake of personal illumination, I would like to understand
>what can be done in Python for simple problems. To this aim I propose here
>a toy problem which I think this can be solved by using the parser and/or
>compiler modules (disclaimer: I am not particularly familiar with these
>modules), i.e. the substitution of functions in expressions.
>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"
[1] moving this down to [2] for comparison
>
>The difficult part is that e can contains arbitrarily nested invocations
>of square: notice for instance that Mathematica is unable to solve this:
^^^^^^--?? (cf. [1],[2])
>
>$ 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])
^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
| |
+-----------+ |
| |
vvvvvvvv |
[2] "((x+y)**2+z)**2+(x+w)**2" |
^^^^^^^^^^^^^^^ |
| |
+-------------------+
What am I missing?
Regards,
Bengt Richter
More information about the Python-list
mailing list