Skip to content
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

Some corrections to findn #25365

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ This section lists changes that do not have deprecation warnings.
trait; see its documentation for details. Types which support subtraction (operator
`-`) must now implement `widen` for hashing to work inside heterogeneous arrays.

* `findn(x::AbstractVector)` now return a 1-tuple with the vector of indices, to be
consistent with higher order arrays ([#25365]).

Library improvements
--------------------

Expand Down
44 changes: 0 additions & 44 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1800,50 +1800,6 @@ end
find(x::Bool) = x ? [1] : Vector{Int}()
find(testf::Function, x::Number) = !testf(x) ? Vector{Int}() : [1]

findn(A::AbstractVector) = find(A)

"""
findn(A)

Return a vector of indices for each dimension giving the locations of the non-zeros in `A`
(determined by `A[i]!=0`).
If there are no non-zero elements of `A`, return a 2-tuple of empty arrays.

# Examples
```jldoctest
julia> A = [1 2 0; 0 0 3; 0 4 0]
3×3 Array{Int64,2}:
1 2 0
0 0 3
0 4 0

julia> findn(A)
([1, 1, 3, 2], [1, 2, 2, 3])

julia> A = zeros(2,2)
2×2 Array{Float64,2}:
0.0 0.0
0.0 0.0

julia> findn(A)
(Int64[], Int64[])
```
"""
function findn(A::AbstractMatrix)
nnzA = count(t -> t != 0, A)
I = similar(A, Int, nnzA)
J = similar(A, Int, nnzA)
cnt = 1
for j=axes(A,2), i=axes(A,1)
if A[i,j] != 0
I[cnt] = i
J[cnt] = j
cnt += 1
end
end
return (I, J)
end

"""
findnz(A)

Expand Down
28 changes: 27 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -689,13 +689,39 @@ end
# small helper function since we cannot use a closure in a generated function
_countnz(x) = x != 0

"""
findn(A)

Return one vector for each dimension containing indices giving the
locations of the non-zeros in `A` (determined by `A[i] != 0`).

# Examples
```jldoctest
julia> A = [1 2 0; 0 0 3; 0 4 0]
3×3 Array{Int64,2}:
1 2 0
0 0 3
0 4 0

julia> findn(A)
([1, 1, 3, 2], [1, 2, 2, 3])

julia> A = [0 0; 0 0]
2×2 Array{Int64,2}:
0 0
0 0

julia> findn(A)
(Int64[], Int64[])
```
"""
@generated function findn(A::AbstractArray{T,N}) where {T,N}
quote
nnzA = count(_countnz, A)
@nexprs $N d->(I_d = Vector{Int}(uninitialized, nnzA))
k = 1
@nloops $N i A begin
@inbounds if (@nref $N A i) != zero(T)
@inbounds if (@nref $N A i) != 0
@nexprs $N d->(I_d[k] = i_d)
k += 1
end
Expand Down
2 changes: 2 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ end
z[a[1][i],a[2][i],a[3][i]] = 10
end
@test isequal(a,findn(z))

@test findn([1, 0, 2]) == ([1, 3], )
end

@testset "findmin findmax indmin indmax" begin
Expand Down