Skip to content

Commit

Permalink
SparseMatrixCSC constructor with a Tuple of Integers (#523)
Browse files Browse the repository at this point in the history
* SparseMatrixCSC constructor with a Tuple of Integers

* SparseVector constructors

---------

Co-authored-by: Viral B. Shah <[email protected]>
  • Loading branch information
jishnub and ViralBShah authored Apr 4, 2024
1 parent 08d6ae1 commit 33fbc75
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ SparseMatrixCSC(m, n, colptr::ReadOnly, rowval::ReadOnly, nzval::Vector) =

"""
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer)
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, (m,n)::NTuple{2,Integer})
Creates an empty sparse matrix with element type `Tv` and integer type `Ti` of size `m × n`.
"""
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer) where {Tv, Ti} = spzeros(Tv, Ti, m, n)
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, mn::NTuple{2,Integer}) where {Tv, Ti} = spzeros(Tv, Ti, mn...)

"""
FixedSparseCSC{Tv,Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}
Expand Down
1 change: 1 addition & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SparseVector(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) where {Tv,Ti} =
SparseVector{Tv,Ti}(n, nzind, nzval)

SparseVector{Tv, Ti}(::UndefInitializer, n::Integer) where {Tv, Ti} = SparseVector{Tv, Ti}(n, Ti[], Tv[])
SparseVector{Tv, Ti}(::UndefInitializer, (n,)::Tuple{Integer}) where {Tv, Ti} = SparseVector{Tv, Ti}(n, Ti[], Tv[])

"""
FixedSparseVector{Tv,Ti<:Integer} <: AbstractCompressedVector{Tv,Ti}
Expand Down
11 changes: 7 additions & 4 deletions test/sparsematrix_constructors_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ end
@test sparse([1, 1, 2, 2, 2], [1, 2, 1, 2, 2], -1.0, 2, 2, *) == sparse([1, 1, 2, 2], [1, 2, 1, 2], [-1.0, -1.0, -1.0, 1.0], 2, 2)
@test sparse(sparse(Int32.(1:5), Int32.(1:5), trues(5))') isa SparseMatrixCSC{Bool,Int32}
# undef initializer
m = SparseMatrixCSC{Float32, Int16}(undef, 3, 4)
@test size(m) == (3, 4)
@test eltype(m) === Float32
@test m == spzeros(3, 4)
sz = (3, 4)
for m in (SparseMatrixCSC{Float32, Int16}(undef, sz...), SparseMatrixCSC{Float32, Int16}(undef, sz),
similar(SparseMatrixCSC{Float32, Int16}, sz))
@test size(m) == sz
@test eltype(m) === Float32
@test m == spzeros(sz...)
end
end

@testset "spzeros for pattern creation (structural zeros)" begin
Expand Down
12 changes: 8 additions & 4 deletions test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,14 @@ end
end

@testset "Undef initializer" begin
v = SparseVector{Float32, Int16}(undef, 4)
@test size(v) == (4, )
@test eltype(v) === Float32
@test v == spzeros(Float32, 4)
sz = (4,)
for v in (SparseVector{Float32, Int16}(undef, sz),
SparseVector{Float32, Int16}(undef, sz...),
similar(SparseVector{Float32, Int16}, sz))
@test size(v) == sz
@test eltype(v) === Float32
@test v == spzeros(Float32, sz...)
end
end
end
### Element access
Expand Down

0 comments on commit 33fbc75

Please sign in to comment.