Let \( \refphi_r(X) \) be a Lagrange polynomial of degree d
:
import sympy as sp
import numpy as np
def phi_r(r, X, d):
if isinstance(X, sp.Symbol):
h = sp.Rational(1, d) # node spacing
nodes = [2*i*h - 1 for i in range(d+1)]
else:
# assume X is numeric: use floats for nodes
nodes = np.linspace(-1, 1, d+1)
return Lagrange_polynomial(X, r, nodes)
def Lagrange_polynomial(x, i, points):
p = 1
for k in range(len(points)):
if k != i:
p *= (x - points[k])/(points[i] - points[k])
return p
def basis(d=1):
"""Return the complete basis."""
X = sp.Symbol('X')
phi = [phi_r(r, X, d) for r in range(d+1)]
return phi