commit 0effc7ff2347e1ae8fcdf3b3dc2f3989ba14f1b3 Author: Lena Date: Tue Oct 27 18:49:00 2026 +0100 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/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..2e9527c --- /dev/null +++ b/compute.py @@ -0,0 +1,161 @@ +from typing import TypeAlias +from math import cos, sin + +Number: TypeAlias = int | float +Matrix: TypeAlias = list[list[Number]] + + +def matmul(A: Matrix, B: Matrix) -> Matrix: + """Multiply two matrices. + + Args: + A: The first matrix. + B: The second matrix. + + Returns: + The resulting matrix product. + + Raises: + ValueError: If either matrix is empty, not rectangular, + or if their dimensions are incompatible. + """ + if not A or not B: + raise ValueError("Matrices must not be empty.") + + if not A[0] or not B[0]: + raise ValueError("Matrices must have at least one column.") + + cols_A = len(A[0]) + cols_B = len(B[0]) + + if any(len(row) != cols_A for row in A): + raise ValueError("Matrix A is not rectangular.") + + if any(len(row) != cols_B for row in B): + raise ValueError("Matrix B is not rectangular.") + + if cols_A != len(B): + raise ValueError( + "The number of columns in A must equal the number of rows in B." + ) + + rows_A = len(A) + rows_B = len(B) + + result: Matrix = [ + [0 for _ in range(cols_B)] + for _ in range(rows_A) + ] + + for i in range(rows_A): + for j in range(cols_B): + for k in range(rows_B): + result[i][j] += A[i][k] * B[k][j] + + return result + + +def transpose(matrix: Matrix) -> Matrix: + """Return the transpose of a matrix. + + Parameters + ---------- + matrix : List[List[Number]] + Matrix to transpose. + + Returns + ------- + List[List[Number]] + Transposed matrix where rows become columns. + + Raises + ------ + ValueError + If the input matrix is empty or not rectangular. + """ + if not matrix: + raise ValueError("Cannot transpose an empty matrix.") + n_rows = len(matrix) + n_cols = len(matrix[0]) + if any(len(row) != n_cols for row in matrix): + raise ValueError("Input matrix is not rectangular.") + return [[matrix[i][j] for i in range(n_rows)] for j in range(n_cols)] + + +def rotate_matrix(matrix: Matrix) -> None: + """Rotate a square matrix 90 degrees clockwise in-place. + + The algorithm combines transposition and row reversal, + running in O(n²) time with O(1) additional space. + + Parameters + ---------- + matrix : Matrix + Square matrix to be modified. + + Raises + ------ + ValueError + If the matrix is empty or not square. + """ + n = len(matrix) + + if n == 0: + raise ValueError("Matrix must not be empty.") + + for row in matrix: + if len(row) != n: + raise ValueError("Matrix must be square.") + + # Transpose the matrix + for i in range(n): + for j in range(i + 1, n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + + # Reverse each row + for row in matrix: + row.reverse() + + +def rot_2D(theta: float) -> Matrix: + """Create a rotation matrix for a 2D rotation. + + Parameters + ---------- + theta : float + Rotation angle in radians. + + Returns + ------- + Matrix + A 2x2 rotation matrix. + """ + return [ + [cos(theta), -sin(theta)], + [sin(theta), cos(theta)], + ] + + +if __name__ == "__main__": + # Beispiel Multiplikation + A1 = [[1, 2, 3], [4, 5, 6]] + B1 = [[7, 8], [9, 10], [11, 12]] + expected1 = [[58, 64], [139, 154]] + + assert matmul(A1, B1) == expected1 + print("Beispiel 1 erfolgreich:", matmul(A1, B1)) + + # Beispiel Rotation + matrix = [[1, 2], [3, 4]] + expected = [[3, 1], [4, 2]] + + rotate_matrix(matrix) + assert matrix == expected + print("Rotation erfolgreich:", matrix) + + # Beispiel Transponierung halldierjef + matrix = [[1, 2, 3], [4, 5, 6]] + expected = [[1, 4], [2, 5], [3, 6]] + + assert transpose(matrix) == expected + print("Transponierung erfolgreich:", transpose(matrix)) \ No newline at end of file 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