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