1

The ufunc documentation states:

where

New in version 1.7. Accepts a boolean array which is broadcast together with the operands. Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.

What is the default behavior, when out is not given?

I observed some behavior, which doesn't really make sense to me:

import numpy as np
a,b = np.ones((2,2))
np.add(a,b,where = False) #returns 0
np.exp(a, where = False)  #returns 1
np.sin(a, where = False)  #returns 1
np.sign(a, where = False) #returns 0
np.reciprocal(a, where = False) #returns 0

Does anyone know the underlying reason/behavior? Especially np.reciprocal doesn't really make sense, as the reciprocal value can never be 0

EDIT: The behavior is even more complex:

a,b = np.ones(2)
np.add(a,b,where = False) #returns 6.0775647498958414e-316
a,b = 1,1
np.add(a,b, where = False) #returns 12301129, 
#running this line several times doesn't give the same result every time...

I'm using Numpy version 1.11.1

Community
  • 1
  • 1
Jürg W. Spaak
  • 2,057
  • 1
  • 15
  • 34
  • You're first line raises a `ValueError`; the other lines also look awkward. Could you make an actual runnable example to test? –  Aug 07 '17 at 09:26
  • NB: it may be my reading, but `False` is not a boolean array. Perhaps the functions cast it to one, but it's not what the documentation states. –  Aug 07 '17 at 09:28
  • I can't reproduce your issue: the second raises `ValueError: Automatic allocation was requested for an iterator operand, and it was flagged as readable, but buffering without delayed allocation was enabled`. That's for NumPy 1.13. –  Aug 07 '17 at 09:31
  • Fun fact: the same line for NumPy 1.11 does not raise an exception, but returns -1.4916681462400413e-154. Definitely smaller than `np.finfo(np.float64).eps`, thus 0, but not an integer. –  Aug 07 '17 at 09:32
  • It's not more complex, it's incorrect because it's being used incorrectly. It is actually a bug in NumPy, and should raise an exception. Not the one in my other comment: that's Python 3 catching a problem in NumPy. Python 2 doesn't catch this problem, and as a result, you get unpredictable (undefined?) behaviour. –  Aug 07 '17 at 09:40
  • You may want to raise an issue on the NumPy issue tracker, and point people to this question. I don't think NumPy should behave as such. –  Aug 07 '17 at 09:41
  • @MSeifert: I see you've edited the question. Any suggestion perhaps? –  Aug 07 '17 at 09:42
  • 2
    It's tricky because `where` without `out` simply doesn't make much sense because neither the ufunc-identity nor zero make any sense as "default value" in the returned array. I guess it's just allocating an `np.empty_like(a)` out array and then you're stuck with "random values" especially if you define `where=False` and don't put any values in the newly created array. – MSeifert Aug 07 '17 at 09:44

1 Answers1

5

It looks like garbage becasue that's exactly what it is - memory that's been garbage collected.

Whatever function you are calling sets aside a block of memory to put the results in, but never puts any results there because where=False. You're getting the same values you would from np.empty - i.e. whatever garbage was in that memory block before the function assigned it.

Daniel F
  • 13,620
  • 2
  • 29
  • 55