np.multiply – Python Numpy multiply() Function

Numpy multiply() Function:

np.multiply: The product of the two NumPy arrays is calculated using the NumPy multiply function. It calculates the element-by-element product of the two arrays, say l1 and l2. The numpy.multiply() function is a universal function, which means it has numerous options that can be used to optimize its performance based on the algorithm’s characteristics.

Syntax:

numpy.multiply(l1, l2, out=None)

Parameters:

l1 and l2: (Required)

np multiply: Required. The arrays that will be multiplicated must be specified. They must be broadcastable to a common shape if l1.shape!= l2.shape

out:

This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

Numpy multiply arrays: The product of l1 and l2 is returned by the Numpy multiply function. The multiply() function of nd-array can be a scalar. It is determined by the l1 and l2 variables. If l1 and l2 are both scalars, then NumPy is the way to go. A scalar value will be returned by multiply(). Otherwise, an nd-array will be returned.

NumPy multiply() Function in Python

Approach:

  • Import NumPy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Create some sample arrays of different shapes to test the multiply() function.
  • Pass the first array and some random number to multiply() function of NumPy module and print the result.
  • Here it multiplies the second argument for each element of the array.
  • Print the result array.
  • Pass the first array and second array to multiply() function of NumPy module and print the result.
  • Here it multiplies the second array elements for the first array element.
  • Print the result array.
  • Similarly, test it with other arrays of different shapes.
  • The Exit of the Program.

Below is the implementation:

# Import NumPy module using the import keyword.
import numpy as np
# Pass some random list as an argument to the array() function to create an array.
# Store it in a variable.
arry1 = np.array([[30, 60], [90, 120], [150, 180]])
# Create some sample arrays of different shapes to test the multiply() function.
arry2 = np.array([[10, 20]])
arry3 = np.array([[100], [200], [300]])
arry4 = np.array([[55, 65], [75, 85], [95, 105]])
# Pass the first array and some random number to multiply() function of NumPy module and print the result.
# Here it multiplies the second argument for each element of the array.
print('Multiplying random number say 10 to the first array : ')
print('Multiplying 10 to first array gives :\n', np.multiply(arry1, 10))
# Print the result array.
# Pass the first array and second array to multiply() function of NumPy module and print the result.
# Here it multiplies the second array elements for the first array element.
# Print the result array.
print('Multiplying first array and second array : ')
print(np.multiply(arry1, arry2))
# Similarly, test it with other arrays of different shapes.
print('Multiplying first array and third array : ')
print(np.multiply(arry1, arry3))
print('Multiplying first array and fourth array : ')
print(np.multiply(arry1, arry4))

Output:

Multiplying random number say 3 to the first array : 
Multiplying 10 to first array gives :
[[ 300 600]
[ 900 1200]
[1500 1800]]
Multiplying first array and second array : 
[[ 300 1200]
[ 900 2400]
[1500 3600]]
Multiplying first array and third array : 
[[ 3000 6000]
[18000 24000]
[45000 54000]]
Multiplying first array and fourth array : 
[[ 1650 3900]
[ 6750 10200]
[14250 18900]]