Skip to content

Commit

Permalink
Deprecate vectorized | in favor of compact broadcast syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Dec 31, 2016
1 parent 97139e0 commit 6e8cc8b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 58 deletions.
4 changes: 2 additions & 2 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ promote_array_type{S<:Integer}(::typeof(/), ::Type{S}, ::Type{Bool}, T::Type) =
promote_array_type{S<:Integer}(::typeof(\), ::Type{S}, ::Type{Bool}, T::Type) = T
promote_array_type{S<:Integer}(F, ::Type{S}, ::Type{Bool}, T::Type) = T

for f in (:+, :-, :|)
for f in (:+, :-)
@eval function ($f)(A::AbstractArray, B::AbstractArray)
promote_shape(A, B) # check size compatibility
broadcast($f, A, B)
end
end

for f in (:|, :/, :\, :*, :+, :-)
for f in (:/, :\, :*, :+, :-)
if f != :/
@eval ($f){T}(A::Number, B::AbstractArray{T}) = broadcast($f, A, B)
end
Expand Down
25 changes: 0 additions & 25 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1237,31 +1237,6 @@ end
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
(/)(x::Number, B::BitArray) = (/)(x, Array(B))

function (|)(B::BitArray, x::Bool)
x ? trues(size(B)) : copy(B)
end
(|)(x::Bool, B::BitArray) = B | x

for f in (:|,)
@eval begin
function ($f)(A::BitArray, B::BitArray)
F = BitArray(promote_shape(size(A),size(B))...)
Fc = F.chunks
Ac = A.chunks
Bc = B.chunks
(isempty(Ac) || isempty(Bc)) && return F
for i = 1:length(Fc)
Fc[i] = ($f)(Ac[i], Bc[i])
end
Fc[end] &= _msk_end(F)
return F
end
($f)(A::DenseArray{Bool}, B::BitArray) = ($f)(BitArray(A), B)
($f)(B::BitArray, A::DenseArray{Bool}) = ($f)(B, BitArray(A))
($f)(x::Number, B::BitArray) = ($f)(x, Array(B))
($f)(B::BitArray, x::Number) = ($f)(Array(B), x)
end
end

## promotion to complex ##

Expand Down
7 changes: 7 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1432,4 +1432,11 @@ end
@deprecate (&)(A::AbstractArray, b::Number) A .& b
@deprecate (&)(A::AbstractArray, B::AbstractArray) A .& B

# Deprecate vectorized | in favor of compact broadcast syntax
@deprecate (|)(a::Bool, B::BitArray) a .| B
@deprecate (|)(A::BitArray, b::Bool) A .| b
@deprecate (|)(a::Number, B::AbstractArray) a .| B
@deprecate (|)(A::AbstractArray, b::Number) A .| b
@deprecate (|)(A::AbstractArray, B::AbstractArray) A .| B

# End deprecations scheduled for 0.6
2 changes: 0 additions & 2 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1407,8 +1407,6 @@ conj!(A::SparseMatrixCSC) = (broadcast!(conj, A.nzval, A.nzval); A)
## Binary arithmetic and boolean operators
(+)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(+, A, B)
(-)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(-, A, B)
# TODO: Vectorized min, max, |, and xor should be deprecated in favor of compact-broadcast syntax.
(|)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(|, A, B)

( +)(A::SparseMatrixCSC, B::Array ) = Array(A) + B
( +)(A::Array , B::SparseMatrixCSC) = A + Array(B)
Expand Down
34 changes: 17 additions & 17 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ timesofar("unary arithmetic")
let b1 = bitrand(n1, n2)
b2 = bitrand(n1, n2)
@check_bit_operation broadcast(&, b1, b2) BitMatrix
@check_bit_operation (|)(b1, b2) BitMatrix
@check_bit_operation broadcast(|, b1, b2) BitMatrix
@check_bit_operation broadcast(xor, b1, b2) BitMatrix
@check_bit_operation (+)(b1, b2) Matrix{Int}
@check_bit_operation (-)(b1, b2) Matrix{Int}
Expand Down Expand Up @@ -812,7 +812,7 @@ end

let b0 = falses(0)
@check_bit_operation broadcast(&, b0, b0) BitVector
@check_bit_operation (|)(b0, b0) BitVector
@check_bit_operation broadcast(|, b0, b0) BitVector
@check_bit_operation broadcast(xor, b0, b0) BitVector
@check_bit_operation broadcast(*, b0, b0) BitVector
@check_bit_operation (*)(b0, b0') Matrix{Int}
Expand All @@ -823,7 +823,7 @@ end
let b1 = bitrand(n1, n2)
i2 = rand(1:10, n1, n2)
@check_bit_operation broadcast(&, b1, i2) Matrix{Int}
@check_bit_operation (|)(b1, i2) Matrix{Int}
@check_bit_operation broadcast(|, b1, i2) Matrix{Int}
@check_bit_operation broadcast(xor, b1, i2) Matrix{Int}
@check_bit_operation (+)(b1, i2) Matrix{Int}
@check_bit_operation (-)(b1, i2) Matrix{Int}
Expand Down Expand Up @@ -856,14 +856,14 @@ let b2 = bitrand(n1, n2)
cf1 = complex(f1)

@check_bit_operation broadcast(&, i1, b2) Matrix{Int}
@check_bit_operation (|)(i1, b2) Matrix{Int}
@check_bit_operation broadcast(|, i1, b2) Matrix{Int}
@check_bit_operation broadcast(xor, i1, b2) Matrix{Int}
@check_bit_operation broadcast(+, i1, b2) Matrix{Int}
@check_bit_operation broadcast(-, i1, b2) Matrix{Int}
@check_bit_operation broadcast(*, i1, b2) Matrix{Int}

@check_bit_operation broadcast(&, u1, b2) Matrix{UInt8}
@check_bit_operation (|)(u1, b2) Matrix{UInt8}
@check_bit_operation broadcast(|, u1, b2) Matrix{UInt8}
@check_bit_operation broadcast(xor, u1, b2) Matrix{UInt8}
@check_bit_operation broadcast(+, u1, b2) Matrix{UInt8}
@check_bit_operation broadcast(-, u1, b2) Matrix{UInt8}
Expand Down Expand Up @@ -935,10 +935,10 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(&, b1, false) BitMatrix
@check_bit_operation broadcast(&, true, b1) BitMatrix
@check_bit_operation broadcast(&, false, b1) BitMatrix
@check_bit_operation (|)(b1, true) BitMatrix
@check_bit_operation (|)(b1, false) BitMatrix
@check_bit_operation (|)(true, b1) BitMatrix
@check_bit_operation (|)(false, b1) BitMatrix
@check_bit_operation broadcast(|, b1, true) BitMatrix
@check_bit_operation broadcast(|, b1, false) BitMatrix
@check_bit_operation broadcast(|, true, b1) BitMatrix
@check_bit_operation broadcast(|, false, b1) BitMatrix
@check_bit_operation broadcast(xor, b1, true) BitMatrix
@check_bit_operation broadcast(xor, b1, false) BitMatrix
@check_bit_operation broadcast(xor, true, b1) BitMatrix
Expand All @@ -957,13 +957,13 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(mod,b1, true) BitMatrix

@check_bit_operation broadcast(&, b1, b2) BitMatrix
@check_bit_operation (|)(b1, b2) BitMatrix
@check_bit_operation broadcast(|, b1, b2) BitMatrix
@check_bit_operation broadcast(xor, b1, b2) BitMatrix
@check_bit_operation broadcast(&, b2, b1) BitMatrix
@check_bit_operation (|)(b2, b1) BitMatrix
@check_bit_operation broadcast(|, b2, b1) BitMatrix
@check_bit_operation broadcast(xor, b2, b1) BitMatrix
@check_bit_operation broadcast(&, b1, i2) Matrix{Int}
@check_bit_operation (|)(b1, i2) Matrix{Int}
@check_bit_operation broadcast(|, b1, i2) Matrix{Int}
@check_bit_operation broadcast(xor, b1, i2) Matrix{Int}
@check_bit_operation broadcast(+, b1, i2) Matrix{Int}
@check_bit_operation broadcast(-, b1, i2) Matrix{Int}
Expand All @@ -973,7 +973,7 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(mod, b1, i2) Matrix{Int}

@check_bit_operation broadcast(&, b1, u2) Matrix{UInt8}
@check_bit_operation (|)(b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(|, b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(xor, b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(+, b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(-, b1, u2) Matrix{UInt8}
Expand Down Expand Up @@ -1116,7 +1116,7 @@ let b1 = trues(v1)

for i = 3:(v1-1), j = 2:i
submask = b1 << (v1-j+1)
@test findnext((b1 >> i) | submask, j) == i+1
@test findnext((b1 >> i) .| submask, j) == i+1
@test findnextnot((~(b1 >> i)) .⊻ submask, j) == i+1
end
end
Expand Down Expand Up @@ -1273,7 +1273,7 @@ for l = [0, 1, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]
@test map(identity, b1) == map(x->x, b1) == b1

@test map(&, b1, b2) == map((x,y)->x&y, b1, b2) == broadcast(&, b1, b2)
@test map(|, b1, b2) == map((x,y)->x|y, b1, b2) == b1 | b2
@test map(|, b1, b2) == map((x,y)->x|y, b1, b2) == broadcast(|, b1, b2)
@test map(, b1, b2) == map((x,y)->xy, b1, b2) == broadcast(, b1, b2) == broadcast(xor, b1, b2)

@test map(^, b1, b2) == map((x,y)->x^y, b1, b2) == b1 .^ b2
Expand All @@ -1298,7 +1298,7 @@ for l = [0, 1, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]
@test map!(one, b, b1) == map!(x->true, b, b1) == trues(l) == b

@test map!(&, b, b1, b2) == map!((x,y)->x&y, b, b1, b2) == broadcast(&, b1, b2) == b
@test map!(|, b, b1, b2) == map!((x,y)->x|y, b, b1, b2) == b1 | b2 == b
@test map!(|, b, b1, b2) == map!((x,y)->x|y, b, b1, b2) == broadcast(|, b1, b2) == b
@test map!(, b, b1, b2) == map!((x,y)->xy, b, b1, b2) == broadcast(, b1, b2) == broadcast(xor, b1, b2) == b

@test map!(^, b, b1, b2) == map!((x,y)->x^y, b, b1, b2) == b1 .^ b2 == b
Expand Down Expand Up @@ -1407,7 +1407,7 @@ for sz = [(n1,n1), (n1,n2), (n2,n1)], (f,isf) = [(tril,istril), (triu,istriu)]
end

let b1 = bitrand(n1,n1)
b1 |= b1.'
b1 .|= b1.'
@check_bit_operation issymmetric(b1) Bool
@check_bit_operation ishermitian(b1) Bool
end
Expand Down
4 changes: 2 additions & 2 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)
@test all(x->x>0, [4]) == true
@test all(x->x>0, [-3, 4, 5]) == false

@test reduce(|, fill(trues(5), 24)) == trues(5)
@test reduce(|, fill(falses(5), 24)) == falses(5)
@test reduce((a, b) -> a .| b, fill(trues(5), 24)) == trues(5)
@test reduce((a, b) -> a .| b, fill(falses(5), 24)) == falses(5)
@test reduce((a, b) -> a .& b, fill(trues(5), 24)) == trues(5)
@test reduce((a, b) -> a .& b, fill(falses(5), 24)) == falses(5)

Expand Down
14 changes: 4 additions & 10 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ do33 = ones(3)
sqrfloatmat, colfloatmat = sprand(4, 4, 0.5), sprand(4, 1, 0.5)
@test_throws DimensionMismatch (+)(sqrfloatmat, colfloatmat)
@test_throws DimensionMismatch (-)(sqrfloatmat, colfloatmat)
# @test_throws DimensionMismatch min(sqrfloatmat, colfloatmat) # vectorized min no longer exists
# @test_throws DimensionMismatch max(sqrfloatmat, colfloatmat) # vectorized max no longer exists
sqrboolmat, colboolmat = sprand(Bool, 4, 4, 0.5), sprand(Bool, 4, 1, 0.5)
# @test_throws DimensionMismatch (&)(sqrboolmat, colboolmat) # vectorized & no longer exists
@test_throws DimensionMismatch (|)(sqrboolmat, colboolmat)
# @test_throws DimensionMismatch xor(sqrboolmat, colboolmat) # vectorized xor no longer exists
end
end

Expand Down Expand Up @@ -1509,8 +1503,8 @@ end
@test broadcast(&, A13024, B13024) == sparse([1,2,5], [1,2,5], fill(true,3))
@test typeof(broadcast(&, 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 broadcast(|, A13024, B13024) == sparse([1,2,3,4,4,5], [1,2,3,3,4,5], fill(true,6))
@test typeof(broadcast(|, A13024, B13024)) == SparseMatrixCSC{Bool,Int}

@test broadcast(, A13024, B13024) == sparse([3,4,4], [3,3,4], fill(true,3), 5, 5)
@test typeof(broadcast(, A13024, B13024)) == SparseMatrixCSC{Bool,Int}
Expand All @@ -1521,10 +1515,10 @@ end
@test broadcast(min, A13024, B13024) == sparse([1,2,5], [1,2,5], fill(true,3))
@test typeof(broadcast(min, A13024, B13024)) == SparseMatrixCSC{Bool,Int}

for op in (+, -, |)
for op in (+, -)
@test op(A13024, B13024) == op(Array(A13024), Array(B13024))
end
for op in (max, min, &, xor)
for op in (max, min, &, |, xor)
@test op.(A13024, B13024) == op.(Array(A13024), Array(B13024))
end
end
Expand Down

0 comments on commit 6e8cc8b

Please sign in to comment.