Matrix multiplication is an operation that takes two matrices as input and produces single matrix by multiplying rows of the first matrix to the column of the second matrix.In matrix multiplication make sure that the number of rows of the first matrix should be equal to the number of columns of the second matrix.
Example: Multiplication of two matrices by each other of size 3×3.
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 to multiply two matrices in python
1.Using explicit for loops: This is a simple technique to multiply matrices but one of the expensive method for larger input data set.In this, we use nested for loops to iterate each row and each column.
If matrix1 is a n x m matrix and matrix2 is a m x l matrix.
[[114 160 60] [ 74 97 73] [119 157 112]]
In this program, we have used nested for loops for computation of result which will iterate through each row and column of the matrices, at last it will accumulate the sum of product in the result.
2. Using Numpy : Multiplication using Numpy also know as vectorization which main aim to reduce or remove the explicit use of for loops in the program by which computation becomes faster.
Numpy is a build in a package in python for array-processing and manipulation.For larger matrix operations we use numpy python package which is 1000 times faster than iterative one method.
For detail about Numpy please visit the Link
[[ 63 320 83] [ 77 484 102] [ 84 248 117]]
In the above example we have used dot product and in mathematics the dot product is an algebraic operation that takes two vectors of equal size and returns a single number. The result is calculated by multiplying corresponding entries and adding up those products.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
This article is attributed to GeeksforGeeks.org