[Python small exercise] Simple floating point matrix multiplication

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.
Insert image description hereInsert image description here

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

Insert image description here
Program running result:
Insert image description here
Calculator calculation result:
Insert image description here
Insert image description hereInsert image description here

4. Thinking process

Insert image description here

Summarize

Faster than a calculator.

Guess you like

Origin blog.csdn.net/weixin_43031313/article/details/133300976