first commit
This commit is contained in:
commit
9ab055beca
92
compute.py
Normal file
92
compute.py
Normal file
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user