%
% Computing the cardinal polynomial given data X.
% 
%     output=l_i(x)     using data X
%
function output=ld(i,x,X)
%
n=length(x);
N=length(X);
%
% Denominator of cardinal polynomial l_i(x) is a constant.
%
%      denom = product of (X_i-X_j) over i~=j
%
fden=X(i)*ones(1,N)-X;
fden(i)=1;
denom=prod(fden);
%
% Numerator of cardinal polynomial l_i(x)
%
%      numer = product of (x-X_j) over i~=j
%
fnum=zeros(N,n);
for j=1:N
    fnum(j,:)=x-X(j);
end;
fnum(i,:)=ones(1,n);
%
%    x=row vector
%    X=row vector
%
%FNUM=prod(kron(x,ones(N,1))-kron(X',ones(1,n))
%
output=prod(fnum)/denom;
end
%
% The code can be streamlined someonewhat by collapsing
% the for loop into a single matlab line code:
%
% fnum=prod(kron(x,ones(N,1))-kron(X',ones(1,n))
%
% which though short is not intuitive. Both x,X
% must be row vectors for this to work.
%
