-
-
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
make count(f,itr) count the number of nonzero; countnz(itr) -> count(itr) #20405
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -531,7 +531,6 @@ export | |
minmax, | ||
ndims, | ||
nonzeros, | ||
countnz, | ||
ones, | ||
parent, | ||
parentindexes, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,7 +348,7 @@ julia> sum(1:20) | |
``` | ||
""" | ||
sum(a) = mapreduce(identity, +, a) | ||
sum(a::AbstractArray{Bool}) = countnz(a) | ||
sum(a::AbstractArray{Bool}) = count(a) | ||
|
||
|
||
# Kahan (compensated) summation: O(1) error growth, at the expense | ||
|
@@ -637,42 +637,37 @@ function contains(eq::Function, itr, x) | |
return false | ||
end | ||
|
||
|
||
## countnz & count | ||
|
||
""" | ||
count(p, itr) -> Integer | ||
count(itr) | ||
|
||
Count the number of elements in `itr` for which predicate `p` returns `true`. | ||
Count the number of elements in `itr` for which the function `p` returns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would make sense to update |
||
a nonzero value (or returns `true`, for boolean `p`) according to the | ||
[`iszero`](@ref) function. `count(itr)` is equivalent to `p = identity`, | ||
i.e. it counts the number of nonzero (or `true`) elements of `itr`. | ||
|
||
```jldoctest | ||
julia> count(i->(4<=i<=6), [2,3,4,5,6]) | ||
3 | ||
``` | ||
""" | ||
function count(pred, itr) | ||
n = 0 | ||
for x in itr | ||
n += pred(x) | ||
end | ||
return n | ||
end | ||
|
||
""" | ||
countnz(A) -> Integer | ||
|
||
Counts the number of nonzero values in array `A` (dense or sparse). Note that this is not a constant-time operation. | ||
For sparse matrices, one should usually use [`nnz`](@ref), which returns the number of stored values. | ||
|
||
```jldoctest | ||
julia> A = [1 2 4; 0 0 1; 1 1 0] | ||
3×3 Array{Int64,2}: | ||
1 2 4 | ||
0 0 1 | ||
1 1 0 | ||
|
||
julia> countnz(A) | ||
julia> count(A) | ||
6 | ||
``` | ||
""" | ||
countnz(a) = count(x -> x != 0, a) | ||
function count end | ||
|
||
function count(pred, itr) | ||
n = 0 | ||
for x in itr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really related, but should we have a more efficient version for |
||
n += !iszero(pred(x)) | ||
end | ||
return n | ||
end | ||
|
||
count(itr) = count(identity, itr) |
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.
Repeat
-> Integer
? BTW, should be written as::Integer
now.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 don't think most of the Base docstrings have made the
::
switch yet. IMO better to wait and do them all at once, keeping->
in the interim.