Compare commits

...
2 Commits
2 changed files with 145 additions and 1 deletions

No files matched your search

+24 -1
View File
@@ -104,7 +104,30 @@ def print_matrix(matrix: Matrix) -> None:
"""Print a matrix as a nicely formatted table.""" """Print a matrix as a nicely formatted table."""
print(tabulate(matrix, tablefmt="grid")) print(tabulate(matrix, tablefmt="grid"))
def rot_3D(angle: float, axis: str) -> list[list[float]] def rot_3D(angle: float, axis: str) -> list[list[float]]:
axis = axis.lower()
c, s = cos(angle), sin(angle)
if axis == "x":
return [[1.0, 0.0, 0.0],
[0.0, c, -s],
[0.0, s, c]]
if axis == "y":
return [[c, 0.0, s],
[0.0, 1.0, 0.0],
[-s, 0.0, c]]
if axis == "z":
return [[c, -s, 0.0],
[s, c, 0.0],
[0.0, 0.0, 1.0]]
raise ValueError(f"Invalid axis '{axis}': expected 'x', 'y' or 'z'.")
def project_ortho(point: tuple[float, float, float], scale: float = 1,) -> tupel[float, float]:
x, y, _z = point
return(x * scale, y * scale)
if __name__ == "__main__": if __name__ == "__main__":
+121
View File
@@ -0,0 +1,121 @@
"""Machine tests for the rotation functions in compute.py.
Every test is structured according to the
Arrange - Act - Assert pattern.
"""
from math import pi, sqrt
import pytest
from compute import matmul, rot_2D, rot_3D
# ---------------------------------------------------------------------------
# rot_2D
# ---------------------------------------------------------------------------
def test_rot_2D_quarter_turn() -> None:
"""rot_2D(pi/2) maps the unit vector (1, 0) onto (0, 1)."""
# Arrange
expected = [[0.0, -1.0], [1.0, 0.0]]
# Act
matrix = rot_2D(pi / 2)
# Assert
for row, exp_row in zip(matrix, expected):
for value, exp_value in zip(row, exp_row):
assert value == pytest.approx(exp_value)
def test_rot_2D_full_turn_is_identity() -> None:
"""rot_2D(2*pi) is the identity matrix."""
# Arrange
expected = [[1.0, 0.0], [0.0, 1.0]]
# Act
matrix = rot_2D(2 * pi)
# Assert
for row, exp_row in zip(matrix, expected):
for value, exp_value in zip(row, exp_row):
assert value == pytest.approx(exp_value)
def test_rot_2D_known_values_45_degrees() -> None:
"""rot_2D(pi/4) has the well-known sqrt(2)/2 entries."""
# Arrange
s = sqrt(2) / 2
expected = [[s, -s], [s, s]]
# Act
matrix = rot_2D(pi / 4)
# Assert
for row, exp_row in zip(matrix, expected):
for value, exp_value in zip(row, exp_row):
assert value == pytest.approx(exp_value)
# ---------------------------------------------------------------------------
# rot_3D
# ---------------------------------------------------------------------------
def test_rot_3D_z_axis_quarter_turn() -> None:
"""rot_3D(pi/2, 'z') rotates (1, 0, 0) onto (0, 1, 0)."""
# Arrange
vector = [[1.0], [0.0], [0.0]] # column vector
expected = [[0.0], [1.0], [0.0]]
# Act
matrix = rot_3D(pi / 2, "z")
result = matmul(matrix, vector)
# Assert
for row, exp_row in zip(result, expected):
for value, exp_value in zip(row, exp_row):
assert value == pytest.approx(exp_value)
def test_rot_3D_x_axis_45_degrees() -> None:
"""rot_3D(pi/4, 'x') has the well-known sqrt(2)/2 entries."""
# Arrange
s = sqrt(2) / 2
expected = [[1.0, 0.0, 0.0],
[0.0, s, -s],
[0.0, s, s]]
# Act
matrix = rot_3D(pi / 4, "x")
# Assert
for row, exp_row in zip(matrix, expected):
for value, exp_value in zip(row, exp_row):
assert value == pytest.approx(exp_value)
def test_rot_3D_y_axis_keeps_y_axis() -> None:
"""rot_3D(angle, 'y') leaves the y-axis unchanged."""
# Arrange
vector = [[0.0], [1.0], [0.0]] # y-axis as column vector
expected = [[0.0], [1.0], [0.0]]
# Act
matrix = rot_3D(1.234, "y") # arbitrary angle
result = matmul(matrix, vector)
# Assert
for row, exp_row in zip(result, expected):
for value, exp_value in zip(row, exp_row):
assert value == pytest.approx(exp_value)
def test_rot_3D_invalid_axis_raises() -> None:
"""rot_3D raises a ValueError for an unknown axis."""
# Arrange
bad_axis = "a"
# Act / Assert
with pytest.raises(ValueError):
rot_3D(pi / 2, bad_axis)