Aufgabe rot_3D
This commit is contained in:
1 parent
c9fabdf896
commit
3f7c8b1195
4 files changed
+453
-8
No files matched your search
@@ -0,0 +1,154 @@
|
||||
"""Unit-Tests für die mathematischen Matrixfunktionen in compute.py."""
|
||||
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from matrixmania.compute import matmul, rot_2d, rot_3d, test_functions, transpose
|
||||
|
||||
|
||||
def test_matmul_valid() -> None:
|
||||
"""Testet die Matrixmultiplikation mit gültigen Matrizen."""
|
||||
a = [
|
||||
[3.0, 4.0, -1.0, 4.0],
|
||||
[-2.0, 2.0, 5.0, 1.0],
|
||||
]
|
||||
b = [
|
||||
[1.0, 3.0, -2.0],
|
||||
[2.0, 5.0, 1.0],
|
||||
[-1.0, 4.0, -4.0],
|
||||
[2.0, 3.0, 6.0],
|
||||
]
|
||||
expected = [
|
||||
[20.0, 37.0, 26.0],
|
||||
[-1.0, 27.0, -8.0],
|
||||
]
|
||||
assert matmul(a, b) == expected
|
||||
|
||||
|
||||
def test_matmul_invalid_dimensions() -> None:
|
||||
"""Testet, dass bei inkompatiblen Matrixdimensionen ValueError geworfen wird."""
|
||||
a = [[1.0, 2.0], [3.0, 4.0]]
|
||||
b = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
|
||||
with pytest.raises(ValueError):
|
||||
matmul(a, b)
|
||||
|
||||
|
||||
def test_matmul_empty_or_uneven() -> None:
|
||||
"""Testet matmul mit leeren oder ungleichmäßigen Matrizen."""
|
||||
with pytest.raises(ValueError):
|
||||
matmul([], [[1.0]])
|
||||
with pytest.raises(ValueError):
|
||||
matmul([[1.0, 2.0], [3.0]], [[1.0], [2.0]])
|
||||
|
||||
|
||||
def test_transpose_valid() -> None:
|
||||
"""Testet das Transponieren einer regulären Matrix."""
|
||||
matrix = [
|
||||
[1.0, 2.0, 3.0],
|
||||
[4.0, 5.0, 6.0],
|
||||
]
|
||||
expected = [
|
||||
[1.0, 4.0],
|
||||
[2.0, 5.0],
|
||||
[3.0, 6.0],
|
||||
]
|
||||
assert transpose(matrix) == expected
|
||||
|
||||
|
||||
def test_transpose_invalid() -> None:
|
||||
"""Testet, dass transpose bei leeren oder unregelmäßigen Matrizen Fehler wirft."""
|
||||
with pytest.raises(ValueError):
|
||||
transpose([])
|
||||
with pytest.raises(ValueError):
|
||||
transpose([[1.0, 2.0], [3.0]])
|
||||
|
||||
|
||||
def test_rot_2d() -> None:
|
||||
"""Testet die 2D-Rotation für verschiedene Winkel."""
|
||||
rot_0 = rot_2d(0.0)
|
||||
assert math.isclose(rot_0[0][0], 1.0, abs_tol=1e-9)
|
||||
assert math.isclose(rot_0[0][1], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rot_0[1][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rot_0[1][1], 1.0, abs_tol=1e-9)
|
||||
|
||||
rot_90 = rot_2d(math.pi / 2)
|
||||
assert math.isclose(rot_90[0][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rot_90[0][1], -1.0, abs_tol=1e-9)
|
||||
assert math.isclose(rot_90[1][0], 1.0, abs_tol=1e-9)
|
||||
assert math.isclose(rot_90[1][1], 0.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_x_axis() -> None:
|
||||
"""Testet die 3D-Rotation um die x-Achse."""
|
||||
matrix_x_0 = rot_3d(0.0, "x")
|
||||
expected_identity = [
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 1.0],
|
||||
]
|
||||
for r in range(3):
|
||||
for c in range(3):
|
||||
assert math.isclose(matrix_x_0[r][c], expected_identity[r][c], abs_tol=1e-9)
|
||||
|
||||
matrix_x_90 = rot_3d(math.pi / 2, "x")
|
||||
v_y = [[0.0], [1.0], [0.0]]
|
||||
rotated = matmul(matrix_x_90, v_y)
|
||||
assert math.isclose(rotated[0][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rotated[1][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rotated[2][0], 1.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_y_axis() -> None:
|
||||
"""Testet die 3D-Rotation um die y-Achse."""
|
||||
matrix_y_0 = rot_3d(0.0, "y")
|
||||
expected_identity = [
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 1.0],
|
||||
]
|
||||
for r in range(3):
|
||||
for c in range(3):
|
||||
assert math.isclose(matrix_y_0[r][c], expected_identity[r][c], abs_tol=1e-9)
|
||||
|
||||
matrix_y_90 = rot_3d(math.pi / 2, "y")
|
||||
v_z = [[0.0], [0.0], [1.0]]
|
||||
rotated = matmul(matrix_y_90, v_z)
|
||||
assert math.isclose(rotated[0][0], 1.0, abs_tol=1e-9)
|
||||
assert math.isclose(rotated[1][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rotated[2][0], 0.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_z_axis() -> None:
|
||||
"""Testet die 3D-Rotation um die z-Achse."""
|
||||
matrix_z_0 = rot_3d(0.0, "z")
|
||||
expected_identity = [
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 1.0],
|
||||
]
|
||||
for r in range(3):
|
||||
for c in range(3):
|
||||
assert math.isclose(matrix_z_0[r][c], expected_identity[r][c], abs_tol=1e-9)
|
||||
|
||||
matrix_z_90 = rot_3d(math.pi / 2, "z")
|
||||
v_x = [[1.0], [0.0], [0.0]]
|
||||
rotated = matmul(matrix_z_90, v_x)
|
||||
assert math.isclose(rotated[0][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(rotated[1][0], 1.0, abs_tol=1e-9)
|
||||
assert math.isclose(rotated[2][0], 0.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_invalid_axis() -> None:
|
||||
"""Testet, dass ungültige Achsen einen ValueError auslösen."""
|
||||
with pytest.raises(ValueError, match="Ungültige Achse"):
|
||||
rot_3d(1.0, "w")
|
||||
with pytest.raises(ValueError):
|
||||
rot_3d(0.5, "123")
|
||||
with pytest.raises(ValueError):
|
||||
rot_3d(0.0, "")
|
||||
|
||||
|
||||
def test_test_functions_execution() -> None:
|
||||
"""Testet die Ausführung der integrierten Testfunktion."""
|
||||
test_functions()
|
||||
@@ -0,0 +1,198 @@
|
||||
"""Unit-Tests für die Rotationsfunktionen in compute.py."""
|
||||
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from matrixmania import rot_2d, rot_3d
|
||||
|
||||
|
||||
def test_rot_2d_zero_angle() -> None:
|
||||
"""Testet die 2D-Rotation um 0 Radiant (Einheitsmatrix)."""
|
||||
# Arrange
|
||||
angle = 0.0
|
||||
|
||||
# Act
|
||||
result = rot_2d(angle)
|
||||
|
||||
# Assert
|
||||
assert math.isclose(result[0][0], 1.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[0][1], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][1], 1.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_2d_pi_half() -> None:
|
||||
"""Testet die 2D-Rotation um pi / 2 Radiant (90 Grad)."""
|
||||
# Arrange
|
||||
angle = math.pi / 2
|
||||
|
||||
# Act
|
||||
result = rot_2d(angle)
|
||||
|
||||
# Assert
|
||||
assert math.isclose(result[0][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[0][1], -1.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][0], 1.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][1], 0.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_2d_pi() -> None:
|
||||
"""Testet die 2D-Rotation um pi Radiant (180 Grad)."""
|
||||
# Arrange
|
||||
angle = math.pi
|
||||
|
||||
# Act
|
||||
result = rot_2d(angle)
|
||||
|
||||
# Assert
|
||||
assert math.isclose(result[0][0], -1.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[0][1], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][1], -1.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_2d_negative_angle() -> None:
|
||||
"""Testet die 2D-Rotation mit einem negativen Winkel (-pi / 2)."""
|
||||
# Arrange
|
||||
angle = -math.pi / 2
|
||||
|
||||
# Act
|
||||
result = rot_2d(angle)
|
||||
|
||||
# Assert
|
||||
assert math.isclose(result[0][0], 0.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[0][1], 1.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][0], -1.0, abs_tol=1e-9)
|
||||
assert math.isclose(result[1][1], 0.0, abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_x_axis_zero() -> None:
|
||||
"""Testet die 3D-Rotation um die x-Achse um 0 Radiant."""
|
||||
# Arrange
|
||||
angle = 0.0
|
||||
axis = "x"
|
||||
|
||||
# Act
|
||||
result = rot_3d(angle, axis)
|
||||
|
||||
# Assert
|
||||
expected = [
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 1.0],
|
||||
]
|
||||
for row in range(3):
|
||||
for col in range(3):
|
||||
assert math.isclose(result[row][col], expected[row][col], abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_x_axis_pi_half() -> None:
|
||||
"""Testet die 3D-Rotation um die x-Achse um pi / 2 Radiant."""
|
||||
# Arrange
|
||||
angle = math.pi / 2
|
||||
axis = "x"
|
||||
|
||||
# Act
|
||||
result = rot_3d(angle, axis)
|
||||
|
||||
# Assert
|
||||
expected = [
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 0.0, -1.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
]
|
||||
for row in range(3):
|
||||
for col in range(3):
|
||||
assert math.isclose(result[row][col], expected[row][col], abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_y_axis_zero() -> None:
|
||||
"""Testet die 3D-Rotation um die y-Achse um 0 Radiant."""
|
||||
# Arrange
|
||||
angle = 0.0
|
||||
axis = "y"
|
||||
|
||||
# Act
|
||||
result = rot_3d(angle, axis)
|
||||
|
||||
# Assert
|
||||
expected = [
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 1.0],
|
||||
]
|
||||
for row in range(3):
|
||||
for col in range(3):
|
||||
assert math.isclose(result[row][col], expected[row][col], abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_y_axis_pi_half() -> None:
|
||||
"""Testet die 3D-Rotation um die y-Achse um pi / 2 Radiant."""
|
||||
# Arrange
|
||||
angle = math.pi / 2
|
||||
axis = "y"
|
||||
|
||||
# Act
|
||||
result = rot_3d(angle, axis)
|
||||
|
||||
# Assert
|
||||
expected = [
|
||||
[0.0, 0.0, 1.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
[-1.0, 0.0, 0.0],
|
||||
]
|
||||
for row in range(3):
|
||||
for col in range(3):
|
||||
assert math.isclose(result[row][col], expected[row][col], abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_z_axis_zero() -> None:
|
||||
"""Testet die 3D-Rotation um die z-Achse um 0 Radiant."""
|
||||
# Arrange
|
||||
angle = 0.0
|
||||
axis = "z"
|
||||
|
||||
# Act
|
||||
result = rot_3d(angle, axis)
|
||||
|
||||
# Assert
|
||||
expected = [
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 1.0],
|
||||
]
|
||||
for row in range(3):
|
||||
for col in range(3):
|
||||
assert math.isclose(result[row][col], expected[row][col], abs_tol=1e-9)
|
||||
|
||||
|
||||
def test_rot_3d_z_axis_pi_half() -> None:
|
||||
"""Testet die 3D-Rotation um die z-Achse um pi / 2 Radiant."""
|
||||
# Arrange
|
||||
angle = math.pi / 2
|
||||
axis = "z"
|
||||
|
||||
# Act
|
||||
result = rot_3d(angle, axis)
|
||||
|
||||
# Assert
|
||||
expected = [
|
||||
[0.0, -1.0, 0.0],
|
||||
[1.0, 0.0, 0.0],
|
||||
[0.0, 0.0, 1.0],
|
||||
]
|
||||
for row in range(3):
|
||||
for col in range(3):
|
||||
assert math.isclose(result[row][col], expected[row][col], abs_tol=1e-9)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("invalid_axis", ["w", "123", "", "X", "Y", "Z"])
|
||||
def test_rot_3d_invalid_axis(invalid_axis: str) -> None:
|
||||
"""Testet, dass ungültige Achsen einen ValueError auslösen."""
|
||||
# Arrange
|
||||
angle = 1.0
|
||||
|
||||
# Act & Assert
|
||||
with pytest.raises(ValueError, match="Ungültige Achse"):
|
||||
rot_3d(angle, invalid_axis)
|
||||
Reference in new issue
Block a user