Skip to content

Commit 0155f51

Browse files
authored
Merge pull request #21657 from iamnapo/napo/0I_zeroinsert
Fixed zero insertion when concatenate sparse matrices with 0*I
2 parents 70db8be + 45dc18d commit 0155f51

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/sparse/sparsematrix.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,9 @@ speye_scaled(diag, m::Integer, n::Integer) = speye_scaled(typeof(diag), diag, m,
14301430

14311431
function speye_scaled(T, diag, m::Integer, n::Integer)
14321432
((m < 0) || (n < 0)) && throw(ArgumentError("invalid array dimensions"))
1433+
if iszero(diag)
1434+
return SparseMatrixCSC(m, n, ones(Int, n+1), Vector{Int}(0), Vector{T}(0))
1435+
end
14331436
nnz = min(m,n)
14341437
colptr = Vector{Int}(1+n)
14351438
colptr[1:nnz+1] = 1:nnz+1

test/sparse/sparse.jl

+5
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ do33 = ones(3)
4545
end
4646

4747
@testset "concatenation tests" begin
48+
sp33 = speye(3, 3)
49+
4850
@testset "horizontal concatenation" begin
4951
@test all([se33 se33] == sparse([1, 2, 3, 1, 2, 3], [1, 2, 3, 4, 5, 6], ones(6)))
52+
@test length(([sp33 0I]).nzval) == 3
5053
end
5154

5255
@testset "vertical concatenation" begin
5356
@test all([se33; se33] == sparse([1, 4, 2, 5, 3, 6], [1, 1, 2, 2, 3, 3], ones(6)))
5457
se33_32bit = convert(SparseMatrixCSC{Float32,Int32}, se33)
5558
@test all([se33; se33_32bit] == sparse([1, 4, 2, 5, 3, 6], [1, 1, 2, 2, 3, 3], ones(6)))
59+
@test length(([sp33; 0I]).nzval) == 3
5660
end
5761

5862
se44 = speye(4)
@@ -62,6 +66,7 @@ end
6266
se77 = speye(7)
6367
@testset "h+v concatenation" begin
6468
@test all([se44 sz42 sz41; sz34 se33] == se77)
69+
@test length(([sp33 0I; 1I 0I]).nzval) == 6
6570
end
6671

6772
@testset "blkdiag concatenation" begin

0 commit comments

Comments
 (0)