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

Fixed zero insertion when concatenate sparse matrices with 0*I #21657

Merged
merged 1 commit into from
May 3, 2017
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
3 changes: 3 additions & 0 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,9 @@ speye_scaled(diag, m::Integer, n::Integer) = speye_scaled(typeof(diag), diag, m,

function speye_scaled(T, diag, m::Integer, n::Integer)
((m < 0) || (n < 0)) && throw(ArgumentError("invalid array dimensions"))
if iszero(diag)
return SparseMatrixCSC(m, n, ones(Int, n+1), Vector{Int}(0), Vector{T}(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not very consequential, but I wonder how often this gets called in a way that makes the Int(m), Int(n) conversions below necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think none. I even think the ones below are unnecessary, now that you mention it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe if you called it directly with a non Int sized integer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't the constructor of SparseMatrixCSC take care of that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks like the SparseMatrixCSC constructor takes care of the Int conversions, so they could be removed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think is easier if someone changes it directly, instead of me opening another PR just for that.)

end
nnz = min(m,n)
colptr = Vector{Int}(1+n)
colptr[1:nnz+1] = 1:nnz+1
Expand Down
5 changes: 5 additions & 0 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ do33 = ones(3)
end

@testset "concatenation tests" begin
sp33 = speye(3, 3)

@testset "horizontal concatenation" begin
@test all([se33 se33] == sparse([1, 2, 3, 1, 2, 3], [1, 2, 3, 4, 5, 6], ones(6)))
@test length(([sp33 0I]).nzval) == 3
end

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

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

@testset "blkdiag concatenation" begin
Expand Down