function [P,L] = LIP(x,f)
% function [P,L] = LIP(x,f)
% Lagrange interpolation polynomial
% x - points
% f - function values
% P - coefficients of the interpolation polynomial
% L - Lagrange base polynomials (in rows)


[m,n]=size(x);
% test for empty input or matrix instead of vector
lx=length(x);
if lx==0 | lx<m*n
    error('Matrix or empty vector on input!')
end
% test for uniqueness of points
if length(unique(x))<lx
    error('Some points are repeated!')
end
% test forthe same size of input
if any([m,n]~=size(f))
    error('Inputs are not of the same size!')
end

% conversion of input to row vwctors
if m>1
    x=x';f=f';
end

om=poly(x); % function omega

% matrix of Lagrange base polynomials
L=[];
for c=x
    N=deconv(om,[1, -c]);  % numerator of the base polynomial
    D=polyval(N,c);        % denominator of the base polynomial
    L=[L;N/D];
end

% Lagrange interpolation polynomial
P=f*L;

end

