# Numpy is absurd

I always gripe about Python not having built-in array type and Numpy doesn't distinguish "vector of vector" from "matrix", but this still surprised me.

`in`

is actually `intersect`

?

It seems that Numpy uses `intersect`

logic to check `a in b`

:

```
In [1]: import numpy as np
In [2]: np.__version__
Out[2]: '1.23.1'
In [3]: np.array([1]) in np.array([1,2,3])
Out[3]: True
In [4]: np.array([1,2,3]) in np.array([1])
Out[4]: True
```

also it doesn't care about `dtype`

at all, so it's `intersect`

but with value comparison:

```
In [5]: np.array([1,2,3,4,5]) in np.array([2.0])
Out[5]: True
In [6]: float(1) in np.array([1,2,3,4,5])
Out[6]: True
```

`in`

is actually `intersect(a.flat, b.flat)`

?

Things only get weired when we go to higher dimensional array:

```
In [7]: 2 in np.array([[1,2], [3, 4]])
Out[7]: True
In [8]: [2] in np.array([[1,2], [3, 4]])
Out[8]: True
In [9]: [[2]] in np.array([[1,2], [3, 4]])
Out[9]: True
In [10]: [[[2]]] in np.array([[1,2], [3, 4]])
Out[10]: True
In [11]: np.array([[[2]]]) in np.array([[1,2], [3, 4]])
Out[11]: True
```

Furthur more, because `intersect`

is commutitive, `in`

is also commutitive!

```
In [12]: np.array([[1,2], [3, 4]]) in np.array([2])
Out[12]: True
```

## Wait, wtf

Finally, just when you throught you understand what Numpy is doing, it seems there's special treatment for `ndarray`

of `len() == 1`

```
In [12]: np.array([1,2,3]) in np.array([2])
Out[12]: True
In [14]: np.array([1,2,3]) in np.array([2, 3])
<ipython-input-32-e5b43569579c>:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
np.array([1,2,3]) in np.array([2, 3])
Out[14]: False
```

But then what is this?

```
In [15]: np.array([99,2,123]) in np.array([2, 30, 50])
Out[15]: False
In [16]: np.array([99,2,123]) in np.array([2, 123, 50])
Out[16]: False
In [17]: np.array([99,2,123]) in np.array([99, 123, 50])
Out[17]: True
```

© Jerry Ling. Last modified: October 15, 2024. Powered by Franklin.jl.