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

Make issparse handle nested wrappers #34308

Merged
merged 3 commits into from
May 13, 2021
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
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