Skip to content

SparseMatrixCSC constructor with a Tuple of Integers #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2024
Merged
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
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