Add rot_3D and tests for rot_2D/rot_3D

This commit is contained in:
washingtonta103380 committed 2026-10-20 21:06:16 +02:00
1 parent 833161651b
commit d065d4d17d
7 files changed
+102 -7

No files matched your search

View File
Whitespace-only changes.
+58
View File
@@ -0,0 +1,58 @@
import math
import pytest
from matrixmania.compute import matmul, rot_2D, rot_3D, transpose
def assert_matrix_close(actual, expected):
"""Vergleicht zwei Matrizen elementweise mit Toleranz."""
assert len(actual) == len(expected)
for row_a, row_e in zip(actual, expected):
assert row_a == pytest.approx(row_e, abs=1e-9)
# ---------- rot_2D ----------
def test_rot_2D_zero():
assert_matrix_close(rot_2D(0), [[1, 0], [0, 1]])
def test_rot_2D_90_degree():
assert_matrix_close(rot_2D(math.pi / 2), [[0, -1], [1, 0]])
def test_rot_2D_invalid_input():
with pytest.raises(TypeError):
rot_2D("abc")
# ---------- rot_3D ----------
@pytest.mark.parametrize("axis", ["x", "y", "z"])
def test_rot_3D_zero_is_identity(axis):
assert_matrix_close(rot_3D(0, axis), [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
@pytest.mark.parametrize(
"axis, expected",
[
("x", [[1, 0, 0], [0, 0, -1], [0, 1, 0]]),
("y", [[0, 0, 1], [0, 1, 0], [-1, 0, 0]]),
("z", [[0, -1, 0], [1, 0, 0], [0, 0, 1]]),
],
)
def test_rot_3D_90_degree(axis, expected):
assert_matrix_close(rot_3D(math.pi / 2, axis), expected)
@pytest.mark.parametrize("axis", ["x", "y", "z"])
def test_rot_3D_is_orthogonal(axis):
r = rot_3D(1.0, axis)
assert_matrix_close(matmul(r, transpose(r)), [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
@pytest.mark.parametrize("axis", ["w", "X", "", "xy"])
def test_rot_3D_invalid_axis(axis):
with pytest.raises(ValueError):
rot_3D(1.0, axis)