Hier ein erster Versuch mit 5 x 3 und 3 x 3 Matrizen.
Verlauf fehlerfrei und , händisch noch nicht überprüft
bin mir aber sicher.
Werde auch noch Multiplikation mit Vektoren etc durchführen.,
sowie andere Konstellationen und Matrizentypen.
Gute Zeit OSWALD
Code: Alles auswählen
#############################################################
# [A] x [B] = 5 x 3 / 3x3 Matrix = C
# [B] x [A] nicht möglich
#
def matrix_multiplication(A, B):
if len(A[0]) != len(B):
raise ValueError("Number of columns in A must equal number of rows in B")
# Number of rows and columns in resulting matrix
num_rows_A = len(A)
num_cols_B = len(B[0])
# Perform matrix multiplication using list comprehension
result = [[sum(A[i][k] * B[k][j] for k in range(len(B))) for j in range(num_cols_B)] for i in range(num_rows_A)]
return result
# Example usage:
if __name__ == "__main__":
# Example matrices A and B
A = [[ 4 ,8 ,3],
[ 6,7,-3] ,
[ 1,2,9] ,
[ 3, 4, 8 ] ,
[ 9, 2 ,6]]
B = [[ 7 ,3,6] ,
[ 9 , 6 ,1],
[ 6 , 4 , 4]]
# Print the result of matrix multiplication
print ("C = A * B : ", matrix_multiplication(A, B))