Welcome, guest | Sign In | My Account | Store | Cart

This package does simple polynomial manipulation: adding, multiplying, taking to powers, evaluating at a value, taking integrals and derivatives. Nothing as sophisticated as Mathematica, but useful all the same. I find that I make lots of dumb errors in multiplying out polynomials by hand (when I don't have Mathematica at my disposal), and this little script helps prevent those errors.

Python, 274 lines

I'm constantly making mistakes when I multiply out simple polynomials, and this program lets me evaluate, add, subtract, multiply, and take powers of simple polynomials. There are lots of limitations to the functionality here. All polynomials have to use 'x' as the dependent variable, and the syntax that you may use is fairly limited. Still, you can do some neat things like:

>>> from Polynomial import *
>>> Polynomial('x^2+5')**2-1
x^4 + 10x^2 + 24
>>> Polynomial('x^2+5')**2-'x'
x^4 + 10x^2 - x + 25 Hunter Peress 16 years, 11 months ago Travis Oliphant 16 years, 10 months ago

A similar Polynomial class is in scipy. There is a similar polynomial class in scipy (http://www.scipy.org): scipy.poly1d

Here are some examples:

>>> import scipy
>>> a = scipy.poly1d([1,2,3])
>>> print a
2
x + 2 x + 3
>>> a = scipy.poly1d([10,20,30])
>>> print a
2
10 x + 20 x + 30
>>> print a * a
4       3        2
100 x + 400 x + 1000 x + 1200 x + 900

It has many of the same features and more (also can find the roots of the polynomial). bruce wernick 12 years, 6 months ago

Very nice.

I must say, I prefer this to scipy because it uses only primitive Python. Almost every attempt I have made to use code snippets that import scipy have failed for some or the other reason (other library needed, function not found...).

How would you extend this to solve for x

and then

read n polynomial strings to solve for n unknowns? Created by Rick Muller on Thu, 6 Jan 2005 (PSF)