]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/interpolation.py
Add the Hermite functions to interpolation.py.
[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 def divided_difference(xs, ys):
97 """
98 Return the Newton divided difference of the points (xs[k],
99 ys[k]). Reference:
100
101 http://en.wikipedia.org/wiki/Divided_differences
102
103 INPUT:
104
105 - ``xs`` -- The list of x-values.
106
107 - ``ys`` -- The function values at `xs`.
108
109 OUTPUT:
110
111 The (possibly symbolic) divided difference function.
112
113 TESTS::
114
115 sage: xs = [0]
116 sage: ys = map(sin, xs)
117 sage: divided_difference(xs, ys)
118 0
119 sage: xs = [0, pi]
120 sage: ys = map(sin, xs)
121 sage: divided_difference(xs, ys)
122 0
123 sage: xs = [0, pi, 2*pi]
124 sage: ys = map(sin, xs)
125 sage: divided_difference(xs, ys)
126 0
127
128 We try something entirely symbolic::
129
130 sage: f = function('f', x)
131 sage: divided_difference([x], [f(x=x)])
132 f(x)
133 sage: x1,x2 = var('x1,x2')
134 sage: divided_difference([x1,x2], [f(x=x1),f(x=x2)])
135 f(x1)/(x1 - x2) - f(x2)/(x1 - x2)
136
137 """
138 coeffs = divided_difference_coefficients(xs)
139 v_cs = vector(coeffs)
140 v_ys = vector(ys)
141 return v_cs.dot_product(v_ys)
142
143
144 def newton_polynomial(x, xs, ys):
145 """
146 Return the Newton form of the interpolating polynomial of the
147 points (xs[k], ys[k]) in the variable `x`.
148
149 INPUT:
150
151 - ``x`` -- The independent variable to use for the interpolating
152 polynomial.
153
154 - ``xs`` -- The list of x-values.
155
156 - ``ys`` -- The function values at `xs`.
157
158 OUTPUT:
159
160 A symbolic expression.
161
162 TESTS:
163
164 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
165 sage: ys = map(sin, xs)
166 sage: L = lagrange_polynomial(x, xs, ys)
167 sage: N = newton_polynomial(x, xs, ys)
168 sage: bool(N == L)
169 True
170
171 """
172 degree = len(xs) - 1
173
174 N = SR(0)
175
176 for k in range(0, degree+1):
177 term = divided_difference(xs[:k+1], ys[:k+1])
178 term *= product([ x - xk for xk in xs[:k]])
179 N += term
180
181 return N
182
183
184 def hermite_coefficient(k, x, xs):
185 """
186 Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160.
187
188 INPUT:
189
190 - ``k`` -- The index of the coefficient.
191
192 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
193
194 - ``xs`` -- The list of points at which the function values are
195 known.
196
197 OUTPUT:
198
199 A symbolic expression.
200
201 """
202 lk = lagrange_coefficient(k, x, xs)
203 return (1 - 2*lk.diff(x)(x=xs[k])*(x - xs[k]))*(lk**2)
204
205
206 def hermite_deriv_coefficient(k, x, xs):
207 """
208 Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See
209 Atkinson, p. 160.
210
211 INPUT:
212
213 - ``k`` -- The index of the coefficient.
214
215 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
216
217 - ``xs`` -- The list of points at which the function values are
218 known.
219
220 OUTPUT:
221
222 A symbolic expression.
223
224 """
225 lk = lagrange_coefficient(k, x, xs)
226 return (x - xs[k])*(lk**2)
227
228
229 def hermite_interpolant(x, xs, ys, y_primes):
230 """
231 Return the Hermite interpolant `H(x)` such that H(xs[k]) = ys[k]
232 and H'(xs[k]) = y_primes[k] for each k.
233
234 Reference: Atkinson, p. 160.
235
236 INPUT:
237
238 - ``x`` -- The symbolic variable to use as the argument of H(x).
239
240 - ``xs`` -- The list of points at which the function values are
241 known.
242
243 - ``ys`` -- The function values at the `xs`.
244
245 - ``y_primes`` -- The derivatives at the `xs`.
246
247 OUTPUT:
248
249 A symbolic expression.
250
251 TESTS:
252
253 sage: xs = [ 0, pi/6, pi/2 ]
254 sage: ys = map(sin, xs)
255 sage: y_primes = map(cos, xs)
256 sage: H = hermite_interpolant(x, xs, ys, y_primes)
257 sage: expected = -27/4*(pi - 6*x)*(pi - 2*x)^2*sqrt(3)*x^2/pi^4
258 sage: expected += (5*(pi - 2*x)/pi + 1)*(pi - 6*x)^2*x^2/pi^4
259 sage: expected += 81/2*((pi - 6*x)/pi + 1)*(pi - 2*x)^2*x^2/pi^4
260 sage: expected += (pi - 6*x)^2*(pi - 2*x)^2*x/pi^4
261 sage: bool(H == expected)
262 True
263
264 """
265 s1 = sum([ ys[k] * hermite_coefficient(k, x, xs)
266 for k in range(0, len(xs)) ])
267
268 s2 = sum([ y_primes[k] * hermite_deriv_coefficient(k, x, xs)
269 for k in range(0, len(xs)) ])
270
271 return (s1 + s2)