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