-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Add constant function to ufunc? #13577
Comments
@hanzckernel - I'm a bit confused about what you want to achieve precisely. In what way is |
@mhvk Sorry for not making it clear. The problem here is when others have potential to modify my function from constant function to non-constant function (or the other way around) it will create a great mess. for instance if I have: def f(x, y): return 5 * x there might be cases when I actually encounter this problem when writing some function while implementing a class, when I do not know if certain methods is function which like As mentioned use And also from a math perspective, as |
|
@seberg Thanks a lot! That seems the best solution I have other than customizing ufunc from beginning. After digging a bit in performance: arr = np.arange(1, 2, 0.0001).reshape(10, -1)
def master_f(x): return np.broadcast_arrays(x, f(x))[1].copy('K')
def master_f_nocopy(x): return np.broadcast_arrays(x, f(x))[1]
%timeit arr+1 # this takes about 10microsec
%timeit master_f(arr) # this takes about 40 mircrosec
%timeit master_f_nocopy(arr) # this takes about 20 microsec But still a great improvement in comparison with plain Followup Question: is there any reason this broadcast is not built in Nonetheless, I shall consider this case closed. My thanks again. |
@seberg And just to make things complete |
You could use Frankly, I am not sure that answered all your questions? |
@seberg Thanks that's really helpful. My original intention was merely to write a thin wrapper for my function to force them broadcast, while maintaining the readability. That was why I dig in ufunc in the first place, as my first thought would be to create some subclass to overwrite But after all it was just me being ignorant. Moreover, though |
I was wondering if there is a reason that constant function not included in built-in universal function. (Or am I just blind?)
This can cause a problem when we want to force broadcasting a function to keep dimension. As for functions like
lambda x: 5
andlambda x, y: y
will automatically reduce dimension.Currently this can only be achieved by methods like
np.vectorize
ornp.frompyfunc
which are both not very efficient.np.full
ornp.full_like
surely does the trick, but then one needs give arguments likex.shape
etc., and can cause a problem when later these constant function/projection function being rewritten.by passing constant function we can set
keepdims = False
by default and those who wants to force broadcasting can overwrite it to beTrue
.If it's designed not to include constant function to ufunc, can someone enlighten me why is that so?
The text was updated successfully, but these errors were encountered: