]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/interpolation.py
2 from misc
import product
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 interpolation polynomial in `x` of
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 divided_difference_coefficients(xs
):
99 Assuming some function `f`, compute the coefficients of the
100 divided difference f[xs[0], ..., xs[n]].
104 sage: divided_difference_coefficients([0])
106 sage: divided_difference_coefficients([0, pi])
108 sage: divided_difference_coefficients([0, pi, 2*pi])
109 [1/2/pi^2, -1/pi^2, 1/2/pi^2]
112 coeffs
= [ QQ(1)/lagrange_denominator(k
, xs
) for k
in range(0, len(xs
)) ]
116 def divided_difference(xs
, ys
):
118 Return the Newton divided difference of the points (xs[k],
121 http://en.wikipedia.org/wiki/Divided_differences
125 - ``xs`` -- The list of x-values.
127 - ``ys`` -- The function values at `xs`.
131 The (possibly symbolic) divided difference function.
136 sage: ys = map(sin, xs)
137 sage: divided_difference(xs, ys)
140 sage: ys = map(sin, xs)
141 sage: divided_difference(xs, ys)
143 sage: xs = [0, pi, 2*pi]
144 sage: ys = map(sin, xs)
145 sage: divided_difference(xs, ys)
148 We try something entirely symbolic::
150 sage: f = function('f', x)
151 sage: divided_difference([x], [f(x=x)])
153 sage: x1,x2 = var('x1,x2')
154 sage: divided_difference([x1,x2], [f(x=x1),f(x=x2)])
155 f(x1)/(x1 - x2) - f(x2)/(x1 - x2)
158 coeffs
= divided_difference_coefficients(xs
)
159 v_cs
= vector(coeffs
)
161 return v_cs
.dot_product(v_ys
)
164 def newton_polynomial(x
, xs
, ys
):
166 Return the Newton form of the interpolating polynomial of the
167 points (xs[k], ys[k]) in the variable `x`.
171 - ``x`` -- The independent variable to use for the interpolating
174 - ``xs`` -- The list of x-values.
176 - ``ys`` -- The function values at `xs`.
180 A symbolic expression.
184 sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
185 sage: ys = map(sin, xs)
186 sage: L = lagrange_polynomial(x, xs, ys)
187 sage: N = newton_polynomial(x, xs, ys)
196 for k
in range(0, degree
+1):
197 term
= divided_difference(xs
[:k
+1], ys
[:k
+1])
198 term
*= lagrange_psi(x
, xs
[:k
])
204 def hermite_coefficient(k
, x
, xs
):
206 Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160.
210 - ``k`` -- The index of the coefficient.
212 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
214 - ``xs`` -- The list of points at which the function values are
219 A symbolic expression.
222 lk
= lagrange_coefficient(k
, x
, xs
)
223 return (1 - 2*lk
.diff(x
)(x
=xs
[k
])*(x
- xs
[k
]))*(lk
**2)
226 def hermite_deriv_coefficient(k
, x
, xs
):
228 Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See
233 - ``k`` -- The index of the coefficient.
235 - ``x`` -- The symbolic variable to use as the argument of h_{k}.
237 - ``xs`` -- The list of points at which the function values are
242 A symbolic expression.
245 lk
= lagrange_coefficient(k
, x
, xs
)
246 return (x
- xs
[k
])*(lk
**2)
249 def hermite_interpolant(x
, xs
, ys
, y_primes
):
251 Return the Hermite interpolant `H(x)` such that H(xs[k]) = ys[k]
252 and H'(xs[k]) = y_primes[k] for each k.
254 Reference: Atkinson, p. 160.
258 - ``x`` -- The symbolic variable to use as the argument of H(x).
260 - ``xs`` -- The list of points at which the function values are
263 - ``ys`` -- The function values at the `xs`.
265 - ``y_primes`` -- The derivatives at the `xs`.
269 A symbolic expression.
273 sage: xs = [ 0, pi/6, pi/2 ]
274 sage: ys = map(sin, xs)
275 sage: y_primes = map(cos, xs)
276 sage: H = hermite_interpolant(x, xs, ys, y_primes)
277 sage: expected = -27/4*(pi - 6*x)*(pi - 2*x)^2*sqrt(3)*x^2/pi^4
278 sage: expected += (5*(pi - 2*x)/pi + 1)*(pi - 6*x)^2*x^2/pi^4
279 sage: expected += 81/2*((pi - 6*x)/pi + 1)*(pi - 2*x)^2*x^2/pi^4
280 sage: expected += (pi - 6*x)^2*(pi - 2*x)^2*x/pi^4
281 sage: bool(H == expected)
285 s1
= sum([ ys
[k
] * hermite_coefficient(k
, x
, xs
)
286 for k
in range(0, len(xs
)) ])
288 s2
= sum([ y_primes
[k
] * hermite_deriv_coefficient(k
, x
, xs
)
289 for k
in range(0, len(xs
)) ])
294 def lagrange_psi(x
, xs
):
298 Psi(x) = (x - xs[0])*(x - xs[1])* ... *(x - xs[-1])
300 used in Lagrange and Hermite interpolation.
304 - ``x`` -- The independent variable of the resulting expression.
306 - ``xs`` -- A list of points.
310 A symbolic expression in one variable, `x`.
314 return product([ (x
- xj
) for xj
in xs
])