Matrix Multiplication

def matmul(X, Y):
  nrowsX = len(X)
  ncolsX = len(X[0])
  nrowsY = len(Y)
  ncolsY = len(Y[0])
  result = [[0 for _ in range(nrowsX)] for _ in range(ncolsY)]  #initialize as separate lists for each row
  for i in range(nrowsX):
   # iterate through columns of Y
   for j in range(ncolsY)):
       # iterate through rows of Y
       for k in range(ncolsX):
           result[i][j] += X[i][k] * Y[k][j]

# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]

print(matmul(X, Y))

Matrix Exponentiation

Given matrix X, compute X^n:
//Assumes exp >= 0
def power(base, exp):
  if exp == 0:
    return 1
  elif exp % 2 == 0:
    return power(matmul(base, base), exp/2) #recursive case 1
  elif:
    return matmul(base, power(base, exp - 1)) #recursive case 2

Practicing Recursion