From 3711e67d1535fdeea0adec7e85f496fd2a9315ed Mon Sep 17 00:00:00 2001 From: laura Date: Wed, 28 Oct 2026 21:20:20 +0100 Subject: [PATCH] Erster Stand: compute.py und Projektstruktur --- .gitignore | 3 + .idea/.gitignore | 5 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/matrixmania.iml | 10 ++ .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + AGENTS.md | 3 + compute.py | 168 ++++++++++++++++++ pyproject.toml | 3 + 10 files changed, 219 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore 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/.gitignore b/.gitignore new file mode 100644 index 0000000..de734a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv/ +__pycache__/ +.env 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/.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..c5f5bf2 --- /dev/null +++ b/.idea/matrixmania.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ff55ae9 --- /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..d42fcef --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,3 @@ +- 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`. diff --git a/compute.py b/compute.py new file mode 100644 index 0000000..deba19c --- /dev/null +++ b/compute.py @@ -0,0 +1,168 @@ +from typing import List, Sequence +import math + + +def matmul(a: Sequence[Sequence[float]], b: Sequence[Sequence[float]]) -> List[List[float]]: + """ + Multiply two matrices. + + Args: + a: Left matrix as a sequence of sequences of numbers. + b: Right matrix as a sequence of sequences of numbers. + + Returns: + The product matrix as a list of lists. + + Raises: + ValueError: If inputs are not valid matrices or dimensions are incompatible. + """ + # ---- Validate a ---- + if not a or not isinstance(a, Sequence): + raise ValueError("First argument must be a non-empty matrix.") + if not all(isinstance(row, Sequence) for row in a): + raise ValueError("First argument must be a sequence of sequences.") + n_rows_a = len(a) + n_cols_a = len(a[0]) + if n_cols_a == 0: + raise ValueError("First matrix rows cannot be empty.") + for row in a: + if len(row) != n_cols_a: + raise ValueError("First matrix rows must have the same length.") + + # ---- Validate b ---- + if not b or not isinstance(b, Sequence): + raise ValueError("Second argument must be a non-empty matrix.") + if not all(isinstance(row, Sequence) for row in b): + raise ValueError("Second argument must be a sequence of sequences.") + n_rows_b = len(b) + n_cols_b = len(b[0]) + if n_cols_b == 0: + raise ValueError("Second matrix rows cannot be empty.") + for row in b: + if len(row) != n_cols_b: + raise ValueError("Second matrix rows must have the same length.") + + # ---- Dimension compatibility ---- + if n_cols_a != n_rows_b: + raise ValueError( + f"Incompatible dimensions: {n_rows_a}x{n_cols_a} cannot be " + f"multiplied by {n_rows_b}x{n_cols_b}" + ) + + # ---- Compute product ---- + result: List[List[float]] = [ + [0.0 for _ in range(n_cols_b)] for _ in range(n_rows_a) + ] + + for i in range(n_rows_a): + for j in range(n_cols_b): + sum_prod = 0.0 + for k in range(n_cols_a): + sum_prod += a[i][k] * b[k][j] + result[i][j] = sum_prod + + return result + + +def transpose(matrix: Sequence[Sequence[float]]) -> List[List[float]]: + """ + Return the transpose of a matrix. + + Raises + ------- + ValueError + If the matrix is empty or rows have differing lengths. + """ + if not matrix or not isinstance(matrix, Sequence): + raise ValueError("Argument must be a non-empty matrix.") + if not all(isinstance(row, Sequence) for row in matrix): + raise ValueError("Argument must be a sequence of sequences.") + n_rows = len(matrix) + n_cols = len(matrix[0]) + if n_cols == 0: + raise ValueError("Matrix rows cannot be empty.") + for row in matrix: + if len(row) != n_cols: + raise ValueError("All rows must have the same length.") + return [[matrix[i][j] for i in range(n_rows)] for j in range(n_cols)] + + +def rotate(matrix: Sequence[Sequence[float]]) -> List[List[float]]: + """ + Rotate the matrix 90 degrees clockwise. + + Raises + ------- + ValueError + If the matrix is empty or rows have differing lengths. + """ + if not matrix or not isinstance(matrix, Sequence): + raise ValueError("Argument must be a non-empty matrix.") + if not all(isinstance(row, Sequence) for row in matrix): + raise ValueError("Argument must be a sequence of sequences.") + n_rows = len(matrix) + n_cols = len(matrix[0]) + if n_cols == 0: + raise ValueError("Matrix rows cannot be empty.") + for row in matrix: + if len(row) != n_cols: + raise ValueError("All rows must have the same length.") + transposed = transpose(matrix) + return [list(reversed(row)) for row in transposed] + + +def rot_2D(theta_degrees: float) -> List[List[float]]: + """ + Return a 2x2 rotation matrix for the given angle in degrees. + + Args: + theta_degrees: Rotation angle in degrees. + + Returns: + A 2x2 matrix [[cos, -sin], [sin, cos]]. + + Raises: + ValueError: If theta_degrees is not a number. + """ + + if not isinstance(theta_degrees, (int, float)): + raise ValueError("Angle must be a number.") + theta = math.radians(theta_degrees) + cos_theta = math.cos(theta) + sin_theta = math.sin(theta) + return [[cos_theta, -sin_theta], [sin_theta, cos_theta]] + + +if __name__ == "__main__": + print("matmul test 1") + a = [[1, 2, 3], [4, 5, 6]] + b = [[7, 8], [9, 10], [11, 12]] + print(matmul(a, b)) + + print("matmul test 2") + a = [[1, 0], [0, 1]] + b = [[5, 6], [7, 8]] + print(matmul(a, b)) + + print("\ntranspose test 1") + m = [[1, 2, 3], [4, 5, 6]] + print(transpose(m)) + + print("\ntranspose test 2") + m = [[1, 2], [3, 4], [5, 6]] + print(transpose(m)) + + print("\nrotate test 1") + m = [[1, 2, 3], [4, 5, 6]] + print(rotate(m)) + +print("\nrotate test 2") +m = [[1, 2], [3, 4]] +print(rotate(m)) + +print("\nrot_2D test 1") +print(rot_2D(90)) + +print("\nrot_2D test 2") +print(rot_2D(45)) + diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..7a6417e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[project] +name = "matrixmania" +version = "0.1.0"