Skip to content

Commit c604d05

Browse files
Sacha0tkelman
authored andcommitted
replace broadcast with map in sparse binary op definitions (#19527)
* Make some sparse elementwise binary operations short children of map rather than {broadcast + shape check}. * Test shape checks for sparse elementwise binary operations equivalent to map.
1 parent 071c60a commit c604d05

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

base/sparse/sparsematrix.jl

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,17 +2278,14 @@ round{To}(::Type{To}, A::SparseMatrixCSC) = round.(To, A)
22782278

22792279
## Binary arithmetic and boolean operators
22802280

2281-
# TODO: These seven functions should probably be reimplemented in terms of sparse map
2282-
# when a better sparse map exists. (And vectorized min, max, &, |, and xor should be
2283-
# deprecated in favor of compact-broadcast syntax.)
2284-
_checksameshape(A, B) = size(A) == size(B) || throw(DimensionMismatch("size(A) must match size(B)"))
2285-
(+)(A::SparseMatrixCSC, B::SparseMatrixCSC) = (_checksameshape(A, B); broadcast(+, A, B))
2286-
(-)(A::SparseMatrixCSC, B::SparseMatrixCSC) = (_checksameshape(A, B); broadcast(-, A, B))
2287-
min(A::SparseMatrixCSC, B::SparseMatrixCSC) = (_checksameshape(A, B); broadcast(min, A, B))
2288-
max(A::SparseMatrixCSC, B::SparseMatrixCSC) = (_checksameshape(A, B); broadcast(max, A, B))
2289-
(&)(A::SparseMatrixCSC, B::SparseMatrixCSC) = (_checksameshape(A, B); broadcast(&, A, B))
2290-
(|)(A::SparseMatrixCSC, B::SparseMatrixCSC) = (_checksameshape(A, B); broadcast(|, A, B))
2291-
xor(A::SparseMatrixCSC, B::SparseMatrixCSC) = (_checksameshape(A, B); broadcast(xor, A, B))
2281+
(+)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(+, A, B)
2282+
(-)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(-, A, B)
2283+
# TODO: Vectorized min, max, |, and xor should be deprecated in favor of compact-broadcast syntax.
2284+
min(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(min, A, B)
2285+
max(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(max, A, B)
2286+
(&)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(&, A, B)
2287+
(|)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(|, A, B)
2288+
xor(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(xor, A, B)
22922289

22932290
(.+)(A::SparseMatrixCSC, B::Number) = Array(A) .+ B
22942291
( +)(A::SparseMatrixCSC, B::Array ) = Array(A) + B

test/sparse/sparse.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ do33 = ones(3)
2424
# check sparse binary op
2525
@test all(Array(se33 + convert(SparseMatrixCSC{Float32,Int32}, se33)) == 2*eye(3))
2626
@test all(Array(se33 * convert(SparseMatrixCSC{Float32,Int32}, se33)) == eye(3))
27+
@testset "Shape checks for sparse elementwise binary operations equivalent to map" begin
28+
sqrfloatmat, colfloatmat = sprand(4, 4, 0.5), sprand(4, 1, 0.5)
29+
@test_throws DimensionMismatch (+)(sqrfloatmat, colfloatmat)
30+
@test_throws DimensionMismatch (-)(sqrfloatmat, colfloatmat)
31+
@test_throws DimensionMismatch min(sqrfloatmat, colfloatmat)
32+
@test_throws DimensionMismatch max(sqrfloatmat, colfloatmat)
33+
sqrboolmat, colboolmat = sprand(Bool, 4, 4, 0.5), sprand(Bool, 4, 1, 0.5)
34+
@test_throws DimensionMismatch (&)(sqrboolmat, colboolmat)
35+
@test_throws DimensionMismatch (|)(sqrboolmat, colboolmat)
36+
@test_throws DimensionMismatch xor(sqrboolmat, colboolmat)
37+
end
2738

2839
# check horiz concatenation
2940
@test all([se33 se33] == sparse([1, 2, 3, 1, 2, 3], [1, 2, 3, 4, 5, 6], ones(6)))

0 commit comments

Comments
 (0)