function [fek,weights] = quadfek(deg,mymesh,leja,rect,mom);

% extracts approximate Fekete or Leja interpolation points from a 2d 
% or 3d mesh/cloud and computes the weights of a corresponding algebraic 
% integration formula

% by Alvise Sommariva and Marco Vianello, University of Padova 
% December ?, 2012

% see, e.g., the papers: 
%
% A. Sommariva and M. Vianello, Computing approximate Fekete points by QR 
% factorizations of Vandermonde matrices, Comput. Math. Appl. 57 (2009)
% preprint: http://www.math.unipd.it/~marcov/pdf/afek.pdf
% 
% M. Gentile, A. Sommariva and M. Vianello, Polynomial interpolation 
% and cubature over polygons, J. Comput. Appl. Math. 235 (2011)
% preprint: http://www.math.unipd.it/~marcov/pdf/wamp.pdf


% INPUT: 

% deg = interpolation degree

% mymesh = 2- or 3-column array of mesh points coordinates, from which the 
% approximate Fekete or Leja points will be extracted

% leja=0: computes approximate Fekete points
% leja=1: computes approximate Leja points

% 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 meshes
% if rect == [ ], it will be automatically tailored to the meshes 

% mom = column vector of the integrals (moments) of the total-degree 
% product Chebyshev basis of the rectangle with respect to some measure 
% on a compact containing the meshes; if mom == [ ], it will be 
% automatically set to the averages of the basis polynomials on mymesh


% OUTPUT: 

% fek = 2- or 3-column array of approximate Fekete or Leja 
% points coordinates

% weights = column vector of the integration weights corresponding 
% to the approximate Fekete or Leja points 


% FUNCTION BODY

% setting rectangle if not defined 
if size(rect) == [0 0]
rect(1)=min(mymesh(:,1));
rect(2)=max(mymesh(:,1));
rect(3)=min(mymesh(:,2));
rect(4)=max(mymesh(:,2));
if length(mymesh(1,:)) == 3
rect(5)=min(mymesh(:,3));
rect(6)=max(mymesh(:,3));
end;
end;

% dimension of the polynomial space
if length(mymesh(1,:)) == 2 
dim=(deg+1)*(deg+2)/2;
end;
if length(mymesh(1,:)) == 3 
dim=(deg+1)*(deg+2)*(deg+3)/6;
end;

% approximate Fekete or Leja points extraction from mymesh 

% computing the rectangular Chebyshev-Vandermonde matrix on the mesh 
W=chebvand(deg,mymesh,rect);

% change of basis via 2 successive QR factorizations of the 
% Vandermonde matrices; to keep the original basis set ort=0
ort=1; 
B=W;
if ort==1
[Q,R]=qr(B,0);
P1=inv(R);
B=B*P1;
[Q,R]=qr(B,0);
P2=inv(R);
B=B*P2;
P=P1*P2;
else
P=eye(dim);
end;

% P is the basis transformation matrix: 
% now B = W * P is the Chebyshev-Vandermonde matrix on mymesh 
% in the new basis and is nearly-orthogonal 
% (unless W is too severely ill conditioned) 

% extracting the interpolation points and computing the weights 

% moment transformation 
if length(mom) == 0 
% setting the moments if not defined    
m=length(mymesh(:,1));
newmom=B'*ones(m,1)/m;
else
newmom=P'*mom;
end;

if leja == 0
% approximate Fekete points and corresponding weights
% by QR factorization with column pivoting of B'=(W*P)'
w=B'\newmom; 
ind=find(abs(w)>0);
fek=mymesh(ind,:);
weights=w(ind);
else
% approximate Leja points and corresponding weights  
% by LU factorization with row pivoting of B=W*P
[L,U,perm]=lu(B,'vector');
fek=mymesh(perm(1:dim),:);
op1.LT=true;
y=linsolve(U',newmom,op1);
op2.UT=true;
weights=linsolve(L(1:dim,:)',y,op2); 
end;

end


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 

% couples with length less or equal to deg
j=(0:1:deg);
[j1,j2]=meshgrid(j);
good=find(j1(:)+j2(:)<deg+1);
couples=[j1(good) j2(good)];

% 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 
    
%triples with length less or equal to deg
j=(0:1:deg);
[j1,j2,j3]=meshgrid(j,j,j);
good=find(j1(:)+j2(:)+j3(:)<deg+1);
triples=[j1(good) j2(good) j3(good)];

%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

