[Matrix-SIG] Re: cephesmodule

Mike Miller miller5@uiuc.edu
10 Mar 1999 16:01:29 -0600


>>>>> "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**3 - 3.0 * x ) / 2.0
        return result

    def P4(self,x):
        result = ( 35.0 * x**4 - 30.0 * x**2 + 3.0 ) / 8.0
        return result

    def P5(self,x):
        result = ( 63.0 * x**5 - 70.0 * x**3 + 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()