]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/interpolation.py
Clean up a little bit of the interpolation code.
[sage.d.git] / mjo / interpolation.py
1 from sage.all import *
2 from misc import product
3
4
5 def lagrange_denominator(k, xs):
6 """
7 Return the denominator of the kth Lagrange coefficient.
8
9 INPUT:
10
11 - ``k`` -- The index of the coefficient.
12
13 - ``xs`` -- The list of points at which the function values are
14 known.
15
16 OUTPUT:
17
18 The product of all xs[j] with j != k.
19
20 """
21 return product([xs[k] - xs[j] for j in range(0, len(xs)) if j != k])
22
23
24 def lagrange_coefficient(k, x, xs):
25 """
26 Returns the coefficient function l_{k}(variable) of y_{k} in the
27 Lagrange polynomial of f. See,
28
29 http://en.wikipedia.org/wiki/Lagrange_polynomial
30
31 for more information.
32
33 INPUT:
34
35 - ``k`` -- The index of the coefficient.
36
37 - ``x`` -- The symbolic variable to use for the first argument
38 of l_{k}.
39
40 - ``xs`` -- The list of points at which the function values are
41 known.
42
43 OUTPUT:
44
45 A symbolic expression of one variable.
46
47 TESTS::
48
49 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
50 sage: lagrange_coefficient(0, x, xs)
51 1/8*(pi - 6*x)*(pi - 2*x)*(pi + 6*x)*x/pi^4
52
53 """
54 numerator = lagrange_psi(x, xs)/(x - xs[k])
55 denominator = lagrange_denominator(k, xs)
56
57 return (numerator / denominator)
58
59
60
61 def lagrange_polynomial(x, xs, ys):
62 """
63 Return the Lagrange form of the interpolation polynomial in `x` of
64 at the points (xs[k], ys[k]).
65
66 INPUT:
67
68 - ``x`` - The independent variable of the resulting polynomial.
69
70 - ``xs`` - The list of points at which we interpolate `f`.
71
72 - ``ys`` - The function values at `xs`.
73
74 OUTPUT:
75
76 A symbolic expression (polynomial) interpolating each (xs[k], ys[k]).
77
78 TESTS::
79
80 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
81 sage: ys = map(sin, xs)
82 sage: L = lagrange_polynomial(x, xs, ys)
83 sage: expected = 27/16*(pi - 6*x)*(pi - 2*x)*(pi + 2*x)*x/pi^4
84 sage: expected -= 1/8*(pi - 6*x)*(pi - 2*x)*(pi + 6*x)*x/pi^4
85 sage: expected -= 1/8*(pi - 6*x)*(pi + 2*x)*(pi + 6*x)*x/pi^4
86 sage: expected += 27/16*(pi - 2*x)*(pi + 2*x)*(pi + 6*x)*x/pi^4
87 sage: bool(L == expected)
88 True
89
90 """
91 ls = [ lagrange_coefficient(k, x, xs) for k in range(0, len(xs)) ]
92 sigma = sum([ ys[k] * ls[k] for k in range(0, len(xs)) ])
93 return sigma
94
95
96
97 def divided_difference_coefficients(xs):
98 """
99 Assuming some function `f`, compute the coefficients of the
100 divided difference f[xs[0], ..., xs[n]].
101
102 TESTS:
103
104 sage: divided_difference_coefficients([0])
105 [1]
106 sage: divided_difference_coefficients([0, pi])
107 [-1/pi, 1/pi]
108 sage: divided_difference_coefficients([0, pi, 2*pi])
109 [1/2/pi^2, -1/pi^2, 1/2/pi^2]
110
111 """
112 coeffs = [ QQ(1)/lagrange_denominator(k, xs) for k in range(0, len(xs)) ]
113 return coeffs
114
115
116 def divided_difference(xs, ys):
117 """
118 Return the Newton divided difference of the points (xs[k],
119 ys[k]). Reference:
120
121 http://en.wikipedia.org/wiki/Divided_differences
122
123 INPUT:
124
125 - ``xs`` -- The list of x-values.
126
127 - ``ys`` -- The function values at `xs`.
128
129 OUTPUT:
130
131 The (possibly symbolic) divided difference function.
132
133 TESTS::
134
135 sage: xs = [0]
136 sage: ys = map(sin, xs)
137 sage: divided_difference(xs, ys)
138 0
139 sage: xs = [0, pi]
140 sage: ys = map(sin, xs)
141 sage: divided_difference(xs, ys)
142 0
143 sage: xs = [0, pi, 2*pi]
144 sage: ys = map(sin, xs)
145 sage: divided_difference(xs, ys)
146 0
147
148 We try something entirely symbolic::
149
150 sage: f = function('f', x)
151 sage: divided_difference([x], [f(x=x)])
152 f(x)
153 sage: x1,x2 = var('x1,x2')
154 sage: divided_difference([x1,x2], [f(x=x1),f(x=x2)])
155 f(x1)/(x1 - x2) - f(x2)/(x1 - x2)
156
157 """
158 coeffs = divided_difference_coefficients(xs)
159 v_cs = vector(coeffs)
160 v_ys = vector(ys)
161 return v_cs.dot_product(v_ys)
162
163
164 def newton_polynomial(x, xs, ys):
165 """
166 Return the Newton form of the interpolating polynomial of the
167 points (xs[k], ys[k]) in the variable `x`.
168
169 INPUT:
170
171 - ``x`` -- The independent variable to use for the interpolating
172 polynomial.
173
174 - ``xs`` -- The list of x-values.
175
176 - ``ys`` -- The function values at `xs`.
177
178 OUTPUT:
179
180 A symbolic expression.
181
182 TESTS:
183
184 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
185 sage: ys = map(sin, xs)
186 sage: L = lagrange_polynomial(x, xs, ys)
187 sage: N = newton_polynomial(x, xs, ys)
188 sage: bool(N == L)
189 True
190
191 """
192 degree = len(xs) - 1
193
194 N = SR(0)
195
196 for k in range(0, degree+1):
197 term = divided_difference(xs[:k+1], ys[:k+1])
198 term *= lagrange_psi(x, xs[:k])
199 N += term
200
201 return N
202
203
204 def hermite_coefficient(k, x, xs):
205 """
206 Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160.
207
208 INPUT:
209
210 - ``k`` -- The index of the coefficient.
211
212 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
213
214 - ``xs`` -- The list of points at which the function values are
215 known.
216
217 OUTPUT:
218
219 A symbolic expression.
220
221 """
222 lk = lagrange_coefficient(k, x, xs)
223 return (1 - 2*lk.diff(x)(x=xs[k])*(x - xs[k]))*(lk**2)
224
225
226 def hermite_deriv_coefficient(k, x, xs):
227 """
228 Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See
229 Atkinson, p. 160.
230
231 INPUT:
232
233 - ``k`` -- The index of the coefficient.
234
235 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
236
237 - ``xs`` -- The list of points at which the function values are
238 known.
239
240 OUTPUT:
241
242 A symbolic expression.
243
244 """
245 lk = lagrange_coefficient(k, x, xs)
246 return (x - xs[k])*(lk**2)
247
248
249 def hermite_interpolant(x, xs, ys, y_primes):
250 """
251 Return the Hermite interpolant `H(x)` such that H(xs[k]) = ys[k]
252 and H'(xs[k]) = y_primes[k] for each k.
253
254 Reference: Atkinson, p. 160.
255
256 INPUT:
257
258 - ``x`` -- The symbolic variable to use as the argument of H(x).
259
260 - ``xs`` -- The list of points at which the function values are
261 known.
262
263 - ``ys`` -- The function values at the `xs`.
264
265 - ``y_primes`` -- The derivatives at the `xs`.
266
267 OUTPUT:
268
269 A symbolic expression.
270
271 TESTS:
272
273 sage: xs = [ 0, pi/6, pi/2 ]
274 sage: ys = map(sin, xs)
275 sage: y_primes = map(cos, xs)
276 sage: H = hermite_interpolant(x, xs, ys, y_primes)
277 sage: expected = -27/4*(pi - 6*x)*(pi - 2*x)^2*sqrt(3)*x^2/pi^4
278 sage: expected += (5*(pi - 2*x)/pi + 1)*(pi - 6*x)^2*x^2/pi^4
279 sage: expected += 81/2*((pi - 6*x)/pi + 1)*(pi - 2*x)^2*x^2/pi^4
280 sage: expected += (pi - 6*x)^2*(pi - 2*x)^2*x/pi^4
281 sage: bool(H == expected)
282 True
283
284 """
285 s1 = sum([ ys[k] * hermite_coefficient(k, x, xs)
286 for k in range(0, len(xs)) ])
287
288 s2 = sum([ y_primes[k] * hermite_deriv_coefficient(k, x, xs)
289 for k in range(0, len(xs)) ])
290
291 return (s1 + s2)
292
293
294 def lagrange_psi(x, xs):
295 """
296 The function,
297
298 Psi(x) = (x - xs[0])*(x - xs[1])* ... *(x - xs[-1])
299
300 used in Lagrange and Hermite interpolation.
301
302 INPUT:
303
304 - ``x`` -- The independent variable of the resulting expression.
305
306 - ``xs`` -- A list of points.
307
308 OUTPUT:
309
310 A symbolic expression in one variable, `x`.
311
312 """
313
314 return product([ (x - xj) for xj in xs ])