Translate matrix function documentation to English
This commit is contained in:
1 parent
79042907b4
commit
ebfe4630ee
1 file changed
+54
-54
+54
-54
@@ -1,78 +1,73 @@
|
||||
from math import cos, sin
|
||||
from typing import TypeAlias
|
||||
|
||||
from tabulate import tabulate
|
||||
|
||||
# Funktionen für Matrixberechnungen
|
||||
from math import cos, sin
|
||||
|
||||
Number: TypeAlias = int | float
|
||||
Matrix: TypeAlias = list[list[Number]]
|
||||
|
||||
|
||||
def matmul(A: Matrix, B: Matrix) -> Matrix:
|
||||
"""
|
||||
Multipliziert zwei Matrizen miteinander.
|
||||
|
||||
"""Multiply two matrices.
|
||||
|
||||
Args:
|
||||
A: Die erste Matrix.
|
||||
B: Die zweite Matrix.
|
||||
|
||||
A: The first matrix.
|
||||
B: The second matrix.
|
||||
|
||||
Returns:
|
||||
Die Ergebnismatrix des Produkts.
|
||||
|
||||
The resulting matrix product.
|
||||
|
||||
Raises:
|
||||
ValueError: Wenn eine Matrix leer oder nicht rechteckig ist
|
||||
oder die Dimensionen nicht zusammenpassen.
|
||||
ValueError: If either matrix is empty, not rectangular,
|
||||
or if their dimensions are incompatible.
|
||||
"""
|
||||
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]:
|
||||
raise ValueError("Matrizen müssen mindestens eine Spalte haben.")
|
||||
|
||||
raise ValueError("Matrices must have at least one column.")
|
||||
|
||||
cols_A = len(A[0])
|
||||
cols_B = len(B[0])
|
||||
|
||||
|
||||
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):
|
||||
raise ValueError("Matrix B ist nicht rechteckig.")
|
||||
|
||||
raise ValueError("Matrix B is not rectangular.")
|
||||
|
||||
if cols_A != len(B):
|
||||
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_B = len(B)
|
||||
|
||||
|
||||
result: Matrix = [
|
||||
[0 for _ in range(cols_B)]
|
||||
for _ in range(rows_A)
|
||||
]
|
||||
|
||||
|
||||
for i in range(rows_A):
|
||||
for j in range(cols_B):
|
||||
for k in range(rows_B):
|
||||
result[i][j] += A[i][k] * B[k][j]
|
||||
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def transpose(matrix: Matrix) -> Matrix:
|
||||
"""Return the transpose of a matrix.
|
||||
|
||||
|
||||
Parameters
|
||||
----------
|
||||
matrix : List[List[Number]]
|
||||
Matrix to transpose.
|
||||
|
||||
|
||||
Returns
|
||||
-------
|
||||
List[List[Number]]
|
||||
Transposed matrix where rows become columns.
|
||||
|
||||
|
||||
Raises
|
||||
------
|
||||
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)]
|
||||
|
||||
|
||||
|
||||
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
|
||||
----------
|
||||
matrix : Matrix
|
||||
Quadratische Matrix, die geändert wird.
|
||||
|
||||
Square matrix to be modified.
|
||||
|
||||
Raises
|
||||
------
|
||||
ValueError
|
||||
Wenn die Matrix leer oder nicht quadratisch ist.
|
||||
If the matrix is empty or not square.
|
||||
"""
|
||||
n = len(matrix)
|
||||
|
||||
if n == 0:
|
||||
raise ValueError("Matrix darf nicht leer sein.")
|
||||
raise ValueError("Matrix must not be empty.")
|
||||
|
||||
for row in matrix:
|
||||
if len(row) != n:
|
||||
raise ValueError("Matrix muss quadratisch sein.")
|
||||
# Transpose
|
||||
raise ValueError("Matrix must be square.")
|
||||
|
||||
# Transpose the matrix
|
||||
for i in range(n):
|
||||
for j in range(i + 1, n):
|
||||
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
|
||||
# Reverse rows
|
||||
|
||||
# Reverse each row
|
||||
for row in matrix:
|
||||
row.reverse()
|
||||
|
||||
|
||||
def rot_2D(theta: float) -> Matrix:
|
||||
"""
|
||||
Erstellt eine Rotationsmatrix für eine Drehung in 2D.
|
||||
"""Create a rotation matrix for a 2D rotation.
|
||||
|
||||
Args:
|
||||
theta: Drehwinkel im Bogenmaß.
|
||||
Parameters
|
||||
----------
|
||||
theta : float
|
||||
Rotation angle in radians.
|
||||
|
||||
Returns:
|
||||
Eine 2x2-Rotationsmatrix.
|
||||
Returns
|
||||
-------
|
||||
Matrix
|
||||
A 2x2 rotation matrix.
|
||||
"""
|
||||
return [
|
||||
[cos(theta), -sin(theta)],
|
||||
[sin(theta), cos(theta)]
|
||||
[sin(theta), cos(theta)],
|
||||
]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(tabulate(rot_2D(1.0)))
|
||||
|
||||
# Beispiel Multiplikation
|
||||
A1 = [[1, 2, 3], [4, 5, 6]]
|
||||
B1 = [[7, 8], [9, 10], [11, 12]]
|
||||
@@ -145,7 +145,7 @@ if __name__ == "__main__":
|
||||
assert matmul(A1, B1) == expected1
|
||||
print("Beispiel 1 erfolgreich:", matmul(A1, B1))
|
||||
|
||||
#Beispiel Rotation
|
||||
# Beispiel Rotation
|
||||
matrix = [[1, 2], [3, 4]]
|
||||
expected = [[3, 1], [4, 2]]
|
||||
|
||||
@@ -153,7 +153,7 @@ if __name__ == "__main__":
|
||||
assert matrix == expected
|
||||
print("Rotation erfolgreich:", matrix)
|
||||
|
||||
#Beispiel Transponierung
|
||||
# Beispiel Transponierung
|
||||
matrix = [[1, 2, 3], [4, 5, 6]]
|
||||
expected = [[1, 4], [2, 5], [3, 6]]
|
||||
|
||||
|
||||
Reference in new issue
Block a user