]>
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: 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
54 numerator
= lagrange_psi(x
, xs
)/(x
- xs
[k
])
55 denominator
= lagrange_denominator(k
, xs
)
57 return (numerator
/ denominator
)
61 def lagrange_polynomial(x
, xs
, ys
):
63 Return the Lagrange form of the interpolating polynomial in `x`
64 at the points (xs[k], ys[k]).
68 - ``x`` - The independent variable of the resulting polynomial.
70 - ``xs`` - The list of points at which we interpolate `f`.
72 - ``ys`` - The function values at `xs`.
76 A symbolic expression (polynomial) interpolating each (xs[k], ys[k]).
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)
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
)) ])
97 def lagrange_interpolate(f
, x
, xs
):
99 Interpolate the function ``f`` at the points ``xs`` using the
100 Lagrange form of the interpolating polynomial.
104 - ``f`` -- The function to interpolate.
106 - ``x`` -- The independent variable of the resulting polynomial.
108 - ``xs`` -- A list of points at which to interpolate ``f``.
112 A polynomial in ``x`` which interpolates ``f`` at ``xs``.
116 We're exact on polynomials of degree `n` if we use `n+1` points::
118 sage: t = SR.symbol('t', domain='real')
119 sage: lagrange_interpolate(x^2, t, [-1,0,1]).simplify_rational()
123 # f should be a function of one variable.
125 # We're really just doing map(f, xs) here; the additional
126 # gymnastics are to avoid a warning when calling `f` with an
128 ys
= [ f({z: xk}
) for xk
in xs
]
129 return lagrange_polynomial(x
, xs
, ys
)
133 def divided_difference_coefficients(xs
):
135 Assuming some function `f`, compute the coefficients of the
136 divided difference f[xs[0], ..., xs[n]].
140 sage: divided_difference_coefficients([0])
142 sage: divided_difference_coefficients([0, pi])
144 sage: divided_difference_coefficients([0, pi, 2*pi])
145 [1/2/pi^2, -1/pi^2, 1/2/pi^2]
148 coeffs
= [ QQ(1)/lagrange_denominator(k
, xs
) for k
in range(0, len(xs
)) ]
152 def divided_difference(xs
, ys
):
154 Return the Newton divided difference of the points (xs[k],
157 http://en.wikipedia.org/wiki/Divided_differences
161 - ``xs`` -- The list of x-values.
163 - ``ys`` -- The function values at `xs`.
167 The (possibly symbolic) divided difference function.
172 sage: ys = map(sin, xs)
173 sage: divided_difference(xs, ys)
176 sage: ys = map(sin, xs)
177 sage: divided_difference(xs, ys)
179 sage: xs = [0, pi, 2*pi]
180 sage: ys = map(sin, xs)
181 sage: divided_difference(xs, ys)
184 We try something entirely symbolic::
186 sage: f = function('f')(x)
187 sage: divided_difference([x], [f(x=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)
194 coeffs
= divided_difference_coefficients(xs
)
195 v_cs
= vector(coeffs
)
197 return v_cs
.dot_product(v_ys
)
200 def newton_polynomial(x
, xs
, ys
):
202 Return the Newton form of the interpolating polynomial of the
203 points (xs[k], ys[k]) in the variable `x`.
207 - ``x`` -- The independent variable to use for the interpolating
210 - ``xs`` -- The list of x-values.
212 - ``ys`` -- The function values at `xs`.
216 A symbolic expression.
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)
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
])
240 def hermite_coefficient(k
, x
, xs
):
242 Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160.
246 - ``k`` -- The index of the coefficient.
248 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
250 - ``xs`` -- The list of points at which the function values are
255 A symbolic expression.
258 lk
= lagrange_coefficient(k
, x
, xs
)
259 return (1 - 2*lk
.diff(x
)(x
=xs
[k
])*(x
- xs
[k
]))*(lk
**2)
262 def hermite_deriv_coefficient(k
, x
, xs
):
264 Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See
269 - ``k`` -- The index of the coefficient.
271 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
273 - ``xs`` -- The list of points at which the function values are
278 A symbolic expression.
281 lk
= lagrange_coefficient(k
, x
, xs
)
282 return (x
- xs
[k
])*(lk
**2)
285 def hermite_interpolant(x
, xs
, ys
, y_primes
):
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.
290 Reference: Atkinson, p. 160.
294 - ``x`` -- The symbolic variable to use as the argument of H(x).
296 - ``xs`` -- The list of points at which the function values are
299 - ``ys`` -- The function values at the `xs`.
301 - ``y_primes`` -- The derivatives at the `xs`.
305 A symbolic expression.
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)
321 s1
= sum([ ys
[k
] * hermite_coefficient(k
, x
, xs
)
322 for k
in range(0, len(xs
)) ])
324 s2
= sum([ y_primes
[k
] * hermite_deriv_coefficient(k
, x
, xs
)
325 for k
in range(0, len(xs
)) ])
330 def lagrange_psi(x
, xs
):
334 Psi(x) = (x - xs[0])*(x - xs[1])* ... *(x - xs[-1])
336 used in Lagrange and Hermite interpolation.
340 - ``x`` -- The independent variable of the resulting expression.
342 - ``xs`` -- A list of points.
346 A symbolic expression in one variable, `x`.
350 return product([ (x
- xj
) for xj
in xs
])