From 41093db2ad401966cafb4fdc01d9fe78b77f78fd Mon Sep 17 00:00:00 2001 From: taraseidler Date: Wed, 28 Oct 2026 15:45:55 +0100 Subject: [PATCH] =?UTF-8?q?in=20compute.py:=20rot=5F3D=20Implementiert,=20?= =?UTF-8?q?f=C3=BCr=20alle=203=20Achsen(x,y,z),=20ValueError=20f=C3=BCr=20?= =?UTF-8?q?ung=C3=BCltige=20Achsen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test_rot.py Datei erstellt mit 7 Testläufen (3x rot_2D, 4x rot_3D) --- compute.py | 21 ++++++++- test_rot.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 test_rot.py diff --git a/compute.py b/compute.py index 93d00ab..a038d67 100644 --- a/compute.py +++ b/compute.py @@ -104,7 +104,26 @@ def print_matrix(matrix: Matrix) -> None: """Print a matrix as a nicely formatted table.""" 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'.") + + if __name__ == "__main__": diff --git a/test_rot.py b/test_rot.py new file mode 100644 index 0000000..a4f41f4 --- /dev/null +++ b/test_rot.py @@ -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) \ No newline at end of file