41 lines
1019 B
Python
41 lines
1019 B
Python
from typing import List
|
|
import math
|
|
|
|
def matmul(mat_a: List[List[float]], mat_b: List[List[float]]) -> List[List[float]]:
|
|
m, n = len(mat_a), len(mat_a[0])
|
|
n2, p = len(mat_b), len(mat_b[0])
|
|
|
|
# Dimension prüfen: Spaltenzahl von A == Zeilenzahl von B
|
|
if n != n2:
|
|
raise ValueError('Matrix dimensions do not match')
|
|
|
|
result = []
|
|
for i in range(m): # Zeilen von A
|
|
line = []
|
|
for j in range(p): # Spalten von B
|
|
s = 0.0
|
|
for k in range(n): # Spalten von A / Zeilen von B
|
|
s += mat_a[i][k] * mat_b[k][j] # <--- wichtig: b[k][j]
|
|
line.append(s)
|
|
result.append(line)
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
matrix_a = [
|
|
[3, 4, -1, 4],
|
|
[-2, 2, 5, 1]
|
|
]
|
|
|
|
matrix_b = [
|
|
[1, 3, -2],
|
|
[2, 5, 1],
|
|
[-1, 4, -4],
|
|
[2, 3, 6]
|
|
]
|
|
|
|
matrix_c = matmul(matrix_a, matrix_b)
|
|
print("Ergebnis C = A * B:")
|
|
for row in matrix_c:
|
|
print(row)
|