-
Notifications
You must be signed in to change notification settings - Fork 0
/
Legendre.py
53 lines (44 loc) · 1.11 KB
/
Legendre.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Legendre.py
#
# Daniel R. Reynolds
# SMU Mathematics
# Math 4315
# imports
import numpy
def Legendre(x, k):
"""
Usage: p = Legendre(x,k)
Function to evaluate the kth Legendre polynomial at a point x.
Inputs: x - evaluation point(s)
k - Legendre polynomial index
Outputs: p - value(s) of p_k(x)
"""
# quick return for first two Legendre polynomials
if (k == 0):
if (numpy.isscalar(x)): # if problem is scalar-valued
return 1.0
else:
return numpy.ones(x.shape)
if (k == 1):
if (numpy.isscalar(x)):
return x
else:
return x.copy()
# initialize 3-term recurrence
if (numpy.isscalar(x)):
p0 = 1.0
p1 = x
else:
p0 = numpy.ones(x.shape)
p1 = x.copy()
# perform recurrence to evaluate p, and update 'old' values
for i in range(2,k+1):
p = (2.0*i-1.0)/i*x*p1 - (i-1.0)/i*p0
if (numpy.isscalar(x)):
p0 = p1
p1 = p
else:
p0 = p1.copy()
p1 = p.copy()
return p
# end of file