The numpy module of Python provides a function to perform the dot product of two arrays.
dot(a, b)[i,j,k,n] = sum(a[i,j,:] * b[k,:,n])
numpy.dot(a, b, out=None)
a: array_like
This parameter defines the first array.
b: array_like
This parameter defines the second array.
out: ndarray(optional)
It is an output argument. It should have the exact kind which would be returned in the case when it was not used. Particularly, it should meet the performance feature, i.e., it must contain the right type, i.e., it must be C-contiguous, and its dtype must be the dtype that would be returned for dot(a,b). Thus, if it does not meet these specified conditions, it raises an exception.
This function returns the dot product of 'a' and 'b'. This function returns a scalar if 'a' and 'b' are both scalars or 1-dimensional; otherwise, it returns an array. If 'out' is given, then it is returned.
The ValueError occurs when the last dimension of 'a' is not having the same size as the second-to-last dimension of 'b'.
import numpy as np a=np.dot(6,12) a
Output:
import numpy as np a=np.dot([2j, 3j], [5j, 8j]) a
Output:
import numpy as np a = [[1, 2], [4, 1]] b = [[4, 11], [2, 3]] c=np.dot(a, b) c
Output:
In the above code
In the output, it shows the matrix product as an array.
import numpy as np x = np.arange(3*4*5*6).reshape((3,4,5,6)) y = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3)) p=np.dot(a, b)[2,3,2,1,2,2] q=sum(a[2,3,2,:] * b[1,2,:,2]) p q
Output:
In the above code
In the output, it shows the matrix product as an array.