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