Skip to content

Commit

Permalink
Deprecate findn(x) in favor of find(!iszero, x), which now returns ca…
Browse files Browse the repository at this point in the history
…rtesian indices
  • Loading branch information
nalimilan committed Jan 12, 2018
1 parent a16a526 commit 4a27baf
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 105 deletions.
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ 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 returns a 1-tuple with the vector of indices, to be
consistent with higher order arrays ([#25365]).
* `findn(x::AbstractArray)` has been deprecated in favor of `find(!iszero, x)`, which
now returns cartesian indices for multidimensional arrays (see below, [#25532]).

* `find` now returns the same type of indices as `keys`/`pairs` for `AbstractArray`,
`AbstractDict`, `AbstractString`, `Tuple` and `NamedTuple` objects ([#24774]).
Expand Down Expand Up @@ -1191,3 +1191,4 @@ Command-line option changes
[#25231]: https://github.com/JuliaLang/julia/issues/25231
[#25365]: https://github.com/JuliaLang/julia/issues/25365
[#25424]: https://github.com/JuliaLang/julia/issues/25424
[#25532]: https://github.com/JuliaLang/julia/issues/25532
10 changes: 3 additions & 7 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1629,9 +1629,10 @@ function find(B::BitArray)
return I
end

findn(B::BitVector) = find(B)
# For performance
find(::typeof(!iszero), B::BitArray) = find(B)

function findn(B::BitMatrix)
function findnz(B::BitMatrix)
nnzB = count(B)
I = Vector{Int}(uninitialized, nnzB)
J = Vector{Int}(uninitialized, nnzB)
Expand All @@ -1643,11 +1644,6 @@ function findn(B::BitMatrix)
cnt += 1
end
end
return I, J
end

function findnz(B::BitMatrix)
I, J = findn(B)
return I, J, trues(length(I))
end

Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2785,6 +2785,8 @@ end

@deprecate findin(a, b) find(occursin(b), a)

@deprecate findn(x::AbstractArray) (I = find(!iszero, x); (getindex.(I, 1), getindex.(I, 2)))


# END 0.7 deprecations

Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ export
findmin,
findmin!,
findmax!,
findn,
findnext,
findprev,
findnz,
Expand Down
64 changes: 0 additions & 64 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -686,51 +686,6 @@ 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) != 0
@nexprs $N d->(I_d[k] = i_d)
k += 1
end
end
@ntuple $N I
end
end


# see discussion in #18364 ... we try not to widen type of the resulting array
# from cumsum or cumprod, but in some cases (+, Bool) we may not have a choice.
rcum_promote_type(op, ::Type{T}, ::Type{S}) where {T,S<:Number} = promote_op(op, T, S)
Expand Down Expand Up @@ -1596,25 +1551,6 @@ end
end
end

## findn

@generated function findn(B::BitArray{N}) where N
quote
nnzB = count(B)
I = ntuple(x->Vector{Int}(uninitialized, nnzB), Val($N))
if nnzB > 0
count = 1
@nloops $N i B begin
if (@nref $N B i) # TODO: should avoid bounds checking
@nexprs $N d->(I[d][count] = i_d)
count += 1
end
end
end
return I
end
end

## isassigned

@generated function isassigned(B::BitArray, I_0::Int, I::Int...)
Expand Down
1 change: 0 additions & 1 deletion doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ Base.circshift!
Base.circcopy!
Base.find(::Any)
Base.find(::Function, ::Any)
Base.findn
Base.findnz
Base.findfirst(::Any)
Base.findfirst(::Function, ::Any)
Expand Down
27 changes: 0 additions & 27 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,6 @@ end
@test find(isascii, g) == Int[]
@test find(!iszero, (i % 2 for i in 1:10)) == 1:2:9
end
@testset "findn" begin
b = findn(fill(1,2,2,2,2))
@test (length(b[1]) == 16)
@test (length(b[2]) == 16)
@test (length(b[3]) == 16)
@test (length(b[4]) == 16)

#hand made case
a = ([2,1,2],[1,2,2],[2,2,2])
z = zeros(2,2,2)
for i = 1:3
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
@test indmax([10,12,9,11]) == 2
Expand Down Expand Up @@ -1852,16 +1835,6 @@ end
fill!(B, 2)
@test all(x->x==2, B)

iall = repmat(1:size(A,1), 1, size(A,2))
jall = repmat((1:size(A,2))', size(A,1), 1)
i,j = findn(B)
@test vec(i) == vec(iall)
@test vec(j) == vec(jall)
fill!(S, 2)
i,j = findn(S)
@test vec(i) == vec(iall)
@test vec(j) == vec(jall)

copyto!(B, A)
copyto!(S, A)

Expand Down
1 change: 1 addition & 0 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ timesofar("datamove")

b1 = bitrand(n1, n2)
@check_bit_operation find(b1) Vector{CartesianIndex{2}}
@check_bit_operation find(!iszero, b1) Vector{CartesianIndex{2}}
@check_bit_operation findnz(b1) Tuple{Vector{Int}, Vector{Int}, BitArray}
end

Expand Down
5 changes: 2 additions & 3 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,8 @@ pmax, ipmax = findmax(parent(A))
@test A[iamax] == amax
@test amax == parent(A)[ipmax]
z = OffsetArray([0 0; 2 0; 0 0; 0 0], (-3,-1))
I,J = findn(z)
@test I == [-1]
@test J == [0]
I = find(!iszero, z)
@test I == [CartesianIndex(-1, 0)]
I,J,N = findnz(z)
@test I == [-1]
@test J == [0]
Expand Down

0 comments on commit 4a27baf

Please sign in to comment.