Numpy matmul – Python NumPy matmul() Function

NumPy matmul() Function:

Numpy matmul: Matrix product of two arrays is returned by the matmul() function of the NumPy module. Specifically,

While it gives a normal product for 2-D arrays, if either argument’s dimensions are greater than two(>2), it is considered as a stack of matrices residing in the last two indexes and broadcast as such.

If either argument is a 1-D array, it is converted to a matrix by adding a 1 to its dimension, which is then removed after multiplication.

Note: The matmul() function does not support scalar multiplication.

Syntax:

numpy.matmul(a, b, out=None)

Parameters

a: This is required. It is the first array_like parameter given as input. Scalars are not accepted.

b: This is required. It is the second array_like parameter given as input.

out: This is optional. It is the output array for the result. None is the default value. It must have the same shape as output if it is provided.

Return Value: 

The matrix product of the given two arrays is returned.

Note: If the last dimension of “a” is not the same size as the second-to-last dimension of b, or if a scalar value is provided in, a ValueError exception is thrown.

NumPy matmul() Function in Python

Example1: For 1D arrays

Matmul python: The function returns the inner product of two 1-D arrays when two 1-D arrays are used.

Approach:

  • Import numpy module using the import keyword.
  • Give the first array as static input and store it in a variable.
  • Give the second array as static input and store it in another variable.
  • Pass the given two array’s as an argument to the matmul() function of numpy module to get the inner product of the given two arrays.
  • Store it in another variable.
  • Print the inner product of the given two arrays.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the first array as static input and store it in a variable.
fst_arry = [1, 9, 2]
# Give the second array as static input and store it in another variable.
scnd_arry = [10, 3, 5]
# Pass the given two array's as an argument to the matmul() function of numpy module 
# to get the inner product of the given two arrays.
# (1*10 + 9*3 + 2*5) = 47
# Store it in another variable.
rslt = np.matmul(fst_arry, scnd_arry)
# Print the inner product of the given two arrays.
print("The inner product of the given two arrays = ", rslt)

Output:

The inner product of the given two arrays = 47

Example2

The function returns matrix multiplication when 2D matrices are used.

Approach:

  • Import numpy module using the import keyword.
  • Pass the random list(2D) as an argument to the array() function to create an array.
  • Store it in a variable.
  • Pass the random list(2D) as an argument to the array() function to create another array.
  • Store it in another variable.
  • Pass the given two array’s as the argument to the matmul() function of numpy module to get the matrix multiplication of given two arrays(matrices).
  • Store it in another variable.
  • Print the matrix multiplication of given two arrays(matrices).
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the random list(2D) as an argument to the array() function 
# to create an array. 
# Store it in a variable.
fst_arry = np.array([[5, 6], 
                     [1, 3]])
# Pass the random list(2D) as an argument to the array() function 
# to create another array. 
# Store it in another variable.
scnd_arry = np.array([[12, 2], 
                      [4, 1]])
# Pass the given two array's as the argument to the matmul() function of numpy module 
# to get the matrix multiplcation of given two arrays(matrices)
# Store it in another variable.
rslt = np.matmul(fst_arry, scnd_arry)
# Print the matrix multiplication of given two arrays(matrices)
print("The matrix multiplication of given two arrays(matrices) = ")
print(rslt)

Output:

The matrix multiplication of given two arrays(matrices) = 
[[84 16]
 [24 5]]

Example3: 2D array+1D array

Approach:

  • Import numpy module using the import keyword.
  • Give the random 2D array as static input and store it in a variable.
  • Give the random 1D array as static input and store it in another variable.
  • Pass the given two array’s as an argument to the matmul() function of numpy module to get the multiplication of the given two arrays and print it.
  • Similarly print, vice-varsa by swapping two arrays.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np 
# Give the random 2D array as static input and store it in a variable.
arry_2D = [[2,1],[3,2]] 
# Give the random 1D array as static input and store it in another variable.
arry_1D = [3,1] 
# Pass the given two array's as an argument to the matmul() function of numpy module 
# to get the multiplication of given two arrays and print it.
# Similarly print, vice-varsa by swapping two arrays
print(np.matmul(arry_2D, arry_1D))
print()
print(np.matmul(arry_1D, arry_2D))

Output:

[ 7 11]

[9 5]