"""
Really simple Poly (polynomial class)
Example usage:
>>> from simppoly import Poly
>>> p = Poly([1,2,0,0,3,-4])
>>> p
(1 * (x**0))+(2 * (x**1))+(3 * (x**4))+(-4 * (x**5))
>>> p(10)
-369979
"""
class Poly(object):
def __init__(self, coeffs):
"""
Coefficients in order of increasing degree, 0 where necessary
"""
self.coeffs = coeffs
def __call__(self, x):
"""
Triggers evaluation of string representation for 'x' = x
"""
return eval(repr(self), {'x':x})
def __repr__(self):
"""
Build a string representation, low to high degree, skipping 0 coefficients
"""
monomials = [ '(%s * (x**%s))' % (c,e)
for c,e in zip(self.coeffs, range(len(self.coeffs))) if c<>0]
return ' + '.join(monomials)