# Implement of Lagrange Polynomial in Sympy

Lagrange Polynomial has a general form like:

Its parameters include:

1. $n$: number of given points,

2. $x_i$: coordinates of interpolation points,

3. order: n-1, the max power in the poly.,

4. $L_i$: Lagrange Poly. at point $i$ ($i$ start from 0).

We introduce a Sympy implement for the polynomial, since we do not find an official implement. The code is presented here:

def LagrangPoly(x,order,i,xi=None):
if xi==None:
xi=symbols('x:%d'%(order+1))
index = range(order+1)
index.pop(i)
return prod([(x-xi[j])/(xi[i]-xi[j]) for j in index])


This function is rather easy. But yeah it can generate any one variable Lagrange Polynomial. Here is some examples:

x=symbols('x')
LagrangPoly(x,5,0)


Since the parameter xi only needs to be a sequence, the function works with any Sympy supported sequence whether a list or a numpy array. Here we pass a Python list into the function.

x=symbols('x')
simplify(LagrangPoly(x,2,0,[-1,0,1])),
simplify(LagrangPoly(x,2,1,[-1,0,1])),
simplify(LagrangPoly(x,2,2,[-1,0,1]))


\begin{pmatrix}\frac{1}{2} x \left(x -1\right), & - x^{2} + 1, & \frac{1}{2} x \left(x + 1\right)\end{pmatrix}

And the sum of upper three polynomials is 1, of course. It is simple to implement Hermite polynomial by Langange polynomial, which will be left for you practice.