function [Abar,U,k] = putv(A,method,tol)
% PURPOSE: transforms a matrix A to [Abar; 0], where Abar has full row rank
%
% ---------------------------------------------------
% USAGE: [Abar,U,k] = putv(A,method,tol)
% where:
% A a matrix
% method method used
% 1 - qr with pivoting
% 2 - svd
% tol uses the tolerance tol when calculating
% null subspaces (optional)
%
% Abar matrix with full row rank
% U an orthogonal matrix such that U'A =
% [Abar;0]
% k estimated rank of Abar
%
% COMMENTS:
% Version based on qr decomposition is faster but less
% accurate than version based on svd. If null space and range are not clearly separated,
% then version based on svd decomposition should be used.
%
% Copyright (c) Pawel Kowal (2006)
% All rights reserved
% LREM_SOLVE toolbox is available free for noncommercial academic use only.
% pkowal3@sgh.waw.pl
if nargin<3
tol = 10 * max(size(A)) * norm(A,1) * eps;
end
switch method
case 1
[U,R,P] = qr(A);
k = rank2(R,tol);
Abar = R(1:k,:)*P';
case 2
[U,S,V] = svd(A);
if size(A,1)>1 && size(A,2)>1
s = diag(S);
else
s = S;
end
tol = 10*max(size(A)') * eps(max(s));
k = sum(s>tol);
Abar = S(1:k,1:k)*V(:,1:k)';
end