Skip to content

Commit 3e4ab51

Browse files
fredrikekreandreasnoack
authored andcommitted
fix deprecation warning for findn(::AbstractVector) (#25365)
adjust documentation for findn (fix #25343) remove special special cases for findn(::AbstractArray{T,(1|2)})
1 parent caf31eb commit 3e4ab51

File tree

4 files changed

+32
-45
lines changed

4 files changed

+32
-45
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ This section lists changes that do not have deprecation warnings.
359359
trait; see its documentation for details. Types which support subtraction (operator
360360
`-`) must now implement `widen` for hashing to work inside heterogeneous arrays.
361361

362+
* `findn(x::AbstractVector)` now return a 1-tuple with the vector of indices, to be
363+
consistent with higher order arrays ([#25365]).
364+
362365
Library improvements
363366
--------------------
364367

base/array.jl

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,50 +1800,6 @@ end
18001800
find(x::Bool) = x ? [1] : Vector{Int}()
18011801
find(testf::Function, x::Number) = !testf(x) ? Vector{Int}() : [1]
18021802

1803-
findn(A::AbstractVector) = find(A)
1804-
1805-
"""
1806-
findn(A)
1807-
1808-
Return a vector of indices for each dimension giving the locations of the non-zeros in `A`
1809-
(determined by `A[i]!=0`).
1810-
If there are no non-zero elements of `A`, return a 2-tuple of empty arrays.
1811-
1812-
# Examples
1813-
```jldoctest
1814-
julia> A = [1 2 0; 0 0 3; 0 4 0]
1815-
3×3 Array{Int64,2}:
1816-
1 2 0
1817-
0 0 3
1818-
0 4 0
1819-
1820-
julia> findn(A)
1821-
([1, 1, 3, 2], [1, 2, 2, 3])
1822-
1823-
julia> A = zeros(2,2)
1824-
2×2 Array{Float64,2}:
1825-
0.0 0.0
1826-
0.0 0.0
1827-
1828-
julia> findn(A)
1829-
(Int64[], Int64[])
1830-
```
1831-
"""
1832-
function findn(A::AbstractMatrix)
1833-
nnzA = count(t -> t != 0, A)
1834-
I = similar(A, Int, nnzA)
1835-
J = similar(A, Int, nnzA)
1836-
cnt = 1
1837-
for j=axes(A,2), i=axes(A,1)
1838-
if A[i,j] != 0
1839-
I[cnt] = i
1840-
J[cnt] = j
1841-
cnt += 1
1842-
end
1843-
end
1844-
return (I, J)
1845-
end
1846-
18471803
"""
18481804
findnz(A)
18491805

base/multidimensional.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,13 +689,39 @@ end
689689
# small helper function since we cannot use a closure in a generated function
690690
_countnz(x) = x != 0
691691

692+
"""
693+
findn(A)
694+
695+
Return one vector for each dimension containing indices giving the
696+
locations of the non-zeros in `A` (determined by `A[i] != 0`).
697+
698+
# Examples
699+
```jldoctest
700+
julia> A = [1 2 0; 0 0 3; 0 4 0]
701+
3×3 Array{Int64,2}:
702+
1 2 0
703+
0 0 3
704+
0 4 0
705+
706+
julia> findn(A)
707+
([1, 1, 3, 2], [1, 2, 2, 3])
708+
709+
julia> A = [0 0; 0 0]
710+
2×2 Array{Int64,2}:
711+
0 0
712+
0 0
713+
714+
julia> findn(A)
715+
(Int64[], Int64[])
716+
```
717+
"""
692718
@generated function findn(A::AbstractArray{T,N}) where {T,N}
693719
quote
694720
nnzA = count(_countnz, A)
695721
@nexprs $N d->(I_d = Vector{Int}(uninitialized, nnzA))
696722
k = 1
697723
@nloops $N i A begin
698-
@inbounds if (@nref $N A i) != zero(T)
724+
@inbounds if (@nref $N A i) != 0
699725
@nexprs $N d->(I_d[k] = i_d)
700726
k += 1
701727
end

test/arrayops.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ end
481481
z[a[1][i],a[2][i],a[3][i]] = 10
482482
end
483483
@test isequal(a,findn(z))
484+
485+
@test findn([1, 0, 2]) == ([1, 3], )
484486
end
485487

486488
@testset "findmin findmax indmin indmax" begin

0 commit comments

Comments
 (0)