Preface
I recently took the "Computer Control System" class, which involved a lot of matrix operations (mostly multiplications). I felt that I couldn't do it by hand and the calculator was too slow, so I wrote a small Python program to do it.
2. Code
import numpy as np
from numpy import shape
m = int(input("Enter m: "))
n = int(input("Enter n: "))
k = int(input("Enter k: "))
A = np.zeros((m, n), dtype=float)
print(A)
print(shape(A))
B = np.zeros((n, k), dtype=float)
print(B)
print(shape(B))
print("Enter Matrix A: ")
for i in range(0, m):
for j in range(0, n):
A[i][j] = float(input())
print("Enter Matrix B: ")
for i in range(0, n):
for j in range(0, k):
B[i][j] = float(input())
print("Here are your matrices A and B: ")
print(A)
print(B)
C = np.zeros((m, k), dtype=float)
print(C)
print(shape(C))
for i in range(0, m):
for j in range(0, k):
for l in range(0, n):
C[i][j] += A[i][l] * B[l][j]
print("Here is your answer matrix C: ")
print(C)
3. A simple example
Program running result:
Calculator calculation result:
4. Thinking process
Summarize
Faster than a calculator.