]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/interpolation.py
mjo/interpolation.py: use xrange everywhere.
[sage.d.git] / mjo / interpolation.py
1 from sage.all import *
2 product = prod
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 xrange(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 SETUP::
48
49 sage: from mjo.interpolation import lagrange_coefficient
50
51 TESTS::
52
53 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
54 sage: lagrange_coefficient(0, x, xs)
55 1/8*(pi + 6*x)*(pi - 2*x)*(pi - 6*x)*x/pi^4
56
57 """
58 numerator = lagrange_psi(x, xs)/(x - xs[k])
59 denominator = lagrange_denominator(k, xs)
60
61 return (numerator / denominator)
62
63
64
65 def lagrange_polynomial(x, xs, ys):
66 """
67 Return the Lagrange form of the interpolating polynomial in `x`
68 at the points (xs[k], ys[k]).
69
70 INPUT:
71
72 - ``x`` - The independent variable of the resulting polynomial.
73
74 - ``xs`` - The list of points at which we interpolate `f`.
75
76 - ``ys`` - The function values at `xs`.
77
78 OUTPUT:
79
80 A symbolic expression (polynomial) interpolating each (xs[k], ys[k]).
81
82 SETUP::
83
84 sage: from mjo.interpolation import lagrange_polynomial
85
86 TESTS::
87
88 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
89 sage: ys = map(sin, xs)
90 sage: L = lagrange_polynomial(x, xs, ys)
91 sage: expected = 27/16*(pi - 6*x)*(pi - 2*x)*(pi + 2*x)*x/pi^4
92 sage: expected -= 1/8*(pi - 6*x)*(pi - 2*x)*(pi + 6*x)*x/pi^4
93 sage: expected -= 1/8*(pi - 6*x)*(pi + 2*x)*(pi + 6*x)*x/pi^4
94 sage: expected += 27/16*(pi - 2*x)*(pi + 2*x)*(pi + 6*x)*x/pi^4
95 sage: bool(L == expected)
96 True
97
98 """
99 ls = [ lagrange_coefficient(k, x, xs) for k in xrange(len(xs)) ]
100 sigma = sum([ ys[k] * ls[k] for k in xrange(len(xs)) ])
101 return sigma
102
103
104
105 def lagrange_interpolate(f, x, xs):
106 """
107 Interpolate the function ``f`` at the points ``xs`` using the
108 Lagrange form of the interpolating polynomial.
109
110 INPUT:
111
112 - ``f`` -- The function to interpolate.
113
114 - ``x`` -- The independent variable of the resulting polynomial.
115
116 - ``xs`` -- A list of points at which to interpolate ``f``.
117
118 OUTPUT:
119
120 A polynomial in ``x`` which interpolates ``f`` at ``xs``.
121
122 SETUP::
123
124 sage: from mjo.interpolation import lagrange_interpolate
125
126 EXAMPLES:
127
128 We're exact on polynomials of degree `n` if we use `n+1` points::
129
130 sage: t = SR.symbol('t', domain='real')
131 sage: lagrange_interpolate(x^2, t, [-1,0,1]).simplify_rational()
132 t^2
133
134 """
135 # f should be a function of one variable.
136 z = f.variables()[0]
137 # We're really just doing map(f, xs) here; the additional
138 # gymnastics are to avoid a warning when calling `f` with an
139 # unnamed argument.
140 ys = [ f({z: xk}) for xk in xs ]
141 return lagrange_polynomial(x, xs, ys)
142
143
144
145 def divided_difference_coefficients(xs):
146 """
147 Assuming some function `f`, compute the coefficients of the
148 divided difference f[xs[0], ..., xs[n]].
149
150 SETUP::
151
152 sage: from mjo.interpolation import divided_difference_coefficients
153
154 TESTS::
155
156 sage: divided_difference_coefficients([0])
157 [1]
158 sage: divided_difference_coefficients([0, pi])
159 [-1/pi, 1/pi]
160 sage: divided_difference_coefficients([0, pi, 2*pi])
161 [1/2/pi^2, -1/pi^2, 1/2/pi^2]
162
163 """
164 coeffs = [ QQ(1)/lagrange_denominator(k, xs) for k in xrange(len(xs)) ]
165 return coeffs
166
167
168 def divided_difference(xs, ys):
169 """
170 Return the Newton divided difference of the points (xs[k],
171 ys[k]). Reference:
172
173 http://en.wikipedia.org/wiki/Divided_differences
174
175 INPUT:
176
177 - ``xs`` -- The list of x-values.
178
179 - ``ys`` -- The function values at `xs`.
180
181 OUTPUT:
182
183 The (possibly symbolic) divided difference function.
184
185 SETUP::
186
187 sage: from mjo.interpolation import divided_difference
188
189 TESTS::
190
191 sage: xs = [0]
192 sage: ys = map(sin, xs)
193 sage: divided_difference(xs, ys)
194 0
195 sage: xs = [0, pi]
196 sage: ys = map(sin, xs)
197 sage: divided_difference(xs, ys)
198 0
199 sage: xs = [0, pi, 2*pi]
200 sage: ys = map(sin, xs)
201 sage: divided_difference(xs, ys)
202 0
203
204 We try something entirely symbolic::
205
206 sage: f = function('f')(x)
207 sage: divided_difference([x], [f(x=x)])
208 f(x)
209 sage: x1,x2 = SR.var('x1,x2')
210 sage: divided_difference([x1,x2], [f(x=x1),f(x=x2)])
211 f(x1)/(x1 - x2) - f(x2)/(x1 - x2)
212
213 """
214 coeffs = divided_difference_coefficients(xs)
215 v_cs = vector(coeffs)
216 v_ys = vector(ys)
217 return v_cs.dot_product(v_ys)
218
219
220 def newton_polynomial(x, xs, ys):
221 """
222 Return the Newton form of the interpolating polynomial of the
223 points (xs[k], ys[k]) in the variable `x`.
224
225 INPUT:
226
227 - ``x`` -- The independent variable to use for the interpolating
228 polynomial.
229
230 - ``xs`` -- The list of x-values.
231
232 - ``ys`` -- The function values at `xs`.
233
234 OUTPUT:
235
236 A symbolic expression.
237
238 SETUP::
239
240 sage: from mjo.interpolation import lagrange_polynomial, newton_polynomial
241
242 TESTS::
243
244 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
245 sage: ys = map(sin, xs)
246 sage: L = lagrange_polynomial(x, xs, ys)
247 sage: N = newton_polynomial(x, xs, ys)
248 sage: bool(N == L)
249 True
250
251 """
252 degree = len(xs) - 1
253
254 N = SR(0)
255
256 for k in xrange(degree+1):
257 term = divided_difference(xs[:k+1], ys[:k+1])
258 term *= lagrange_psi(x, xs[:k])
259 N += term
260
261 return N
262
263
264 def hermite_coefficient(k, x, xs):
265 """
266 Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160.
267
268 INPUT:
269
270 - ``k`` -- The index of the coefficient.
271
272 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
273
274 - ``xs`` -- The list of points at which the function values are
275 known.
276
277 OUTPUT:
278
279 A symbolic expression.
280
281 """
282 lk = lagrange_coefficient(k, x, xs)
283 return (1 - 2*lk.diff(x)(x=xs[k])*(x - xs[k]))*(lk**2)
284
285
286 def hermite_deriv_coefficient(k, x, xs):
287 """
288 Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See
289 Atkinson, p. 160.
290
291 INPUT:
292
293 - ``k`` -- The index of the coefficient.
294
295 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
296
297 - ``xs`` -- The list of points at which the function values are
298 known.
299
300 OUTPUT:
301
302 A symbolic expression.
303
304 """
305 lk = lagrange_coefficient(k, x, xs)
306 return (x - xs[k])*(lk**2)
307
308
309 def hermite_interpolant(x, xs, ys, y_primes):
310 """
311 Return the Hermite interpolant `H(x)` such that H(xs[k]) = ys[k]
312 and H'(xs[k]) = y_primes[k] for each k.
313
314 Reference: Atkinson, p. 160.
315
316 INPUT:
317
318 - ``x`` -- The symbolic variable to use as the argument of H(x).
319
320 - ``xs`` -- The list of points at which the function values are
321 known.
322
323 - ``ys`` -- The function values at the `xs`.
324
325 - ``y_primes`` -- The derivatives at the `xs`.
326
327 OUTPUT:
328
329 A symbolic expression.
330
331 SETUP::
332
333 sage: from mjo.interpolation import hermite_interpolant
334
335 TESTS::
336
337 sage: xs = [ 0, pi/6, pi/2 ]
338 sage: ys = map(sin, xs)
339 sage: y_primes = map(cos, xs)
340 sage: H = hermite_interpolant(x, xs, ys, y_primes)
341 sage: expected = -27/4*(pi - 6*x)*(pi - 2*x)^2*sqrt(3)*x^2/pi^4
342 sage: expected += (5*(pi - 2*x)/pi + 1)*(pi - 6*x)^2*x^2/pi^4
343 sage: expected += 81/2*((pi - 6*x)/pi + 1)*(pi - 2*x)^2*x^2/pi^4
344 sage: expected += (pi - 6*x)^2*(pi - 2*x)^2*x/pi^4
345 sage: bool(H == expected)
346 True
347
348 """
349 s1 = sum([ ys[k] * hermite_coefficient(k, x, xs)
350 for k in xrange(len(xs)) ])
351
352 s2 = sum([ y_primes[k] * hermite_deriv_coefficient(k, x, xs)
353 for k in xrange(len(xs)) ])
354
355 return (s1 + s2)
356
357
358 def lagrange_psi(x, xs):
359 """
360 The function,
361
362 Psi(x) = (x - xs[0])*(x - xs[1])* ... *(x - xs[-1])
363
364 used in Lagrange and Hermite interpolation.
365
366 INPUT:
367
368 - ``x`` -- The independent variable of the resulting expression.
369
370 - ``xs`` -- A list of points.
371
372 OUTPUT:
373
374 A symbolic expression in one variable, `x`.
375
376 """
377
378 return product([ (x - xj) for xj in xs ])