[Matrix-SIG] Re: cephesmodule
Rob Managan
managan@llnl.gov
Wed, 10 Mar 1999 14:23:40 -0800
Probably doesn't make much difference up to P5 but the polynomials should
be nested to help avoid cancelation problems.
At 4:01 PM -0600 3/10/99, Mike Miller wrote:
>>>>>> "Les" == Les Schaffer <godzilla@netmeg.net> writes:
>
> >> Do you know of some code that computes them? Isn't there
> >> someway to write them as a hypergeometric functions?
>
>#!/usr/bin/env python
>#
># Legendre Polynomials.
>#
># Oct-25-1998
># M.A.Miller, NPL UIUC <miller5@uiuc.edu>
>
>"""
>class Legendre: Legendre polynomials. For n = 0-5, they are hard
>coded: P0(x), P1(x), P2(x), P3(x), P4(x), P5(x). For n > 5, the
>recurrence relation is used: Pn(n,x).
>"""
>class Legendre:
> def __init__(self):
> pass
>
> def P0(self,x):
> result = 1.0
> return result
>
> def P1(self,x):
> result = x
> return result
>
> def P2(self,x):
> result = ( 3.0 * x**2 - 1 ) / 2.0
> return result
>
> def P3(self,x):
> result = ( (5.0 * x*x - 3.0) * x ) / 2.0
> return result
>
> def P4(self,x):
> result = ( (35.0 * x*x - 30.0) * x*x + 3.0 ) / 8.0
> return result
>
> def P5(self,x):
> result = ( ((63.0 * x*x - 70.0) * x*x + 15.0) * x ) / 8.0
> return result
>
> def Pn(self,n,x):
> if n == 0:
> result = self.P0(x)
> elif n == 1:
> result = self.P1(x)
> elif n == 2:
> result = self.P2(x)
> elif n == 3:
> result = self.P3(x)
> elif n == 4:
> result = self.P4(x)
> elif n == 5:
> result = self.P5(x)
> else:
> result= ( ( 2.0 * n - 1.0 ) * x * self.Pn(n-1,x) -
> ( n - 1.0 ) * self.Pn(n-2,x) ) / n
> return result
>
>def test():
> import Numeric
> L = Legendre()
> for x in Numeric.arrayrange( -1.0, 1.0, 0.1 ):
> print '%6.2f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f
>%7.4f' % \
> ( x, L.P0(x), L.P1(x), L.P2(x), L.P3(x), L.P4(x), L.P5(x),
> L.Pn(5,x), L.Pn(7,x) )
>
>if __name__ == '__main__':
> test()
>
>_______________________________________________
>Matrix-SIG maillist - Matrix-SIG@python.org
>http://www.python.org/mailman/listinfo/matrix-sig
*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-
Rob Managan mailto://managan@llnl.gov
LLNL ph: 925-423-0903
P.O. Box 808, L-098 FAX: 925-423-5804
Livermore, CA 94551-0808