Tabulate integriert und Requirements ergänzt
This commit is contained in:
1 parent
6643e5a70b
commit
1415833496
1 file changed
+36
-22
+36
-22
@@ -1,21 +1,15 @@
|
||||
"""Compute operations for matrices.
|
||||
|
||||
This module provides a single public function, :func:`matmul`, which multiplies two
|
||||
2‑D matrices represented as lists of lists of floats. The implementation is
|
||||
This module provides public functions for matrix multiplication, transposition,
|
||||
matrix rotation, and generating 2D rotation matrices. The implementation is
|
||||
lightweight, well typed and contains detailed Google‑style documentation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import List
|
||||
|
||||
import math
|
||||
|
||||
def rot_2D(theta: float) -> List[List[float]]:
|
||||
"""Erzeugt eine 2D-Rotationsmatrix für einen Winkel theta (im Bogenmaß)."""
|
||||
c = math.cos(theta)
|
||||
s = math.sin(theta)
|
||||
return [[c, -s], [s, c]]
|
||||
|
||||
def matmul(
|
||||
matrix_a: List[List[float]],
|
||||
@@ -48,7 +42,7 @@ def matmul(
|
||||
|
||||
n_cols_a = len(matrix_a[0])
|
||||
|
||||
# SCHRITT 2: Konsistenzprüfung für Matrix A (Programm schaut erste Zeile an merkt sich die Spaltenanzahl, geht jede weitere Zeile durch und zählt, gibt es eine Zeile die abweicht und nicht gleich viele Spalten wie die andere hat wirft er einen sauberen Fehler statt abzustürzen)
|
||||
# SCHRITT 2: Konsistenzprüfung für Matrix A
|
||||
if any(len(row) != n_cols_a for row in matrix_a):
|
||||
raise ValueError("All rows of matrix_a must have the same number of columns.")
|
||||
|
||||
@@ -71,7 +65,6 @@ def matmul(
|
||||
for i in range(len(matrix_a)): # Gehe jede Zeile von Matrix A durch
|
||||
row: List[float] = []
|
||||
for j in range(n_cols_b): # Gehe jede Spalte von Matrix B durch
|
||||
# Berechne das Skalarprodukt: Summe der Produkte aus Zeilenelementen und Spaltenelementen
|
||||
elem = sum(matrix_a[i][k] * matrix_b[k][j] for k in range(n_cols_a))
|
||||
row.append(elem)
|
||||
result.append(row)
|
||||
@@ -92,19 +85,15 @@ def transpose(matrix: List[List[float]]) -> List[List[float]]:
|
||||
ValueError: If the input matrix is empty or ragged.
|
||||
"""
|
||||
|
||||
# Prüfen, ob die Matrix leer ist
|
||||
if not matrix:
|
||||
raise ValueError("Cannot transpose an empty matrix.")
|
||||
|
||||
n_rows = len(matrix)
|
||||
n_cols = len(matrix[0])
|
||||
|
||||
# Konsistenzprüfung sicherstellen
|
||||
if any(len(row) != n_cols for row in matrix):
|
||||
raise ValueError("All rows of the matrix must have the same number of columns.")
|
||||
|
||||
# SCHRITT: Zeilen und Spalten vertauschen (List Comprehension)
|
||||
# Aus Matrix[i][j] wird Matrix[j][i]
|
||||
return [[matrix[i][j] for i in range(n_rows)] for j in range(n_cols)]
|
||||
|
||||
|
||||
@@ -124,23 +113,45 @@ def rotate_clockwise(matrix: List[List[float]]) -> List[List[float]]:
|
||||
ValueError: If the matrix is empty.
|
||||
"""
|
||||
|
||||
# Prüfen, ob die Matrix leer ist
|
||||
if not matrix:
|
||||
raise ValueError("Cannot rotate an empty matrix.")
|
||||
|
||||
n_rows = len(matrix)
|
||||
n_cols = len(matrix[0])
|
||||
|
||||
# Konsistenzprüfung sicherstellen
|
||||
if any(len(row) != n_cols for row in matrix):
|
||||
raise ValueError("All rows of the matrix must have the same number of columns.")
|
||||
|
||||
# SCHRITT: 90-Grad-Drehung im Uhrzeigersinn
|
||||
# Trick: Erst transponieren (Spiegeln) und danach jede Zeile umkehren!
|
||||
transposed = transpose(matrix)
|
||||
return [row[::-1] for row in transposed]
|
||||
|
||||
|
||||
def rot_2D(theta: float) -> List[List[float]]:
|
||||
"""Return the 2D rotation matrix for a given angle.
|
||||
|
||||
Calculates the 2x2 rotation matrix used in computer graphics to rotate a point
|
||||
counterclockwise around the origin by angle theta (in radians).
|
||||
|
||||
Args:
|
||||
theta: The rotation angle in radians.
|
||||
|
||||
Returns:
|
||||
A 2x2 rotation matrix represented as a list of lists of floats:
|
||||
[[cos(theta), -sin(theta)],
|
||||
[sin(theta), cos(theta)]]
|
||||
"""
|
||||
c = math.cos(theta)
|
||||
s = math.sin(theta)
|
||||
return [
|
||||
[c, -s],
|
||||
[s, c]
|
||||
]
|
||||
def rot_2D(theta: float) -> List[List[float]]:
|
||||
"""Erzeugt eine 2D-Rotationsmatrix für einen Winkel theta (im Bogenmaß)."""
|
||||
c = math.cos(theta)
|
||||
s = math.sin(theta)
|
||||
return [[c, -s], [s, c]]
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Testdaten für die Demonstration
|
||||
a = [[1, 2, 3], [4, 5, 6]]
|
||||
@@ -156,6 +167,9 @@ if __name__ == "__main__":
|
||||
print("rotate_clockwise(c):")
|
||||
print(rotate_clockwise(c))
|
||||
|
||||
from tabulate import tabulate
|
||||
|
||||
print(tabulate(rot_2D(1.0)))
|
||||
# Demonstration von rot_2D (z. B. 90 Grad = pi / 2 Radiant)
|
||||
angle = math.pi / 2
|
||||
print(f"rot_2D (pi / 2 radians / 90 degrees):")
|
||||
rotation_matrix = rot_2D(angle)
|
||||
for row in rotation_matrix:
|
||||
print([round(val, 4) for val in row])
|
||||
Reference in new issue
Block a user