 function [deltax,deltad,deltaz,deltay]= solvlss(RHS,muu,...
                            noiter,toler)
%         [deltax,deltad,deltaz,deltay]= solvlss(x,d,z,y,RHS,muu,...
%                           noiter,toler)
%%
%  Solve the least squares solution for the Gauss-Newton Direction.
%  Use the lsqr routing of Paige-Saunders.
%
%  Input:
%        x,d,z,y,RHS    -   data from the current iteration ???details
%                       
%        muu                        contains current value of muu
%                                   to use for accuracy in lsqr
%        noiter      -  iteration number in calling program mcsdp
%                    -  used to help in determining accuracy for lsqr
%        toler       -  requested tolerance for relative 
%                         duality gap in     mcsdp.m
%
%  Output:
%          deltax,deltad,deltaz,deltay  - search direction  

global indsE indsEc iE jE iEc jEc X  d x Z z y   



n=size(d,1)+1;
tn=n*(n+1)/2;
%%%%%%%%%%%%%%%%%%%%%%5
%%%%%%%%%%%%%%%%%%%%%%5
%%prepare for MS lss iterative solve
damp=0;
%atol   = max([1.0e-12,1.0e-1*toler,...    % new way????
 atol   = max(1.0e-1*toler,...           % old way
           (1.0e-6)*(min(muu,1))^(1/3)  );  
 btol   = max(1.0e-1*toler,...
           (1.0e-6)*(min(muu,1))^(1/3)  ); 
conlim = 1.0e+10;
itnlim = 10*tn;
show   = 0;
mm=size(y,1);
mc=(n*(n-1)/2) -mm;
npmmpmc=n+mc+mm;

%  following to be used for preconditioning
Qy=[];
xy=[];
timeprec=cputime;
znorm=(sum(Z.*Z))'/2;
zvecd=sqrt( znorm(2:n)+znorm(1) );
zvecx=sqrt( znorm(iEc)+znorm(jEc) );

xnorm=(sum(X.*X))'/2;
xvecz=sqrt(sum(xnorm));
xvecy=sqrt( xnorm(iE)+xnorm(jE) );

   xy=[xy
       zvecd
       zvecx
       xvecz
       xvecy  ];
timeprec=cputime-timeprec;
     
%%%%%%%%%%%%%%%%%%%


  [ deltas, istop, itn, rnorm, xnorm  var    ] =  ...
  MSlsqr( n^2, npmmpmc, 'MSFprecondfun', Qy, xy, RHS, damp, atol, btol, conlim, itnlim, show );
  
deltas=deltas./xy;    % for preconditioning
deltad=deltas(1:n-1);
deltax=deltas(n:n+mc-1);
deltaz=deltas(n+mc);
deltay=deltas(n+mc+1:end);

