Translate matrix function documentation to English

This commit is contained in:
Amalia Sucipto committed 2026-10-22 13:02:00 +02:00
1 parent 79042907b4
commit ebfe4630ee
1 file changed
+54 -54
+54 -54
View File
@@ -1,78 +1,73 @@
from math import cos, sin
from typing import TypeAlias from typing import TypeAlias
from math import cos, sin
from tabulate import tabulate
# Funktionen für Matrixberechnungen
Number: TypeAlias = int | float Number: TypeAlias = int | float
Matrix: TypeAlias = list[list[Number]] Matrix: TypeAlias = list[list[Number]]
def matmul(A: Matrix, B: Matrix) -> Matrix: def matmul(A: Matrix, B: Matrix) -> Matrix:
""" """Multiply two matrices.
Multipliziert zwei Matrizen miteinander.
Args: Args:
A: Die erste Matrix. A: The first matrix.
B: Die zweite Matrix. B: The second matrix.
Returns: Returns:
Die Ergebnismatrix des Produkts. The resulting matrix product.
Raises: Raises:
ValueError: Wenn eine Matrix leer oder nicht rechteckig ist ValueError: If either matrix is empty, not rectangular,
oder die Dimensionen nicht zusammenpassen. or if their dimensions are incompatible.
""" """
if not A or not B: if not A or not B:
raise ValueError("Matrizen dürfen nicht leer sein.") raise ValueError("Matrices must not be empty.")
if not A[0] or not B[0]: if not A[0] or not B[0]:
raise ValueError("Matrizen müssen mindestens eine Spalte haben.") raise ValueError("Matrices must have at least one column.")
cols_A = len(A[0]) cols_A = len(A[0])
cols_B = len(B[0]) cols_B = len(B[0])
if any(len(row) != cols_A for row in A): if any(len(row) != cols_A for row in A):
raise ValueError("Matrix A ist nicht rechteckig.") raise ValueError("Matrix A is not rectangular.")
if any(len(row) != cols_B for row in B): if any(len(row) != cols_B for row in B):
raise ValueError("Matrix B ist nicht rechteckig.") raise ValueError("Matrix B is not rectangular.")
if cols_A != len(B): if cols_A != len(B):
raise ValueError( raise ValueError(
"Die Spaltenzahl von A muss der Zeilenzahl von B entsprechen." "The number of columns in A must equal the number of rows in B."
) )
rows_A = len(A) rows_A = len(A)
rows_B = len(B) rows_B = len(B)
result: Matrix = [ result: Matrix = [
[0 for _ in range(cols_B)] [0 for _ in range(cols_B)]
for _ in range(rows_A) for _ in range(rows_A)
] ]
for i in range(rows_A): for i in range(rows_A):
for j in range(cols_B): for j in range(cols_B):
for k in range(rows_B): for k in range(rows_B):
result[i][j] += A[i][k] * B[k][j] result[i][j] += A[i][k] * B[k][j]
return result return result
def transpose(matrix: Matrix) -> Matrix: def transpose(matrix: Matrix) -> Matrix:
"""Return the transpose of a matrix. """Return the transpose of a matrix.
Parameters Parameters
---------- ----------
matrix : List[List[Number]] matrix : List[List[Number]]
Matrix to transpose. Matrix to transpose.
Returns Returns
------- -------
List[List[Number]] List[List[Number]]
Transposed matrix where rows become columns. Transposed matrix where rows become columns.
Raises Raises
------ ------
ValueError ValueError
@@ -87,56 +82,61 @@ def transpose(matrix: Matrix) -> Matrix:
return [[matrix[i][j] for i in range(n_rows)] for j in range(n_cols)] return [[matrix[i][j] for i in range(n_rows)] for j in range(n_cols)]
def rotate_matrix(matrix: Matrix) -> None: def rotate_matrix(matrix: Matrix) -> None:
"""Rotiert eine quadratische Matrix 90° im Uhrzeigersinn in‑Place. """Rotate a square matrix 90 degrees clockwise in-place.
The algorithm combines transposition and row reversal,
running in O(n²) time with O(1) additional space.
Das Verfahren kombiniert Transponieren und Zeilendrehen, was in O(n²) Zeit und O(1) zusätzlichem Speicher arbeitet.
Parameters Parameters
---------- ----------
matrix : Matrix matrix : Matrix
Quadratische Matrix, die geändert wird. Square matrix to be modified.
Raises Raises
------ ------
ValueError ValueError
Wenn die Matrix leer oder nicht quadratisch ist. If the matrix is empty or not square.
""" """
n = len(matrix) n = len(matrix)
if n == 0: if n == 0:
raise ValueError("Matrix darf nicht leer sein.") raise ValueError("Matrix must not be empty.")
for row in matrix: for row in matrix:
if len(row) != n: if len(row) != n:
raise ValueError("Matrix muss quadratisch sein.") raise ValueError("Matrix must be square.")
# Transpose
# Transpose the matrix
for i in range(n): for i in range(n):
for j in range(i + 1, n): for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Reverse rows
# Reverse each row
for row in matrix: for row in matrix:
row.reverse() row.reverse()
def rot_2D(theta: float) -> Matrix: def rot_2D(theta: float) -> Matrix:
""" """Create a rotation matrix for a 2D rotation.
Erstellt eine Rotationsmatrix für eine Drehung in 2D.
Args: Parameters
theta: Drehwinkel im Bogenmaß. ----------
theta : float
Rotation angle in radians.
Returns: Returns
Eine 2x2-Rotationsmatrix. -------
Matrix
A 2x2 rotation matrix.
""" """
return [ return [
[cos(theta), -sin(theta)], [cos(theta), -sin(theta)],
[sin(theta), cos(theta)] [sin(theta), cos(theta)],
] ]
if __name__ == "__main__": if __name__ == "__main__":
print(tabulate(rot_2D(1.0)))
# Beispiel Multiplikation # Beispiel Multiplikation
A1 = [[1, 2, 3], [4, 5, 6]] A1 = [[1, 2, 3], [4, 5, 6]]
B1 = [[7, 8], [9, 10], [11, 12]] B1 = [[7, 8], [9, 10], [11, 12]]
@@ -145,7 +145,7 @@ if __name__ == "__main__":
assert matmul(A1, B1) == expected1 assert matmul(A1, B1) == expected1
print("Beispiel 1 erfolgreich:", matmul(A1, B1)) print("Beispiel 1 erfolgreich:", matmul(A1, B1))
#Beispiel Rotation # Beispiel Rotation
matrix = [[1, 2], [3, 4]] matrix = [[1, 2], [3, 4]]
expected = [[3, 1], [4, 2]] expected = [[3, 1], [4, 2]]
@@ -153,7 +153,7 @@ if __name__ == "__main__":
assert matrix == expected assert matrix == expected
print("Rotation erfolgreich:", matrix) print("Rotation erfolgreich:", matrix)
#Beispiel Transponierung # Beispiel Transponierung
matrix = [[1, 2, 3], [4, 5, 6]] matrix = [[1, 2, 3], [4, 5, 6]]
expected = [[1, 4], [2, 5], [3, 6]] expected = [[1, 4], [2, 5], [3, 6]]