Multiplication of two Matrices in Single line using Numpy in Python

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 columns of the first matrix should be equal to the number of rows 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.

Implementation:

Python3

 `# input two matrices of size n x m` `matrix1 ``=` `[[``12``,``7``,``3``],` `        ``[``4` `,``5``,``6``],` `        ``[``7` `,``8``,``9``]]` `matrix2 ``=` `[[``5``,``8``,``1``],` `        ``[``6``,``7``,``3``],` `        ``[``4``,``5``,``9``]]`   `res ``=` `[[``0` `for` `x ``in` `range``(``3``)] ``for` `y ``in` `range``(``3``)] `   `# explicit for loops` `for` `i ``in` `range``(``len``(matrix1)):` `    ``for` `j ``in` `range``(``len``(matrix2[``0``])):` `        ``for` `k ``in` `range``(``len``(matrix2)):`   `            ``# resulted matrix` `            ``res[i][j] ``+``=` `matrix1[i][k] ``*` `matrix2[k][j]`   `print` `(res)`

Output

`[[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.

Implementation:

Python3

 `# We need install numpy in order to import it` `import` `numpy as np`   `# input two matrices` `mat1 ``=` `([``1``, ``6``, ``5``],[``3` `,``4``, ``8``],[``2``, ``12``, ``3``])` `mat2 ``=` `([``3``, ``4``, ``6``],[``5``, ``6``, ``7``],[``6``,``56``, ``7``])`   `# This will return dot product` `res ``=` `np.dot(mat1,mat2)`     `# print resulted matrix` `print``(res)`

Output:

```[[ 63 320  83]
[ 77 484 102]
[ 84 248 117]]```

Using numpy

Python3

 `# same result will be obtained when we use @ operator ` `# as shown below(only in python >3.5)` `import` `numpy as np`   `# input two matrices` `mat1 ``=` `([``1``, ``6``, ``5``],[``3` `,``4``, ``8``],[``2``, ``12``, ``3``])` `mat2 ``=` `([``3``, ``4``, ``6``],[``5``, ``6``, ``7``],[``6``,``56``, ``7``])`   `# This will return matrix product of two array` `res ``=` `mat1 @ mat2`   `# print resulted matrix` `print``(res)`

Output:

```[[ 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.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next