144 lines
3.7 KiB
Python
144 lines
3.7 KiB
Python
from typing import List
|
||
import math
|
||
|
||
def count_rows_and_columns(matr_a: List[List[int]], matr_b: List[List[int]]) -> List:
|
||
"""
|
||
counts all rows and columns
|
||
:param matr_a: matrix_a
|
||
:param matr_b: matrix_b
|
||
:return: list of rows and columns of matrix_a and matrix_b
|
||
"""
|
||
count_ra = 0
|
||
count_rb = 0
|
||
count_ca = 0
|
||
count_cb = 0
|
||
|
||
for element in matr_a :
|
||
if type(element) == list: # Prüfen, ob das Element eine Liste ist
|
||
count_ra += 1
|
||
|
||
for element in matr_b :
|
||
if type(element) == list: # Prüfen, ob das Element eine Liste ist
|
||
count_rb += 1
|
||
|
||
count_ca = len(matr_a[0])
|
||
count_cb = len(matr_b[0])
|
||
|
||
if count_ca != count_rb:
|
||
raise ValueError("the dimensions of the two matrices dont match")
|
||
|
||
list_c_r = [count_ra, count_rb, count_ca, count_cb]
|
||
#print( "Matrix A has rows:", count_ra ,"und columns:", count_ca,
|
||
# "Matrix B has rows:", count_rb, "and columns:", count_cb)
|
||
return list_c_r
|
||
|
||
def matmul (matr_a: List[List[int]], matr_b: List[List[int]]) -> List[List[int]]:
|
||
"""
|
||
multplies two matrices
|
||
:param matr_a: Matrix_A
|
||
:param matr_b: Matrix_B
|
||
:return: product of Matrix_A and Matrix_B
|
||
"""
|
||
|
||
list_cr = count_rows_and_columns(matr_a, matr_b)
|
||
rows_a= list_cr[0]
|
||
rows_b = list_cr[1]
|
||
columns_a= list_cr[2]
|
||
columns_b = list_cr[3]
|
||
temp_list = []
|
||
c2 = 0
|
||
c1 = 0
|
||
result = [[0 for _ in range(columns_b)] for _ in range(rows_a)] # durch chat gpt
|
||
|
||
|
||
# if columns_a == rows_b:
|
||
# print("all good")
|
||
for i in range(rows_a):
|
||
for j in range(columns_b):
|
||
s = 0
|
||
for k in range(columns_a): # durch Chat gpt
|
||
s += matr_a[i][k] * matr_b[k][j]
|
||
result[i][j] = s
|
||
|
||
return result
|
||
|
||
def count_solo(matr_a: List[List[int]]) -> List:
|
||
"""
|
||
counting rows and columns of one matrix only
|
||
:param matr_a: matrix a
|
||
:return: the number of list and columns
|
||
"""
|
||
count_ra = 0
|
||
count_ca = 0
|
||
|
||
for element in matr_a :
|
||
if type(element) == list: # Prüfen, ob das Element eine Liste ist
|
||
count_ra += 1
|
||
count_ca = len(matr_a[0])
|
||
list_c_r = [count_ra, count_ca, ]
|
||
return list_c_r
|
||
|
||
|
||
def transpose(matr_a: List[List[int]]) -> List[List[int]]:
|
||
"""
|
||
transposes a matrix
|
||
:param matr_a: matrix_a
|
||
:return: the transposed matrix
|
||
"""
|
||
new_list= count_solo(matr_a)
|
||
rows = new_list[0]
|
||
columns = new_list[1]
|
||
|
||
trans_mat = [[0 for _ in range(rows)] for _ in range(columns)]
|
||
|
||
for i in range(rows):
|
||
for j in range(columns):
|
||
trans_mat[j][i] = matr_a[i][j] #kleine ergänzung durch chatti
|
||
|
||
return trans_mat
|
||
|
||
#def rot_2D(rad: float) -> List[List[int]]:
|
||
#if rad <= 0:
|
||
# return ValueError
|
||
# if rad >= 6.29: #2π=2×3,141592653589793=6,283185307179586
|
||
# return ValueError
|
||
# rot_matr = [
|
||
# [math.cos(rad), -math.sin(rad)],
|
||
# [math.sin(rad), math.cos(rad)]
|
||
# ]
|
||
# return rot_matr
|
||
|
||
def rot_2D(rad: float) -> List[List[float]]:
|
||
rot_matr = [
|
||
[math.cos(rad), -math.sin(rad)],
|
||
[math.sin(rad), math.cos(rad)]
|
||
]
|
||
return rot_matr
|
||
|
||
|
||
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]
|
||
]
|
||
print(transpose(matrix))
|
||
# Erwartete Ausgabe:
|
||
# [
|
||
# [1, 4],
|
||
# [2, 5],
|
||
# [3, 6]
|
||
# ]
|
||
matr = rot_2D(2)
|
||
print(matr) |