Skip to content

Commit

Permalink
make issparse handle nested wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Jan 8, 2020
1 parent 9c96131 commit 922a031
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
16 changes: 6 additions & 10 deletions stdlib/SparseArrays/src/abstractsparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,13 @@ julia> issparse(Array(sv))
false
```
"""
issparse(A::AbstractArray) = 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
function SparseArrays.issparse(x::AbstractArray)
p = parent(x)
# Note: For non-wrapper arrays it is defined that `parent(x)===x`, so we use that to check
iswrapper = p !== x
return iswrapper ? issparse(p) : false
end

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

Expand Down
38 changes: 23 additions & 15 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2039,21 +2039,29 @@ end
end

@testset "issparse for specialized matrix types" begin
m = sprand(10, 10, 0.1)
@test issparse(Symmetric(m))
@test issparse(Hermitian(m))
@test issparse(LowerTriangular(m))
@test issparse(LinearAlgebra.UnitLowerTriangular(m))
@test issparse(UpperTriangular(m))
@test issparse(LinearAlgebra.UnitUpperTriangular(m))
@test issparse(adjoint(m))
@test issparse(transpose(m))
@test issparse(Symmetric(Array(m))) == false
@test issparse(Hermitian(Array(m))) == false
@test issparse(LowerTriangular(Array(m))) == false
@test issparse(LinearAlgebra.UnitLowerTriangular(Array(m))) == false
@test issparse(UpperTriangular(Array(m))) == false
@test issparse(LinearAlgebra.UnitUpperTriangular(Array(m))) == false
wraps = [
Symmetric, Hermitian, Diagonal,
LowerTriangular, LinearAlgebra.UnitLowerTriangular,
UpperTriangular, LinearAlgebra.UnitUpperTriangular,
adjoint, transpose, x->view(x, :, :),
]

function test_all(mat, expected)
@test issparse(mat) == expected
for w1 in wraps
@test issparse(w1(mat)) == expected
end
for (w1,w2) in Iterators.product(wraps, wraps)
@test issparse(w1(w2(mat))) == expected
end
for (w1,w2,w3) in Iterators.product(wraps, wraps, wraps)
@test issparse(w1(w2(w3(mat)))) == expected
end
end

test_all(sprand(10, 10, 0.1), true)
test_all((1+im) .* sprand(10, 10, 0.1), true)
test_all(rand(10,10), false)
end

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

0 comments on commit 922a031

Please sign in to comment.