function main_QR_test

rng(0)
m = 20;

%%% Generate a random unitary matrix Q (from a "Haar" distribution)
[Q,~]   = qr(randn(m));

%%% Generate a random upper triangular matrix R.
R       = triu(randn(m));

%%% Compute the product of Q and R:
A       = Q*R;

%%% Ensure that the diagonal entries or R are non-negative:
S       = diag(sign(diag(R)));
R       = S*R;
Q       = Q*S;

%%% Compute the QR factorization of A:
[QQ,RR] = qr(A);

%%% Ensure that the diagonal entries of RR are non-negative:
S       = diag(sign(diag(RR)));
RR      = S*RR;
QQ      = QQ*S;

%%% Print the various errors:
fprintf(1,'|R - RR|         = %10.3e\n',max(max(abs(R - RR))))
fprintf(1,'|Q - QQ|         = %10.3e\n',max(max(abs(Q - QQ))))
fprintf(1,'|trans(Q)*Q - I| = %10.3e\n',max(max(abs(QQ'*QQ - eye(m)))))
fprintf(1,'|QQ*RR - A|      = %10.3e\n',max(max(abs(QQ*RR - A))))

 keyboard


return