From 2d209069a36898d190e8bebe443e20609b30f3c2 Mon Sep 17 00:00:00 2001 From: marcelbls Date: Thu, 6 Nov 2025 16:32:30 +0100 Subject: [PATCH] Added Rot3D --- .idea/MatrixMania.iml | 6 ++++++ matrixmania/compute.py | 29 ++++++++++++++++++++++++++-- tests/test_matmul.py | 1 + tests/{test_rot.py => test_rot2D.py} | 2 ++ tests/test_rot3D.py | 1 + tests/test_transpose.py | 1 + 6 files changed, 38 insertions(+), 2 deletions(-) rename tests/{test_rot.py => test_rot2D.py} (63%) create mode 100644 tests/test_rot3D.py create mode 100644 tests/test_transpose.py diff --git a/.idea/MatrixMania.iml b/.idea/MatrixMania.iml index 51bcf32..3b1d427 100644 --- a/.idea/MatrixMania.iml +++ b/.idea/MatrixMania.iml @@ -7,4 +7,10 @@ + + + + \ No newline at end of file diff --git a/matrixmania/compute.py b/matrixmania/compute.py index e9b3c0a..3e1c779 100644 --- a/matrixmania/compute.py +++ b/matrixmania/compute.py @@ -74,7 +74,32 @@ def rot_2D(angle: int) -> List[List[float]]: return [[zahl1, zahl2], [zahl3, zahl4]] +def rot_3D(angle: float, axis: str) -> List[List[float]]: + """ + Rotation matrix 3D + :param angle: between 0 and 360 degrees + :param axis: x,y,z axis of rotation matrix + :return: 3D rotation matrix + """ + if angle > 360: + raise ValueError("angle must be less than 360") + elif angle < 0: + raise ValueError("angle must be greater than 0") + + cos = math.cos(math.radians(angle)) + sin = math.sin(math.radians(angle)) + + if axis == "x": + return [[1,0,0],[0, cos, -sin],[0, sin, cos]] + elif axis == "y": + return [[cos, 0, sin],[0,1,0],[-sin,0,cos]] + elif axis == "z": + return [[cos, -sin, 0],[sin, cos, 0],[0, 0, 1]] + + else: + raise ValueError("Not a valid axis. Choose from 'x', 'y', 'z'.") + if __name__ == "__main__": - "alles selbst gemacht" - print(rot_2D()) \ No newline at end of file + + print(rot_3D(90, "x")) \ No newline at end of file diff --git a/tests/test_matmul.py b/tests/test_matmul.py index a56903d..240618e 100644 --- a/tests/test_matmul.py +++ b/tests/test_matmul.py @@ -12,4 +12,5 @@ def test_matmul(): matrix_c = matmul(matrix_a, matrix_b) print(f"Matrix A: {matrix_a} mal Matrix B: {matrix_b} = Matrix C: {matrix_c}") + assert matrix_c == [[20, 37, 26], [-1, 27, -8]] diff --git a/tests/test_rot.py b/tests/test_rot2D.py similarity index 63% rename from tests/test_rot.py rename to tests/test_rot2D.py index 5c9b55d..3b92cca 100644 --- a/tests/test_rot.py +++ b/tests/test_rot2D.py @@ -5,4 +5,6 @@ def test_rot_2D(): result = rot_2D(angle) print(f"Angle:{angle} Rot-Matrix:{result}") + assert result == [[6.123233995736766e-17, -1.0], [1.0, 6.123233995736766e-17]] + diff --git a/tests/test_rot3D.py b/tests/test_rot3D.py new file mode 100644 index 0000000..ad408f3 --- /dev/null +++ b/tests/test_rot3D.py @@ -0,0 +1 @@ +from matrixmania import rot_3D \ No newline at end of file diff --git a/tests/test_transpose.py b/tests/test_transpose.py new file mode 100644 index 0000000..b73f7bd --- /dev/null +++ b/tests/test_transpose.py @@ -0,0 +1 @@ +from matrixmania import transpose