5

Is there a library to work with polynomial arithmetic when polynomials can have negative exponents? I found the poly1d class in numpy, but I cannot figure out how I could represent a polynomial like x**-3 + x**-2 + x**2 + x**3.

Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
  • 1
    Just multiply everything by the x raised to the absolute value of the largest negative exponent and use the normal polynomial class. – David Zwicker Jul 11 '12 at 13:18

2 Answers2

6

To quote Wikipedia:

In mathematics, a polynomial is an expression of finite length constructed from variables (also called indeterminates) and constants, using only the operations of addition, subtraction, multiplication, and non-negative integer exponents.

What you're asking about isn't a polynomial -- for example polynomials are always finite, but what you want has a singularity at 0. On the positive side, there are libraries for symbolic manipulation. Take a look at sympy.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thank you for the definition. Now I understand why polynomial classes work like this. And David Zwicker comment tell me how I should do. – Charles Brunet Jul 11 '12 at 14:43
3

You could just use the Law of Exponents (archived link), to shift the exponent to the bottom of a fraction and make it positive:

This:

print (5**-2)
print (1.0/(5**2))

Yields:

0.04
0.04
Matt Binford
  • 620
  • 8
  • 15
Alex W
  • 37,233
  • 13
  • 109
  • 109