2025-11-11 16:08:47 +01:00

169 lines
4.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import List
import math
from tabulate import tabulate
from typing import Tuple
def matmul(mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
"""
Multiplies two matrices and returns the resulting matrix.
:param mat1: The first matrix
:param mat2: The second matrix
:return: A new matrix representing the product of mat1 and mat2
:raises ValueError: If the number of columns in mat1 does not equal the number of rows in mat2
"""
if len(mat1[0]) != len(mat2):
raise ValueError("Matrix isnt computable")
mat3: List[List[int]] = []
for i in range(len(mat1)):
new_row: List[int] = []
for j in range(len(mat2[0])):
summe = 0
for k in range(len(mat2)):
summe += mat1[i][k] * mat2[k][j]
new_row.append(summe)
mat3.append(new_row)
return mat3
def transpose(matrix: List[List[int]]) -> List[List[int]]:
"""
Transposes a given matrix (rows become columns and columns become rows).
:param matrix: A list of lists representing the matrix.
:return: A new matrix that is the transpose of the input matrix.
"""
if not matrix:
raise ValueError("Matrix is empty")
row_length = len(matrix[0])
if any(len(row) != row_length for row in matrix):
raise ValueError("Matrix rows have uneven lengths")
transposed: List[List[int]] = []
for i in range(row_length):
new_row = []
for j in range(len(matrix)):
new_row.append(matrix[j][i])
transposed.append(new_row)
return transposed
def rot_2D(theta: float) -> List[List[float]]:
"""
Returns a 2D rotation matrix for a given angle in radians.
The rotation matrix can be used to rotate points in the 2D plane
around the origin by the angle theta (counterclockwise).
:param theta: The rotation angle in radians.
:return: A 2x2 rotation matrix as a list of lists.
"""
cos_t = math.cos(theta)
sin_t = math.sin(theta)
return [
[cos_t, -sin_t],
[sin_t, cos_t]
]
def rot_3D(angle: float, axis: str) -> List[List[float]]:
"""
Returns a 3D rotation matrix for a given angle and axis.
Rotates points in 3D space around the specified axis by the given angle.
The rotation follows the right-hand rule (counterclockwise when looking
along the axis of rotation).
Rotation matrices:
x-axis:
[[1, 0, 0],
[0, cosθ, -sinθ],
[0, sinθ, cosθ]]
y-axis:
[[ cosθ, 0, sinθ],
[ 0, 1, 0],
[-sinθ, 0, cosθ]]
z-axis:
[[cosθ, -sinθ, 0],
[sinθ, cosθ, 0],
[ 0, 0, 1]]
:param angle: Rotation angle in radians.
:param axis: Axis of rotation ('x', 'y', or 'z').
:return: A 3×3 rotation matrix as a list of lists.
:raises ValueError: If the axis is not 'x', 'y', or 'z'.
"""
cos_t = math.cos(angle)
sin_t = math.sin(angle)
if axis == 'x':
return [
[1, 0, 0],
[0, cos_t, -sin_t],
[0, sin_t, cos_t]
]
elif axis == 'y':
return [
[cos_t, 0, sin_t],
[0, 1, 0],
[-sin_t, 0, cos_t]
]
elif axis == 'z':
return [
[cos_t, -sin_t, 0],
[sin_t, cos_t, 0],
[0, 0, 1]
]
else:
raise ValueError("Axis must be 'x', 'y', or 'z'.")
def project_ortho(point: Tuple[float, float, float], scale: float = 1) -> Tuple[float, float]:
"""
Retruns a 2d point with optional scaling
ignores the z axis and if scaling needed scales the 2d point
:param point: Coordinates of a 3d point
:param scale: scaling factor (else 1)
:return: A 2d point
"""
return scale * point[0], scale * point[1]
def main():
# Test matrix multiplication
matrix_a = [[3, 4, -1, 4],
[-2, 2, 5, 1]]
matrix_b = [[1, 3, -2],
[2, 5, 1],
[-1, 4, -4],
[2, 3, 6]]
matrix_c = matmul(matrix_a, matrix_b)
print("Ergebnis C = A * B:")
for row in matrix_c:
print(row)
# Test transpose
matrix = [[1, 2, 3],
[4, 5, 6]]
print("\nTransposed:")
for row in transpose(matrix):
print(row)
# Test rotation
print("\nRotation matrix for 90° (pi/2 radians):")
for row in rot_2D(math.pi / 2):
print(row)
print("Matrix A:")
print(tabulate(matrix_a))
if __name__ == "__main__":
main()
# hab ich alles selbst programmiert