init
This commit is contained in:
commit
65832361af
82
compute.py
Normal file
82
compute.py
Normal file
@ -0,0 +1,82 @@
|
||||
from typing import List
|
||||
import math
|
||||
from tabulate import tabulate
|
||||
Matrix = List[List[float]]
|
||||
|
||||
def matmul(Matrix_1: Matrix, Matrix_2: Matrix) -> Matrix:
|
||||
"""Multipliziert zwei Matrizen."""
|
||||
HeightA = len(Matrix_1)
|
||||
HeightB = len(Matrix_2)
|
||||
|
||||
if HeightA == 0 or HeightB == 0:
|
||||
raise ValueError("Matrizen dürfen nicht leer sein")
|
||||
|
||||
WidthA = len(Matrix_1[0])
|
||||
WidthB = len(Matrix_2[0])
|
||||
|
||||
if WidthA != HeightB:
|
||||
raise ValueError("Multiplikation der Matrizen nicht zulässig (Spalten A != Zeilen B)")
|
||||
|
||||
|
||||
Matrix_3 = [[0.0 for _ in range(WidthB)] for _ in range(HeightA)]
|
||||
|
||||
|
||||
for i in range(HeightA):
|
||||
for j in range(WidthB):
|
||||
for k in range(WidthA):
|
||||
Matrix_3[i][j] += Matrix_1[i][k] * Matrix_2[k][j]
|
||||
|
||||
return Matrix_3
|
||||
|
||||
|
||||
def transpose(matrix: Matrix) -> Matrix:
|
||||
"""Transponiert eine Matrix."""
|
||||
if not matrix:
|
||||
raise ValueError("Matrix darf nicht leer sein")
|
||||
|
||||
rows = len(matrix)
|
||||
cols = len(matrix[0])
|
||||
|
||||
if not all(len(row) == cols for row in matrix):
|
||||
raise ValueError("Matrix hat ungleichmäßige Zeilenlängen")
|
||||
|
||||
|
||||
transposed_matrix = [[0.0 for _ in range(rows)] for _ in range(cols)]
|
||||
|
||||
for i in range(rows):
|
||||
for j in range(cols):
|
||||
transposed_matrix[j][i] = matrix[i][j]
|
||||
|
||||
return transposed_matrix
|
||||
|
||||
|
||||
def rot_2D(angle_degrees: float) -> Matrix:
|
||||
"""Erstellt eine 2D-Rotationsmatrix für den gegebenen Winkel (in Grad)."""
|
||||
angle_radians = math.radians(angle_degrees)
|
||||
cos_theta = math.cos(angle_radians)
|
||||
sin_theta = math.sin(angle_radians)
|
||||
|
||||
rotation_matrix = [
|
||||
[cos_theta, -sin_theta],
|
||||
[sin_theta, cos_theta]
|
||||
]
|
||||
return rotation_matrix
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
A = [[1, 2], [3, 4]]
|
||||
B = [[2, 0], [1, 2]]
|
||||
|
||||
print("Matrixmultiplikation:")
|
||||
print(matmul(A, B))
|
||||
|
||||
print("\nTransponierte Matrix:")
|
||||
print(transpose(A))
|
||||
|
||||
print("\nRotationsmatrix (45°):")
|
||||
print(rot_2D(45))
|
||||
|
||||
matrix= [[1,2,3], [2,3,4]]
|
||||
print("Matrix A:")
|
||||
print(tabulate(matrix))
|
||||
Loading…
x
Reference in New Issue
Block a user