function x_trs=trs(A,b,alpha)
%INPUT
%================
%A ................... an nxn matrix
%b ................... an n-length vector
%alpha ............. positive scalar
%OUTPUT
%================
% x_trs ............ an optimal solution of
%                      min{x'*A*x+2b'*x:||x||^2<=alpha}

n=length(b);
f=@(lam) norm((A+lam*eye(n))\b)^2-alpha;
[L,p]=chol(A,'lower');
% the case when A is positive definite
if (p==0)
    x_naive=-L'\(L\b);
    if(norm(x_naive)^2<=alpha)
        x_trs=x_naive;
    else
        u=1;
        while (f(u)>0)
            u=2*u;
        end
        lam=bisection(f,0,u,1e-7);
        x_trs=-(A+lam*eye(n))\b;
    end
else
    %when A is not positive definite
    u=1;
    while (f(u)>0)
        u=2*u;
    end
    lam=bisection(f,-min(eig(A))+1e-7,u,1e-7);
    x_trs=-(A+lam*eye(n))\b;
end
