Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to compute numerical negative value for all elements in a...

How to compute numerical negative value for all elements in a given NumPy array?

In this article, we will see how to compute the negative value for all elements in a given NumPy array. So, The negative value is actually the number which when added to any number becomes 0. 

Example: 

If we take a number as 4 then -4 is its negative number because when we add -4 to 4 we get sum as 0. Now let us take another example, Suppose we take a number -6 Now when we add +6 to it then the sum becomes zero. hence +6 is the negative value of -6. Now suppose we have an array of numbers: 
 

A = [1,2,3,-1,-2,-3,0]
So, the negative value of A is 
A'=[-1,-2,-3,1,2,3,0].

So, for finding the numerical negative value of an element we have to use numpy.negative() function of NumPy library.

Syntax: numpy.negative(arr, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘negative’)

Return: [ndarray or scalar] Returned array or scalar = -(input arr or scalar )

Now, let’s see the examples:

Example 1:

Python3




# importing library
import numpy as np
 
# creating a array
x = np.array([-1, -2, -3,
              1, 2, 3, 0])
 
print("Printing the Original array:",
      x)
 
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
 
print("Printing the negative value of the given array:",
      r1)


 Output:

Printing the Original array: [-1 -2 -3  1  2  3  0] 
Printing the negative value of the given array: [ 1  2  3 -1 -2 -3  0] 
 

Example 2:

Python3




# importing library
import numpy as np
 
# creating a numpy 2D array
x = np.array([[1, 2],
              [2, 3]])
 
print("Printing the Original array Content:\n",
      x)
 
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
 
print("Printing the negative value of the given array:\n",
      r1)


 
Output: 

Printing the Original array Content:
[[1 2]
[2 3]]
Printing the negative value of the given array:
[[-1 -2]
[-2 -3]]

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments