170 lines
5.0 KiB
Python
170 lines
5.0 KiB
Python
# Matrixfunktionen für das Projekt matrixmania
|
|
from typing import List, Sequence
|
|
import math
|
|
|
|
|
|
def matmul(a: Sequence[Sequence[float]], b: Sequence[Sequence[float]]) -> List[List[float]]:
|
|
"""
|
|
Multiply two matrices.
|
|
|
|
Args:
|
|
a: Left matrix as a sequence of sequences of numbers.
|
|
b: Right matrix as a sequence of sequences of numbers.
|
|
|
|
Returns:
|
|
The product matrix as a list of lists.
|
|
|
|
Raises:
|
|
ValueError: If inputs are not valid matrices or dimensions are incompatible.
|
|
"""
|
|
# ---- Validate a ----
|
|
if not a or not isinstance(a, Sequence):
|
|
raise ValueError("First argument must be a non-empty matrix.")
|
|
if not all(isinstance(row, Sequence) for row in a):
|
|
raise ValueError("First argument must be a sequence of sequences.")
|
|
n_rows_a = len(a)
|
|
n_cols_a = len(a[0])
|
|
if n_cols_a == 0:
|
|
raise ValueError("First matrix rows cannot be empty.")
|
|
for row in a:
|
|
if len(row) != n_cols_a:
|
|
raise ValueError("First matrix rows must have the same length.")
|
|
|
|
# ---- Validate b ----
|
|
if not b or not isinstance(b, Sequence):
|
|
raise ValueError("Second argument must be a non-empty matrix.")
|
|
if not all(isinstance(row, Sequence) for row in b):
|
|
raise ValueError("Second argument must be a sequence of sequences.")
|
|
n_rows_b = len(b)
|
|
n_cols_b = len(b[0])
|
|
if n_cols_b == 0:
|
|
raise ValueError("Second matrix rows cannot be empty.")
|
|
for row in b:
|
|
if len(row) != n_cols_b:
|
|
raise ValueError("Second matrix rows must have the same length.")
|
|
|
|
# ---- Dimension compatibility ----
|
|
if n_cols_a != n_rows_b:
|
|
raise ValueError(
|
|
f"Incompatible dimensions: {n_rows_a}x{n_cols_a} cannot be "
|
|
f"multiplied by {n_rows_b}x{n_cols_b}"
|
|
)
|
|
|
|
# ---- Compute product ----
|
|
result: List[List[float]] = [
|
|
[0.0 for _ in range(n_cols_b)] for _ in range(n_rows_a)
|
|
]
|
|
|
|
for i in range(n_rows_a):
|
|
for j in range(n_cols_b):
|
|
sum_prod = 0.0
|
|
for k in range(n_cols_a):
|
|
sum_prod += a[i][k] * b[k][j]
|
|
result[i][j] = sum_prod
|
|
|
|
return result
|
|
|
|
|
|
def transpose(matrix: Sequence[Sequence[float]]) -> List[List[float]]:
|
|
"""
|
|
Return the transpose of a matrix.
|
|
|
|
Raises
|
|
-------
|
|
ValueError
|
|
If the matrix is empty or rows have differing lengths.
|
|
"""
|
|
if not matrix or not isinstance(matrix, Sequence):
|
|
raise ValueError("Argument must be a non-empty matrix.")
|
|
if not all(isinstance(row, Sequence) for row in matrix):
|
|
raise ValueError("Argument must be a sequence of sequences.")
|
|
n_rows = len(matrix)
|
|
n_cols = len(matrix[0])
|
|
if n_cols == 0:
|
|
raise ValueError("Matrix rows cannot be empty.")
|
|
for row in matrix:
|
|
if len(row) != n_cols:
|
|
raise ValueError("All rows must have the same length.")
|
|
return [[matrix[i][j] for i in range(n_rows)] for j in range(n_cols)]
|
|
|
|
|
|
def rotate(matrix: Sequence[Sequence[float]]) -> List[List[float]]:
|
|
"""
|
|
Rotate the matrix 90 degrees clockwise.
|
|
|
|
Raises
|
|
-------
|
|
ValueError
|
|
If the matrix is empty or rows have differing lengths.
|
|
"""
|
|
if not matrix or not isinstance(matrix, Sequence):
|
|
raise ValueError("Argument must be a non-empty matrix.")
|
|
if not all(isinstance(row, Sequence) for row in matrix):
|
|
raise ValueError("Argument must be a sequence of sequences.")
|
|
n_rows = len(matrix)
|
|
n_cols = len(matrix[0])
|
|
if n_cols == 0:
|
|
raise ValueError("Matrix rows cannot be empty.")
|
|
for row in matrix:
|
|
if len(row) != n_cols:
|
|
raise ValueError("All rows must have the same length.")
|
|
transposed = transpose(matrix)
|
|
return [list(reversed(row)) for row in transposed]
|
|
|
|
|
|
def rot_2D(theta_degrees: float) -> List[List[float]]:
|
|
"""
|
|
Return a 2x2 rotation matrix for the given angle in degrees.
|
|
|
|
Args:
|
|
theta_degrees: Rotation angle in degrees.
|
|
|
|
Returns:
|
|
A 2x2 matrix [[cos, -sin], [sin, cos]].
|
|
|
|
Raises:
|
|
ValueError: If theta_degrees is not a number.
|
|
"""
|
|
|
|
if not isinstance(theta_degrees, (int, float)):
|
|
raise ValueError("Angle must be a number.")
|
|
theta = math.radians(theta_degrees)
|
|
cos_theta = math.cos(theta)
|
|
sin_theta = math.sin(theta)
|
|
return [[cos_theta, -sin_theta], [sin_theta, cos_theta]]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("matmul test 1")
|
|
a = [[1, 2, 3], [4, 5, 6]]
|
|
b = [[7, 8], [9, 10], [11, 12]]
|
|
print(matmul(a, b))
|
|
|
|
print("matmul test 2")
|
|
a = [[1, 0], [0, 1]]
|
|
b = [[5, 6], [7, 8]]
|
|
print(matmul(a, b))
|
|
|
|
print("\ntranspose test 1")
|
|
m = [[1, 2, 3], [4, 5, 6]]
|
|
print(transpose(m))
|
|
|
|
print("\ntranspose test 2")
|
|
m = [[1, 2], [3, 4], [5, 6]]
|
|
print(transpose(m))
|
|
|
|
print("\nrotate test 1")
|
|
m = [[1, 2, 3], [4, 5, 6]]
|
|
print(rotate(m))
|
|
|
|
print("\nrotate test 2")
|
|
m = [[1, 2], [3, 4]]
|
|
print(rotate(m))
|
|
|
|
print("\nrot_2D test 1")
|
|
print(rot_2D(90))
|
|
|
|
print("\nrot_2D test 2")
|
|
print(rot_2D(45))
|
|
|