Skip to content

Commit

Permalink
Fix #13024, sparse & and |
Browse files Browse the repository at this point in the history
also fix eltype promotion in sparse max and min

Also add $ because Jiahao asked nicely

(cherry picked from commit cdb6496)
ref #13024
  • Loading branch information
tkelman committed Sep 11, 2015
1 parent 624e558 commit f35a6ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -884,15 +884,21 @@ broadcast_zpreserving{Tv,Ti}(f::Function, A_1::Union{Array,BitArray,Number}, A_2

## Binary arithmetic and boolean operators

for op in (+, -, min, max)
for (op, pro) in ((+, :eltype_plus),
(-, :eltype_plus),
(min, :promote_eltype),
(max, :promote_eltype),
(&, :promote_eltype),
(|, :promote_eltype),
($, :promote_eltype))
body = gen_broadcast_body_sparse(op, true)
OP = Symbol(string(op))
@eval begin
function ($OP){Tv1,Ti1,Tv2,Ti2}(A_1::SparseMatrixCSC{Tv1,Ti1}, A_2::SparseMatrixCSC{Tv2,Ti2})
if size(A_1,1) != size(A_2,1) || size(A_1,2) != size(A_2,2)
throw(DimensionMismatch(""))
end
Tv = eltype_plus(A_1, A_2)
Tv = ($pro)(A_1, A_2)
B = spzeros(Tv, promote_type(Ti1, Ti2), broadcast_shape(A_1, A_2)...)
$body
B
Expand Down
27 changes: 27 additions & 0 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

using Base.Test

# check sparse matrix construction
@test isequal(full(sparse(complex(ones(5,5),ones(5,5)))), complex(ones(5,5),ones(5,5)))

Expand Down Expand Up @@ -1102,3 +1104,28 @@ q = randperm(10)
# issue #13008
@test_throws ArgumentError sparse(collect(1:100), collect(1:100), fill(5,100), 5, 5)
@test_throws ArgumentError sparse(Int[], collect(1:5), collect(1:5))

# issue #13024
let
A13024 = sparse([1,2,3,4,5], [1,2,3,4,5], fill(true,5))
B13024 = sparse([1,2,4,5], [1,2,3,5], fill(true,4))

@test A13024 & B13024 == sparse([1,2,5], [1,2,5], fill(true,3))
@test typeof(A13024 & B13024) == SparseMatrixCSC{Bool,Int}

@test A13024 | B13024 == sparse([1,2,3,4,4,5], [1,2,3,3,4,5], fill(true,6))
@test typeof(A13024 | B13024) == SparseMatrixCSC{Bool,Int}

@test A13024 $ B13024 == sparse([3,4,4], [3,3,4], fill(true,3), 5, 5)
@test typeof(A13024 $ B13024) == SparseMatrixCSC{Bool,Int}

@test max(A13024, B13024) == sparse([1,2,3,4,4,5], [1,2,3,3,4,5], fill(true,6))
@test typeof(max(A13024, B13024)) == SparseMatrixCSC{Bool,Int}

@test min(A13024, B13024) == sparse([1,2,5], [1,2,5], fill(true,3))
@test typeof(min(A13024, B13024)) == SparseMatrixCSC{Bool,Int}

for op in (+, -, &, |, $, max, min)
@test op(A13024, B13024) == op(full(A13024), full(B13024))
end
end

2 comments on commit f35a6ff

@sbromberger
Copy link
Contributor

Choose a reason for hiding this comment

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

@tkelman - does this commit mean that it’s been backported and will be available in 0.4? (How to tell whether it's been merged?)

@yuyichao
Copy link
Contributor

Choose a reason for hiding this comment

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

@sbromberger There's the link to the related pull request below the commit message on the github web interface. You can find the discussion there (and in particular Tony's plan).

Please sign in to comment.