Hausaufgabe Blatt 2: rot_3D implementiert, Tests ergänzt und Paketstruktur erstellt

This commit is contained in:
2025-11-20 14:06:50 +01:00
parent 762d0ab9dc
commit c6a8b0dc68
6 changed files with 141 additions and 1 deletions
+39
View File
@@ -0,0 +1,39 @@
import pytest
from matrixmania.compute import matmul, transpose
# --- Tests für Transponieren ---
def test_transpose_regular():
# Testet eine normale 2x3 Matrix
matrix = [
[1, 2, 3],
[4, 5, 6]
]
expected = [
[1, 4],
[2, 5],
[3, 6]
]
assert transpose(matrix) == expected
def test_transpose_empty():
# Testet, ob der ValueError bei leerer Matrix kommt
with pytest.raises(ValueError):
transpose([])
# --- Tests für Matrixmultiplikation (matmul) ---
def test_matmul_simple():
# Einfache Multiplikation 2x2 * 2x1
A = [[1, 2], [3, 4]]
B = [[5], [6]]
# Rechnung:
# 1*5 + 2*6 = 5 + 12 = 17
# 3*5 + 4*6 = 15 + 24 = 39
expected = [[17], [39]]
assert matmul(A, B) == expected
def test_matmul_invalid_dimensions():
# Testet, ob Fehler geworfen wird, wenn Dimensionen nicht passen
A = [[1, 2]] # 1x2
B = [[1, 2]] # 1x2 -> Spalten A (2) != Zeilen B (1)
with pytest.raises(ValueError):
matmul(A, B)
+48
View File
@@ -0,0 +1,48 @@
import pytest
from matrixmania.compute import rot_2D, rot_3D
# --- Tests für Rotationen (2D & 3D) ---
def test_rot_2D():
# 90 Grad Rotation in 2D: (1, 0) -> (0, 1)
# Matrix sollte [[0, -1], [1, 0]] sein (cos(90)=0, sin(90)=1)
result = rot_2D(90)
# Wir nutzen pytest.approx für Float-Vergleiche, da 0.00000000001 != 0 ist
assert result[0][0] == pytest.approx(0.0) # cos
assert result[0][1] == pytest.approx(-1.0) # -sin
assert result[1][0] == pytest.approx(1.0) # sin
assert result[1][1] == pytest.approx(0.0) # cos
def test_rot_3D_x_axis():
# 90 Grad um X-Achse
# Erwartet:
# 1 0 0
# 0 0 -1
# 0 1 0
result = rot_3D(90, 'x')
assert result[0] == [1.0, 0.0, 0.0] # X-Achse bleibt stabil
assert result[1][1] == pytest.approx(0.0)
assert result[1][2] == pytest.approx(-1.0)
assert result[2][1] == pytest.approx(1.0)
def test_rot_3D_z_axis():
# 180 Grad um Z-Achse
# Erwartet:
# -1 0 0
# 0 -1 0
# 0 0 1
result = rot_3D(180, 'z')
assert result[0][0] == pytest.approx(-1.0)
assert result[1][1] == pytest.approx(-1.0)
assert result[2][2] == pytest.approx(1.0)
def test_rot_3D_invalid_input():
# Testet, ob Fehler geworfen wird bei falscher Achse
with pytest.raises(ValueError):
rot_3D(90, 'q') # 'q' gibt es nicht