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

Use _setindex! to fix DimensionMismatch in sparse assignment, Fixes #28963 #30507

Merged
merged 1 commit into from
Dec 26, 2018
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
1 change: 1 addition & 0 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2575,6 +2575,7 @@ function setindex!(A::SparseMatrixCSC{Tv,Ti}, V::AbstractVecOrMat, Ix::Union{Int
@assert !has_offset_axes(A, V, Ix, Jx)
(I, J) = Base.ensure_indexable(to_indices(A, (Ix, Jx)))
checkbounds(A, I, J)
Base._setindex!(IndexStyle(A), A, V, to_indices(A, (Ix, Jx))...)
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this just assign into the array twice? Once with the generic (slow) dense version, and then once with the optimized sparse version? We just want to be checking the shape of V, no?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed: before:

julia> A = spzeros(1000,1000)
1000×1000 SparseMatrixCSC{Float64,Int64} with 0 stored entries

julia> @btime $A[:,:] = $(spzeros(1000, 1000))
  5.557 μs (5 allocations: 8.39 KiB)

After:

julia> @btime $A[:,:] = $(spzeros(1000, 1000))
  9.545 ms (5 allocations: 8.39 KiB)

Copy link
Member

Choose a reason for hiding this comment

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

B = _to_same_csc(A, V, I, J)

issortedI = issorted(I)
Expand Down
4 changes: 4 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ end
end
end

@testset "Issue #28963" begin
@test_throws DimensionMismatch (spzeros(10,10)[:, :] = sprand(10,20,0.5))
end

@testset "matrix-vector multiplication (non-square)" begin
for i = 1:5
a = sprand(10, 5, 0.5)
Expand Down