The source code corresponding to numpy.invert.

I noticed the following documentation on `numpy.invert`: [1] ######## numpy.invert [...] Compute bit-wise inversion, or bit-wise NOT, element-wise. Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ~. [...] The ~ operator can be used as a shorthand for np.invert on ndarrays. x1 = np.array([True, False]) ~x1 array([False, True]) ######## So, C/Python operator `~` has been overridden by the corresponding user function in numpy, but where is the corresponding source code implementation? [1] https://numpy.org/doc/stable/reference/generated/numpy.invert.html#numpy-inv... Regards, HZ

On Sun, Oct 3, 2021 at 9:27 PM <hongyi.zhao@gmail.com> wrote:
ufuncs are implemented in C. We provide so-called loop functions that iterate over contiguous segments of the operand arrays. We use a custom code generator to make implementations for all of the types. Here are the ones that correspond to `np.invert`. (the bool implementation uses the `logical_not` loop). https://github.com/numpy/numpy/blob/main/numpy/core/src/umath/loops.c.src#L6... -- Robert Kern

(the bool implementation uses the `logical_not` loop).
Do you the following code snippet: https://github.com/numpy/numpy/blob/3c1e9b4717b2eb33a2bf2d495006bc300f5b8765...

On Mon, Oct 4, 2021 at 12:09 AM <hongyi.zhao@gmail.com> wrote:
This is the one that gets expaneded to `BOOL_logical_not`: https://github.com/numpy/numpy/blob/main/numpy/core/src/umath/loops.c.src#L4... -- Robert Kern

Thank you for pointing this out. This is the code block which includes the first appearance of the keyword `logical_not`. BTW, why can't the ~ operator be tested equal to 'np.invert', as shown below: ``` In [1]: import numpy as np In [3]: np.invert is np.bitwise_not Out[3]: True In [4]: np.invert is ~ File "<ipython-input-4-32abf1603b17>", line 1 np.invert is ~ ^ SyntaxError: invalid syntax ```

On Mon, Oct 4, 2021 at 1:09 AM <hongyi.zhao@gmail.com> wrote:
That’s just the way Python’s syntax works. Operators are not names that can be resolved to objects that can be compared with the `is` operator. Instead, when that operator is evaluated in an expression, the Python interpreter will look up a specially-named method on the operand object (in this case `__invert__`). Numpy array objects implement this method using `np.invert`. -- Robert Kern

On Mon, Oct 4, 2021 at 9:33 PM Robert Kern <robert.kern@gmail.com> wrote:
It seems that the above calling/invoking logic/mechanism is not so clear or easy to understand/figure out only by reading the document, say, by the following commands in IPython: import numpy as np help(np.invert) np.invert? np.info(np.invert) Regards, HY

On 10/4/21 10:07 AM, Hongyi Zhao wrote:
You probably want to read the Python Language Reference regarding "Special Methods": https://docs.python.org/3.9/reference/datamodel.html#special-method-names <https://docs.python.org/3.9/reference/datamodel.html#special-method-names> HTH, Steve

On Sun, Oct 3, 2021 at 9:27 PM <hongyi.zhao@gmail.com> wrote:
ufuncs are implemented in C. We provide so-called loop functions that iterate over contiguous segments of the operand arrays. We use a custom code generator to make implementations for all of the types. Here are the ones that correspond to `np.invert`. (the bool implementation uses the `logical_not` loop). https://github.com/numpy/numpy/blob/main/numpy/core/src/umath/loops.c.src#L6... -- Robert Kern

(the bool implementation uses the `logical_not` loop).
Do you the following code snippet: https://github.com/numpy/numpy/blob/3c1e9b4717b2eb33a2bf2d495006bc300f5b8765...

On Mon, Oct 4, 2021 at 12:09 AM <hongyi.zhao@gmail.com> wrote:
This is the one that gets expaneded to `BOOL_logical_not`: https://github.com/numpy/numpy/blob/main/numpy/core/src/umath/loops.c.src#L4... -- Robert Kern

Thank you for pointing this out. This is the code block which includes the first appearance of the keyword `logical_not`. BTW, why can't the ~ operator be tested equal to 'np.invert', as shown below: ``` In [1]: import numpy as np In [3]: np.invert is np.bitwise_not Out[3]: True In [4]: np.invert is ~ File "<ipython-input-4-32abf1603b17>", line 1 np.invert is ~ ^ SyntaxError: invalid syntax ```

On Mon, Oct 4, 2021 at 1:09 AM <hongyi.zhao@gmail.com> wrote:
That’s just the way Python’s syntax works. Operators are not names that can be resolved to objects that can be compared with the `is` operator. Instead, when that operator is evaluated in an expression, the Python interpreter will look up a specially-named method on the operand object (in this case `__invert__`). Numpy array objects implement this method using `np.invert`. -- Robert Kern

On Mon, Oct 4, 2021 at 9:33 PM Robert Kern <robert.kern@gmail.com> wrote:
It seems that the above calling/invoking logic/mechanism is not so clear or easy to understand/figure out only by reading the document, say, by the following commands in IPython: import numpy as np help(np.invert) np.invert? np.info(np.invert) Regards, HY

On 10/4/21 10:07 AM, Hongyi Zhao wrote:
You probably want to read the Python Language Reference regarding "Special Methods": https://docs.python.org/3.9/reference/datamodel.html#special-method-names <https://docs.python.org/3.9/reference/datamodel.html#special-method-names> HTH, Steve
participants (4)
-
Hongyi Zhao
-
hongyi.zhao@gmail.com
-
Robert Kern
-
Stephen Waterbury