from sage.all import *
from misc import product
+
+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(0, len(xs)) if j != k])
+
+
def lagrange_coefficient(k, x, xs):
"""
Returns the coefficient function l_{k}(variable) of y_{k} in the
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
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)
[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 ]
+ coeffs = [ QQ(1)/lagrange_denominator(k, xs) for k in range(0, len(xs)) ]
return coeffs
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]])
+ term *= lagrange_psi(x, xs[:k])
N += term
return N
OUTPUT:
A symbolic expression in one variable, `x`.
+
"""
return product([ (x - xj) for xj in xs ])