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

def polyx(a,x):
    f = 0
    for i in range(0,len(a)):
        f+=a[i]*x**i
    return f


def vandint(x,xp,yp):
    V = xp**0 #hstack concatenates vectors horizontally to make a matrix
    for i in range(1,len(xp)):
        V= np.hstack([V,xp**i])
    VI = np.linalg.inv(V)
    return polyx(np.linalg.matmul(VI,yp),x)


def lagx(j,x,xp):
    num = 1
    den = 1
    for i in range(0,len(xp)):
        if i!=j:
            num*=x-xp[i]
            den*=xp[j]-xp[i]
    return num/den

def lagint(x,xp,yp):
    ans = 0
    for i in range(0,len(xp)):
        ans+=lagx(i,x,xp)*yp[i]
    return ans
