%
%   version 05.07.2002
%
% The function calulates the solution of the linear system
%        W = x Z,
% where Z decribes a full culumn rank matrix by
%        x = W Z^-
% and Z^- describes an arbitrary generalized inverse of Z
% calculated by QR decomposition.
% The full rank of Z is checked (sing).
%
function [x,sing]=WZm(W,Z,thresh)
[U,R]=qr(Z);
absdiagR=abs(diag(R));
rank=sum(absdiagR>thresh);
dim=size(R,2);
if rank < dim
    disp(['WZm: rank(Z) = ',num2str(rank),...
        ' < ',num2str(dim),'. [U,R]=qr(Z), diag(R)(rank+1) = ',...
        num2str(absdiagR(rank+1)),' < threshold = ',num2str(thresh)]);
    x=[];
    sing=1;
else
    %disp('WZm: Konditionsschaetzung');
    %condest(R(1:dim,:))
    %diag(R)
    m=sparse(dim,size(Z,1)-dim);
    x=[W/R(1:dim,:),W*m]*U';
    sing=0;
end;