Files
matrixmania/test/test_matrixmania/test_rot.py
T

58 lines
1.5 KiB
Python

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)