From 1611950fe879e96f3af90f38128977909be36208 Mon Sep 17 00:00:00 2001 From: "Viral B. Shah" Date: Mon, 26 Oct 2026 13:11:07 +0000 Subject: [PATCH 1/2] Return an `n x 1` matrix from `SparseMatrixCSC` of a sparse vector view `SparseMatrixCSC(view(A, :, j))` and `SparseMatrixCSC{Tv}(view(A, :, j))` returned a `SparseVector`; they now return the `n x 1` matrix that `SparseMatrixCSC{Tv,Ti}` of the same view and `SparseMatrixCSC` of a sparse vector already return. Co-Authored-By: Claude Opus 5.5 --- src/sparseconvert.jl | 3 +++ test/constructors.jl | 1 + 2 files changed, 4 insertions(+) diff --git a/src/sparseconvert.jl b/src/sparseconvert.jl index 3f05adcd..4b8ff4a1 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) diff --git a/test/constructors.jl b/test/constructors.jl index 1cc66d3d..43040c4f 100644 --- a/test/constructors.jl +++ b/test/constructors.jl @@ -30,6 +30,7 @@ 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 end @test collect(a) == a @test SparseMatrixCSC(a)::typeof(a) == a From 7535d570841138706df86a3cb1e8608bd1a38dfb Mon Sep 17 00:00:00 2001 From: "Viral B. Shah" Date: Mon, 26 Oct 2026 13:55:38 +0000 Subject: [PATCH 2/2] Throw a MethodError from SparseMatrixCSC of a dense vector view, as Matrix does Co-Authored-By: Claude Opus 5.5 --- src/sparseconvert.jl | 2 ++ test/constructors.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/sparseconvert.jl b/src/sparseconvert.jl index 4b8ff4a1..2678d4bf 100644 --- a/src/sparseconvert.jl +++ b/src/sparseconvert.jl @@ -69,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 43040c4f..053feb63 100644 --- a/test/constructors.jl +++ b/test/constructors.jl @@ -31,6 +31,8 @@ end @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