[Numpy-discussion] polynomial with negative exponents

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Dec 12 09:35:07 EST 2011


On Mon, Dec 12, 2011 at 9:04 AM, LASAGNA DAVIDE
<davide.lasagna at 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 at polito.it; lasagnadavide at gmail.com
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list