function V = chebvand(deg,gmesh,rect);

% computes the Chebyshev-Vandermonde matrix on a 2d or 3d given mesh 
% in the total-degree product Chebyshev basis of a rectangle containing 
% the mesh points

% INPUT:

% deg = polynomial degree

% gmesh = 2- or 3-column array of mesh point coordinates

% rect = 4- or 6-component vector such that the rectangle 
% [rect(1),rect(2)] x [rect(3),rect(4)] in 2d
% or [rect(1),rect(2)] x [rect(3),rect(4)] x [rect(5),rect(6)] in 3d
% contains the mesh


% OUTPUT:

% V = Chebyshev-Vandermonde matrix 


% FUNCTION BODY

if length(gmesh(1,:)) == 2 

% rectangle containing the mesh 
if isempty(rect)
rect=[min(gmesh(:,1)) max(gmesh(:,1)) min(gmesh(:,2)) max(gmesh(:,2))];
end
    
% couples with length less or equal to deg
% graded lexicographical order
j=(0:1:deg);
[j1,j2]=meshgrid(j);
dim=(deg+1)*(deg+2)/2;
couples=zeros(dim,2);
for s=0:deg
good=find(j1(:)+j2(:)==s);
couples(1+s*(s+1)/2:(s+1)*(s+2)/2,:)=[j1(good) j2(good)];
end;

% mapping the mesh in the square [-1,1]^2
a=rect(1);b=rect(2);c=rect(3);d=rect(4);
map=[(2*gmesh(:,1)-b-a)/(b-a) (2*gmesh(:,2)-d-c)/(d-c)];

% Chebyshev-Vandermonde matrix on the mesh
V1=cos(couples(:,1)*acos(map(:,1)'));
V2=cos(couples(:,2)*acos(map(:,2)'));
V=(V1.*V2)';

end;


if length(gmesh(1,:)) == 3 
        
% parallelepiped containing the mesh  
if isempty(rect)
rect=[min(gmesh(:,1)) max(gmesh(:,1)) min(gmesh(:,2)) max(gmesh(:,2)) ... 
 min(gmesh(:,3)) max(gmesh(:,3))];
end
    
% triples with length less or equal to deg
% graded lexicographical order
j=(0:1:deg);
[j1,j2,j3]=meshgrid(j,j,j);
dim=(deg+1)*(deg+2)*(deg+3)/6;
triples=zeros(dim,3);
for s=0:deg
good=find(j1(:)+j2(:)+j3(:)==s);
triples(1+s*(s+1)*(s+2)/6:(s+1)*(s+2)*(s+3)/6,:)=[j1(good) j2(good) 
j3(good)];
end;

%mapping the mesh in the cube [-1,1]^3
a=rect(1);b=rect(2);
c=rect(3);d=rect(4);
e=rect(5);f=rect(6);
map=[(2*gmesh(:,1)-b-a)/(b-a) (2*gmesh(:,2)-d-c)/(d-c) ... 
(2*gmesh(:,3)-f-e)/(f-e)];

%Chebyshev-Vandermonde matrix on the mesh
V1=cos(triples(:,1)*acos(map(:,1)'));
V2=cos(triples(:,2)*acos(map(:,2)'));
V3=cos(triples(:,3)*acos(map(:,3)'));
V=((V1.*V2).*V3)';

end; 

end



