C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Program to Multiply Two MatricesWe will write a Python program to get the multiplication of two input matrices and print the result in output. This Python program specifies how to multiply two matrices, having some certain values. Before writing the Python program, let's first look at the overview of the multiplication of two matrices. Matrix multiplicationMatrix multiplication is a binary operation that uses a pair of matrices to produce another matrix. The elements within the matrix are multiplied according to elementary arithmetic. In the multiplication of two matrices, the row elements of the first matrix are multiplied to the column elements of the second matrix. Example: Suppose we have given following two A and B matrices: A = [[5, 4, 3] [2, 4, 6] [4, 7, 9]] and, B = [[3, 2, 4] [4, 3, 6] [2, 7, 5]] C would be the addition of above given two matrices, i.e., C = A+B, and therefore C should be: C = [[37, 43, 59] [34, 58, 62] [58, 92, 103]] As we can see that the resulting matrix C, which is also known as matrix product, has the same number of rows as the first matrix (A matrix) and the same number of columns as the second matrix (B matrix). We also know this type of multiplication of matrices as dot product of matrices. Multiplication of two matricesNow, we will write a Python program for the multiplication of two matrices where we perform the multiplication as we have performed in the above-given example. We can use various methods to write a Python program like this, but in this tutorial, we will only use the following two methods:
In both methods, we will write an example program to understand their implementation for multiplying two matrices. Method 1: Using nested for loop method:In this method, we are going to use nested for loop on two matrices and perform multiplication on them and store multiplication result in the third matrix as the result value. Let's understand implementation of this method through the following example. Example: Look at the following Python program: # Define two matrix A and B in program A = [[5, 4, 3], [2, 4, 6], [4, 7, 9]] B = [[3, 2, 4], [4, 3, 6], [2, 7, 5]] # Define an empty matrix to store multiplication result multiResult = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # Using nested for loop method on A & B matrix for m in range(len(A)): for n in range(len(B[0])): for o in range(len(B)): multiResult[m][n] += A[m][o] * B[o][n] # Storing multiplication result in empty matrix # Printing multiplication result in the output print("The multiplication result of matrix A and B is: ") for res in multiResult: print(res) Output: The multiplication result of matrix A and B is: [37, 43, 59] [34, 58, 62] [58, 92, 103] Method 2: Using nested list comprehension method:In this method, we will use nested list comprehension to get the multiplication result of two input matrices. While using the list comprehension method in the program, we will also use 'zip in Python' on the nested list. Let's understand the implementation of this method through the following example. Example: Look at the following Python program: # Define two matrix A & B in the program A = [[5, 4, 3], [2, 4, 6], [4, 7, 9]] B = [[3, 2, 4], [4, 3, 6], [2, 7, 5]] # Using nested list method with zip in Python multiResult = [[sum(a * b for a, b in zip(Arow, Bcol)) for Bcol in zip(*B)] for Arow in A] # Printing multiplication result in the output print("The multiplication result of matrix A and B is: ") for res in multiResult: print(res) Output: The multiplication result of matrix A and B is: [37, 43, 59] [34, 58, 62] [58, 92, 103]
Next TopicPyhton Transpose Matrix
|