170 lines
4.1 KiB
Python
170 lines
4.1 KiB
Python
# Matrixberechnungen fuer das Projekt MatrixMania
|
||
import math
|
||
|
||
from tabulate import tabulate
|
||
|
||
|
||
def matmul(
|
||
matrix_a: list[list[float]], matrix_b: list[list[float]]
|
||
) -> list[list[float]]:
|
||
|
||
# Basic dimensionality checks
|
||
if not matrix_a:
|
||
raise ValueError("matrix_a cannot be empty")
|
||
if not matrix_b:
|
||
raise ValueError("matrix_b cannot be empty")
|
||
|
||
rows_a = len(matrix_a)
|
||
cols_a = len(matrix_a[0])
|
||
rows_b = len(matrix_b)
|
||
cols_b = len(matrix_b[0])
|
||
|
||
if cols_a != rows_b:
|
||
raise ValueError(
|
||
f"Cannot multiply: matrix_a has {cols_a} columns "
|
||
f"but matrix_b has {rows_b} rows."
|
||
)
|
||
|
||
# Ensure all rows are rectangular
|
||
for r in matrix_a:
|
||
if len(r) != cols_a:
|
||
raise ValueError("matrix_a is not rectangular")
|
||
for r in matrix_b:
|
||
if len(r) != cols_b:
|
||
raise ValueError("matrix_b is not rectangular")
|
||
|
||
# Perform multiplication
|
||
result: list[list[float]] = [[0.0 for _ in range(cols_b)] for _ in range(rows_a)]
|
||
|
||
for i in range(rows_a):
|
||
for j in range(cols_b):
|
||
total = 0.0
|
||
for k in range(cols_a):
|
||
total += matrix_a[i][k] * matrix_b[k][j]
|
||
result[i][j] = total
|
||
|
||
return result
|
||
|
||
|
||
def transpose(matrix: list[list[float]]) -> list[list[float]]:
|
||
"""
|
||
Transpose a 2D matrix.
|
||
|
||
Parameters
|
||
----------
|
||
matrix : list[list[float]]
|
||
A 2D matrix represented as a list of rows, each containing
|
||
floating‑point numbers.
|
||
|
||
Returns
|
||
-------
|
||
list[list[float]]
|
||
The transposed matrix, where rows become columns.
|
||
|
||
Raises
|
||
------
|
||
ValueError
|
||
If the input matrix is empty or has non‑rectangular rows.
|
||
"""
|
||
# Check for empty matrix
|
||
if not matrix:
|
||
raise ValueError("matrix cannot be empty")
|
||
|
||
rows = len(matrix)
|
||
cols = len(matrix[0])
|
||
|
||
# Verify rectangular shape
|
||
for r in matrix:
|
||
if len(r) != cols:
|
||
raise ValueError("matrix is not rectangular")
|
||
|
||
# Transpose by column index
|
||
return [[matrix[r][c] for r in range(rows)] for c in range(cols)]
|
||
|
||
|
||
def rot_2D(theta: float) -> list[list[float]]:
|
||
"""
|
||
Return a 2D rotation matrix for angle ``theta`` in radians.
|
||
|
||
Parameters
|
||
----------
|
||
theta : float
|
||
Rotation angle in radians.
|
||
|
||
Returns
|
||
-------
|
||
list[list[float]]
|
||
2 × 2 rotation matrix
|
||
``[[cos(theta), -sin(theta)],
|
||
[sin(theta), cos(theta)]]``
|
||
|
||
Notes
|
||
-----
|
||
Uses :func:`math.sin` and :func:`math.cos` from the standard library.
|
||
"""
|
||
c = math.cos(theta)
|
||
s = math.sin(theta)
|
||
return [[c, -s], [s, c]]
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# Test Matmul
|
||
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)
|
||
|
||
# Test Transpose
|
||
matrix = [[1, 2, 3], [4, 5, 6]]
|
||
print("Original matrix:", matrix)
|
||
print("Transposed matrix:", transpose(matrix))
|
||
|
||
# Test Rotate
|
||
angle = math.pi / 2 # 90 degrees
|
||
rotation_matrix = rot_2D(angle)
|
||
|
||
print("Rotation matrix (90 degrees):")
|
||
for row in rotation_matrix:
|
||
print(row)
|
||
|
||
# Test rotation of a point using matmul
|
||
point = [[1], [0]]
|
||
rotated_point = matmul(rotation_matrix, point)
|
||
print("Original point:", point)
|
||
print("Rotated point:", rotated_point)
|
||
|
||
# Test invalid matrices
|
||
print("\nTest invalid matrices:")
|
||
|
||
# Test Empty matrix
|
||
try:
|
||
transpose([])
|
||
except ValueError as error:
|
||
print("Empty matrix:", error)
|
||
|
||
# Test Matrix with different row lengths
|
||
try:
|
||
transpose([[1, 2, 3], [4, 5]])
|
||
except ValueError as error:
|
||
print("Non-rectangular matrix:", error)
|
||
|
||
# Test 3 Invalid matrix multiplication
|
||
try:
|
||
matmul([[1, 2, 3]], [[1, 2], [3, 4]])
|
||
except ValueError as error:
|
||
print("Invalid dimensions:", error)
|
||
|
||
print("Rotationsmatrix als Tabelle:")
|
||
print(tabulate(rot_2D(1.0)))
|