%%%%%%%%%%%%%%%%% freduce %%%%%%%%%%%%%%%%%%%%
%
%Given an exposing vector, reduce the spectrahedron to the
%conjugate face of the exposing vector


function[Anew,bnew,svind,sMind,n,m] = freduce(A,b,V,nold,sMind)

m = length(b);
n = size(V,2);
tn = n*(n+1)/2;
svind = find(triu(ones(n),1));
Anew = zeros(m,tn);
for ii=1:m
    temp = V'*ssMat(A(ii,:),nold,sMind)*V;
    temp = (temp+temp')/2;
    Anew(ii,:) = ssvec(temp,svind);
end

%ensure linear independence
[~,R] = qr(Anew',0);
inds = diag(abs(R)>10e-8);
Anew = Anew(inds,:);
bnew = b(inds);
m = length(bnew);

%upgrade sMind
n2=n^2;
svind = find(triu(ones(n),1)); 
sMind = zeros(n2,1); 
sMind(svind) = 1:tn-n; 
sMind(1:n+1:n2) = tn-n+1:tn;
indM = zeros(n); 
indM(:) = 1:n2;
indM = indM';
lsvind = indM(svind);  
sMind(lsvind) = 1:tn-n;

end