]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/interpolation.py
5 def lagrange_denominator(k
, xs
):
7 Return the denominator of the kth Lagrange coefficient.
11 - ``k`` -- The index of the coefficient.
13 - ``xs`` -- The list of points at which the function values are
18 The product of all xs[j] with j != k.
21 return product([xs
[k
] - xs
[j
] for j
in range(0, len(xs
)) if j
!= k
])
24 def lagrange_coefficient(k
, x
, xs
):
26 Returns the coefficient function l_{k}(variable) of y_{k} in the
27 Lagrange polynomial of f. See,
29 http://en.wikipedia.org/wiki/Lagrange_polynomial
35 - ``k`` -- The index of the coefficient.
37 - ``x`` -- The symbolic variable to use for the first argument
40 - ``xs`` -- The list of points at which the function values are
45 A symbolic expression of one variable.
49 sage: from mjo.interpolation import lagrange_coefficient
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
58 numerator
= lagrange_psi(x
, xs
)/(x
- xs
[k
])
59 denominator
= lagrange_denominator(k
, xs
)
61 return (numerator
/ denominator
)
65 def lagrange_polynomial(x
, xs
, ys
):
67 Return the Lagrange form of the interpolating polynomial in `x`
68 at the points (xs[k], ys[k]).
72 - ``x`` - The independent variable of the resulting polynomial.
74 - ``xs`` - The list of points at which we interpolate `f`.
76 - ``ys`` - The function values at `xs`.
80 A symbolic expression (polynomial) interpolating each (xs[k], ys[k]).
84 sage: from mjo.interpolation import lagrange_polynomial
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)
99 ls
= [ lagrange_coefficient(k
, x
, xs
) for k
in range(0, len(xs
)) ]
100 sigma
= sum([ ys
[k
] * ls
[k
] for k
in range(0, len(xs
)) ])
105 def lagrange_interpolate(f
, x
, xs
):
107 Interpolate the function ``f`` at the points ``xs`` using the
108 Lagrange form of the interpolating polynomial.
112 - ``f`` -- The function to interpolate.
114 - ``x`` -- The independent variable of the resulting polynomial.
116 - ``xs`` -- A list of points at which to interpolate ``f``.
120 A polynomial in ``x`` which interpolates ``f`` at ``xs``.
124 sage: from mjo.interpolation import lagrange_interpolate
128 We're exact on polynomials of degree `n` if we use `n+1` points::
130 sage: t = SR.symbol('t', domain='real')
131 sage: lagrange_interpolate(x^2, t, [-1,0,1]).simplify_rational()
135 # f should be a function of one variable.
137 # We're really just doing map(f, xs) here; the additional
138 # gymnastics are to avoid a warning when calling `f` with an
140 ys
= [ f({z: xk}
) for xk
in xs
]
141 return lagrange_polynomial(x
, xs
, ys
)
145 def divided_difference_coefficients(xs
):
147 Assuming some function `f`, compute the coefficients of the
148 divided difference f[xs[0], ..., xs[n]].
152 sage: from mjo.interpolation import divided_difference_coefficients
156 sage: divided_difference_coefficients([0])
158 sage: divided_difference_coefficients([0, pi])
160 sage: divided_difference_coefficients([0, pi, 2*pi])
161 [1/2/pi^2, -1/pi^2, 1/2/pi^2]
164 coeffs
= [ QQ(1)/lagrange_denominator(k
, xs
) for k
in range(0, len(xs
)) ]
168 def divided_difference(xs
, ys
):
170 Return the Newton divided difference of the points (xs[k],
173 http://en.wikipedia.org/wiki/Divided_differences
177 - ``xs`` -- The list of x-values.
179 - ``ys`` -- The function values at `xs`.
183 The (possibly symbolic) divided difference function.
187 sage: from mjo.interpolation import divided_difference
192 sage: ys = map(sin, xs)
193 sage: divided_difference(xs, ys)
196 sage: ys = map(sin, xs)
197 sage: divided_difference(xs, ys)
199 sage: xs = [0, pi, 2*pi]
200 sage: ys = map(sin, xs)
201 sage: divided_difference(xs, ys)
204 We try something entirely symbolic::
206 sage: f = function('f')(x)
207 sage: divided_difference([x], [f(x=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)
214 coeffs
= divided_difference_coefficients(xs
)
215 v_cs
= vector(coeffs
)
217 return v_cs
.dot_product(v_ys
)
220 def newton_polynomial(x
, xs
, ys
):
222 Return the Newton form of the interpolating polynomial of the
223 points (xs[k], ys[k]) in the variable `x`.
227 - ``x`` -- The independent variable to use for the interpolating
230 - ``xs`` -- The list of x-values.
232 - ``ys`` -- The function values at `xs`.
236 A symbolic expression.
240 sage: from mjo.interpolation import lagrange_polynomial, newton_polynomial
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)
256 for k
in range(0, degree
+1):
257 term
= divided_difference(xs
[:k
+1], ys
[:k
+1])
258 term
*= lagrange_psi(x
, xs
[:k
])
264 def hermite_coefficient(k
, x
, xs
):
266 Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160.
270 - ``k`` -- The index of the coefficient.
272 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
274 - ``xs`` -- The list of points at which the function values are
279 A symbolic expression.
282 lk
= lagrange_coefficient(k
, x
, xs
)
283 return (1 - 2*lk
.diff(x
)(x
=xs
[k
])*(x
- xs
[k
]))*(lk
**2)
286 def hermite_deriv_coefficient(k
, x
, xs
):
288 Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See
293 - ``k`` -- The index of the coefficient.
295 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
297 - ``xs`` -- The list of points at which the function values are
302 A symbolic expression.
305 lk
= lagrange_coefficient(k
, x
, xs
)
306 return (x
- xs
[k
])*(lk
**2)
309 def hermite_interpolant(x
, xs
, ys
, y_primes
):
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.
314 Reference: Atkinson, p. 160.
318 - ``x`` -- The symbolic variable to use as the argument of H(x).
320 - ``xs`` -- The list of points at which the function values are
323 - ``ys`` -- The function values at the `xs`.
325 - ``y_primes`` -- The derivatives at the `xs`.
329 A symbolic expression.
333 sage: from mjo.interpolation import hermite_interpolant
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)
349 s1
= sum([ ys
[k
] * hermite_coefficient(k
, x
, xs
)
350 for k
in range(0, len(xs
)) ])
352 s2
= sum([ y_primes
[k
] * hermite_deriv_coefficient(k
, x
, xs
)
353 for k
in range(0, len(xs
)) ])
358 def lagrange_psi(x
, xs
):
362 Psi(x) = (x - xs[0])*(x - xs[1])* ... *(x - xs[-1])
364 used in Lagrange and Hermite interpolation.
368 - ``x`` -- The independent variable of the resulting expression.
370 - ``xs`` -- A list of points.
374 A symbolic expression in one variable, `x`.
378 return product([ (x
- xj
) for xj
in xs
])