% Basic test for the constrained least square driver in problem 4.

m = 20000;
n = 10000;
p = 2;

% Note: This test is designed to be *slow* if you don't permute!
% Try "help colamd" to see a good permutation.
% You can initially make m and n smaller to see if your code
% works without permutations...
%
A = spalloc(m,n, 2*m);
A(:,1) = rand(m,1);
for i = 2:m-1
A(i,ceil(n*(i/m))) = rand;
end
B = rand(p,n);
b = rand(m,1);

tic;
x = p4solve(A,b,B);
toc

r = b-A*x;
ATr = A'*r;
mu = B'\ATr;

fprintf('Normal equation error: %g\n', norm(A'*(A*x-b)+B'*mu)/norm(A'*b));
fprintf('Constraint error: %g\n', norm(B*x));