Skip to content

Commit

Permalink
Make issparse handle nested wrappers (JuliaLang#34308)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed May 13, 2021
1 parent 853a936 commit 55c32b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Standard library changes

* new `sizehint!(::SparseMatrixCSC, ::Integer)` method ([#30676]).
* `cholesky()` now fully preserves the user-specified permutation. ([#40560])

* `issparse` now applies consistently to all wrapper arrays, including nested, by checking `issparse` on the wrapped parent array ([#37644]).

#### Dates

Expand Down
22 changes: 12 additions & 10 deletions stdlib/SparseArrays/src/abstractsparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ julia> issparse(Array(sv))
false
```
"""
issparse(A::AbstractArray) = false
function issparse(A::AbstractArray)
# Handle wrapper arrays: sparse if it is wrapping a sparse array.
# This gets compiled away during specialization.
p = parent(A)
if p === A
# have reached top of wrapping without finding a sparse array, assume it is not.
return false
else
return issparse(p)
end
end
issparse(A::DenseArray) = false
issparse(S::AbstractSparseArray) = true
issparse(S::LinearAlgebra.Adjoint{<:Any,<:AbstractSparseArray}) = true
issparse(S::LinearAlgebra.Transpose{<:Any,<:AbstractSparseArray}) = true

issparse(S::LinearAlgebra.Symmetric{<:Any,<:AbstractSparseMatrix}) = true
issparse(S::LinearAlgebra.Hermitian{<:Any,<:AbstractSparseMatrix}) = true
issparse(S::LinearAlgebra.LowerTriangular{<:Any,<:AbstractSparseMatrix}) = true
issparse(S::LinearAlgebra.UnitLowerTriangular{<:Any,<:AbstractSparseMatrix}) = true
issparse(S::LinearAlgebra.UpperTriangular{<:Any,<:AbstractSparseMatrix}) = true
issparse(S::LinearAlgebra.UnitUpperTriangular{<:Any,<:AbstractSparseMatrix}) = true

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

Expand Down
6 changes: 6 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,12 @@ end
@test issparse(LinearAlgebra.UnitLowerTriangular(Array(m))) == false
@test issparse(UpperTriangular(Array(m))) == false
@test issparse(LinearAlgebra.UnitUpperTriangular(Array(m))) == false
@test issparse(Base.ReshapedArray(m, (20, 5), ()))
@test issparse(@view m[1:3, :])

# greater nesting
@test issparse(Symmetric(UpperTriangular(m)))
@test issparse(Symmetric(UpperTriangular(Array(m)))) == false
end

@testset "issparse for sparse vectors #34253" begin
Expand Down

0 comments on commit 55c32b5

Please sign in to comment.