Use cases of `numpy.positive`
Asked Answered
S

2

13

There is a positive function in numpy (version 1.13+), which seemingly does nothing:

In [1]: import numpy as np                                                                               

In [2]: A = np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j, np.inf, -np.inf])                     

In [3]: A == np.positive(A)                                                                              
Out[3]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True])

The documentation says: Returned array or scalar: `y = +x`

What are the use cases of this function?

Snowbound answered 29/3, 2019 at 14:26 Comment(7)
It is the unary + (plus) operator. See, for example, #16819523 and #10748685.Ochlocracy
The doc also says Equivalent to x.copy(), but only defined for types that support arithmetic.. I guess that's it, that look pretty uselessAcuate
Searched GitHub with "np.positive" extension:.py language:Python and there are a lot of hits - many false hits. Sorting it by reently indexed brought some to the top: those look like there being used in unittests.Whimwham
Can it be used to convert everything to numbers?Cure
@Acuate why does someone might want to use np.positive for the purpose of copying, if he has an explicit np.copy? (Apart from the fact that np.positive is a ufunc which means it is written in C.) For me it would look like a bad code style.Snowbound
@SergeyKirienko Nope.Snowbound
It's probably there for completeness. You might want to apply np.negative to some arrays, and np.positive to others, both with the same set of added parameters (order, casting etc). You probably wouldn't use it plain and in isolation, but as part of larger code it might be useful. Why are we allowed to write +12.34 when 12.34 is just as good? Why does Python define a pass that does nothing?Brittle
F
1

There are likely very few use-cases for this function. It is provided because every python operator is exposed as a ufunc in numpy:

  • Unary +: np.positive
  • Unary -: np.negative
  • Binary +: np.add
  • Binary -: np.subtract
  • etc ...

As the documentation states, and noted in the other answer, np.positive makes a copy of the data, just as np.copy does, but with two caveats:

  1. It can change the dtype of the input

  2. It is only defined for arithmetic types. If you attempt to call it on a boolean array, for example, you will get

     UFuncTypeError: ufunc 'positive' did not contain a loop with signature matching types dtype('bool') -> dtype('bool')
    

One other thing, is that since positive is a ufunc, it can work in-place, making it an effective no-op function for arithmetic types:

np.positive(x, out=x)
Fernandez answered 28/9, 2020 at 16:25 Comment(0)
S
0

if you have a vector x, then np.positive(x) gives you, +1*(x) and np.negative(x) gives you -1*(x).

np.positive([-1,0.7])

output: array([-1. ,  0.7])


np.negative([-1.5,0.7])

output:array([ 1.5, -0.7])


np.positive(np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j, np.inf, -np.inf]))

output: array([  0.+0.j,   1.+0.j,  -1.+0.j,   0.+1.j,  -0.-1.j,   1.+1.j,
         1.-1.j,  -1.+1.j,  -1.-1.j,  inf+0.j, -inf+0.j])


np.negative(np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j, np.inf, -np.inf]))

output: array([ -0.-0.j,  -1.-0.j,   1.-0.j,  -0.-1.j,   0.+1.j,  -1.-1.j,
        -1.+1.j,   1.-1.j,   1.+1.j, -inf-0.j,  inf-0.j])


Use Case depends though. Once use case its an alternative of x1 = copy(x). Its creates an duplicate array for your use.

Schug answered 21/4, 2020 at 4:49 Comment(3)
If you wanted a copy, you could have just done x.copy(), or +x if you really want to be confusing.Heideheidegger
I know. ndarray.copy is faster than this operation. One thing is different in this. Its supports dtype. whereas copy doesn't support. So there can be some of the implications while writing complex math algorithm. You can check the details here.Schug
You should use copy where just you want to make a duplicate. But doing some mathematical operation, if you want to copy as well want to take advantage of casting, order and other stuff. Then only you should go for this.Schug

© 2022 - 2025 — McMap. All rights reserved.