% checkcspsolve checks the solution returned by cspsolve for
% the eigenvalues specified in the vector lambda. Each solution
% function is checked at all values in lambda with a call to the
% helper function checkcspsoln. The output maxres is the largest
% value of ||(A - l B) x(l)||_2 over all l in lambda and x(l)
% in X. The output matrix res is defined by
% res(i,j) = ||(A - lambda(i) B) x_j(lambda(i))||_2.

function [maxres,res] = checkcspsolve(A,B,lambda,X,epsilon)

[m,n] = size(A);
p = size(X,2);
nlambda = length(lambda);
res = zeros(nlambda,p);

maxres = -inf;
for j=1:p
  x = X(1:(epsilon(j)+1)*n,j);
  [maxresj, res(:,j)] = checkcspsoln(A,B,lambda,x,epsilon(j));
  if (maxresj > maxres) maxres = maxresj; end
end
