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