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

user_type scalars have array-like functionality, and it's weird #520

Open
ofloveandhate opened this issue Dec 12, 2024 · 1 comment
Open

Comments

@ofloveandhate
Copy link

In the EigenPy user_type that's compiled and tested as part of eigenpy, the scalar types end up having numpy-array-like functions, which is weird.

For example,

import user_type
a = user_type.CustomDouble(1) # makes a scalar.  

dir(a) # reveals a ton of functionality that I wouldn't expect for a scalar

For example, we get .flatten, .prod and .cumsum. This seems weird to me. Like, I don't think a Scalar should have these properties. Furthermore, many of these functions produce errors when called.

a.std() # error!

produces the following error message:

---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)
Cell In[12], line 1
----> 1 a.std()

File ~/env/b2/lib/python3.12/site-packages/numpy/_core/_methods.py:227, in _std(a, axis, dtype, out, ddof, keepdims, where, mean)
    225 def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,
    226          where=True, mean=None):
--> 227     ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
    228                keepdims=keepdims, where=where, mean=mean)
    230     if isinstance(ret, mu.ndarray):
    231         ret = um.sqrt(ret, out=ret)

File ~/env/b2/lib/python3.12/site-packages/numpy/_core/_methods.py:173, in _var(a, axis, dtype, out, ddof, keepdims, where, mean)
    168     arrmean = mean
    169 else:
    170     # Compute the mean.
    171     # Note that if dtype is not of inexact type then arraymean will
    172     # not be either.
--> 173     arrmean = umr_sum(arr, axis, dtype, keepdims=True, where=where)
    174     # The shape of rcount has to match arrmean to not change the shape of
    175     # out in broadcasting. Otherwise, it cannot be stored back to arrmean.
    176     if rcount.ndim == 0:
    177         # fast-path for default case when where is True

SystemError: <built-in method reduce of numpy.ufunc object at 0x10768ac40> returned NULL without setting an exception

What am I not understanding? Thanks for your help!

ofloveandhate added a commit to ofloveandhate/eigenpy that referenced this issue Dec 18, 2024
much of the functions I added were me trying to get Python to crash in a specific way.  I failed, which is a good thing, because it means that EigenPy doesn't have the bug I thought it does.

BUT.  EigenPy *does* have two issues exercised in the unit tests for the custom type, issues stack-of-tasks#519  and stack-of-tasks#520 .  Additionally, this code exercises issue stack-of-tasks#521 , where I try to compute vector norms in two different ways and fail.

Additionally, I bumped the C++ standard to C++14, since Boost 1.87 didn't work correctly with only C++11, and 1.87 is now distributed by homebrew (I develop on a Mac)
@jcarpent
Copy link
Contributor

jcarpent commented Jan 9, 2025

I have followed the same strategy as the example provided by NumPy here.
We can further investigate if we really need to inherit from the GenericType of NumPy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants