import math from typing import List, Tuple from tabulate import tabulate def matmul(matrix_a:List[List[float]], matrix_b:List[List[float]]) -> List[List[float]]: """ calculates matrix multiplication with 2 matrices :param matrix_a: the first matrix :param matrix_b: the second matrix :return: the product of 2 matrices """ a_rows = len(matrix_a) a_columns = len(matrix_a[0]) b_rows = len(matrix_b) b_columns = len(matrix_b[0]) if not a_columns == b_rows: raise ValueError("Matrices not valid") result = [] for i in range(a_rows): line = [] for j in range(b_columns): elem = 0 for k in range(a_columns): elem += matrix_a[i][k] * matrix_b[k][j] line.append(elem) result.append(line) return result def transpose(matrix:List[List[int]]) -> List[List[int]]: """ transposes the matrix :param matrix: the matrix :return: the transpose of the matrix """ if not matrix: raise ValueError("Matrices not valid") rows = len(matrix) cols = len(matrix[0]) #for i in matrix: # if rows != cols: # raise ValueError("Matrices not valid") result = [] for i in range(cols): line = [] for j in range(rows): line.append(matrix[j][i]) result.append(line) return result def rot_2D(angle: float) -> List[List[float]]: ''' calculates the rotation matrix in 2D :param angle: the angle :return: rotation matrix ''' return [[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]] def rot_3D(angle: float, axis: str) -> List[List[float]]: ''' calculates the rotation matrix for one 3D axis :param angle: the angle :param axis: the axis :return: rotation matrix for the wanted axis ''' axis = axis.lower() if axis == "x": return [[1, 0, 0], [0, math.cos(angle), -math.sin(angle)], [0, math.sin(angle), math.cos(angle)]] elif axis == "y": return [[math.cos(angle), 0, math.sin(angle)], [0, 1, 0], [-math.sin(angle), 0, math.cos(angle)]] elif axis == "z": return [[math.cos(angle), -math.sin(angle), 0], [math.sin(angle), math.cos(angle), 0], [0, 0, 1]] else: raise ValueError("Axis not valid") def project_ortho(point: Tuple[float, float, float], scale: float = 1) -> Tuple[float, float]: point_2D = (point[0] * scale, point[1] * scale) return point_2D if __name__ == "__main__": # ich habe alles selbst programmiert 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("Matrix C:") print(tabulate(matrix_c)) #for row in matrix_c: # print(row) matrix = [ [1, 2, 3], [4, 5, 6] ] new_matrix = transpose(matrix) print("transposed matrix:") print(tabulate(new_matrix)) #for row in new_matrix: # print(row) #empty_matrix = [] #transpose(empty_matrix) theta = math.pi / 2 R = rot_2D(theta) print("Rotatiosnmatrix für 90 Grad:") print(tabulate(R)) p = [[1, 0]] rotated = matmul(p, R) print("rotated matrix:") print(rotated) print("Matrix A:") print(tabulate(matrix_a)) theta = math.pi / 2 R = rot_3D(theta, "y") print("rotation matrix for 90° on the y axis:") print(tabulate(R)) print("Projection:") print(project_ortho((0.5, -0.5, 10), scale=200)) # -> (100, -100)