Open In App

NumPy ndarray.__mul__() Method | Element Wise Multiplication of Array

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 the important module in python
import numpy as np
   
# make an array with numpy
gfg = np.array([[1, 2, 3, 4.45, 5],
                [6, 5.5, 4, 3, 2.62]])
   
# applying ndarray.__mul__() method
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
Previous
Next
Share your thoughts in the comments
Similar Reads