From 83717af427eeb6f7a4bb82e77ce65032aa8f9427 Mon Sep 17 00:00:00 2001 From: Rebecca Date: Thu, 15 Oct 2026 16:50:34 +0200 Subject: [PATCH] Erster Stand: compute.py --- .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/matrixmania.iml | 8 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ AGENTS.md | 4 + compute.py | 101 ++++++++++++++++++ pyproject.toml | 3 + 8 files changed, 143 insertions(+) create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/matrixmania.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 AGENTS.md create mode 100644 compute.py create mode 100644 pyproject.toml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/matrixmania.iml b/.idea/matrixmania.iml new file mode 100644 index 0000000..d8b3f6c --- /dev/null +++ b/.idea/matrixmania.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1d3ce46 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7b1daae --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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..304353b --- /dev/null +++ b/compute.py @@ -0,0 +1,101 @@ +import math + +def matmul(a: list[list[float]], b: list[list[float]]) -> list[list[float]]: + """Multiply two non-empty rectangular matrices. + + Args: + a: The left matrix. + b: The right matrix. + + Returns: + The matrix product a * b. + + Raises: + ValueError: If either matrix is empty or ragged, or if their + dimensions are incompatible. + """ + _validate_matrix(a, "a") + _validate_matrix(b, "b") + + if len(a[0]) != len(b): + raise ValueError( + "Matrix dimensions are incompatible: the number of columns in a " + "must equal the number of rows in b." + ) + + return [ + [ + sum(a_row[index] * b[index][column] for index in range(len(b))) + for column in range(len(b[0])) + ] + for a_row in a + ] + +def transpose(matrix: list[list[float]]) -> list[list[float]]: + """Return the transpose of a non-empty rectangular matrix. + + Args: + matrix: A matrix represented as rows of numbers. + + Returns: + A new matrix whose rows are the columns of matrix. + + Raises: + ValueError: If the matrix is empty or its rows have different lengths. + """ + _validate_matrix(matrix, "matrix") + return [list(column) for column in zip(*matrix)] + +def rot_2D(angle: float) -> list[list[float]]: + """Return the 2D rotation matrix for a given angle in radians. + + Args: + angle: The rotation angle in radians. + + Returns: + The rotation matrix for rotating points counterclockwise. + """ + return [ + [math.cos(angle), -math.sin(angle)], + [math.sin(angle), math.cos(angle)] + ] + +def _validate_matrix(matrix: list[list[float]], name: str) -> None: + """Raise ValueError unless matrix is non-empty and rectangular.""" + if not matrix: + raise ValueError(f"Matrix {name} must be non-empty.") + + column_count = len(matrix[0]) + + if column_count == 0: + raise ValueError(f"Matrix {name} must have at least one column.") + + if any(len(row) != column_count for row in matrix): + raise ValueError(f"Matrix {name} must have rows of equal length.") + +if __name__ == "__main__": + first = [[1, 2, 3], [4, 5, 6]] + second = [[7, 8], [9, 10], [11.0, 12.0]] + + assert matmul(first, second) == [ + [58.0, 64.0], + [139.0, 154.0] + ] + print("Matrix product:", matmul(first, second)) + + try: + matmul([[1, 2]], [[1, 2]]) + except ValueError as error: + print("Expected error:", error) + else: + raise AssertionError( + "Expected a ValueError for incompatible dimensions." + ) + + assert transpose_matrix(first) == [ + [1, 4], + [2, 5], + [3, 6] + ] + print("Transpose:", transpose_matrix(first)) + 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