question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The norm of a size zero array should be zero.

See original GitHub issue

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:

    def test_empty(self):
        assert_equal(norm([]), 0.0)
        assert_equal(norm(array([], dtype=self.dt)), 0.0)
        assert_equal(norm(atleast_2d(array([], dtype=self.dt))), 0.0)

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.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:20 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
ewmoorecommented, Jan 5, 2015

On Monday, January 5, 2015, argriffing notifications@github.com wrote:

Personally I’d like to have max_abs and sum_abs numpy ufuncs. Unlike max, the max_abs ufunc could have empty max 0. Also these ufuncs could save time and memory, but of course they would bloat the code, pollute the namespace, and invite discussion about which other combinations should be added. Yesterday I looked at adding these myself, but I was too intimidated by the code.

— Reply to this email directly or view it on GitHub https://github.com/numpy/numpy/issues/5420#issuecomment-68746069.

Since the first and obvious use would be in linalg.norm perhaps they could be added, and exposed only through there. If doing so, it might be worth copying the dnrm2 algorthm too instead of calling dot as norm does now.

0reactions
sebergcommented, Nov 18, 2019

Closing as duplicate as noted by Eric. (inf norm on empty array raising error since it calls max([])). It sounds like there may have been more issues here, but those have been resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't C arrays have 0 length?
Especially for variable length arrays it makes perfect sense to have a size of zero every once and a while. It is also...
Read more >
Numpy's linalg.norm() returning 0 on non-zero-vector?
If we're taking the Euclidean norm of an n-dimensional vector, the only case in which the norm can be 0 is when the...
Read more >
Zero Length (Using the GNU Compiler Collection (GCC))
Declaring zero-length arrays is allowed in GNU C as an extension. A zero-length array can be useful as the last element of a...
Read more >
Vector and matrix norms - MATLAB norm - MathWorks
a = [0 3]; b = [-2 1];. Use norm to calculate the distance between the points. d = norm(b-a).
Read more >
numpy.linalg.norm — NumPy v1.24 Manual
This function is able to return one of eight different matrix norms, ... Input array. ... ord{non-zero int, inf, -inf, 'fro', 'nuc'}, optional....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found