Dot Product of Two Matrices in Python
The product of two matrices A and B will be possible if the number of columns of a Matrix A is equal to the number of rows of another Matrix B. A mathematical example of dot product of two matrices A & B is given below.
If
\(\displaystyle A=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 3 & 4 \end{array}} \right]\)
and
\(\displaystyle B=\left[ {\begin{array}{*{20}{c}} 3 & 2 \\ 1 & 4 \end{array}} \right]\)
Then,
\(\displaystyle AB=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 3 & 4 \end{array}} \right] \left[ {\begin{array}{*{20}{c}} 3 & 2 \\ 1 & 4 \end{array}} \right]\)
\(\displaystyle AB=\left[ {\begin{array}{*{20}{c}} {1\times 3+2\times 1} & {1\times 2+2\times 4} \\ {3\times 3+4\times 1} & {3\times 2+4\times 4} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {3+2} & {2+8} \\ {9+4} & {6+16} \end{array}} \right]\)
\(\displaystyle AB=\left[ {\begin{array}{*{20}{c}} 5 & {10} \\ {13} & {22} \end{array}} \right]\)
Let’s start a practical example of dot product of two matrices A & B in python. First, we import the relevant libraries in Jupyter Notebook.
![](png/image-64.png)
Dot Product of two Matrices
![](png/image-65.png)
Let’s see another example of Dot product of two matrices C and D having different values.
![](png/image-66.png)
If all the diagonal elements of a diagonal matrix are same, then it is called a Scalar Matrix. We can also take the dot product of two scalars which result will also a scalar, like this
![](png/image-67.png)
Linear Algebra is mostly concerned with operations on vectors and matrices. Let’s take an example of dot product of one scalar and one vector…
![](png/image-68.png)
It is clear from above snap that, the result obtained after taking dot product of a scalar and a vector is also a vector because a scalar value i.e. 2 is multiplied with each value of a vector i.e. 1, 2, 3 & 4 and we obtained a vector having values 2, 4, 6 & 8.