Matrix multiplication — it is an operation that takes two matrices as input and creates one matrix by multiplying the rows of the first matrix by the column of the second matrix. In matrix multiplication, make sure that the number of rows in the first matrix must be equal to the number of columns in the second matrix.
Example: Multiplying two 3 × 3 matrices by each other
Input: matrix1 = ([1, 2, 3], [3, 4, 5 ], [7, 6, 4]) matrix2 = ([5, 2, 6], [5, 6, 7], [7, 6, 4]) Output: [[36 32 32] [70 60 66] [93 74 100]]
Methods for multiplying two matrices in python
1. Using an explicit for: loop is simple a matrix multiplication method, but one of the expensive methods for a large set of inputs. In this we use nested for loops to iterate over each row and each column.
If Matrix1 matrix is pCht and matrix2 is matrix MXL.
[[114 160 60] [74 97 73] [119 157 112]]
In this program we used nested for loops to compute a result that will iterate over all the rows and columns of the matrices, and finally accumulate the sum of the result as a result.
2. Using Numpy: Multiplication using Numpy is also known as vectorization, the main purpose of which is to reduce or eliminate the explicit use of for loops in the program, which makes calculations faster.
Numpy — it is building a package in python to handle and manipulate arrays. For large matrix operations, we use the numpy python package, which is 1000 times faster than a single iterative method.
For more information on Numpy, please visit the link
[[63 320 83] [77 484 102] [84 248 117]]
In the above example, we used dot product, and in mathematics, dot product — it is an algebraic operation that takes two vectors of the same size and returns one number. The result is calculated by multiplying the respective entries and adding these products.
This article courtesy of Diraj Sharma . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.