function [A,a,b,beta,delta]=secondclass(n,density)
% % This function generates second class of test problems of the form
%   min x'Ax+2*a'x
%       x'x<=delta
%       b'x<=beta

%===================================================
global flag_con A a n delta
flag=1;
while flag
    A=sprandsym(n,density);
    v=full(sum(A))';
    opts.v0=v;
    opts.issym=1;
    [v,lambdamin]=eigs(A,1,'sa',opts);
    a=randn(n,1);
    flag=0;
    if abs(v'*a)<1e-4
        disp('Regenerate test problem due to hard case ')
        flag=1;
    else
        delta=4000;
    end
end
%=========================================================================
%======== computing global optimal solution of TRS =======================
flag_con=0;            % indicates we are solving TRS
M1=[sparse(n,n) speye(n);speye(n) sparse(n,n)];
opts.v0=randn(2*n,1);
opts.issym=1;
[v,d]=eigs(@M0,2*n,-M1,1,'lr',opts);
if norm(v(1:n))<=1e-4
    lambdamin=real(d);
    v=v(n+1:end)/norm(v(n+1:end));
    [base,alpha,~]=base_eig(A,-lambdamin,v);
    [q,~]=pcg(@Hfunc,-a,1e-14,1000);
    qv=q'*v;
    tu=(delta-(norm(q))^2)/(qv+(sign(qv))^2*(qv^2+delta-norm(q)^2)^(1/2));
    xopt=q+tu*v;
else
    xopt=-sign(a'*v(n+1:end))*sqrt(delta)*v(1:n)/norm(v(1:n));
end
% xopt is optimal solution of TRS
%===================================================================
%===========  cutting of optimal solution of TRS
shift=0.1;
bb=(1-shift)*xopt;
alpha=norm(bb)^2;
b=bb;
beta=alpha;

%======================================================================
    function [v] = Hfunc(u)
        
        if flag_con==0
            v=A*u+lambdamin*u+(base*(alpha.*(base'*u)));
        elseif flag_con==1
            v=W'*(A*(W*u))+lambdamin*(W'*(W*u))+(base*(alpha.*(base'*u)));
        end
    end
end









