Tests für rot_2D und rot_3D

This commit is contained in:
Lena committed 2026-10-28 21:29:44 +01:00
1 parent 70e1a351f3
commit 7753115638
6 files changed
+196 -54

No files matched your search

+53 -16
View File
@@ -1,5 +1,3 @@
"""Utility functions for matrix operations.
This module contains:
@@ -38,6 +36,7 @@ def matmul(a: Matrix, b: Matrix) -> Matrix:
ValueError
If matrices are not rectangular or dimensions are incompatible.
"""
def _rect(mat: Matrix) -> bool:
return all(len(row) == len(mat[0]) for row in mat)
@@ -94,22 +93,23 @@ def transpose(matrix: Matrix) -> Matrix:
def rot_2D(theta: float) -> list[list[float]]:
"""Return the 2‑D rotation matrix for angle *theta* in radians.
The matrix is
``[[cos(θ), -sin(θ)],
[sin(θ), cos(θ)]]``
where θ is interpreted as a clockwise rotation.
The matrix is
``[[cos(θ), -sin(θ)],
[sin(θ), cos(θ)]]``
where θ is interpreted as a clockwise rotation.
Parameters
----------
theta : float
Rotation angle in radians (clockwise).
Parameters
----------
theta : float
Rotation angle in radians (clockwise).
Returns
-------
list[list[float]]
2x2 rotation matrix.
Returns
-------
list[list[float]]
2x2 rotation matrix.
"""
import math
# Treat theta as radians directly
rad = theta
cos_t = math.cos(rad)
@@ -143,6 +143,7 @@ def rotate(matrix: Matrix, degrees: float) -> Matrix:
n_rows, n_cols = len(matrix), len(matrix[0])
# rotation matrix
import math
r = rot_2D(math.radians(degrees))
cos_t, sin_t = r[0][0], r[1][0] # cos and sin from 2x2 matrix
# centre of the matrix (half‑index)
@@ -165,6 +166,40 @@ def rotate(matrix: Matrix, degrees: float) -> Matrix:
result[new_i][new_j] = matrix[i][j]
return result
def rot_3D(angle: float, axis: str) -> list[list[float]]:
"""Return a 3×3 rotation matrix for a rotation around *axis*.
Parameters
----------
angle : float
Rotation angle in radians.
axis : str
Axis of rotation, one of "x", "y" or "z".
Returns
-------
list[list[float]]
The 3×3 rotation matrix.
Raises
------
ValueError
If *axis* is not one of "x", "y" or "z".
"""
import math
c = math.cos(angle)
s = math.sin(angle)
if axis == "x":
return [[1, 0, 0], [0, c, -s], [0, s, c]]
if axis == "y":
return [[c, 0, s], [0, 1, 0], [-s, 0, c]]
if axis == "z":
return [[c, -s, 0], [s, c, 0], [0, 0, 1]]
raise ValueError(f"Unsupported rotation axis: {axis}")
if __name__ == "__main__":
# matmul tests
a1 = [[1, 2], [3, 4]]
@@ -172,7 +207,9 @@ if __name__ == "__main__":
assert matmul(a1, b1) == [[19, 22], [43, 50]], "matmul example 1 failed"
a2 = [[1], [2], [3]]
b2 = [[4, 5, 6]]
assert matmul(a2, b2) == [[4, 5, 6], [8, 10, 12], [12, 15, 18]], "matmul example 2 failed"
assert matmul(a2, b2) == [[4, 5, 6], [8, 10, 12], [12, 15, 18]], (
"matmul example 2 failed"
)
# transpose test
mat = [[1, 2, 3], [4, 5, 6]]
@@ -184,5 +221,5 @@ if __name__ == "__main__":
assert rotate(mat3, 180) == [[6, 5], [4, 3], [2, 1]], "rotate 180 failed"
assert rotate(mat3, 270) == [[2, 4, 6], [1, 3, 5]], "rotate 270 failed"
assert rotate(mat3, 0) == mat3, "rotate 0 failed"
print(tabulate(rot_2D(1.0)))