diff --git a/src/sparseconvert.jl b/src/sparseconvert.jl index 3f05adcd..2678d4bf 100644 --- a/src/sparseconvert.jl +++ b/src/sparseconvert.jl @@ -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) @@ -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 diff --git a/test/constructors.jl b/test/constructors.jl index 1cc66d3d..053feb63 100644 --- a/test/constructors.jl +++ b/test/constructors.jl @@ -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