NumPy negative()

The numpy.negative() function computes the numerical negative of each element in an input array. It returns an array with the negated values of the input.

Syntax

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

Parameters

ParameterTypeDescription
xarray_like or scalarInput array whose elements will be negated.
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 negate. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the negative function.
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 negated values of the input array elements. If the input is a scalar, a scalar is returned.


Examples

1. Negating a Single Value

Here, we compute the negative of a single scalar value.

</>
Copy
import numpy as np

# Define a single value
value = 5

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

# Print the result
print("Negative of 5:", result)

Output:

Negative of 5: -5

2. Negating an Array of Values

We compute the negative values for multiple numbers stored in an array.

</>
Copy
import numpy as np

# Define an array of numbers
numbers = np.array([1, -3, 5, -7, 9])

# Compute the negative of each element
negative_values = np.negative(numbers)

# Print the results
print("Original numbers:", numbers)
print("Negated numbers:", negative_values)

Output:

Original numbers: [  1  -3   5  -7   9]
Negated numbers: [ -1   3  -5   7  -9]

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 numbers
numbers = np.array([2, -4, 6, -8, 10])

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

# Compute the negative values and store them in output_array
np.negative(numbers, out=output_array)

# Print the results
print("Negated numbers stored in output array:", output_array)

Output:

Negated numbers stored in output array: [ -2   4  -6   8 -10]

4. Using the where Parameter

Using a condition to negate only selected elements.

</>
Copy
import numpy as np

# Define an array of numbers
numbers = np.array([1, -2, 3, -4, 5])

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

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

# Print the results
print("Negated numbers with mask:", result)

Output:

Negated numbers with mask: [                 -1 4602678819172646912                  -3
 4609434218613702656                  -5]

The negation is applied only to elements where mask=True. The other values remain unchanged.