diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 97a7bcddb91bb..592e00d7aaea9 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -884,7 +884,13 @@ 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 @@ -892,7 +898,7 @@ for op in (+, -, min, max) 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 diff --git a/test/sparsedir/sparse.jl b/test/sparsedir/sparse.jl index 79033902593b7..49446cbdc90d8 100644 --- a/test/sparsedir/sparse.jl +++ b/test/sparsedir/sparse.jl @@ -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))) @@ -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