Skip to content
Open
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
15 changes: 15 additions & 0 deletions ext/FillArraysSparseArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)...)

Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading