Skip to content

Commit 9b8cd14

Browse files
jishnubViralBShah
authored andcommitted
SparseMatrixCSC constructor with a Tuple of Integers (#523)
* SparseMatrixCSC constructor with a Tuple of Integers * SparseVector constructors --------- Co-authored-by: Viral B. Shah <[email protected]>
1 parent 546be18 commit 9b8cd14

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/sparsematrix.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ SparseMatrixCSC(m, n, colptr::ReadOnly, rowval::ReadOnly, nzval::Vector) =
4949

5050
"""
5151
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer)
52+
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, (m,n)::NTuple{2,Integer})
5253
5354
Creates an empty sparse matrix with element type `Tv` and integer type `Ti` of size `m × n`.
5455
"""
5556
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, m::Integer, n::Integer) where {Tv, Ti} = spzeros(Tv, Ti, m, n)
57+
SparseMatrixCSC{Tv,Ti}(::UndefInitializer, mn::NTuple{2,Integer}) where {Tv, Ti} = spzeros(Tv, Ti, mn...)
5658

5759
"""
5860
`FixedSparseCSC{Tv,Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}`

src/sparsevector.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SparseVector(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) where {Tv,Ti} =
5050
SparseVector{Tv,Ti}(n, nzind, nzval)
5151

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

5455
"""
5556
`FixedSparseVector{Tv,Ti<:Integer} <: AbstractCompressedVector{Tv,Ti}`

test/sparsematrix_constructors_indexing.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ end
5757
@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)
5858
@test sparse(sparse(Int32.(1:5), Int32.(1:5), trues(5))') isa SparseMatrixCSC{Bool,Int32}
5959
# undef initializer
60-
m = SparseMatrixCSC{Float32, Int16}(undef, 3, 4)
61-
@test size(m) == (3, 4)
62-
@test eltype(m) === Float32
63-
@test m == spzeros(3, 4)
60+
sz = (3, 4)
61+
for m in (SparseMatrixCSC{Float32, Int16}(undef, sz...), SparseMatrixCSC{Float32, Int16}(undef, sz),
62+
similar(SparseMatrixCSC{Float32, Int16}, sz))
63+
@test size(m) == sz
64+
@test eltype(m) === Float32
65+
@test m == spzeros(sz...)
66+
end
6467
end
6568

6669
@testset "spzeros for pattern creation (structural zeros)" begin

test/sparsevector.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,14 @@ end
219219
end
220220

221221
@testset "Undef initializer" begin
222-
v = SparseVector{Float32, Int16}(undef, 4)
223-
@test size(v) == (4, )
224-
@test eltype(v) === Float32
225-
@test v == spzeros(Float32, 4)
222+
sz = (4,)
223+
for v in (SparseVector{Float32, Int16}(undef, sz),
224+
SparseVector{Float32, Int16}(undef, sz...),
225+
similar(SparseVector{Float32, Int16}, sz))
226+
@test size(v) == sz
227+
@test eltype(v) === Float32
228+
@test v == spzeros(Float32, sz...)
229+
end
226230
end
227231
end
228232
### Element access

0 commit comments

Comments
 (0)