-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Let cond
of an empty matrix return zero
#38372
Conversation
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.
I propose to do isempty(A) && return zero(eltype(A))
just before the call to svdvals
.
That would cover also the cases, when svdvals
does not succeed (for example if the element type is BigFloat
).
Also add test cases for cond(ones(BigFloat, 0, 0), p) for p in (1, 2, Inf)
and for cond(ones(10, 0), 2)
and cond(ones(0,3), 2)
Fixes #38327.
943012c
to
6efb075
Compare
What's the rationale for this? Condition numbers are always greater than 1 so returning 0 feels very weird here. |
I think there are at least 3 good reasons.
|
The main motivation for me here was consistency, see also the original description. In fact, I had been skeptical about this giving zero myself. But there is a point to be made why it indeed should be zero: The docstring of But there is also an argument when using the definition |
OK, the part that I find questionable is that the inverse of an empty matrix should be well-defined. But if that is the way it is then indeed this definition is more consistent. |
It seems well defined to me. The empty matrix is a diagonal matrix with 1s on it's diagonals and 0s elsewhere. Thus, the inverse of an empty matrix is itself. |
That makes no sense to me: where are you taking this from? As far as I can see, the semantics julia defines on empty arrays are those of the "neutral" element for reductions, eg sum, prod, det, tr, etc. I would imagine that eg |
Certainly |
Well OK, the algebra of empty matrices is well-defined as the single-element algebra, where every operation maps to the single empty matrix, so from that point of view it makes sense (although I would imagine in practice it would create more bugs than it simplifies code). Cond is different though, and it doesn't seem to me that there is an unambiguous way of defining it. Eg if you take the definition https://wikimedia.org/api/rest_v1/media/math/render/svg/613bce563088d5e506597b022fb9b09461e39199 seriously, you get -inf. It could also plausibly be 1 (the "best case") or +inf (the "worst case"). So it would perhaps be wiser not to define it. |
Yeah, but the choice made for Would you argue that we should also change the behavior for |
Well naively I would err on the side of caution and error (even if p != 2). But if there's precedent for this (both p != 2) and the matlab convention), maybe it makes sense to make this change. It's unlikely to matter in practice, hopefully. |
We have moved the LinearAlgebra stdlib to an external repo: https://github.com/JuliaLang/LinearAlgebra.jl @martinholters If you think that this PR is still relevant, please open a new PR on the LinearAlgebra.jl repo. |
This is also consistent with
opnorm
:Fixes JuliaLang/LinearAlgebra.jl#778.