Add tabulated rotation matrix output

This commit is contained in:
Rebecca committed 2026-10-22 12:15:20 +02:00
1 parent 58c9a35cfb
commit 50769a5382
2 files changed
+13 -21

No files matched your search

+12 -16
View File
@@ -1,5 +1,8 @@
import math import math
from tabulate import tabulate
def matmul(a: list[list[float]], b: list[list[float]]) -> list[list[float]]: def matmul(a: list[list[float]], b: list[list[float]]) -> list[list[float]]:
"""Multiply two non-empty rectangular matrices. """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 for a_row in a
] ]
def transpose(matrix: list[list[float]]) -> list[list[float]]: def transpose(matrix: list[list[float]]) -> list[list[float]]:
"""Return the transpose of a non-empty rectangular matrix. """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") _validate_matrix(matrix, "matrix")
return [list(column) for column in zip(*matrix)] return [list(column) for column in zip(*matrix)]
def rot_2D(angle: float) -> list[list[float]]: def rot_2D(angle: float) -> list[list[float]]:
"""Return the 2D rotation matrix for a given angle in radians. """Return the 2D rotation matrix for a given angle in radians.
@@ -55,10 +60,8 @@ def rot_2D(angle: float) -> list[list[float]]:
Returns: Returns:
The rotation matrix for rotating points counterclockwise. The rotation matrix for rotating points counterclockwise.
""" """
return [ return [[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]]
[math.cos(angle), -math.sin(angle)],
[math.sin(angle), math.cos(angle)]
]
def _validate_matrix(matrix: list[list[float]], name: str) -> None: def _validate_matrix(matrix: list[list[float]], name: str) -> None:
"""Raise ValueError unless matrix is non-empty and rectangular.""" """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): if any(len(row) != column_count for row in matrix):
raise ValueError(f"Matrix {name} must have rows of equal length.") raise ValueError(f"Matrix {name} must have rows of equal length.")
if __name__ == "__main__": if __name__ == "__main__":
first = [[1, 2, 3], [4, 5, 6]] first = [[1, 2, 3], [4, 5, 6]]
second = [[7, 8], [9, 10], [11.0, 12.0]] second = [[7, 8], [9, 10], [11.0, 12.0]]
assert matmul(first, second) == [ assert matmul(first, second) == [[58.0, 64.0], [139.0, 154.0]]
[58.0, 64.0],
[139.0, 154.0]
]
print("Matrix product:", matmul(first, second)) print("Matrix product:", matmul(first, second))
try: try:
@@ -88,14 +89,9 @@ if __name__ == "__main__":
except ValueError as error: except ValueError as error:
print("Expected error:", error) print("Expected error:", error)
else: else:
raise AssertionError( raise AssertionError("Expected a ValueError for incompatible dimensions.")
"Expected a ValueError for incompatible dimensions."
)
assert transpose(first) == [ assert transpose(first) == [[1, 4], [2, 5], [3, 6]]
[1, 4],
[2, 5],
[3, 6]
]
print("Transpose:", transpose(first)) print("Transpose:", transpose(first))
print(tabulate(rot_2D(1.0)))
+1 -5
View File
@@ -1,8 +1,4 @@
[project] [project]
name = "matrixmania" name = "matrixmania"
version = "0.1.0" version = "0.1.0"
dependencies = ["tabulate", "ruff"]
[project]
name = "matrixmania"
version = "0.1.0"
dependencies = ["tabulate", "ruff"]