Skip to content

Commit 55c32b5

Browse files
authored
Make issparse handle nested wrappers (#34308)
1 parent 853a936 commit 55c32b5

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Standard library changes
117117

118118
* new `sizehint!(::SparseMatrixCSC, ::Integer)` method ([#30676]).
119119
* `cholesky()` now fully preserves the user-specified permutation. ([#40560])
120-
120+
* `issparse` now applies consistently to all wrapper arrays, including nested, by checking `issparse` on the wrapped parent array ([#37644]).
121121

122122
#### Dates
123123

stdlib/SparseArrays/src/abstractsparse.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,19 @@ julia> issparse(Array(sv))
5050
false
5151
```
5252
"""
53-
issparse(A::AbstractArray) = false
53+
function issparse(A::AbstractArray)
54+
# Handle wrapper arrays: sparse if it is wrapping a sparse array.
55+
# This gets compiled away during specialization.
56+
p = parent(A)
57+
if p === A
58+
# have reached top of wrapping without finding a sparse array, assume it is not.
59+
return false
60+
else
61+
return issparse(p)
62+
end
63+
end
64+
issparse(A::DenseArray) = false
5465
issparse(S::AbstractSparseArray) = true
55-
issparse(S::LinearAlgebra.Adjoint{<:Any,<:AbstractSparseArray}) = true
56-
issparse(S::LinearAlgebra.Transpose{<:Any,<:AbstractSparseArray}) = true
57-
58-
issparse(S::LinearAlgebra.Symmetric{<:Any,<:AbstractSparseMatrix}) = true
59-
issparse(S::LinearAlgebra.Hermitian{<:Any,<:AbstractSparseMatrix}) = true
60-
issparse(S::LinearAlgebra.LowerTriangular{<:Any,<:AbstractSparseMatrix}) = true
61-
issparse(S::LinearAlgebra.UnitLowerTriangular{<:Any,<:AbstractSparseMatrix}) = true
62-
issparse(S::LinearAlgebra.UpperTriangular{<:Any,<:AbstractSparseMatrix}) = true
63-
issparse(S::LinearAlgebra.UnitUpperTriangular{<:Any,<:AbstractSparseMatrix}) = true
6466

6567
indtype(S::AbstractSparseArray{<:Any,Ti}) where {Ti} = Ti
6668

stdlib/SparseArrays/test/sparse.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,12 @@ end
21652165
@test issparse(LinearAlgebra.UnitLowerTriangular(Array(m))) == false
21662166
@test issparse(UpperTriangular(Array(m))) == false
21672167
@test issparse(LinearAlgebra.UnitUpperTriangular(Array(m))) == false
2168+
@test issparse(Base.ReshapedArray(m, (20, 5), ()))
2169+
@test issparse(@view m[1:3, :])
2170+
2171+
# greater nesting
2172+
@test issparse(Symmetric(UpperTriangular(m)))
2173+
@test issparse(Symmetric(UpperTriangular(Array(m)))) == false
21682174
end
21692175

21702176
@testset "issparse for sparse vectors #34253" begin

0 commit comments

Comments
 (0)