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
ENH: add squared norm (quadrance) to numpy.linalg. #18250
base: main
Are you sure you want to change the base?
Conversation
some norm cases was rewritten to reuse sqnorm
The test
Unfortunately I don't know how to restart the test. |
Don't worry about it. Because this adds a new function it needs to be run by the mailing list first. My own preference would be to start with an |
ret = add.reduce(absx, axis=axis, keepdims=keepdims) | ||
ret **= (1 / ord) | ||
return ret | ||
return ret ** (1 / ord) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
norm_sq
feels like a weird name when ord != 2
for this case, although I also can't really think of a better name.
I'm not sure that I find the motivation for adding a separate function for this convincing. The accuracy issue in the example given is
The performance improvement amounts to avoiding one square root of a scalar - on the order of 1 us. Neither seems worth going through a lot of trouble for. |
@DPDmancul did this ever hit the mailing list? Also please relate to the comment
|
The main aim of this pull request is to add a function
sqnorm
to easily calculate the squared norm (also called quadrance in an euclidean space or Frobenius norm for matrices) of an array.For notation consistency with
numpy.linalg.norm
this function is also able to return one of eight different matrix norms (possibly squared), or one of an infinite number of vector element powers sum (described in the documentation), depending on the value of theord
parameter.Motivation
When calculating errors it's very common to calculate the squared norm, now there are two solutions: calculate the norm and square it (very common but with a drawback:
norm
applies a (worthless) squared root, so the result can be not very precise) or sum the square of the elements (difficult to read); with the squared norm it's more compact, readable and less prone to approximation errors. Some minimal examples:What can do
The following can be calculated:
Nomenclature
I called it
sqnorm
as a contraction of squared norm, but it could be called in a more meaningful way, since the squared norm is only its main aim, but not all what it does.Other changes
Since
sqnorm
does essentially the same work ofnorm
without extracting the root (except few cases), in this pull request the majority ofnorm
results are based onsqnorm
result in order to avoid double code.