Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add ulab.numpy.absolute function #7995

Closed
jamesra opened this issue May 18, 2023 · 11 comments
Closed

Please add ulab.numpy.absolute function #7995

jamesra opened this issue May 18, 2023 · 11 comments
Labels
enhancement ulab Related to the ulab module
Milestone

Comments

@jamesra
Copy link

jamesra commented May 18, 2023

The abs function from numpy would be nice to add to ulab.numpy. There is a lot of numpy functionality missing for good reason but this function feels particularly fundamental and it would be convenient to have it built-in. Thanks.

@jepler jepler added the ulab Related to the ulab module label May 18, 2023
@github-actions
Copy link

Heads up @v923z - the "ulab" label was applied to this issue.

@dhalbert dhalbert added this to the Third-party milestone May 18, 2023
@v923z
Copy link

v923z commented May 19, 2023

@jamesra The absolute value unary operator was one of the first operators implemented:

https://github.com/v923z/micropython-ulab/blob/3e996d9bd93005edbbb9763c4a0a525fe52dec3d/code/ndarray.c#L1968-L2000
Or do you have something else in mind?

In any case, it's easier to track issues and requests, if you raise them directly in https://github.com/v923z/micropython-ulab/issues.

@jepler
Copy link
Member

jepler commented May 19, 2023

I think the request is for ulab.numpy.abs to exist. It's true that the builtin abs function works on ulab arrays, though.

>>> ulab.numpy.abs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'abs'

@v923z
Copy link

v923z commented May 19, 2023

OK, but then is it worth the flash space? If there is a good reason, we can easily add the function.

@v923z
Copy link

v923z commented May 19, 2023

So, if this is the function in question https://numpy.org/doc/stable/reference/generated/numpy.fabs.html, then the difference to abs is that it can support keyword arguments, which are not supported in ulab, anyway. The only useful argument there is out, I believe, which we could add. But beyond that, I don't see any advantages compared to abs.

On the other hand, math functions always return a floatint point array, while the abs unary operator keeps the dtype.

@jamesra
Copy link
Author

jamesra commented May 19, 2023

This may be a documentation issue instead. abs is only listed on the page for complex arrays. I tried np.abs and np.absolute, but I know I did not try calling it directly off my array. Edit: I didn't think to use the interactive console to run dir(myarray) to find available methods.

np.abs is probably still worth having, but not necessary if it is exposed on the array object itself. (My apologies if abs is documented somewhere else and I just missed it.)

@jamesra
Copy link
Author

jamesra commented May 19, 2023

OK, at the risk of sounding dumb, what is the syntax for the unary absolute value operator in numpy? (I am using 8.1.0-beta 2 to obtain analogbufio on the ESP32-S3)

I launched an interactive Circuit Python session and there is no abs function on the array:

Adafruit CircuitPython 8.1.0-beta.2 on 2023-04-26; Adafruit Feather ESP32S3 4MB Flash 2MB PSRAM with ESP32S3
>>> import ulab.numpy as np
>>> a = np.array([1,-2,3,-4])
>>> dir(a)
['__class__', 'copy', 'sort', 'byteswap', 'dtype', 'flat', 'flatten', 'itemsize', 'reshape', 'shape', 'size', 'strides', 'tobytes', 'tolist', 'transpose']
>>> a.abs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ndarray' object has no attribute 'abs'
>>> a.absolute
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ndarray' object has no attribute 'absolute'

@jepler
Copy link
Member

jepler commented May 19, 2023

MicroPython 8.1.0-beta.2-43-g4901bdceb7-dirty on 2023-05-18; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import ulab.numpy as numpy
>>> abs(numpy.array([-1, 2, -3]))
array([1.0, 2.0, 3.0], dtype=float64)

It corresponds to the abs built-in function. In another post v923z referred to it as a "unary operator", but that reflects some implementation detail of micropython, it's not actually a unary operator like - is in a Python statement like x = -y.

@jamesra
Copy link
Author

jamesra commented May 19, 2023

Oh!!! I'm trying to decide how I feel about that. On one level it makes total sense to use the built-in language operator. On another level it is some lateral thinking to step outside of the numpy library to do one operation... My suggested fix would be documentation. Thank you for explaining the intended use.

@v923z
Copy link

v923z commented May 19, 2023

My suggested fix would be documentation. Thank you for explaining the intended use.

This has actually been documented, unless you have something else in mind: https://micropython-ulab.readthedocs.io/en/latest/ulab-ndarray.html#unary-operators

@dhalbert
Copy link
Collaborator

dhalbert commented Nov 7, 2023

Closing because I think this has been resolved.

@dhalbert dhalbert closed this as completed Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ulab Related to the ulab module
Projects
None yet
Development

No branches or pull requests

4 participants