NumPy ndarray.__mul__() Method | Element Wise Multiplication of Array
The ndarray.__mul__() method does an element-wise multiplication of NumPy ndarray to a particular value that is provided as the parameter.
Example
Python3
import numpy as np
gfg = np.array([ 1 , 2.5 , 3 , 4.8 , 5 ])
print (gfg.__mul__( 5 ))
|
Output
[ 5. 12.5 15. 24. 25. ]
Syntax
Syntax: ndarray.__mul__($self, value, /)
Parameters
- self: The array on which the method is called.
- value: The value or array to multiply with.
Return: New array with elements = self * values
How to Multiply an ndarray with a Value
To perform an element-wise multiplication of an ndarray with a given value we use ndarray.__mul__ method of NumPy library.
Let us understand it better with an example:
Example
In this example, we can see that each element in an array is multiplied with the value given as a parameter in method ndarray.__mul__(). This method will work fine for positive, negative, and floating point values of an array.
Python3
import numpy as np
gfg = np.array([[ 1 , 2 , 3 , 4.45 , 5 ],
[ 6 , 5.5 , 4 , 3 , 2.62 ]])
print (gfg.__mul__( 5 ))
|
Output
[[ 5. 10. 15. 22.25 25. ]
[ 30. 27.5 20. 15. 13.1 ]]
Last Updated :
05 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...