-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Deprecate countnz in favor of using count(predicate, x) #23485
Conversation
8549ba8
to
c1b2b86
Compare
base/multidimensional.jl
Outdated
@@ -576,7 +576,7 @@ end | |||
|
|||
@generated function findn(A::AbstractArray{T,N}) where {T,N} | |||
quote | |||
nnzA = countnz(A) | |||
nnzA = count(!iszero, A) |
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.
This is inconsistent with the findn
definition above and with its docstring, which states "determined by A[i]!=0
".
6 | ||
``` | ||
""" | ||
countnz(a) = count(x -> x != 0, a) |
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.
Note that the deprecation is different from the current definition, which (as you remarked) is technically breaking. I think the best approach would be to use Base.depwarn
to print a custom deprecation warning proposing both forms, but continue calling x -> x != 0
under the hood.
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've changed the deprecation to suggest both versions following your suggestion.
base/sparse/sparsematrix.jl
Outdated
count(S::SparseMatrixCSC) = count(S.nzval) | ||
nnz(S::SparseMatrixCSC) = Int(S.colptr[S.n + 1] - 1) | ||
count(S::SparseMatrixCSC) = count(S.nzval) | ||
count(f, S::SparseMatrixCSC) = count(f, S.nzval) + f(zero(eltype(S)))*(prod(size(S)) - nnz(S)) |
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.
What's this new method? Should go in a different commit if that's really intended.
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.
This method was missing. There is already a (dense) generic fallback here
Lines 698 to 704 in 5645fb2
function count(pred, a::AbstractArray) | |
n = 0 | |
for i in eachindex(a) | |
@inbounds n += pred(a[i])::Bool | |
end | |
return n | |
end |
count(pred, x)
method is suggested by the deprecation warning so I think it makes sense to add it in this PR.
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.
Got it. Might be worth a word in the commit message.
0e2485a
to
47db98d
Compare
Unfortunately, using |
Define missing specialization of count(pred, x) for sparse arrays
47db98d
to
b659ede
Compare
As discussed in #23005, it is ambiguous which kind of non-zero to test with (
!iszero
ort -> t != 0
) so with this PR the user is required to be explicit. To summarize the differenceThe deprecation suggests
!iszero
as well ast -> t != 0
but still callst -> t != 0
to avoid breaking current behavior. We might want to revisit sparse string arrays which is the only thing I'm aware of that relies on this behavior but that is for a different PR.