[Numpy-discussion] polynomial with negative exponents

Charles R Harris charlesr.harris at gmail.com
Mon Dec 12 13:54:51 EST 2011


On Mon, Dec 12, 2011 at 10:17 AM, Gregor Thalhammer <
gregor.thalhammer at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111212/377d1c85/attachment.html>


More information about the NumPy-Discussion mailing list