NumPy reciprocal()

The numpy.reciprocal() function calculates the reciprocal (1/x) of each element in an input array, element-wise.

Syntax

</>
Copy
numpy.reciprocal(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array for which the reciprocal is calculated.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the reciprocal.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the reciprocal values of the input array elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing the Reciprocal of a Single Value

Here, we compute the reciprocal of a single number.

</>
Copy
import numpy as np

# Define a single number
value = 4

# Compute the reciprocal
result = np.reciprocal(value)

# Print the result
print("Reciprocal of", value, ":", result)

Output:

Reciprocal of 4 : 0

Since NumPy performs integer division for integer inputs, the reciprocal of 4 results in 0. Use floating-point numbers for precise results.

2. Computing Reciprocal for an Array of Values

To get accurate results, we use an array of floating-point numbers.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([1.0, 2.0, 4.0, 0.5])

# Compute the reciprocal of each element
reciprocal_values = np.reciprocal(values)

# Print the results
print("Original values:", values)
print("Reciprocal values:", reciprocal_values)

Output:

Original values: [1.  2.  4.  0.5]
Reciprocal values: [1.  0.5  0.25  2. ]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([1.0, 2.0, 3.0])

# Create an output array with the same shape
output_array = np.empty_like(values)

# Compute reciprocal and store the result in output_array
np.reciprocal(values, out=output_array)

# Print the results
print("Computed reciprocal values:", output_array)

Output:

Computed reciprocal values: [1.         0.5        0.33333333]

4. Using the where Parameter

Using a condition to compute the reciprocal only for selected elements.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([1.0, 2.0, 4.0, 0.5])

# Define a mask (compute reciprocal only where mask is True)
mask = np.array([True, False, True, False])

# Compute reciprocal values where mask is True
result = np.reciprocal(values, where=mask)

# Print the results
print("Computed reciprocal values with mask:", result)

Output:

Computed reciprocal values with mask: [1.  2.  0.25 0.5]

The reciprocal values are computed only for elements where mask=True. The other values remain unchanged.