]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/orthogonal_polynomials.py
Fix legendre_p for finite field elements.
[sage.d.git] / mjo / orthogonal_polynomials.py
1 from sage.all import *
2
3 def legendre_p(n, x, a = -1, b = 1):
4 """
5 Returns the ``n``th Legendre polynomial of the first kind over the
6 interval [a, b] with respect to ``x``.
7
8 When [a,b] is not [-1,1], we scale the standard Legendre
9 polynomial (which is defined over [-1,1]) via an affine map. The
10 resulting polynomials are still orthogonal and possess the
11 property that `P(a) = P(b) = 1`.
12
13 INPUT:
14
15 * ``n`` -- The index of the polynomial.
16
17 * ``x`` -- Either the variable to use as the independent
18 variable in the polynomial, or a point at which to evaluate
19 the polynomial.
20
21 * ``a`` -- The "left" endpoint of the interval. Must be a real number.
22
23 * ``b`` -- The "right" endpoint of the interval. Must be a real number.
24
25 OUTPUT:
26
27 If ``x`` is a variable, a polynomial (symbolic expression) will be
28 returned. Otherwise, the value of the ``n``th polynomial at ``x``
29 will be returned.
30
31 EXAMPLES:
32
33 Create the standard Legendre polynomials in `x`::
34
35 sage: legendre_p(0,x)
36 1
37 sage: legendre_p(1,x)
38 x
39
40 Reuse the variable from a polynomial ring::
41 sage: P.<t> = QQ[]
42 sage: legendre_p(2,t)
43 3/2*t^2 - 1/2
44
45 If ``x`` is a real number, the result should be as well::
46
47 sage: legendre_p(3, 1.1)
48 1.67750000000000
49
50 Similarly for complex numbers::
51
52 sage: legendre_p(3, 1 + I)
53 7/2*I - 13/2
54
55 Even matrices work::
56
57 sage: legendre_p(3, MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
58 [-179 242]
59 [-484 547]
60
61 And finite field elements::
62
63 sage: legendre_P(3, GF(11)(5))
64 8
65
66 TESTS:
67
68 We should agree with Maxima for all `n`::
69
70 sage: eq = lambda k: bool(legendre_p(k,x) == legendre_P(k,x))
71 sage: all([eq(k) for k in range(0,20) ]) # long time
72 True
73
74 We can evaluate the result of the zeroth polynomial::
75
76 sage: f = legendre_p(0,x)
77 sage: f(x=10)
78 1
79
80 We should have |P(a)| = |P(b)| = 1 for all a,b::
81
82 sage: a = RR.random_element()
83 sage: b = RR.random_element()
84 sage: k = ZZ.random_element(20)
85 sage: P = legendre_p(k, x, a, b)
86 sage: abs(P(x=a)) # abs tol 1e-12
87 1
88 sage: abs(P(x=b)) # abs tol 1e-12
89 1
90
91 Two different polynomials should be orthogonal with respect to the
92 inner product over `[a,b]`. Note that this test can fail if QQ is
93 replaced with RR, since integrate() can return NaN::
94
95 sage: a = QQ.random_element()
96 sage: b = QQ.random_element()
97 sage: j = ZZ.random_element(20)
98 sage: k = j + 1
99 sage: Pj = legendre_p(j, x, a, b)
100 sage: Pk = legendre_p(k, x, a, b)
101 sage: integrate(Pj*Pk, x, a, b) # abs tol 1e-12
102 0
103
104 The first few polynomials shifted to [0,1] are known to be::
105
106 sage: p0 = 1
107 sage: p1 = 2*x - 1
108 sage: p2 = 6*x^2 - 6*x + 1
109 sage: p3 = 20*x^3 - 30*x^2 + 12*x - 1
110 sage: bool(legendre_p(0, x, 0, 1) == p0)
111 True
112 sage: bool(legendre_p(1, x, 0, 1) == p1)
113 True
114 sage: bool(legendre_p(2, x, 0, 1) == p2)
115 True
116 sage: bool(legendre_p(3, x, 0, 1) == p3)
117 True
118
119 The zeroth polynomial is an element of the ring that we're working
120 in::
121
122 sage: legendre_p(0, MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
123 [1 0]
124 [0 1]
125
126 """
127 if not n in ZZ:
128 raise TypeError('n must be a natural number')
129
130 if n < 0:
131 raise ValueError('n must be nonnegative')
132
133 if not (a in RR and b in RR):
134 raise TypeError('both `a` and `b` must be real numbers')
135
136 if n == 0:
137 # Easy base case, save time. Attempt to return a value in the
138 # same field/ring as `x`.
139 return x.parent()(1)
140
141 # Even though we know a,b are real we use a different ring. We
142 # prefer ZZ so that we can support division of finite field
143 # elements by (a-b). Eventually this should be supported for QQ as
144 # well, although it does not work at the moment. The preference of
145 # SR over RR is to return something attractive when e.g. a=pi.
146 if a in ZZ:
147 a = ZZ(a)
148 else:
149 a = SR(a)
150
151 if b in ZZ:
152 b = ZZ(b)
153 else:
154 b = SR(b)
155
156 # Ensure that (2**n) is an element of ZZ. This is used later --
157 # we can divide finite field elements by integers but we can't
158 # multiply them by rationals at the moment.
159 n = ZZ(n)
160
161 def phi(t):
162 # This is an affine map from [a,b] into [-1,1] and so
163 # preserves orthogonality.
164 return (a + b - 2*t)/(a - b)
165
166 def c(m):
167 return binomial(n,m)*binomial(n, n-m)
168
169 def g(m):
170 # As given in A&S, but with `x` replaced by `phi(x)`.
171 return ( ((phi(x) - 1)**(n-m)) * (phi(x) + 1)**m )
172
173 # From Abramowitz & Stegun, (22.3.2) with alpha = beta = 0.
174 # Also massaged to support finite field elements.
175 P = sum([ c(m)*g(m) for m in range(0,n+1) ])/(2**n)
176
177 return P