From d447a60651d5863f67286b51b96e378534fb9e8c Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Sat, 24 Dec 2016 16:05:46 -0800 Subject: [PATCH] Deprecate vectorized | in favor of compact broadcast syntax. --- base/arraymath.jl | 4 ++-- base/bitarray.jl | 25 ------------------------- base/deprecated.jl | 7 +++++++ base/sparse/sparsematrix.jl | 2 -- test/bitarray.jl | 34 +++++++++++++++++----------------- test/reduce.jl | 4 ++-- test/sparse/sparse.jl | 14 ++++---------- 7 files changed, 32 insertions(+), 58 deletions(-) diff --git a/base/arraymath.jl b/base/arraymath.jl index abbc509304136a..75ea8897a131ea 100644 --- a/base/arraymath.jl +++ b/base/arraymath.jl @@ -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 diff --git a/base/bitarray.jl b/base/bitarray.jl index 1a5e901af1c3a9..162845406b236f 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -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 ## diff --git a/base/deprecated.jl b/base/deprecated.jl index 9a6531c2e2a6da..bd2deb760f2aea 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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 diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index dccce39d6b5cad..274f6281315266 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -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) diff --git a/test/bitarray.jl b/test/bitarray.jl index 157cf7cc8ccce6..9b4649e3f11116 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -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} @@ -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} @@ -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} @@ -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} @@ -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 @@ -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} @@ -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} @@ -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 @@ -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)->x⊻y, b1, b2) == broadcast(⊻, b1, b2) == broadcast(xor, b1, b2) @test map(^, b1, b2) == map((x,y)->x^y, b1, b2) == b1 .^ b2 @@ -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)->x⊻y, 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 @@ -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 diff --git a/test/reduce.jl b/test/reduce.jl index afc1c73efe81ea..bf55acd49cb50c 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -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) diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index bfa134a05a8562..f288826ec2ff34 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -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 @@ -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} @@ -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