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.
python math numpy scipy matlab
Spike
source share