import numpy as np

# Defining function for computing component wise addition of 2 BCN matrices
def add(A, B):
    row, column, _, _ = A.shape
    element = np.zeros_like(A, dtype = object)
    
    # Component wise addition operations
    for i in range(row):
        for j in range(column):
            # Positive membership functions
            element[i, j, 0, 0] = max(A[i, j, 0, 0], B[i, j, 0, 0])  # alpha+ amplitude
            element[i, j, 0, 1] = max(A[i, j, 0, 1], B[i, j, 0, 1])  # alpha+ angle
            
            element[i, j, 1, 0] = max(A[i, j, 1, 0], B[i, j, 1, 0])  # beta+ amplitude
            element[i, j, 1, 1] = max(A[i, j, 1, 1], B[i, j, 1, 1])  # beta+ angle
            
            element[i, j, 2, 0] = min(A[i, j, 2, 0], B[i, j, 2, 0])  # gamma+ amplitude
            element[i, j, 2, 1] = min(A[i, j, 2, 1], B[i, j, 2, 1])  # gamma+ angle
            
            # Negative membership functions
            element[i, j, 3, 0] = min(A[i, j, 3, 0], B[i, j, 3, 0])  # alpha- amplitude
            element[i, j, 3, 1] = min(A[i, j, 3, 1], B[i, j, 3, 1])  # alpha- angle
            
            element[i, j, 4, 0] = min(A[i, j, 4, 0], B[i, j, 4, 0])  # beta- amplitude
            element[i, j, 4, 1] = min(A[i, j, 4, 1], B[i, j, 4, 1])  # beta- angle
            
            element[i, j, 5, 0] = max(A[i, j, 5, 0], B[i, j, 5, 0])  # gamma- amplitude
            element[i, j, 5, 1] = max(A[i, j, 5, 1], B[i, j, 5, 1])  # gamma- angle

    return element

# Defining function for computing component wise multiplication of 2 BCN matrices
def multiply(A, B):
    row, column, _, _ = A.shape
    element = np.zeros_like(A, dtype = object)
    
    # Component wise multiplication operations
    for i in range(row):
        for j in range(column):
            # Positive memberships
            element[i, j, 0, 0] = min(A[i, j, 0, 0], B[i, j, 0, 0])  # alpha+ amplitude
            element[i, j, 0, 1] = min(A[i, j, 0, 1], B[i, j, 0, 1])  # alpha+ angle
            
            element[i, j, 1, 0] = min(A[i, j, 1, 0], B[i, j, 1, 0])  # beta+ amplitude
            element[i, j, 1, 1] = min(A[i, j, 1, 1], B[i, j, 1, 1])  # beta+ angle
            
            element[i, j, 2, 0] = max(A[i, j, 2, 0], B[i, j, 2, 0])  # gamma+ amplitude
            element[i, j, 2, 1] = max(A[i, j, 2, 1], B[i, j, 2, 1])  # gamma+ angle
            
            # Negative memberships
            element[i, j, 3, 0] = max(A[i, j, 3, 0], B[i, j, 3, 0])  # alpha- amplitude
            element[i, j, 3, 1] = max(A[i, j, 3, 1], B[i, j, 3, 1])  # alpha- angle
            
            element[i, j, 4, 0] = max(A[i, j, 4, 0], B[i, j, 4, 0])  # beta- amplitude
            element[i, j, 4, 1] = max(A[i, j, 4, 1], B[i, j, 4, 1])  # beta- angle
            
            element[i, j, 5, 0] = min(A[i, j, 5, 0], B[i, j, 5, 0])  # gamma- amplitude
            element[i, j, 5, 1] = min(A[i, j, 5, 1], B[i, j, 5, 1])  # gamma- angle

    return element

# Defining function to print the final matrix
def print_answer(matrix):
    row, column, _, _ = matrix.shape
    for i in range(row):
        print("[")
        for j in range(column):
            element = matrix[i, j]
            format = ", ".join([f"{amplitude:.2f}e^{{i.{angle:.2f}}}" for amplitude, angle in element])
            print(f"<{format}>")
        print("]")

# Entering example matrix
A = np.array([
    [
        [(0.4, 0.7), (0.5, np.pi), (0.5, 0.3), (-0.6, -np.pi/3), (-0.8, -np.pi/4), (-0.3, -0.9)],
        [(0.4, 0.8), (0.2, np.pi/6), (0.4, 0.1), (-0.5, -np.pi/2), (-0.6, -np.pi/4), (-0.4, -0.2)],
        [(0.2, 0.4), (0.3, np.pi/4), (0.4, 0.7), (-0.8, -np.pi/6), (-0.2, -np.pi/4), (-0.2, -0.9)]
    ],
    [
        [(0.5, 0.8), (0.5, np.pi/4), (0.2, np.pi), (-0.2, -np.pi/3), (-0.9, -np.pi/2), (-0.2, 0)],
        [(0.7, 0.6), (0.6, np.pi/2), (0.6, 0.7), (-0.7, -np.pi/2), (-0.5, -np.pi/3), (-0.4, -0.1)],
        [(0.8, 0.7), (0.4, np.pi/3), (0.5, 0.5), (-0.3, -np.pi/4), (-0.6, -np.pi/6), (-0.3, -0.2)]
    ]
])

B = np.array([
    [
        [(0.8, 0.6), (0.7, 2*np.pi), (0.7, np.pi/2), (-0.9, -np.pi/4), (-0.2, -np.pi/4), (-0.8, -0.3)],
        [(0.8, 0.7), (0.5, np.pi/2), (0.8, 0.4), (-0.6, -np.pi/6), (-0.3, -np.pi/6), (-0.4, 0.2)],
        [(0.5, 0.2), (0.1, np.pi/3), (0.4, 0.1), (-0.7, -np.pi/3), (-0.4, -np.pi/2), (-0.7, -0.4)]
    ],
    [
        [(0.9, 0.8), (0.1, np.pi/4), (0.8, np.pi/3), (-0.4, -np.pi/2), (-0.4, -np.pi/3), (-0.3, -0.9)],
        [(0.7, 0.9), (0.2, np.pi/6), (0.9, 0.7), (-0.7, -np.pi/6), (-0.3, -np.pi/2), (-0.6, -0.2)],
        [(0.6, 0.9), (0.8, np.pi/4), (0.9, 0.8), (-0.4, -np.pi/3), (-0.6, -np.pi/6), (-0.5, -0.3)]
    ]
])

addition = add(A, B)  # Calling component wise addition
multiplication = multiply(A, B)  # Calling component wise multiplication

# Print the final answer
print("Component wise addition of the entered matrices is")
print_answer(addition)

print("\n")
print("Component wise multiplication of the entered matrices is")
print_answer(multiplication)
