from typing import List import math from tabulate import tabulate 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 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()