Translate matrix function documentation to English
This commit is contained in:
1 parent
79042907b4
commit
ebfe4630ee
1 file changed
+35
-35
+35
-35
@@ -1,47 +1,42 @@
|
|||||||
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)
|
||||||
@@ -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.
|
||||||
|
|
||||||
Das Verfahren kombiniert Transponieren und Zeilendrehen, was in O(n²) Zeit und O(1) zusätzlichem Speicher arbeitet.
|
The algorithm combines transposition and row reversal,
|
||||||
|
running in O(n²) time with O(1) additional space.
|
||||||
|
|
||||||
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]]
|
||||||
|
|||||||
Reference in new issue
Block a user