Multiplication of two matrix in python

Python program for Multiplication of two matrix. Here mentioned other language solution.
# Python 3 program for
# Two matrix multiplication
# Display the element of given 2d matrix
def printRecord(matrix) :
print(" ",matrix)
def multiplication( a, b) :
# Get the size
row = len(a)
col = len(a[0])
# This matrix are store the result of multiplication
result = [[0] * (col) for _ in range(row)]
i = 0
while (i < row) :
j = 0
while (j < col) :
# Set the initial value of new matrix element
result[i][j] = 0
k = 0
while (k < row) :
# Multiply matrix A [i] row and [k] columns to
# the Matrix B [k] columns and [j] rows.
result[i][j] += a[i][k] * b[k][j]
k += 1
j += 1
i += 1
print(" Matrix A")
# Print element of matrix x
printRecord(a)
print(" Matrix B")
# Print element of matrix y
printRecord(b)
print(" Matrix [(A) x (B)]")
# Display resultant matrix
printRecord(result)
if __name__=="__main__":
# Define matrix A
a = [[1, 2, 3], [6, 1, 2], [5, 4, 3]]
# Define matrix B
b = [[3, 1, 3], [1, 1, 2], [2, 2, 3]]
multiplication(a, b)
Output
Matrix A
[[1, 2, 3], [6, 1, 2], [5, 4, 3]]
Matrix B
[[3, 1, 3], [1, 1, 2], [2, 2, 3]]
Matrix [(A) x (B)]
[[11, 9, 16], [23, 11, 26], [25, 15, 32]]
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment