Skip to content
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

Fix sparse constructor when Tridiagonal/SymTridiagonal are empty #42574

Merged
merged 7 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ SparseMatrixCSC(M::Matrix) = sparse(M)
SparseMatrixCSC(T::Tridiagonal{Tv}) where Tv = SparseMatrixCSC{Tv,Int}(T)
function SparseMatrixCSC{Tv,Ti}(T::Tridiagonal) where {Tv,Ti}
m = length(T.d)
m == 0 && return SparseMatrixCSC{Tv,Ti}(0, 0, ones(Ti, 1), Ti[], Tv[])
m == 1 && return SparseMatrixCSC{Tv,Ti}(fill(T[1,1], (1,1)))

colptr = Vector{Ti}(undef, m+1)
colptr[1] = 1
Expand Down Expand Up @@ -593,6 +595,8 @@ end
SparseMatrixCSC(T::SymTridiagonal{Tv}) where Tv = SparseMatrixCSC{Tv,Int}(T)
function SparseMatrixCSC{Tv,Ti}(T::SymTridiagonal) where {Tv,Ti}
m = length(T.dv)
m == 0 && return SparseMatrixCSC{Tv,Ti}(0, 0, ones(Ti, 1), Ti[], Tv[])
m == 1 && return SparseMatrixCSC{Tv,Ti}(fill(T[1,1], (1,1)))

colptr = Vector{Ti}(undef, m+1)
colptr[1] = 1
Expand Down Expand Up @@ -623,7 +627,7 @@ end
SparseMatrixCSC(B::Bidiagonal{Tv}) where Tv = SparseMatrixCSC{Tv,Int}(B)
function SparseMatrixCSC{Tv,Ti}(B::Bidiagonal) where {Tv,Ti}
m = length(B.dv)
m == 0 && return SparseMatrixCSC{Tv,Ti}(zeros(Tv, 0, 0))
m == 0 && return SparseMatrixCSC{Tv,Ti}(0, 0, ones(Ti, 1), Ti[], Tv[])

colptr = Vector{Ti}(undef, m+1)
colptr[1] = 1
Expand Down
21 changes: 21 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,27 @@ end
@test S == S2
end

@testset "Sparse construction with empty/1x1 structured matrices" begin
empty = spzeros(0, 0)

@test sparse(Diagonal(zeros(0, 0))) == empty
@test sparse(Bidiagonal(zeros(0, 0), :U)) == empty
@test sparse(Bidiagonal(zeros(0, 0), :L)) == empty
@test sparse(SymTridiagonal(zeros(0, 0))) == empty
@test sparse(Tridiagonal(zeros(0, 0))) == empty

one_by_one = rand(1,1)
sp_one_by_one = sparse(one_by_one)

@test sparse(Diagonal(one_by_one)) == sp_one_by_one
@test sparse(Bidiagonal(one_by_one, :U)) == sp_one_by_one
@test sparse(Bidiagonal(one_by_one, :L)) == sp_one_by_one
@test sparse(Tridiagonal(one_by_one)) == sp_one_by_one

s = SymTridiagonal(rand(1), rand(0))
@test sparse(s) == s
end

@testset "error conditions for reshape, and dropdims" begin
local A = sprand(Bool, 5, 5, 0.2)
@test_throws DimensionMismatch reshape(A,(20, 2))
Expand Down