From 9ab055becab513652f7a93cb68a3716091775397 Mon Sep 17 00:00:00 2001 From: Mona Benkert Date: Sat, 1 Nov 2025 17:56:34 +0100 Subject: [PATCH] first commit --- compute.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 compute.py diff --git a/compute.py b/compute.py new file mode 100644 index 0000000..00aa72b --- /dev/null +++ b/compute.py @@ -0,0 +1,92 @@ +import math +from typing import List + +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]]: + rot_matrix = [[math.cos(angle), -math.sin(angle)], + [math.sin(angle), math.cos(angle)]] + return rot_matrix + + +if __name__ == "__main__": + 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) + + matrix = [ + [1, 2, 3], + [4, 5, 6] + ] + new_matrix = transpose(matrix) + print("transposed 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(R) + + p = [[1, 0]] + rotated = matmul(p, R) + print("rotated matrix:") + print(rotated) +