diff --git a/ext/FillArraysSparseArraysExt.jl b/ext/FillArraysSparseArraysExt.jl index 6021296..b75814b 100644 --- a/ext/FillArraysSparseArraysExt.jl +++ b/ext/FillArraysSparseArraysExt.jl @@ -5,6 +5,7 @@ using SparseArrays: SparseVectorUnion import Base: convert, kron using FillArrays using FillArrays: RectDiagonalFill, RectOrDiagonalFill, ZerosVector, ZerosMatrix, getindex_value, AbstractFillVector, _fill_dot +using FillArrays: AbstractFillMatrix # Specifying the full namespace is necessary because of https://github.com/JuliaLang/julia/issues/48533 # See https://github.com/JuliaStats/LogExpFunctions.jl/pull/63 using FillArrays.LinearAlgebra @@ -13,12 +14,26 @@ import LinearAlgebra: dot, kron, I ################## ## Sparse arrays ################## +function SparseVector{Tv,Ti}(F::AbstractFillVector) where {Tv,Ti} + SparseVector{Tv,Ti}(length(F), + convert(Vector{Ti}, axes(F,1)), + fill(convert(Tv, getindex_value(F)), length(F)) + ) +end + SparseVector{T}(Z::ZerosVector) where T = spzeros(T, length(Z)) SparseVector{Tv,Ti}(Z::ZerosVector) where {Tv,Ti} = spzeros(Tv, Ti, length(Z)) convert(::Type{AbstractSparseVector}, Z::ZerosVector{T}) where T = spzeros(T, length(Z)) convert(::Type{AbstractSparseVector{T}}, Z::ZerosVector) where T= spzeros(T, length(Z)) +function SparseMatrixCSC{T,Ti}(F::AbstractFillMatrix) where {T, Ti<:Integer} + SparseMatrixCSC{T,Ti}(size(F)..., + convert(Vector{Ti}, StepRangeLen(oneunit(Ti), Ti(size(F,1)), size(F,2)+1)), + convert(Vector{Ti}, reduce(vcat, fill(map(Ti, axes(F,1)), size(F,2)))), + fill(convert(T, getindex_value(F)), length(F))) +end + SparseMatrixCSC{T}(Z::ZerosMatrix) where T = spzeros(T, size(Z)...) SparseMatrixCSC{Tv,Ti}(Z::Zeros{T,2,Axes}) where {Tv,Ti<:Integer,T,Axes} = spzeros(Tv, Ti, size(Z)...) diff --git a/test/runtests.jl b/test/runtests.jl index 32cd2ed..4a1a0d6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -591,6 +591,14 @@ end testsparsediag(E) end end + + F = Fill(3, 4, 4) + @test sparse(F) == sparse(fill(3, size(F)...)) + @test SparseMatrixCSC{Int8,Int16}(F) isa SparseMatrixCSC{Int8,Int16} + + F = Fill(3, 4) + @test sparse(F) == sparse(fill(3, size(F)...)) + @test SparseVector{Int8,Int16}(F) isa SparseVector{Int8,Int16} end @testset "==" begin