from random import randrange

A = []
B = []

for i in range(3):
    A.append([])
    B.append([])
    for j in range(4):
        A[i].append(randrange(-5,6))
        B[i].append(randrange(-5,6))

print("A is",A)
print("B is",B)

C = []

for i in range(3):
    C.append([])
    for j in range(4):
        C[i].append( 3*A[i][j]-2*B[i][j])

print("C is 3A - 2B")
print("C is",C)        

M = [
    [-2,3,0,4],
    [-1,1,5,9]      
]
N =[
    [1,2,3],
    [4,5,6],
    [7,8,9]
]

# create the transpose of M as a zero matrix
# its dimension is (4x2)
MT = []
for i in range(4):
    MT.append([])
    for j in range(2):
        MT[i].append(0)

# create the transpose of N as a zero matrix 
# its dimension is (3x3)
NT = []
for i in range(3):
    NT.append([])
    for j in range(3):
        NT[i].append(0)

# calculate the MT
for i in range(2):
    for j in range(4):
        MT[j][i]=M[i][j] # check the indices

print("M is")
for i in range(len(M)):
    print(M[i])

print()
print("Transpose of M is")
for i in range(len(MT)):
    print(MT[i])

print()
# calculate the NT
for i in range(3):
    for j in range(3):
        NT[j][i]=N[i][j] # check the indices

print("N is")
for i in range(len(N)):
    print(N[i])

print()
print("Transpose of N is")
for i in range(len(NT)):
    print(NT[i])

N = [
    [-1,1,2],
    [0,-2,-3],
    [3,2,5],
    [0,2,-2]
]

u = [2,-1,3]

uprime =[]

print("N is")
for i in range(len(N)):
    print(N[i])

print()
print("u is",u)

for i in range(len(N)): # the number of rows of N
    S = 0 # summation of pairwise multiplications
    for j in range(len(u)): # the dimension of u
        S = S + N[i][j] * u[j]
    uprime.append(S)

print()
print("u' is",uprime)    

# matrix M
M = [
    [-1,0,1],
    [-2,-1,2],
    [1,2,-2]
]

# matrix N
N = [
    [0,2,1],
    [3,-1,-2],
    [-1,1,0]
]

# matrix K
K = []

for i in range(3):
    K.append([])
    for j in range(3):
        # here we calculate K[i][j]
        # inner product of i-th row of M with j-th row of N
        S = 0
        for k in range(3):
            S = S + M[i][k] * N[k][j]
        K[i].append(S)
        
print("M is")
for i in range(len(M)):
    print(M[i])
    
print()
print("N is")
for i in range(len(N)):
    print(N[i])

print()
print("K is")
for i in range(len(K)):
    print(K[i])

from random import randrange

A = []
B = []
AB = []
BA = []
DIFF = []

# create A, B, AB, BA, DIFF together
for i in range(2):
    A.append([])
    B.append([])
    AB.append([])
    BA.append([])
    DIFF.append([])
    for j in range(2):
        A[i].append(randrange(-10,10)) # the elements of A are random
        B[i].append(randrange(-10,10)) # the elements of B are random
        AB[i].append(0) # the elements of AB are initially set to zeros
        BA[i].append(0) # the elements of BA are initially set to zeros
        DIFF[i].append(0) # the elements of DIFF are initially set to zeros

print("A =",A)
print("B =",B)
print() # print a line
print("AB, BA, and DIFF are initially zero matrices")
print("AB =",AB)
print("BA =",BA)
print("DIFF =",BA)

# let's find AB
for i in range(2):
    for j in range(2):
        # remark that AB[i][j] is already 0, and so we can directly add all pairwise multiplications
        for k in range(2):
            AB[i][j] = AB[i][j] + A[i][k] * B[k][j] # each multiplication is added

print() # print a line
print("AB =",AB)

# let's find BA
for i in range(2):
    for j in range(2):
        # remark that BA[i][j] is already 0, and so we can directly add all pairwise multiplications
        for k in range(2):
            BA[i][j] = BA[i][j] + B[i][k] * A[k][j] # each multiplication is added

print("BA =",BA)

# let's calculate DIFF = AB- BA
for i in range(2):
    for j in range(2):
        DIFF[i][j] = AB[i][j] - BA[i][j]

print() # print a line        
print("DIFF = AB - BA =",DIFF)        

