% identifies connections in a mpbas, for a onebodyoperator enpop. Saves it
% in a matrix, but this could be changed to for example outputting all
% connecting mbstate-indices and the associated one-body operator


function [connMbStates, aPlusaMinus, sign] = nollskilt(mpbas,enpop)

connMbStates = [];
aPlusaMinus = [];
sign = [];


for basind = 1:length(mpbas(:,1))       %loop through mpbasis
    for enind = 1:length(mpbas(1,:))    %loop internally through the mpstate
        for enpopind = 1:length(enpop)  %loop through enpop to find possible connection
            
            % for a given spstate in an mpstate, test for each enpop if it
            % could be used
            if mpbas(basind,enind) == enpop(enpopind,1)
                
                
                activeMpState = mpbas(basind,:);    % practical...
                activeMpState(enind) = enpop(enpopind,2);    % annihilate spstate and create new...
                
                
                stateSorted = sort(activeMpState);  % state might be unsorted, this is of course Matlab-ish
                
                
                
                % loop to search for the new state in the basis. This is
                % not very efficient, and is a major time consumer in this
                % code
                for basind2 = 1 : basind % adapted to the fact that the matrix is symmetrical
                    if mpbas(basind2,:) == stateSorted                    
                        connMbStates = [connMbStates; basind2 basind]; % the indices of connected mbStates
                        aPlusaMinus = [aPlusaMinus; enpop(enpopind,2) enpop(enpopind,1)]; % the operator used for the connection                    
                        sign = [sign ; signedConnection(stateSorted,activeMpState)]; %the sign of the connections
                        break % we wont find the mbState anywhere else
                    end
                end
                
                
                
            end
        end
    end
    
end
end

