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 90f5c24 commit 97139e0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 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
7 changes: 1 addition & 6 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1237,17 +1237,12 @@ end
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
(/)(x::Number, B::BitArray) = (/)(x, Array(B))

function (&)(B::BitArray, x::Bool)
x ? copy(B) : falses(size(B))
end
(&)(x::Bool, B::BitArray) = B & x

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

for f in (:&, :|)
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 @@ -1425,4 +1425,11 @@ end
@deprecate mod{T}(x::Number, A::AbstractArray{T}) mod.(x, A)
@deprecate mod{T}(A::AbstractArray{T}, x::Number) mod.(A, x)

# Deprecate vectorized & in favor of dot 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
1 change: 0 additions & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,6 @@ conj!(A::SparseMatrixCSC) = (broadcast!(conj, A.nzval, A.nzval); A)
(+)(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::SparseMatrixCSC) = map(|, A, B)

( +)(A::SparseMatrixCSC, B::Array ) = Array(A) + B
Expand Down
30 changes: 15 additions & 15 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ timesofar("unary arithmetic")

let b1 = bitrand(n1, n2)
b2 = bitrand(n1, n2)
@check_bit_operation (&)(b1, b2) BitMatrix
@check_bit_operation broadcast(&, b1, b2) BitMatrix
@check_bit_operation (|)(b1, b2) BitMatrix
@check_bit_operation broadcast(xor, b1, b2) BitMatrix
@check_bit_operation (+)(b1, b2) Matrix{Int}
Expand Down Expand Up @@ -811,7 +811,7 @@ let b1 = bitrand(n1, n1)
end

let b0 = falses(0)
@check_bit_operation (&)(b0, b0) BitVector
@check_bit_operation broadcast(&, b0, b0) BitVector
@check_bit_operation (|)(b0, b0) BitVector
@check_bit_operation broadcast(xor, b0, b0) BitVector
@check_bit_operation broadcast(*, b0, b0) BitVector
Expand All @@ -822,7 +822,7 @@ end

let b1 = bitrand(n1, n2)
i2 = rand(1:10, n1, n2)
@check_bit_operation (&)(b1, i2) Matrix{Int}
@check_bit_operation broadcast(&, b1, i2) Matrix{Int}
@check_bit_operation (|)(b1, i2) Matrix{Int}
@check_bit_operation broadcast(xor, b1, i2) Matrix{Int}
@check_bit_operation (+)(b1, i2) Matrix{Int}
Expand Down Expand Up @@ -855,14 +855,14 @@ let b2 = bitrand(n1, n2)
cu1 = complex(u1)
cf1 = complex(f1)

@check_bit_operation (&)(i1, b2) Matrix{Int}
@check_bit_operation broadcast(&, i1, b2) Matrix{Int}
@check_bit_operation (|)(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 broadcast(&, u1, b2) Matrix{UInt8}
@check_bit_operation (|)(u1, b2) Matrix{UInt8}
@check_bit_operation broadcast(xor, u1, b2) Matrix{UInt8}
@check_bit_operation broadcast(+, u1, b2) Matrix{UInt8}
Expand Down Expand Up @@ -931,10 +931,10 @@ let b1 = bitrand(n1, n2)
cf2 = complex(f2)
b2 = Array(bitrand(n1,n2))

@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 (|)(b1, true) BitMatrix
@check_bit_operation (|)(b1, false) BitMatrix
@check_bit_operation (|)(true, b1) BitMatrix
Expand All @@ -956,13 +956,13 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(div, b1, true) BitMatrix
@check_bit_operation broadcast(mod,b1, true) BitMatrix

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

@check_bit_operation (&)(b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(&, b1, u2) Matrix{UInt8}
@check_bit_operation (|)(b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(xor, b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(+, b1, u2) Matrix{UInt8}
Expand Down Expand Up @@ -1272,7 +1272,7 @@ for l = [0, 1, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]
@test map(~, b1) == map(x->~x, b1) == ~b1
@test map(identity, b1) == map(x->x, b1) == b1

@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)->x|y, b1, b2) == b1 | b2
@test map(, b1, b2) == map((x,y)->xy, b1, b2) == broadcast(, b1, b2) == broadcast(xor, b1, b2)

Expand All @@ -1297,7 +1297,7 @@ for l = [0, 1, 63, 64, 65, 127, 128, 129, 255, 256, 257, 6399, 6400, 6401]
@test map!(zero, b, b1) == map!(x->false, b, b1) == falses(l) == b
@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) == 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)->x|y, b, b1, b2) == b1 | b2 == b
@test map!(, b, b1, b2) == map!((x,y)->xy, b, b1, b2) == broadcast(, b1, b2) == broadcast(xor, b1, b2) == b

Expand Down
4 changes: 2 additions & 2 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)

@test reduce(|, fill(trues(5), 24)) == trues(5)
@test reduce(|, fill(falses(5), 24)) == falses(5)
@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_throws TypeError any(x->0, [false])
@test_throws TypeError all(x->0, [false])
Expand Down
11 changes: 5 additions & 6 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ do33 = ones(3)
# @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)
# @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
Expand Down Expand Up @@ -1506,8 +1506,8 @@ end
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 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}
Expand All @@ -1521,11 +1521,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}

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

0 comments on commit 97139e0

Please sign in to comment.