From 50769a53822fb2df85a6395b374106038219b8ce Mon Sep 17 00:00:00 2001 From: Rebecca Date: Thu, 22 Oct 2026 12:15:20 +0200 Subject: [PATCH] Add tabulated rotation matrix output --- compute.py | 28 ++++++++++++---------------- pyproject.toml | 6 +----- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/compute.py b/compute.py index 8d486ff..8d96d51 100644 --- a/compute.py +++ b/compute.py @@ -1,5 +1,8 @@ import math +from tabulate import tabulate + + def matmul(a: list[list[float]], b: list[list[float]]) -> list[list[float]]: """Multiply two non-empty rectangular matrices. @@ -31,6 +34,7 @@ def matmul(a: list[list[float]], b: list[list[float]]) -> list[list[float]]: for a_row in a ] + def transpose(matrix: list[list[float]]) -> list[list[float]]: """Return the transpose of a non-empty rectangular matrix. @@ -46,6 +50,7 @@ def transpose(matrix: list[list[float]]) -> list[list[float]]: _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. @@ -55,10 +60,8 @@ def rot_2D(angle: float) -> list[list[float]]: Returns: The rotation matrix for rotating points counterclockwise. """ - return [ - [math.cos(angle), -math.sin(angle)], - [math.sin(angle), math.cos(angle)] - ] + 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.""" @@ -73,14 +76,12 @@ def _validate_matrix(matrix: list[list[float]], name: str) -> None: 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] - ] + assert matmul(first, second) == [[58.0, 64.0], [139.0, 154.0]] print("Matrix product:", matmul(first, second)) try: @@ -88,14 +89,9 @@ if __name__ == "__main__": except ValueError as error: print("Expected error:", error) else: - raise AssertionError( - "Expected a ValueError for incompatible dimensions." - ) + raise AssertionError("Expected a ValueError for incompatible dimensions.") - assert transpose(first) == [ - [1, 4], - [2, 5], - [3, 6] - ] + assert transpose(first) == [[1, 4], [2, 5], [3, 6]] print("Transpose:", transpose(first)) + print(tabulate(rot_2D(1.0))) diff --git a/pyproject.toml b/pyproject.toml index 6adc8d9..579c5f4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,8 +1,4 @@ [project] name = "matrixmania" version = "0.1.0" - -[project] -name = "matrixmania" -version = "0.1.0" -dependencies = ["tabulate", "ruff"] \ No newline at end of file +dependencies = ["tabulate", "ruff"]