#!/usr/bin/python3
import numpy as np
import math
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as Axes3D



def polyz(z,x):
    f = 1
    for i in range(0,len(z)):
        f*=x-z[i]
    return f



def lagx(j,x,xp):
    n=xp.pop(j)
    return polyz(xp,x)/polyz(xp,n)


xp = np.array([[-1],[0],[1],[2]])
yp = np.array([[2],[-1],[1],[0]])

V = xp**0 #hstack concatenates vectors horizontally to make a matrix
for i in range(1,len(xp)):
    V= np.hstack([V,xp**i])

print(V)

VI = np.linalg.inv(V)
coeff = np.linalg.matmul(VI,yp)
x = np.linspace(-1,2,100)
plt.plot(x, polyx(coeff,x))
plt.show()

