Dot Product

Rumman Ansari   Software Engineer   2023-01-15   94 Share
☰ Table of Contents

Table of Content:


Dot Product

Dot Product
  • If A is a m × n matrix and B is an n × p matrix, then the dot product of matrix A and matrix B is a m × p matrix, where the n entries across a row of A are multiplied with the n entries down a column of B and summed to generate an entry of resulting matrix.

  • The dot product greatly reduces the computation time especially when we have a large number of independent equations to solve.

  • Note that the number of columns in the first matrix should always be equal to the number of rows in the second matrix.

  • We use dot() function of NumPy package in python to compute the dot product.

Element-Wise Product
  • There are few situations where you need to compute element-wise multiplication of two matrices of same dimensions.

  • The resulting dimension will be same as the dimensions of two matrices used for element-wise multiplication.

  • We use multiply() function of numpy package in python to compute element-wise product of two matrices.

Dot product Example

Sample code in Python for a dot product


import numpy as np  

#matrix a

a = np.array([[1,2],[3,4]])

print("matrix a dimension ", a.shape)



#matrix b

b = np.array([[5,6,7],[8,9, 10]]) 

print("matrix b dimension ", b.shape)



#matrix c = a.b

c = np.dot(a,b)

print("dot product of a and b: ", c)

print("matrix c dimension ", c.shape)

Output:


output:

matrix a dimension  (2, 2)

matrix b dimension  (2, 3)

dot product of a and b:  [[21 24 27]

 [47 54 61]]

matrix c dimension  (2, 3)