-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
The norm of a size zero array should be zero. #5420
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
Comments
For a inf-norm of vectors, the empty array is causing exception.
I think this is the control flow In PR #3861 the cases for |
Personally I'd like to have |
On Monday, January 5, 2015, argriffing notifications@github.com wrote:
Since the first and obvious use would be in linalg.norm perhaps they could |
@maniteja123, yeah that PR has nothing to do with normal ufuncs. It is to allow more things for gufuncs, not sure that it would have any changes to accumulate/reduceat. In that case would need to check if everything is right there. |
@seberg Thanks for giving a clear picture. I couldn't distinguish between |
This sounds great! At first I had hoped that I could add a 'maximum':
Ufunc(2, 1, ReorderableNone,
docstrings.get('numpy.core.umath.maximum'),
'PyUFunc_SimpleBinaryOperationTypeResolver',
TD(noobj),
TD(O, f='npy_ObjectMax')
), and 'absolute':
Ufunc(1, 1, None,
docstrings.get('numpy.core.umath.absolute'),
'PyUFunc_AbsoluteTypeResolver',
TD(bints+flts+timedeltaonly),
TD(cmplx, out=('f', 'd', 'g')),
TD(O, f='PyNumber_Absolute'),
), I think it would look like 'max_abs':
Ufunc(2, 1, Zero,
docstrings.get('numpy.core.umath.max_abs'),
[...],
), but I don't know what goes in the |
Well, those things define the dtype stuff basically. Type resolving should be fine. I think |
I looked through the ufunc tutorial but I couldn't find an example of an ndim-reducing ufunc. I guess these functions are called "reductions" in numpy, and are called aggregate functions, "aggregations" or "aggfuncs" in pandas and other literature. I'm also unsure about the connection between numpy ufunc "reductions" and the "combining function" of a fold. For example, numpy |
@argriffing, Have you looked at loops.c.src? When running a regular ufunc (e.g. |
That looks a lot more complicated than I was hoping. There are 7 loop implementations for |
I think it looks a lot worse than it actually ends up being. Here is a branch where I add a |
Eh, I guess it needs the same special handing in the type resolver that the absolute ufunc has since it throws a warning with complex reductions right now. So a few more than 64 lines. |
Thanks, this looks like a great start! Leaving aside the docstring and the timedelta and complex dtypes, this would need more hacking before it actually does a reduction rather than just a binary operation, right? >>> import numpy as np
>>> a = np.array([[1, 2], [-3, 4]])
>>> np.max_abs(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid number of arguments
>>> np.max_abs(a, axis=0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid number of arguments
>>> np.max_abs(a, -3)
array([[3, 3],
[3, 4]]) Edit: I see that |
No, it works now. Try np.max_abs.reduce(arr) On Tuesday, January 6, 2015, argriffing notifications@github.com wrote:
|
Yes, this is great, it works analogously to |
I should point out, that on a glibc system, this is actually slower than the This means that for this to really be a viable replacement in Alex, do you want to work on this? I made the branch that I linked to above mostly as a demonstration for adding the ufunc since I touched that code before. I can in principle work on it or parts of it, but I don't want to step on your toes. And in fairness, I can't commit to a timeline nor have I written code using vector intrinsics before and I'll probably work on getting scipy/scipy#4249 merged first. |
as implemented non-vectorized code has to branch due to the nan check which likely makes it slower, this could be handled via the fpu flags like the vector code does, but of course its better to just also vectorize it. |
what was the conclusion as to how to handle vectors of size 0? |
I think this is a duplicate of #3763 |
Closing as duplicate as noted by Eric. (inf norm on empty array raising error since it calls |
Just as the empty sum is 0 and the empty product is 1, the empty norm should be 0 in my opinion. In numpy, this convention has been implemented and is unit-tested for the default vector and matrix norms:
Some of the non-default empty norms raise exceptions. The induced matrix 2-norm raises exceptions for two reasons -- first, attempting to compute the singular values of a size-0 matrix raises an exception, and second, even if the the empty array of singular values were to be returned, attempting to compute the max of an empty sequence also raises an exception. The empty induced matrix 1-norm and np.inf-norm each also raise an exception because they try to compute an empty max.
The text was updated successfully, but these errors were encountered: