polynomial with negative exponents

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. Do you have suggestions on how to speed up this code? Regards, Davide Lasagna -- Phd Student Dipartimento di Ingegneria Aeronautica a Spaziale Politecnico di Torino, Italy tel: 011/0906871 e-mail: davide.lasagna@polito.it; lasagnadavide@gmail.com

On Mon, Dec 12, 2011 at 9:04 AM, LASAGNA DAVIDE <davide.lasagna@polito.it> wrote:
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 ) ) )
something like self.coeffs = np.asarray(self.coeffs) np.sum(self.coeffs * x**(-np.arange(len(self.coeffs))) or np.dot(self.coeffs, x**(-np.arange(len(self.coeffs))) #check shapes, or np.inner Josef
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.
Do you have suggestions on how to speed up this code?
Regards,
Davide Lasagna
-- Phd Student Dipartimento di Ingegneria Aeronautica a Spaziale Politecnico di Torino, Italy tel: 011/0906871 e-mail: davide.lasagna@polito.it; lasagnadavide@gmail.com
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Mon, Dec 12, 2011 at 9:35 AM, <josef.pktd@gmail.com> wrote:
On Mon, Dec 12, 2011 at 9:04 AM, LASAGNA DAVIDE <davide.lasagna@polito.it> wrote:
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 ) ) )
something like
self.coeffs = np.asarray(self.coeffs)
np.sum(self.coeffs * x**(-np.arange(len(self.coeffs)))
or np.dot(self.coeffs, x**(-np.arange(len(self.coeffs))) #check shapes, or np.inner
Josef
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.
there might be numerical problems with large polynomials if the range of values is large Josef
Do you have suggestions on how to speed up this code?
Regards,
Davide Lasagna
-- Phd Student Dipartimento di Ingegneria Aeronautica a Spaziale Politecnico di Torino, Italy tel: 011/0906871 e-mail: davide.lasagna@polito.it; lasagnadavide@gmail.com
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

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

On Mon, Dec 12, 2011 at 10:17 AM, Gregor Thalhammer < gregor.thalhammer@gmail.com> wrote:
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)
Or numpy.polynomial.polynomial.polyval, which will use the coefficients in the same order. Or you could subclass numpy.polynomial.Polynomial and override the call to use 1/x instead of x. Chuck
participants (4)
-
Charles R Harris
-
Gregor Thalhammer
-
josef.pktd@gmail.com
-
LASAGNA DAVIDE