% hopanalyze.m
%
% analyze the hopfield network with parameters b, A, U
%   and lambda and activation function asig
%
% created by:
%       Steven E. Lamberson, Jr.
%
% created on:
%       October 08, 2004
function Stats = hopanalyze(b,A,U,lambda)

m = 1000;
u = linspace(-1,1,m);
f = -b(1)*u + A(1,2)*asig(A(2,1)*asig(u,lambda)/b(2),lambda);
count = 0;
for i = 2:m
    if f(i)/f(i-1) < 0
        count = count + 1;
        fa = f(i-1);
        fb = f(i);
        ua = u(i-1);
        ub = u(i);
        slope = (fb-fa)/(ub-ua);
        u1(count) = ua - fa/slope;
    end
end
u2 = A(2,1)*asig(u1,lambda)/b(2);

sigma = zeros(2,2,count);
S = sigma;
alpha = [1;1];
del = [0 0 0;0 0 0];
type = zeros(1,count);
for i = 1:count
    ue = [u1(i);u2(i)];
    r(i) = min(abs(ue));
    G(:,i) = asig(r(i)+ue,lambda)-asig(ue,lambda);
    sigma(:,2,i) = G(:,i)/r(i);
    for j = 1:2
        if A(j,j) > 0
            del(j,i) = sigma(j,2,i);
        end
        for k = 1:2
            if j == k
                S(j,k,i) = alpha(j)*(-b(j) + del(j,i)*A(j,j));
            else
                S(j,k,i) = 0.5*(alpha(j)*A(j,k)*sigma(k,2,i) + alpha(k)*A(k,j)*sigma(j,2,i));
            end
        end
    end
    try
        V(:,i) = eig(S(:,:,i));
    catch
        count
        sigma
        i
        S(:,:,i)
        return
    end
    if (V(1,i) < 0) & (V(2,i) < 0)
        type(i) = 1;
    end
end

ue = [u1+U(1)/b(1);u2+U(2)/b(2)];

Stats.ue = ue;
Stats.V = V;
Stats.S = S;
Stats.sigma = sigma;
Stats.del = del;
Stats.G = G;
Stats.r = r;
Stats.alpha = alpha;
Stats.count = count;