From: Michael Orlitzky Date: Fri, 26 Oct 2012 19:20:25 +0000 (-0400) Subject: Add the Hermite functions to interpolation.py. X-Git-Url: http://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=c2eb40221f6eb4bea022fcb885f25268c1dbd1e3;p=sage.d.git Add the Hermite functions to interpolation.py. --- diff --git a/mjo/interpolation.py b/mjo/interpolation.py index 1d594c3..70c35c1 100644 --- a/mjo/interpolation.py +++ b/mjo/interpolation.py @@ -22,7 +22,7 @@ def lagrange_coefficient(k, x, xs): OUTPUT: - A symbolic function of one variable. + A symbolic expression of one variable. TESTS:: @@ -53,7 +53,7 @@ def lagrange_polynomial(x, xs, ys): OUTPUT: - A symbolic function (polynomial) interpolating each (xs[k], ys[k]). + A symbolic expression (polynomial) interpolating each (xs[k], ys[k]). TESTS:: @@ -157,7 +157,7 @@ def newton_polynomial(x, xs, ys): OUTPUT: - A symbolic function. + A symbolic expression. TESTS: @@ -179,3 +179,93 @@ def newton_polynomial(x, xs, ys): N += term return N + + +def hermite_coefficient(k, x, xs): + """ + Return the Hermite coefficient h_{k}(x). See Atkinson, p. 160. + + INPUT: + + - ``k`` -- The index of the coefficient. + + - ``x`` -- The symbolic variable to use as the argument of h_{k}. + + - ``xs`` -- The list of points at which the function values are + known. + + OUTPUT: + + A symbolic expression. + + """ + lk = lagrange_coefficient(k, x, xs) + return (1 - 2*lk.diff(x)(x=xs[k])*(x - xs[k]))*(lk**2) + + +def hermite_deriv_coefficient(k, x, xs): + """ + Return the Hermite derivative coefficient, \tilde{h}_{k}(x). See + Atkinson, p. 160. + + INPUT: + + - ``k`` -- The index of the coefficient. + + - ``x`` -- The symbolic variable to use as the argument of h_{k}. + + - ``xs`` -- The list of points at which the function values are + known. + + OUTPUT: + + A symbolic expression. + + """ + lk = lagrange_coefficient(k, x, xs) + return (x - xs[k])*(lk**2) + + +def hermite_interpolant(x, xs, ys, y_primes): + """ + Return the Hermite interpolant `H(x)` such that H(xs[k]) = ys[k] + and H'(xs[k]) = y_primes[k] for each k. + + Reference: Atkinson, p. 160. + + INPUT: + + - ``x`` -- The symbolic variable to use as the argument of H(x). + + - ``xs`` -- The list of points at which the function values are + known. + + - ``ys`` -- The function values at the `xs`. + + - ``y_primes`` -- The derivatives at the `xs`. + + OUTPUT: + + A symbolic expression. + + TESTS: + + sage: xs = [ 0, pi/6, pi/2 ] + sage: ys = map(sin, xs) + sage: y_primes = map(cos, xs) + sage: H = hermite_interpolant(x, xs, ys, y_primes) + sage: expected = -27/4*(pi - 6*x)*(pi - 2*x)^2*sqrt(3)*x^2/pi^4 + sage: expected += (5*(pi - 2*x)/pi + 1)*(pi - 6*x)^2*x^2/pi^4 + sage: expected += 81/2*((pi - 6*x)/pi + 1)*(pi - 2*x)^2*x^2/pi^4 + sage: expected += (pi - 6*x)^2*(pi - 2*x)^2*x/pi^4 + sage: bool(H == expected) + True + + """ + s1 = sum([ ys[k] * hermite_coefficient(k, x, xs) + for k in range(0, len(xs)) ]) + + s2 = sum([ y_primes[k] * hermite_deriv_coefficient(k, x, xs) + for k in range(0, len(xs)) ]) + + return (s1 + s2)