
Am 12.12.2011 um 15:04 schrieb LASAGNA DAVIDE:
Hi,
I have written a class for polynomials with negative exponents like:
p(x) = a0 + a1*x**-1 + ... + an*x**-n
The code is this one:
class NegativeExpPolynomial( object ): def __init__ ( self, coeffs ): self.coeffs = np.array( coeffs )
def __call__( self, x ): return sum( (c*x**(-i) for i, c in enumerate( self.coeffs ) ) )
where coeffs = [a0, a1, ..., an].
I find that the way i evaluate the polynomial is kind of *slow*, especially for polynomial with order larger than ~200 and for arrays x large enough.
I fear that such high orders create a lot of troubles since evaluating them is very sensitive to numerical errors.
Do you have suggestions on how to speed up this code?
Your polynomials with negative exponents are equivalent to a polynomial with positive exponents, but evaluated at 1/x. Therefore you can make use of the efficient polynomial functions of numpy. Try np.polyval(self.coeffs, x) Gregor