2

I'm working with some position vectors. I am operating each position with each other position and am using matrices to do it as efficiently as I can. I encountered a problem with my most recent version where it gives me a warning: RuntimeWarning: invalid value encountered in sqrt return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))

An example of some code that gives me this warning is below.

This warning is caused by np.linalg.norm and only happens when I specify a data type for the array, it also only happens in the example code below when I have more than 90 vectors.

Is this a NumPy bug, a known limitation in NumPy, or am I doing something wrong?

x = np.full((100, 3), 1)  # Create an array of vectors, in this case all [1, 1, 1]
ps, qs = np.broadcast_arrays(x, np.expand_dims(x, 1))  # Created so that I can operate each vector on each other vector.
z = np.subtract(ps, qs, dtype=np.float32)  # Get the difference between them.
np.linalg.norm(z, axis=2)  # Get the magnitude of the difference.
Xorgon
  • 498
  • 1
  • 5
  • 20
  • 1
    [Can't reproduce.](https://ideone.com/XVUwXR) I get no RuntimeWarning when I run this code. – user2357112 Aug 10 '18 at 19:02
  • @user2357112 Interesting, any idea why an environment difference could cause the warning to appear? – Xorgon Aug 10 '18 at 19:04
  • 1
    @user2357112 I can reproduce. Numpy 1.14.5 and Windows 7. Presumably by ints being 32 bit by default? – roganjosh Aug 10 '18 at 19:09
  • 1
    No, I'm not so sure it's that. But I can reproduce. – roganjosh Aug 10 '18 at 19:12
  • @AhmedFasih `\Anaconda3\lib\site-packages\numpy\linalg\linalg.py:2287`. For me, anyway. – roganjosh Aug 10 '18 at 19:46
  • 1
    Very strange. `norm` will try that `sqrt(add.reduce...` call after it squares the array's values ([source](https://github.com/numpy/numpy/blob/ccfbcc1cd9a4035a467f2e982a565ab27de25b6b/numpy/linalg/linalg.py#L2378)), so even if `z` somehow has a negative-0 float32 value, it should become positive by the time that `sqrt` happens. Just in case, can you post the output of `np.min(z)`? – Ahmed Fasih Aug 10 '18 at 19:48
  • @AhmedFasih for me it's `0.0`. That's in Spyder, though. Give me a few min to check outside of Spyder. – roganjosh Aug 10 '18 at 19:50
  • 1
    Same in cmd with Python 3.6.3, though still technically Anaconda (I have absolutely no idea if that's relevant) – roganjosh Aug 10 '18 at 19:57
  • 2
    I a bug issue for certain builds with Anacoda and MKL (general web search for np.sqrt) – hpaulj Aug 11 '18 at 00:22
  • @AhmedFasih I did some tracing of it before and tested that the whole array passed into sqrt is greater than or equal to zero with no NaN values. – Xorgon Aug 11 '18 at 11:53
  • What was the final resolution? Was there a bug that is now fixed? I had a similar problem: https://stackoverflow.com/questions/63234390/runtimewarning-invalid-value-encountered-in-sqrt – Joe Aug 10 '20 at 20:41
  • @Joe I *think* I just ignored the warning and it seemed to go okay. – Xorgon Aug 11 '20 at 02:47

1 Answers1

-1

You should make sure that Z doesn't contain any negative value! test if you have negative values:

print len([_ for _ in z if _ < 0])
  • It's buried in the comments, but I did test that all values are greater than or equal to zero with no NaN values. – Xorgon Feb 28 '19 at 17:05