Skip to content
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
5 changes: 5 additions & 0 deletions src/sparseconvert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ for wr in (Symmetric, Hermitian, Transpose, Adjoint,
@eval SparseMatrixCSC{Tv}(A::$wr) where Tv = SparseMatrixCSC{Tv}(_sparsem(A))
@eval SparseMatrixCSC{Tv,Ti}(A::$wr) where {Tv,Ti} = SparseMatrixCSC{Tv,Ti}(_sparsem(A))
end
# a vector view converts to an `n x 1` matrix, as a sparse vector does
SparseMatrixCSC(A::SubArray{<:Any,1}) = SparseMatrixCSC(_sparsem(A))
SparseMatrixCSC{Tv}(A::SubArray{Tv,1}) where Tv = SparseMatrixCSC(_sparsem(A))

"""
iswrsparse(::S)
Expand Down Expand Up @@ -66,6 +69,8 @@ function _sparsem(@nospecialize A::AbstractArray{Tv}) where Tv
A
end
else
# a dense vector has no matrix constructor, as `Matrix(v)` has none
A isa AbstractMatrix || throw(MethodError(SparseMatrixCSC, (A,)))
# explicitly call abstract matrix fallback using getindex(A,...)
invoke(SparseMatrixCSC{Tv,Int}, Tuple{AbstractMatrix}, A)
end
Expand Down
3 changes: 3 additions & 0 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ end
@static if COMPREHENSIVE
@test SparseMatrixCSC{ComplexF64}(Array(a)')::SparseMatrixCSC{ComplexF64,Int} == a'
@test Array(SparseMatrixCSC{eltype(a), Int8}(a)) == Array(a)
@test mismatch(SparseMatrixCSC(view(a, :, 1)), Array(a)[:, 1:1]) === nothing
# a view of a dense vector has no matrix constructor, as for `Matrix`
@test_throws MethodError SparseMatrixCSC(view([1.0, 2.0], :))
end
@test collect(a) == a
@test SparseMatrixCSC(a)::typeof(a) == a
Expand Down
Loading