Libraries for managing multidimensional polynomials

I need to write code that generates and manipulates multi-parameter polynomials. I will describe my task with a simplified example.

Suppose I was given three expressions: 2x ^ 2, 3y + 1 and 1z. Then I need to multiply them together, which would give me 6x ^ 2ug + 2x ^ 2g. Then I would like to find the partial derivatives of this expression with respect to x, y and z. This will give me 12xyz + 4xz, 6x ^ 2z and 6x ^ 2y + 2x ^ 2.

My problem is with simple manipulations like these in 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 many project-related functions using numpy / scipy / matplotlib, but if there is a reliable set of tools in another language, I am also open to use this. I do university research, so I'm open to using Matlab.

I have not been able to find good python libraries that could do this for me easily and perfectly, if something like scipy polynomial routines that could work on multidimensional polynomials. Does anyone know of a good library that seems appropriate for this problem, and what would be easy to integrate into 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 very slow for the size of the problem I'm working on, so now I will go investigate the 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 a polynomial of order 2 containing 250 variables.

Follow-up # 2: I probably should have done this while I was still working on this issue, but I could also tell everyone that the Matlab symbolic library is extremely comparable in speed to SymPy. In other words, it was brutally slow for large calculations. Both libraries were surprisingly easy to use, so for small calculations, I also recommend it.

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

+6
python math numpy scipy matlab
source share
3 answers

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

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

Examples of differences 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) 
+9
source share

If you use MATLAB, symbolic TB works well if you have it. If not, use the sympoly tool. Just download it from file sharing.

 sympoly xyz 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 

Please note that this indicates that you made a mistake in differentiating the result with respect to z in your question.

+2
source share

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

+1
source share

All Articles