]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/orthogonal_polynomials.py
Add an example of a least squares problem.
[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 Solve a simple least squares problem over `[-\pi, \pi]`::
67
68 sage: a = -pi
69 sage: b = pi
70 sage: def inner_product(v1, v2):
71 ... return integrate(v1*v2, x, a, b)
72 ...
73 sage: def norm(v):
74 ... return sqrt(inner_product(v,v))
75 ...
76 sage: def project(basis, v):
77 ... return sum([ inner_product(v, b)*b/norm(b)**2
78 ... for b in basis])
79 ...
80 sage: f = sin(x)
81 sage: legendre_basis = [ legendre_p(k, x, a, b) for k in range(0,4) ]
82 sage: proj = project(legendre_basis, f)
83 sage: proj.simplify_trig()
84 5/2*(7*(pi^2 - 15)*x^3 - 3*(pi^4 - 21*pi^2)*x)/pi^6
85
86 TESTS:
87
88 We should agree with Maxima for all `n`::
89
90 sage: eq = lambda k: bool(legendre_p(k,x) == legendre_P(k,x))
91 sage: all([eq(k) for k in range(0,20) ]) # long time
92 True
93
94 We can evaluate the result of the zeroth polynomial::
95
96 sage: f = legendre_p(0,x)
97 sage: f(x=10)
98 1
99
100 We should have |P(a)| = |P(b)| = 1 for all a,b::
101
102 sage: a = RR.random_element()
103 sage: b = RR.random_element()
104 sage: k = ZZ.random_element(20)
105 sage: P = legendre_p(k, x, a, b)
106 sage: abs(P(x=a)) # abs tol 1e-12
107 1
108 sage: abs(P(x=b)) # abs tol 1e-12
109 1
110
111 Two different polynomials should be orthogonal with respect to the
112 inner product over `[a,b]`. Note that this test can fail if QQ is
113 replaced with RR, since integrate() can return NaN::
114
115 sage: a = QQ.random_element()
116 sage: b = QQ.random_element()
117 sage: j = ZZ.random_element(20)
118 sage: k = j + 1
119 sage: Pj = legendre_p(j, x, a, b)
120 sage: Pk = legendre_p(k, x, a, b)
121 sage: integrate(Pj*Pk, x, a, b) # abs tol 1e-12
122 0
123
124 The first few polynomials shifted to [0,1] are known to be::
125
126 sage: p0 = 1
127 sage: p1 = 2*x - 1
128 sage: p2 = 6*x^2 - 6*x + 1
129 sage: p3 = 20*x^3 - 30*x^2 + 12*x - 1
130 sage: bool(legendre_p(0, x, 0, 1) == p0)
131 True
132 sage: bool(legendre_p(1, x, 0, 1) == p1)
133 True
134 sage: bool(legendre_p(2, x, 0, 1) == p2)
135 True
136 sage: bool(legendre_p(3, x, 0, 1) == p3)
137 True
138
139 The zeroth polynomial is an element of the ring that we're working
140 in::
141
142 sage: legendre_p(0, MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
143 [1 0]
144 [0 1]
145
146 """
147 if not n in ZZ:
148 raise TypeError('n must be a natural number')
149
150 if n < 0:
151 raise ValueError('n must be nonnegative')
152
153 if not (a in RR and b in RR):
154 raise TypeError('both `a` and `b` must be real numbers')
155
156 if n == 0:
157 # Easy base case, save time. Attempt to return a value in the
158 # same field/ring as `x`.
159 return x.parent()(1)
160
161 # Even though we know a,b are real we use a different ring. We
162 # prefer ZZ so that we can support division of finite field
163 # elements by (a-b). Eventually this should be supported for QQ as
164 # well, although it does not work at the moment. The preference of
165 # SR over RR is to return something attractive when e.g. a=pi.
166 if a in ZZ:
167 a = ZZ(a)
168 else:
169 a = SR(a)
170
171 if b in ZZ:
172 b = ZZ(b)
173 else:
174 b = SR(b)
175
176 # Ensure that (2**n) is an element of ZZ. This is used later --
177 # we can divide finite field elements by integers but we can't
178 # multiply them by rationals at the moment.
179 n = ZZ(n)
180
181 def phi(t):
182 # This is an affine map from [a,b] into [-1,1] and so
183 # preserves orthogonality.
184 return (a + b - 2*t)/(a - b)
185
186 def c(m):
187 return binomial(n,m)*binomial(n, n-m)
188
189 def g(m):
190 # As given in A&S, but with `x` replaced by `phi(x)`.
191 return ( ((phi(x) - 1)**(n-m)) * (phi(x) + 1)**m )
192
193 # From Abramowitz & Stegun, (22.3.2) with alpha = beta = 0.
194 # Also massaged to support finite field elements.
195 P = sum([ c(m)*g(m) for m in range(0,n+1) ])/(2**n)
196
197 return P