MatrixMania/compute.py

99 lines
2.3 KiB
Python

import math
from tabulate import tabulate
##print("Matrix A:")
##print(tabulate(matrix_a))
def matmul(mA:list[list[int]], mB:list[list[int]]) -> list[list[int]]:
"""
Matrixmultiplication of matrizes with ints -> returns matrix as result
:param mA:
:param mB:
:return:
:raises ValueError: if Mismatch of Width first matrix and Height of second matrix
"""
HeightA = len(mA) ## zählt anzahl der listen in der äußeren Liste
WidthA = len(mA[0]) ## zählt anzahl der elemente der nullten Liste
HeightB = len(mB)
WidthB = len(mB[0])
if WidthA != HeightB:
raise ValueError('Mismatch of Width of A and Height of B')
result = [[0 for _ in range(WidthB)] for _ in range(HeightA)]
for i in range(HeightA):
for j in range(WidthB):
for k in range(WidthA):
result[i][j] += mA[i][k] * mB[k][j]
return result
##=================================================================================
def transpose(matrix:list[list[int]]) -> list[list[int]]:
"""
Transposes given matrix
:param matrix:
:return:
:raises: ValueError if Inconsistent number of columns OR matrix is empty
"""
if not matrix:
raise ValueError('matrix is empty')
height_old = len(matrix)
width_old = len(matrix[0])
for i in range(len(matrix) - 1):
if len(matrix[i]) != len(matrix[i + 1]):
raise ValueError('Inconsistent number of columns')
result = [[0 for _ in range(height_old)] for _ in range(width_old)]
height_new = len(result)
width_new = len(result[0])
for i in range(height_new):
for j in range(width_new):
result[i][j] = matrix[j][i]
return result
##=================================================================================
def rot_2D(bogenmaß: float) -> list[list[float]]: ##auf Bogenmaß
return [
[math.cos(bogenmaß), -math.sin(bogenmaß)],
[math.sin(bogenmaß), math.cos(bogenmaß)]
]
##rota matrix ist: aus Aufgabe ggben
## cos -sin
## sin cos
##=================================================================================
if __name__ == "__main__":
print("Hallo")
#created by Chatgpt
#Das war nicht CHAT sondern ich