11

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?

Yury Kirienko
  • 1,266
  • 1
  • 18
  • 25
  • 3
    It is the [unary + (plus) operator](https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations). See, for example, https://stackoverflow.com/questions/16819023/whats-the-purpose-of-the-pos-unary-operator-in-python and https://stackoverflow.com/questions/10748185/what-does-a-plus-sign-do-in-front-of-a-variable-in-python. – Warren Weckesser Mar 29 '19 at 14:33
  • 2
    The doc also says _Equivalent to `x.copy()`, but only defined for types that support arithmetic._. I guess that's it, that look pretty useless – cglacet Mar 29 '19 at 14:33
  • 1
    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. – wwii Mar 29 '19 at 14:39
  • Can it be used to convert everything to numbers? – Sergey Kirienko Mar 29 '19 at 14:43
  • @cglacet 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. – Yury Kirienko Mar 29 '19 at 14:46
  • @SergeyKirienko Nope. – Yury Kirienko Mar 29 '19 at 14:50
  • 3
    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? – hpaulj Mar 29 '19 at 15:39

2 Answers2

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)
Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
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.

Biswajit
  • 59
  • 6
  • If you wanted a copy, you could have just done `x.copy()`, or `+x` if you really want to be confusing. – user2357112 supports Monica Apr 21 '20 at 04:51
  • 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](https://numpy.org/doc/stable/reference/generated/numpy.positive.html?highlight=positive#numpy.positive). – Biswajit Apr 21 '20 at 05:22
  • 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. – Biswajit Apr 21 '20 at 05:31