Add tabulated rotation matrix output
This commit is contained in:
1 parent
58c9a35cfb
commit
50769a5382
2 files changed
+12
-20
No files matched your search
+12
-16
@@ -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)))
|
||||
@@ -1,7 +1,3 @@
|
||||
[project]
|
||||
name = "matrixmania"
|
||||
version = "0.1.0"
|
||||
|
||||
[project]
|
||||
name = "matrixmania"
|
||||
version = "0.1.0"
|
||||
|
||||
Reference in new issue
Block a user