quiz about symbolic manipulation

Michele Simionato mis6 at pitt.edu
Thu Dec 12 10:48:48 EST 2002


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"

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 ;-)

Bye,

--
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/



More information about the Python-list mailing list