function clas_GS = clgs(A)
% compute the reduced QR decomposition of A using the
% classical Gram-Schmidt
% algorithm
% compute m and n, the number of rows and columns of A
m = size(A,1); n = size(A,2);
% initialize the program: normalize the first column of A
% to get the first orthogonal vector of Q
Q = zeros(m,n);
R(1,1) = norm(A(:,1));
Q(:,1) = A(:,1)/R(1,1);
for j = 2:n
v = A(:,j);
% inner loop of the classical GS; orthogonalize
% the j-th column of A on the space with respect to the
% first j-1 orthogonal vectors from Q
for i = 1:(j-1)
R(i,j) = conj(Q(:,i))'*A(:,j);
v = v - R(i,j) * Q(:,i);
end
R(j,j) = norm(v);
Q(:,j) = v/R(j,j); % set the j-th orthogonal vector of Q
clear v
end
R = [ R
zeros(m-n,n) ];
clas_GS = [ Q R ];
% RECOVER Q AND R:
% cQR = clgs(A);
% m = size(cQR,1); n = size(cQR,2)/2;
% Qclgs = cQR(:,1:n); Rclgs = cQR(1:n,n+1:2*n);