views:

102

answers:

3

Hi. I need to write some code that deals with generating and manipulating multivariable polynomials. I'll outline my task with a simplified example.

Lets say I am given three expressions: 2x^2, 3y + 1, and 1z. I then need to multiply these together which would give me 6x^2yz + 2x^2z. Then I would like to find the partial derivatives of this expression with respect to x, y, and z. This would give me 12xyz + 4xz, 6x^2z, and 6x^2y + 2x^2.

My problem deals with doing simple manipulations like this on expressions containing thousands of variables and I need an easy way to do this systematically. I would really like to use python since I already have a lot of project related functionality completed using numpy/scipy/matplotlib, but if there is a robust toolbox out there in another language I am open to using that as well. I am doing university research so I am open to using Matlab as well.

I haven't been able to find any good python libraries that could do this for me easily and ideally would like something similar to the scipy polynomial routines that could work on multidimensional polynomials. Does anyone know of a good library that seems suitable for this problem and that would be easy to integrate into already existing python code?

Thanks!

Follow up: I spent a couple of days working with sympy which turned out to be very easy to use. However, it was much to slow for the size of the problem I am working on so I will now go explore matlab. To give an extremely rough estimate of the speed using a small sample size, it took approximately 5 seconds to calculate each of the partial derivatives of an order 2 polynomial containing 250 variables.

Follow up #2: I probably should have done this back when I was still working on this problem, but I might as well let everyone know that the matlab symbolic library was extremely comparable in speed to sympy. In other words, it was brutally slow for large computations. Both libraries were amazingly easy to work with, so for small computations I do highly recommend either.

To solve my problem I computed the gradients by hand, simplified them, and then used the patterns I found to hard code some values in my code. It was more work, but made my code exponentially faster and finally usable.

+1  A: 

Matlab and other tools you mention typically do numerical computing. You should consider using Mathematica or an alternative Computer Algebra System (CAS) for symbolic computation. See the wiki link: http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems for a comparison of various CASs.

Anon
Thanks for the input. I'm going to try sympy first because I love python and it will be by far the easiest to code. If it is too slow for whatever reason (I've found certain scipy optimization methods to be slow on large datasets before) then I'll certainly give it a try in another environment.
Spike
+6  A: 

Sympy is perfect for this: http://code.google.com/p/sympy/

Documentation: http://docs.sympy.org/

Examples of differentiation from the tutorial: http://docs.sympy.org/tutorial.html#differentiation

import sympy

x, y, z = sympy.symbols('xyz')

p1 = 2*x*x
p2 = 3*y + 1
p3 = z

p4 = p1*p2*p3

print p4

print p4.diff(x)
print p4.diff(y)
print p4.diff(z)

Output:

2*z*x**2*(1 + 3*y)
4*x*z*(1 + 3*y)
6*z*x**2
2*x**2*(1 + 3*y)
PreludeAndFugue
+1: Rereading the docs, they do mention that the numerical args are optional and why...it's odd that they have **no** examples with only symbolic differentiation.
Nikhil Chelliah
This is perfect for what I need. Thanks for the example too!
Spike
@Nikhil: Thanks for also looking into this for me! I'm really impressed by the help I've gotten here on SO
Spike
+2  A: 

If you are using MATLAB, then the symbolic TB works well, IF you have it. If not, then use my sympoly toolbox. Just download it from the file exchange.

sympoly x y z
A = 2*x^2; B = 3*y + 1;C = 1*z;
gradient(A*B*C)

ans =
Sympoly array has size = [1  3]

Sympoly array element [1  1]
    4*x*z + 12*x*y*z
Sympoly array element [1  2]
    6*x^2*z
Sympoly array element [1  3]
    2*x^2 + 6*x^2*y

Note that this points out that you made a mistake in differentiating the result with respect to z in your question.

woodchips
Thanks for finding my error. Your sympoly toolbox looks nice and if I end up having to use Matlab for this problem I'll give it a whirl.
Spike