commit 118ffb4f8153958f4cc9994d61080104c42762dc Author: Tia Lechner Date: Tue Oct 13 18:19:08 2026 +0200 Erster Stand: compute.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d1eb6e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv/ +__pycache__/ +.env \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..79a2eba --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,4 @@ +# Regeln für dieses Projekt +- Code nach PEP 8, Zeilenlänge maximal 88 Zeichen. +- Jede Funktion und Methode hat einen Docstring und Type Hints. +- Vor jedem Commit: `ruff check` und `ruff format`. \ No newline at end of file diff --git a/compute.py b/compute.py new file mode 100644 index 0000000..2f70fa9 --- /dev/null +++ b/compute.py @@ -0,0 +1,155 @@ +import math + +def matmul(matrix_a: list[list[float | int]], matrix_b: list[list[float | int]]) -> list[list[float | int]]: + """Multipliziert zwei Matrizen, die als verschachtelte Listen dargestellt sind. + + Args: + matrix_a: Die erste Matrix als 2D-Liste (Größe m x n). + matrix_b: Die zweite Matrix als 2D-Liste (Größe n x p). + + Returns: + Die resultierende Produkt-Matrix als 2D-Liste (Größe m x p). + + Raises: + ValueError: Wenn die Breite der ersten Matrix (Anzahl der Spalten) + nicht der Höhe der zweiten Matrix (Anzahl der Zeilen) entspricht. + """ + if not matrix_a or not matrix_b or not matrix_a[0] or not matrix_b[0]: + raise ValueError("Die Matrizen dürfen nicht leer sein.") + + rows_a = len(matrix_a) + cols_a = len(matrix_a[0]) + rows_b = len(matrix_b) + cols_b = len(matrix_b[0]) + + # Überprüfung, ob die Multiplikation mathematisch möglich ist + if cols_a != rows_b: + raise ValueError( + f"Multiplikation nicht möglich: Breite von matrix_a ({cols_a}) " + f"stimmt nicht mit der Höhe von matrix_b ({rows_b}) überein." + ) + + # Berechnung des Matrixprodukts über eine List Comprehension + result = [ + [ + sum(matrix_a[i][k] * matrix_b[k][j] for k in range(cols_a)) + for j in range(cols_b) + ] + for i in range(rows_a) + ] + + return result + + +def transpose(matrix: list[list[float | int]]) -> list[list[float | int]]: + """Transponiert eine Matrix (tauscht Zeilen und Spalten). + + Args: + matrix: Die zu transponierende Matrix als 2D-Liste (m x n). + + Returns: + Die transponierte Matrix als 2D-Liste (n x m). + + Raises: + ValueError: Wenn die Matrix leer ist oder ungleichmäßig viele Spalten + in den Zeilen besitzt (keine echte Matrix). + """ + if not matrix or not matrix[0]: + raise ValueError("Die Matrix darf nicht leer sein.") + + cols = len(matrix[0]) + + # Überprüfen, ob alle Zeilen exakt gleich viele Spalten haben + for row in matrix: + if len(row) != cols: + raise ValueError( + "Ungleichmäßige Matrix: Nicht jede Zeile hat dieselbe Anzahl an Spalten." + ) + + # Transponieren via Zip-Unpacking + return [list(row) for row in zip(*matrix)] + + + +def rot_2D(theta: float) -> list[list[float]]: + """Gibt die 2D-Rotationsmatrix für einen gegebenen Winkel theta (in Bogenmaß) zurück. + + Args: + theta: Der Rotationswinkel in Bogenmaß (Radians). + + Returns: + Die 2x2-Rotationsmatrix als 2D-Liste: + [[cos(theta), -sin(theta)], + [sin(theta), cos(theta)]] + """ + cos_theta = math.cos(theta) + sin_theta = math.sin(theta) + + return [ + [cos_theta, -sin_theta], + [sin_theta, cos_theta] + ] + + +if __name__ == "__main__": + + #matmul prüfung + matrix_a = [ [ 3, 4, -1, 4 ], + [ -2, 2, 5, 1 ] + ] + matrix_b = [ [ 1, 3, -2 ], + [ 2, 5, 1 ], + [ -1, 4, -4 ], + [ 2, 3, 6 ] + ] + matrix_c = matmul(matrix_a, matrix_b) + print("Ergebnis C = A * B:") + for row in matrix_c: + print(row) + + try: + matrix_c = [[1,2],[3,4]] + matrix_d = [[5,6]] + ergebnis = matmul(matrix_c, matrix_d) + print("Ergebnis:", ergebnis) + + except ValueError as e: + print(f"Fehler abgefangen (matmul): {e}") + +#Agent Fehler ausgebessert: list[list/[float | int] -> list[list[float | int] +#Gut gelöst -> TypeHints beachtet und verständlichen DocString; Ruff check passed + +#transpose prüfung + matrix = [ + [1, 2, 3], + [4, 5, 6] + ] + print(transpose(matrix)) + # Erwartete Ausgabe: + # [ + # [1, 4], + # [2, 5], + # [3, 6] + # ] + matrix_2 = [ + [1, 2, 3], + [4, 5, 6] + ] + print(transpose(matrix_2)) + + try: + un_matrix = [ + [1, 2, 3], + [4, 5] + ] + + ergebnis = transpose(un_matrix) + print("Ergebnis:", ergebnis) + + except ValueError as e: + print(f"Fehler erfolgreich abgefangen (transpose): {e}") + + #rotation prüfung + theta = math.pi / 2 #1,57... + print(f"Winkel: {math.degrees(theta)} Grad (pi / 2)") + print(rot_2D(theta)) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..46e3b47 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[project] +name = "matrixmania" +version = "0.1.0" \ No newline at end of file