]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/interpolation.py
COPYING,LICENSE: add (AGPL-3.0+)
[sage.d.git] / mjo / interpolation.py
index 8c3936edabaf5aff434f5f1728edffa899330f6d..6e0ecb3d79c5129a2e6270177b648142085786d1 100644 (file)
@@ -1,5 +1,25 @@
 from sage.all import *
-from misc import product
+product = prod
+
+
+def lagrange_denominator(k, xs):
+    """
+    Return the denominator of the kth Lagrange coefficient.
+
+    INPUT:
+
+      - ``k`` -- The index of the coefficient.
+
+      - ``xs`` -- The list of points at which the function values are
+        known.
+
+    OUTPUT:
+
+    The product of all xs[j] with j != k.
+
+    """
+    return product( xs[k] - xs[j] for j in range(len(xs)) if j != k )
+
 
 def lagrange_coefficient(k, x, xs):
     """
@@ -12,9 +32,9 @@ def lagrange_coefficient(k, x, xs):
 
     INPUT:
 
-      - ``k`` -- the index of the coefficient.
+      - ``k`` -- The index of the coefficient.
 
-      - ``x`` -- the symbolic variable to use for the first argument
+      - ``x`` -- The symbolic variable to use for the first argument
         of l_{k}.
 
       - ``xs`` -- The list of points at which the function values are
@@ -24,15 +44,19 @@ def lagrange_coefficient(k, x, xs):
 
     A symbolic expression of one variable.
 
+    SETUP::
+
+        sage: from mjo.interpolation import lagrange_coefficient
+
     TESTS::
 
         sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
         sage: lagrange_coefficient(0, x, xs)
-        1/8*(pi - 6*x)*(pi - 2*x)*(pi + 6*x)*x/pi^4
+        1/8*(pi + 6*x)*(pi - 2*x)*(pi - 6*x)*x/pi^4
 
     """
-    numerator = product([x - xs[j] for j in range(0, len(xs)) if j != k])
-    denominator = product([xs[k] - xs[j] for j in range(0, len(xs)) if j != k])
+    numerator = lagrange_psi(x, xs)/(x - xs[k])
+    denominator = lagrange_denominator(k, xs)
 
     return (numerator / denominator)
 
@@ -40,7 +64,7 @@ def lagrange_coefficient(k, x, xs):
 
 def lagrange_polynomial(x, xs, ys):
     """
-    Return the Lagrange form of the interpolation polynomial in `x` of
+    Return the Lagrange form of the interpolating polynomial in `x`
     at the points (xs[k], ys[k]).
 
     INPUT:
@@ -55,10 +79,14 @@ def lagrange_polynomial(x, xs, ys):
 
     A symbolic expression (polynomial) interpolating each (xs[k], ys[k]).
 
+    SETUP::
+
+        sage: from mjo.interpolation import lagrange_polynomial
+
     TESTS::
 
         sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
-        sage: ys = map(sin, xs)
+        sage: ys = list(map(sin, xs))
         sage: L = lagrange_polynomial(x, xs, ys)
         sage: expected  = 27/16*(pi - 6*x)*(pi - 2*x)*(pi + 2*x)*x/pi^4
         sage: expected -= 1/8*(pi - 6*x)*(pi - 2*x)*(pi + 6*x)*x/pi^4
@@ -68,9 +96,48 @@ def lagrange_polynomial(x, xs, ys):
         True
 
     """
-    ls = [ lagrange_coefficient(k, x, xs) for k in range(0, len(xs)) ]
-    sigma = sum([ ys[k] * ls[k] for k in range(0, len(xs)) ])
-    return sigma
+    ls = [ lagrange_coefficient(k, x, xs) for k in range(len(xs)) ]
+    return sum( ys[k] * ls[k] for k in range(len(xs)) )
+
+
+
+def lagrange_interpolate(f, x, xs):
+    """
+    Interpolate the function ``f`` at the points ``xs`` using the
+    Lagrange form of the interpolating polynomial.
+
+    INPUT:
+
+      - ``f`` -- The function to interpolate.
+
+      - ``x`` -- The independent variable of the resulting polynomial.
+
+      - ``xs`` -- A list of points at which to interpolate ``f``.
+
+    OUTPUT:
+
+    A polynomial in ``x`` which interpolates ``f`` at ``xs``.
+
+    SETUP::
+
+        sage: from mjo.interpolation import lagrange_interpolate
+
+    EXAMPLES:
+
+    We're exact on polynomials of degree `n` if we use `n+1` points::
+
+        sage: t = SR.symbol('t', domain='real')
+        sage: lagrange_interpolate(x^2, t, [-1,0,1]).simplify_rational()
+        t^2
+
+    """
+    # f should be a function of one variable.
+    z = f.variables()[0]
+    # We're really just doing map(f, xs) here; the additional
+    # gymnastics are to avoid a warning when calling `f` with an
+    # unnamed argument.
+    ys = [ f({z: xk}) for xk in xs ]
+    return lagrange_polynomial(x, xs, ys)
 
 
 
@@ -79,7 +146,11 @@ def divided_difference_coefficients(xs):
     Assuming some function `f`, compute the coefficients of the
     divided difference f[xs[0], ..., xs[n]].
 
-    TESTS:
+    SETUP::
+
+        sage: from mjo.interpolation import divided_difference_coefficients
+
+    TESTS::
 
         sage: divided_difference_coefficients([0])
         [1]
@@ -89,9 +160,7 @@ def divided_difference_coefficients(xs):
         [1/2/pi^2, -1/pi^2, 1/2/pi^2]
 
     """
-    coeffs = [ product([ (QQ(1) / (xj - xi)) for xi in xs if xi != xj ])
-               for xj in xs ]
-    return coeffs
+    return [ ~lagrange_denominator(k, xs) for k in range(len(xs)) ]
 
 
 def divided_difference(xs, ys):
@@ -111,27 +180,31 @@ def divided_difference(xs, ys):
 
     The (possibly symbolic) divided difference function.
 
+    SETUP::
+
+        sage: from mjo.interpolation import divided_difference
+
     TESTS::
 
         sage: xs = [0]
-        sage: ys = map(sin, xs)
+        sage: ys = list(map(sin, xs))
         sage: divided_difference(xs, ys)
         0
         sage: xs = [0, pi]
-        sage: ys = map(sin, xs)
+        sage: ys = list(map(sin, xs))
         sage: divided_difference(xs, ys)
         0
         sage: xs = [0, pi, 2*pi]
-        sage: ys = map(sin, xs)
+        sage: ys = list(map(sin, xs))
         sage: divided_difference(xs, ys)
         0
 
     We try something entirely symbolic::
 
-        sage: f = function('f'x)
+        sage: f = function('f')(x)
         sage: divided_difference([x], [f(x=x)])
         f(x)
-        sage: x1,x2 = var('x1,x2')
+        sage: x1,x2 = SR.var('x1,x2')
         sage: divided_difference([x1,x2], [f(x=x1),f(x=x2)])
         f(x1)/(x1 - x2) - f(x2)/(x1 - x2)
 
@@ -160,26 +233,22 @@ def newton_polynomial(x, xs, ys):
 
     A symbolic expression.
 
-    TESTS:
+    SETUP::
+
+        sage: from mjo.interpolation import lagrange_polynomial, newton_polynomial
+
+    TESTS::
 
         sage: xs = [ -pi/2, -pi/6, 0, pi/6, pi/2 ]
-        sage: ys = map(sin, xs)
+        sage: ys = list(map(sin, xs))
         sage: L = lagrange_polynomial(x, xs, ys)
         sage: N = newton_polynomial(x, xs, ys)
         sage: bool(N == L)
         True
 
     """
-    degree = len(xs) - 1
-
-    N = SR(0)
-
-    for k in range(0, degree+1):
-        term  = divided_difference(xs[:k+1], ys[:k+1])
-        term *= product([ x - xk for xk in xs[:k]])
-        N += term
-
-    return N
+    return sum( divided_difference(xs[:k+1], ys[:k+1])*lagrange_psi(x, xs[:k])
+                for k in range(len(xs)) )
 
 
 def hermite_coefficient(k, x, xs):
@@ -249,11 +318,15 @@ def hermite_interpolant(x, xs, ys, y_primes):
 
     A symbolic expression.
 
-    TESTS:
+    SETUP::
+
+        sage: from mjo.interpolation import hermite_interpolant
+
+    TESTS::
 
         sage: xs = [ 0, pi/6, pi/2 ]
-        sage: ys = map(sin, xs)
-        sage: y_primes = map(cos, xs)
+        sage: ys = list(map(sin, xs))
+        sage: y_primes = list(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
@@ -263,11 +336,11 @@ def hermite_interpolant(x, xs, ys, y_primes):
         True
 
     """
-    s1 = sum([ ys[k] * hermite_coefficient(k, x, xs)
-               for k in range(0, len(xs)) ])
+    s1 = sum( ys[k] * hermite_coefficient(k, x, xs)
+               for k in range(len(xs)) )
 
-    s2 = sum([ y_primes[k] * hermite_deriv_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(len(xs)) )
 
     return (s1 + s2)
 
@@ -289,6 +362,7 @@ def lagrange_psi(x, xs):
     OUTPUT:
 
     A symbolic expression in one variable, `x`.
+
     """
 
-    return product([ (x - xj) for xj in xs ])
+    return product( (x - xj) for xj in xs )