import numpy as np

# Defining function to find the score function of the BCN matrix
def score_function(A):
    row, column, membership, _ = A.shape
    Score_Mat = np.zeros((row, column))

    for i in range(row):
        for j in range(column):
            element = A[i,j]

            T1p, T2p = element[0]
            I1p, I2p = element[1]
            F1p, F2p = element[2]
            T1n, T2n = element[3]
            I1n, I2n = element[4]
            F1n, F2n = element[5]

            # Avoiding divided by zero
            try:
                pos_part = (1/(T1p * I1p * F1p)) * ((T2p - I2p - F2p)/3)
            except ZeroDivisionError:
                pos_part = 0

            try:
                neg_part = (1/(T1n * I1n * F1n)) * ((T2n - I2n - F2n)/3)
            except ZeroDivisionError:
                neg_part = 0

            Score_Mat[i,j] = pos_part - neg_part

    return Score_Mat

# Function to compute the Accuracy function for a BCNM
def accuracy_function(A):
    row, column, membership, _ = A.shape
    Accuracy_Mat = np.zeros((row, column))

    for i in range(row):
        for j in range(column):
            element = A[i,j]

            T1p, T2p = element[0]
            F1p, F2p = element[2]
            T1n, T2n = element[3]
            F1n, F2n = element[5]

            # Avoiding divided by zero
            try:
                positive_component = (1/(T1p * F1p)) * ((T2p - F2p)/2)
            except ZeroDivisionError:
                positive_component = 0

            try:
                negative_component = (1/(T1n * F1n)) * ((T2n + F2n)/2)
            except ZeroDivisionError:
                negative_component = 0

            Accuracy_Mat[i,j] = positive_component - negative_component

    return Accuracy_Mat

# Entering an example matrix
A = np.array([
    [[(0.3, 0.1), (0.7, 0.9), (0.3, 0.1), (-0.6, -0.6), (-0.8, -0.5), (-0.3, -0.2)],
     [(0.8, 0.9), (0.6, 0.9), (0.5, 0.9), (-0.8, -0.3), (-0.6, -0.7), (-0.9, -0.4)],
     [(0.6, 0.9), (0.6, 0.3), (0.3, 0.9), (-0.1, -0.3), (-0.8, -0.5), (-0.3, -0.1)],
     [(0.6, 0.3), (0.8, 0.9), (0.7, 0.6), (-0.4, -0.3), (-0.7, -0.7), (-0.6, -0.2)],
     [(0.4, 0.2), (0.1, 0.9), (0.5, 0.2), (-0.5, -0.8), (-0.2, -0.7), (-0.8, -0.3)]],

    [[(0.4, 0.6), (0.5, 0.9), (0.3, 0.1), (-0.1, -0.6), (-0.4, -0.6), (-0.3, -0.2)],
     [(0.3, 0.7), (0.8, 0.9), (0.1, 0.9), (-0.8, -0.3), (-0.4, -0.7), (-0.9, -0.2)],
     [(0.7, 0.9), (0.4, 0.3), (0.1, 0.9), (-0.3, -0.7), (-0.9, -0.5), (-0.1, -0.1)],
     [(0.4, 0.3), (0.6, 0.9), (0.7, 0.9), (-0.4, -0.3), (-0.5, -0.4), (-0.6, -0.7)],
     [(0.8, 0.5), (0.4, 0.9), (0.9, 0.7), (-0.7, -0.6), (-0.2, -0.3), (-0.8, -0.5)]],

    [[(0.6, 0.3), (0.5, 0.9), (0.3, 0.1), (-0.2, -0.6), (-0.8, -0.3), (-0.4, -0.2)],
     [(0.8, 0.7), (0.6, 0.9), (0.3, 0.9), (-0.4, -0.7), (-0.6, -0.3), (-0.9, -0.2)],
     [(0.1, 0.4), (0.1, 0.3), (0.5, 0.9), (-0.1, -0.5), (-0.7, -0.3), (-0.4, -0.2)],
     [(0.4, 0.1), (0.8, 0.9), (0.7, 0.8), (-0.2, -0.8), (-0.4, -0.6), (-0.1, -0.2)],
     [(0.3, 0.6), (0.3, 0.9), (0.5, 0.2), (-0.3, -0.3), (-0.2, -0.5), (-0.1, -0.3)]],
])

# Compute Score and Accuracy matrix
Score_Matrix = score_function(A)
Accuracy_Matrix = accuracy_function(A)

print("The score matrix of the entered BCNM is\n", Score_Matrix)
print("The accuracy matrix of the entered BCNM is\n", Accuracy_Matrix)
