Skip to content

Commit

Permalink
Merge pull request #19711 from Sacha0/devecxor
Browse files Browse the repository at this point in the history
Deprecate vectorized xor (⊻) in favor of compact broadcast syntax
  • Loading branch information
JeffBezanson authored Dec 28, 2016
2 parents 8ed946b + 8335884 commit 309657f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ 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 (:+, :-, :div, :mod, :&, :|, :xor)
for f in (:+, :-, :div, :mod, :&, :|)
@eval ($f)(A::AbstractArray, B::AbstractArray) =
_elementwise($f, promote_eltype_op($f, A, B), A, B)
end
Expand All @@ -89,7 +89,7 @@ function _elementwise{T}(op, ::Type{T}, A::AbstractArray, B::AbstractArray)
return F
end

for f in (:div, :mod, :rem, :&, :|, :xor, :/, :\, :*, :+, :-)
for f in (:div, :mod, :rem, :&, :|, :/, :\, :*, :+, :-)
if f != :/
@eval ($f){T}(A::Number, B::AbstractArray{T}) = broadcast($f, A, B)
end
Expand Down
7 changes: 1 addition & 6 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1301,12 +1301,7 @@ function (|)(B::BitArray, x::Bool)
end
(|)(x::Bool, B::BitArray) = B | x

function xor(B::BitArray, x::Bool)
x ? ~B : copy(B)
end
xor(x::Bool, B::BitArray) = xor(B, x)

for f in (:&, :|, :xor)
for f in (:&, :|)
@eval begin
function ($f)(A::BitArray, B::BitArray)
F = BitArray(promote_shape(size(A),size(B))...)
Expand Down
7 changes: 7 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1168,4 +1168,11 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs),
end
end

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

# End deprecations scheduled for 0.6
1 change: 0 additions & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,6 @@ min(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(min, A, B)
max(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(max, A, B)
(&)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(&, A, B)
(|)(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(|, A, B)
xor(A::SparseMatrixCSC, B::SparseMatrixCSC) = map(xor, A, B)

( +)(A::SparseMatrixCSC, B::Array ) = Array(A) + B
( +)(A::Array , B::SparseMatrixCSC) = A + Array(B)
Expand Down
32 changes: 16 additions & 16 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ let b1 = bitrand(n1, n2)
b2 = bitrand(n1, n2)
@check_bit_operation (&)(b1, b2) BitMatrix
@check_bit_operation (|)(b1, b2) BitMatrix
@check_bit_operation xor(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}
@check_bit_operation broadcast(*, b1, b2) BitMatrix
Expand Down Expand Up @@ -815,7 +815,7 @@ end
let b0 = falses(0)
@check_bit_operation (&)(b0, b0) BitVector
@check_bit_operation (|)(b0, b0) BitVector
@check_bit_operation xor(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}
end
Expand All @@ -826,7 +826,7 @@ let b1 = bitrand(n1, n2)
i2 = rand(1:10, n1, n2)
@check_bit_operation (&)(b1, i2) Matrix{Int}
@check_bit_operation (|)(b1, i2) Matrix{Int}
@check_bit_operation xor(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}
@check_bit_operation broadcast(*, b1, i2) Matrix{Int}
Expand Down Expand Up @@ -859,14 +859,14 @@ let b2 = bitrand(n1, n2)

@check_bit_operation (&)(i1, b2) Matrix{Int}
@check_bit_operation (|)(i1, b2) Matrix{Int}
@check_bit_operation xor(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 (&)(u1, b2) Matrix{UInt8}
@check_bit_operation (|)(u1, b2) Matrix{UInt8}
@check_bit_operation xor(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}
@check_bit_operation broadcast(*, u1, b2) Matrix{UInt8}
Expand Down Expand Up @@ -941,10 +941,10 @@ let b1 = bitrand(n1, n2)
@check_bit_operation (|)(b1, false) BitMatrix
@check_bit_operation (|)(true, b1) BitMatrix
@check_bit_operation (|)(false, b1) BitMatrix
@check_bit_operation xor(b1, true) BitMatrix
@check_bit_operation xor(b1, false) BitMatrix
@check_bit_operation xor(true, b1) BitMatrix
@check_bit_operation xor(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
@check_bit_operation broadcast(xor, false, b1) BitMatrix
@check_bit_operation broadcast(+, b1, true) Matrix{Int}
@check_bit_operation broadcast(+, b1, false) Matrix{Int}
@check_bit_operation broadcast(-, b1, true) Matrix{Int}
Expand All @@ -960,13 +960,13 @@ let b1 = bitrand(n1, n2)

@check_bit_operation (&)(b1, b2) BitMatrix
@check_bit_operation (|)(b1, b2) BitMatrix
@check_bit_operation xor(b1, b2) BitMatrix
@check_bit_operation broadcast(xor, b1, b2) BitMatrix
@check_bit_operation (&)(b2, b1) BitMatrix
@check_bit_operation (|)(b2, b1) BitMatrix
@check_bit_operation xor(b2, b1) BitMatrix
@check_bit_operation broadcast(xor, b2, b1) BitMatrix
@check_bit_operation (&)(b1, i2) Matrix{Int}
@check_bit_operation (|)(b1, i2) Matrix{Int}
@check_bit_operation xor(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}
@check_bit_operation broadcast(*, b1, i2) Matrix{Int}
Expand All @@ -976,7 +976,7 @@ let b1 = bitrand(n1, n2)

@check_bit_operation (&)(b1, u2) Matrix{UInt8}
@check_bit_operation (|)(b1, u2) Matrix{UInt8}
@check_bit_operation xor(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}
@check_bit_operation broadcast(*, b1, u2) Matrix{UInt8}
Expand Down Expand Up @@ -1119,7 +1119,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 findnextnot((~(b1 >> i)) submask, j) == i+1
@test findnextnot((~(b1 >> i)) . submask, j) == i+1
end
end

Expand Down Expand Up @@ -1276,7 +1276,7 @@ for l = [0, 1, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]

@test map(&, b1, b2) == map((x,y)->x&y, b1, b2) == b1 & b2
@test map(|, b1, b2) == map((x,y)->x|y, b1, b2) == b1 | b2
@test map(, b1, b2) == map((x,y)->xy, b1, b2) == b1 b2 == xor(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
@test map(*, b1, b2) == map((x,y)->x*y, b1, b2) == b1 .* b2
Expand All @@ -1301,7 +1301,7 @@ for l = [0, 1, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]

@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) == b1 | b2 == b
@test map!(, b, b1, b2) == map!((x,y)->xy, b, b1, b2) == b1 b2 == xor(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
@test map!(*, b, b1, b2) == map!((x,y)->x*y, b, b1, b2) == b1 .* b2 == b
Expand Down
9 changes: 5 additions & 4 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ do33 = ones(3)
sqrboolmat, colboolmat = sprand(Bool, 4, 4, 0.5), sprand(Bool, 4, 1, 0.5)
@test_throws DimensionMismatch (&)(sqrboolmat, colboolmat)
@test_throws DimensionMismatch (|)(sqrboolmat, colboolmat)
@test_throws DimensionMismatch xor(sqrboolmat, colboolmat)
# @test_throws DimensionMismatch xor(sqrboolmat, colboolmat) # vectorized xor no longer exists
end
end

Expand Down Expand Up @@ -1595,16 +1595,17 @@ end
@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 broadcast(, A13024, B13024) == sparse([3,4,4], [3,3,4], fill(true,3), 5, 5)
@test typeof(broadcast(, 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 (+, -, &, |, xor)
@test broadcast(xor, A13024, B13024) == broadcast(xor, Array(A13024), Array(B13024))
for op in (+, -, &, |)
@test op(A13024, B13024) == op(Array(A13024), Array(B13024))
end
for op in (max, min)
Expand Down

0 comments on commit 309657f

Please sign in to comment.