function [A,a,b,beta,delta,xl]=firstclass(n)
% This function generates  first class of test problems of the form
%   min x'Ax+2*a'x
%       x'x<=delta
%       b'x<=beta
% Note: n must be even
% For this class of test problems, we set delta=1
% xl is LNGM of TRS
global flag_con A a b delta n
warning off
flag_con=0;            % indicates we are handling TRS
flge=1;
delta=1;
while flge
    d=blkdiag(1*diag(rand(n/2,1)),-10,-12,5*diag(rand(n/2-2,1)));
    [q,~]=qr(randn(n));
    A=q'*d*q;
    A=(A+A')/2;
    a=rand(n,1);
    [~,lambda]=eigs(A,2,'sa');
    lambdamin1=lambda(1,1);
    lambdamin2=lambda(2,2);
    fla=0;
    M1=[sparse(n,n) speye(n);speye(n) sparse(n,n)];
    opts.v0=randn(2*n,1);
    opts.issym=1;
    opts.isreal=1;
    [v,d]=eigs(@M0,2*n,-M1,2,'lr',opts);
    if (abs(imag(d(2,2))))>1e-8
%         fprintf('Second eigenvalue of M(lambda) is complex \n')
        if real(d(2,2))<=-lambdamin2
            fla=1;
%             fprintf('fla=1: there is no local nonglobal minimizer\n')
        else
            [~,d,flagsecon]=realM0n(v(:,1),d(1,1),lambdamin2);
            if flagsecon==0,
                fla=1;
%                 fprintf('fla=1: there is no local nonglonal minimizer \n');
            elseif (d>=0)&&((d+lambdamin2)>=1e-8)&&((d+lambdamin1)<-1e-8)
                df=d;
                opts.v0=randn(2*n,1);
                opts.issym=0;
                opts.isreal=1;
                [v,d]=eigs(@Asigma,2*n,1,df,opts);
                xopt1=-sign(a'*v(n+1:end))*sqrt(delta)*v(1:n)/norm(v(1:n));
                fval1=xopt1'*A*xopt1+2*a'*xopt1;
%                 fprintf('eigenvector for the second real eige of M(lambda) is computed\n')
                lambda_1LNGM=d;
                kkt1=norm((A+lambda_1LNGM*speye(n))*xopt1+a,inf);
                kkt2=lambda_1LNGM*(norm(xopt1)^2-delta);
%                 fprintf('kkt1 and kkt2 for LNGM are: %g  %g \n',kkt1,kkt2);
            else
                fla=1;
%                 fprintf('fla=1: there is no local nonglonal minimizer \n');
                
            end
        end
    elseif (d(2,2)>1e-12)&&((d(2,2)+lambdamin2)>=1e-8)&&((d(2,2)+lambdamin1)<-1e-8)
        xopt1=-sign(a'*v(n+1:end,2))*sqrt(delta)*v(1:n,2)/norm(v(1:n,2));
        fval1=xopt1'*A*xopt1+2*a'*xopt1;
        lambda_1LNGM=d(end,end);
        kkt1=norm((A+lambda_1LNGM*speye(n))*xopt1+a,inf);
        kkt2=lambda_1LNGM*(norm(xopt1)^2-delta);
%         fprintf('kkt1 and kkt2 for LNGM are: %g  %g \n',kkt1,kkt2);
    elseif (abs(d(2,2))<=1e-12)&&((d(2,2)+lambdamin2)>=1e-8)&&((d(2,2)+lambdamin1)<-1e-8)
        xopt1=-sign(a'*v(n+1:end,2))*sqrt(delta)*v(1:n,2)/norm(v(1:n,2));
        fval1=xopt1'*A*xopt1+2*a'*xopt1;
        lambda_1LNGM=d(end,end);
        kkt1=norm((A+lambda_1LNGM*speye(n))*xopt1+a,inf);
        kkt2=lambda_1LNGM*(norm(xopt1)^2-delta);
%         fprintf('kkt1 and kkt2 for LNGM are: %g  %g \n',kkt1,kkt2);
    else
%         fprintf('fla=1: there is no  local minimzer\n')
        fla=1;
    end
    if fla==1
        flge=1;
    else
        if abs(norm(xopt1)^2-delta)>1e-10,
%             fprintf('error in norm xopt1  %g \n',abs(norm(xopt1)^2-delta))
            fla=1;
        end
        Lagrerror=norm((A+lambda_1LNGM*speye(n))*xopt1+a,inf);
        if Lagrerror>1e-10,
%             fprintf('error in stationarity %g \n',norm((A+lambda_1LNGM*speye(n))*xopt1 +a))
            fla=1;
        end
        Vxs=null(xopt1');
        if min(eig(Vxs'*(A+lambda_1LNGM*speye(n))*Vxs)) <1e-12,
%             fprintf('2nd order cond not strict %g \n',...
%                 min(eig(Vxs'*(A+lambda_1LNGM*speye(n))*Vxs)) )
            fal=1;
        end
        
        if fla==1
            flge=1;
        else
            flge=0;
        end
    end
    
end
%==============================================================
xl=xopt1;    %xl is LNGM of TRS
%==============================================================
%== computing global optimal solution of 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
%===================================================================
normalv=xopt-xl;
point=xl+0.01*normalv;
if  (normalv'*(xl-point))<0
    beta=normalv'*point;
    b=normalv;
else
    beta=-normalv'*point;
    b=-normalv;
end

%======================================================================
    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







