def compute_loop_currents(matrix_e, matrix_a, matrix_b, dt, sys_loops, branch_info, stiff_info, sys_loop_map, src_list, state_vector):
    """ Calculates the loop currents after an event from branch currents."""


    # Make a list of the nonstiff loops
    # Because stiff loops will essentially have a negligible
    # current.
    nonstiff_loops=[]
    for c1 in range(len(sys_loop_map)):
        # Check if loop c1 has a stiff branch
        is_loop_stiff="no"
        for c2 in range(len(branch_info)):
            if sys_loop_map[c1][c2]=="stiff":
                is_loop_stiff="yes"

        if is_loop_stiff=="no":
            nonstiff_loops.append(c1)



    # To begin with find out which of the branches
    # occur only in one loop. This will make 
    # computations easier as those branch currents
    # will automatically become the initial values
    # for the loop currents in the next loop analysis.

    # A list of loops and branches with this info
    # It is a corresponding mapping.
    single_loops=[]
    single_branches=[]

    # Iterate through the nonstiff loops.
    for c1 in nonstiff_loops:
        # Iterate through the branches
        for c2 in range(len(branch_info)):  
            # Check if the corresponding system map is a "yes"
            # Which means branch exixts.
            if sys_loop_map[c1][c2]=="yes":
                # Look for the branch in all other nonstiff loops
                # A single occurance elsewhere will mean loop exists
                # So start with the default "no"
                does_branch_occur="no"
                for c3 in nonstiff_loops:
                    if not c1==c3:
                        if sys_loop_map[c3][c2]=="yes":
                            does_branch_occur="yes"
                
                if does_branch_occur=="no":
                    # Next check is both the loop and the branch have not been
                    # found before to prevent clashes.
                    if ((c1 not in single_loops) and (c2 not in single_branches)):
                        single_loops.append(c1)
                        single_branches.append(c2)



    # If the number of loops where a branch occurs in only one
    # loop is less than the number of nonstiff loops, row
    # manipulations will have to be done to the remaining loops
    # so that one branch current can be taken as the loop current
    if (len(single_loops)<len(nonstiff_loops)):
        # Iterate through the nonstiff loops
        for c1 in nonstiff_loops:
            # Make sure the loop has not been found.
            # Don't manipulate those which have been isolated.
            if c1 not in single_loops:
                # Look through the branches
                for c2 in range(len(branch_info)):
                    # Again check if the branch has not been isolated.
                    if c2 not in single_branches:
                        # If a loop exists. So this will find the
                        # first loop that exists in a loop
                        if sys_loop_map[c1][c2]=="yes":
                            # Look at the remaining nonstiff loops.
                            for c3 in nonstiff_loops:
                                if not c1==c3:
                                    # If branch exists in any other nonstiff loop
                                    # Need to manipulate the loops and for that
                                    # need to find out the sense in which the branches
                                    # are in the loops.
                                    if sys_loop_map[c3][c2]=="yes":
                                        # Basically take a running counter with loop c1
                                        # starting from the last branch and keep checking
                                        # whether a branch exists. This is because as
                                        # manipulations take place, branches disappear
                                        # so an exception mght be thrown.
                                        c4=len(sys_loops[c1][c1])-1
                                        while c4>=0:
                                            try:
                                                sys_loops[c1][c1][c4]
                                            except:
                                                pass
                                            else:
                                                c5=len(sys_loops[c3][c3])-1
                                                while c5>=0:
                                                    try:
                                                        sys_loops[c3][c3][c5]
                                                    except:
                                                        pass
                                                    else:
                                                        if sys_loops[c1][c1][c4][:-1]==sys_loops[c3][c3][c5][:-1]:
                                                            if branch_info[c2][:-1]==sys_loops[c1][c1][c4][:-1]:
                                                                # If the branches are in the same sense, subtract them
                                                                # or else add them.
                                                                if sys_loops[c1][c1][c4][-1]==sys_loops[c3][c3][c5][-1]:
                                                                    loop_manipulate(sys_loops, c3, c1, "diff")
                                                                else:
                                                                    loop_manipulate(sys_loops, c3, c1, "add")
                                                    c5=c5-1
                                            c4=c4-1

                                        # If both loop and branch have not been found add them
                                        if ((c1 not in single_loops) and (c2 not in single_branches)):
                                            single_loops.append(c1)
                                            single_branches.append(c2)


                                        # Update the sys_loop_map info
                                        for c4 in range(len(branch_info)):
                                            if sys_loop_map[c1][c4]=="yes" and sys_loop_map[c3][c4]=="yes":
                                                sys_loop_map[c3][c4]="no"

                                            elif sys_loop_map[c1][c4]=="yes" and sys_loop_map[c3][c4]=="no":
                                                sys_loop_map[c3][c4]="yes"




    # Now to set the loop currents equal to branch currents.
    # Take every loop and branch in the single_loop and single_branch lists
    # Since they are a one-to-one mapping, just equate the state vectors to
    # to the branch currents.
    for c1 in range(len(single_loops)):
        loop_row=single_loops[c1]
        for c2 in range(len(sys_loops[loop_row][loop_row])):
            if (sys_loops[loop_row][loop_row][c2][:-1]==branch_info[single_branches[c1]][:-1]):
                if sys_loops[loop_row][loop_row][c2][-1]=="forward":
                    state_vector[0].data[single_loops[c1]][0]=branch_info[single_branches[c1]][-1][2]
                    state_vector[1].data[single_loops[c1]][0]=branch_info[single_branches[c1]][-1][2]
                else:
                    state_vector[0].data[single_loops[c1]][0]=-branch_info[single_branches[c1]][-1][2]
                    state_vector[1].data[single_loops[c1]][0]=-branch_info[single_branches[c1]][-1][2]


    # This is to recalculate the sys_loops matrix.
    # First the diagonal loops are recalculated.
    # Then the off-diagonal (interactions)
    readjust_sys_loops(sys_loops, branch_info, stiff_info)


    # Re-initialize the matrices A, B, and E
    matrix_a.zeros(len(sys_loops),len(sys_loops))
    matrix_e.zeros(len(sys_loops),len(sys_loops))
    matrix_b.zeros(len(sys_loops),matrix_b.columns)

    # Recalculate the matrices A, B and E
    for c1 in range(len(sys_loops)):
        for c2 in range(len(sys_loops)):
            for c3 in range(len(sys_loops[c1][c2])):
                for c4 in range(len(branch_info)):
                    if sys_loops[c1][c2][c3][:-1]==branch_info[c4][:-1]:
                        if c1==c2:
                            matrix_a.data[c1][c2]+=branch_info[c4][-1][0][0]
                            matrix_e.data[c1][c2]+=branch_info[c4][-1][0][1]
                            if sys_loops[c1][c2][c3][-1]=="forward":
                                for c5 in range(matrix_b.columns):
                                    matrix_b.data[c1][c5]+=branch_info[c4][-1][1][c5]

                            else:
                                for c5 in range(matrix_b.columns):
                                    matrix_b.data[c1][c5]-=branch_info[c4][-1][1][c5]
                        else:
                            if sys_loops[c1][c2][c3][-1]=="forward":
                                matrix_a.data[c1][c2]+=branch_info[c4][-1][0][0]
                                matrix_e.data[c1][c2]+=branch_info[c4][-1][0][1]
                            else:
                                matrix_a.data[c1][c2]-=branch_info[c4][-1][0][0]
                                matrix_e.data[c1][c2]-=branch_info[c4][-1][0][1]


    # Make sure that the stiff loops have no inductances
    # so that they are treated as static equations.
    for c1 in range(len(sys_loops)):
        for c2 in range(len(sys_loops[c1][c1])):
            for c3 in range(len(branch_info)):
                if sys_loops[c1][c1][c2][:-1]==branch_info[c3][:-1]:
                    if stiff_info[c3]=="yes":
                        for c4 in range(matrix_e.columns):
                            matrix_e.data[c1][c4]=0.0


    return