Skip to content

Commit

Permalink
Deprecate manually vectorized div methods in favor of compact broadca…
Browse files Browse the repository at this point in the history
…st syntax.
  • Loading branch information
Sacha0 committed Sep 20, 2016
1 parent 47fbd9c commit 26ea5dd
Show file tree
Hide file tree
Showing 6 changed files with 57 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 @@ -52,7 +52,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, :&, :|, :$)
for f in (:+, :-, :mod, :&, :|, :$)
@eval ($f)(A::AbstractArray, B::AbstractArray) =
_elementwise($f, promote_eltype_op($f, A, B), A, B)
end
Expand All @@ -68,7 +68,7 @@ function _elementwise{T}(op, ::Type{T}, A::AbstractArray, B::AbstractArray)
return F
end

for f in (:.+, :.-, :.*, :./, :.\, :.^, :, :.%, :.<<, :.>>, :div, :mod, :rem, :&, :|, :$)
for f in (:.+, :.-, :.*, :./, :.\, :.^, :, :.%, :.<<, :.>>, :mod, :rem, :&, :|, :$)
@eval begin
function ($f){T}(A::Number, B::AbstractArray{T})
R = promote_op($f, typeof(A), T)
Expand Down
25 changes: 16 additions & 9 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1128,21 +1128,19 @@ end
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
(/)(x::Number, B::BitArray) = (/)(x, Array(B))

function div(A::BitArray, B::BitArray)
function broadcast(::typeof(div), A::BitArray, B::BitArray)
shp = promote_shape(size(A), size(B))
all(B) || throw(DivideError())
return reshape(copy(A), shp)
end
div(A::BitArray, B::Array{Bool}) = div(A, BitArray(B))
div(A::Array{Bool}, B::BitArray) = div(BitArray(A), B)
function div(B::BitArray, x::Bool)
return x ? copy(B) : throw(DivideError())
end
function div(x::Bool, B::BitArray)
broadcast(::typeof(div), A::BitArray, B::Array{Bool}) = div.(A, BitArray(B))
broadcast(::typeof(div), A::Array{Bool}, B::BitArray) = div.(BitArray(A), B)
broadcast(::typeof(div), B::BitArray, x::Bool) = x ? copy(B) : throw(DivideError())
function broadcast(::typeof(div), x::Bool, B::BitArray)
all(B) || throw(DivideError())
return x ? trues(size(B)) : falses(size(B))
end
function div(x::Number, B::BitArray)
function broadcast(::typeof(div), x::Number, B::BitArray)
all(B) || throw(DivideError())
y = div(x, true)
return fill(y, size(B))
Expand All @@ -1167,8 +1165,17 @@ function mod(x::Number, B::BitArray)
y = mod(x, true)
return fill(y, size(B))
end
function broadcast(::typeof(div), B::BitArray, x::Number)
T = promote_op(div, Bool, typeof(x))
T === Any && return [div(b, x) for b in B]
F = Array{T}(size(B))
for i = 1:length(F)
F[i] = div(B[i], x)
end
return F
end

for f in (:div, :mod)
for f in (:mod,)
@eval begin
function ($f)(B::BitArray, x::Number)
T = promote_op($f, Bool, typeof(x))
Expand Down
3 changes: 2 additions & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Broadcast
using Base.Cartesian
using Base: promote_eltype_op, @get!, _msk_end, unsafe_bitgetindex, linearindices, tail, OneTo, to_shape
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, , .%, .<<, .>>, .^
export broadcast, broadcast!, bitbroadcast, dotview
import Base: broadcast
export broadcast!, bitbroadcast, dotview
export broadcast_getindex, broadcast_setindex!

## Broadcasting utilities ##
Expand Down
5 changes: 5 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,9 @@ macro vectorize_2arg(S,f)
end
export @vectorize_1arg, @vectorize_2arg

# Deprecate manually vectorized div methods in favor of compact broadcast syntax
@deprecate div(A::Number, B::AbstractArray) div.(A, B)
@deprecate div(A::AbstractArray, B::Number) div.(A, B)
@deprecate div(A::AbstractArray, B::AbstractArray) div.(A, B)

# End deprecations scheduled for 0.6
4 changes: 2 additions & 2 deletions base/dft.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ plan_irfft

export fftshift, ifftshift

fftshift(x) = circshift(x, div([size(x)...],2))
fftshift(x) = circshift(x, div.([size(x)...],2))

"""
fftshift(x)
Expand All @@ -376,7 +376,7 @@ Swap the first and second halves of the given dimension of array `x`.
"""
fftshift(x,dim)

ifftshift(x) = circshift(x, div([size(x)...],-2))
ifftshift(x) = circshift(x, div.([size(x)...],-2))

"""
ifftshift(x, [dim])
Expand Down
48 changes: 30 additions & 18 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ tc(r1,r2) = false
bitcheck(b::BitArray) = length(b.chunks) == 0 || (b.chunks[end] == b.chunks[end] & Base._msk_end(b))
bitcheck(x) = true

function check_bitop(ret_type, func, args...)
function check_bitop_call(ret_type, func, args...)
r1 = func(args...)
r2 = func(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
check_bitop_tests(ret_type, r1, r2)
end
function check_bitop_dotcall(ret_type, func, args...)
r1 = func.(args...)
r2 = func.(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
check_bitop_tests(ret_type, r1, r2)
end
function check_bitop_tests(ret_type, r1, r2)
@test isa(r1, ret_type)
@test tc(r1, r2)
@test isequal(r1, convert(ret_type, r2))
@test bitcheck(r1)
end

macro check_bit_operation(ex, ret_type)
@assert Meta.isexpr(ex, :call)
Expr(:call, :check_bitop, esc(ret_type), map(esc,ex.args)...)
if Meta.isexpr(ex, :call)
Expr(:call, :check_bitop_call, esc(ret_type), map(esc, ex.args)...)
elseif Meta.isexpr(ex, :.)
Expr(:call, :check_bitop_dotcall, esc(ret_type), esc(ex.args[1]), map(esc, ex.args[2].args)...)
else
throw(ArgumentError("first argument to @check_bit_operation must be an expression with head either :call or :. !"))
end
end

let t0 = time()
Expand Down Expand Up @@ -611,11 +623,11 @@ b2 = bitrand(n1, n2)
@check_bit_operation (/)(b1,1) Matrix{Float64}

b2 = trues(n1, n2)
@check_bit_operation div(b1, b2) BitMatrix
@check_bit_operation div.(b1, b2) BitMatrix
@check_bit_operation mod(b1, b2) BitMatrix
@check_bit_operation div(b1,Array(b2)) BitMatrix
@check_bit_operation div.(b1,Array(b2)) BitMatrix
@check_bit_operation mod(b1,Array(b2)) BitMatrix
@check_bit_operation div(Array(b1),b2) BitMatrix
@check_bit_operation div.(Array(b1),b2) BitMatrix
@check_bit_operation mod(Array(b1),b2) BitMatrix

while true
Expand Down Expand Up @@ -649,7 +661,7 @@ i2 = rand(1:10, n1, n2)
@check_bit_operation (.*)(b1, i2) Matrix{Int}
@check_bit_operation (./)(b1, i2) Matrix{Float64}
@check_bit_operation (.^)(b1, i2) BitMatrix
@check_bit_operation div(b1, i2) Matrix{Int}
@check_bit_operation div.(b1, i2) Matrix{Int}
@check_bit_operation mod(b1, i2) Matrix{Int}

# Matrix{Bool}/Matrix{Float64}
Expand All @@ -658,7 +670,7 @@ f2 = 1.0 .+ rand(n1, n2)
@check_bit_operation (.*)(b1, f2) Matrix{Float64}
@check_bit_operation (./)(b1, f2) Matrix{Float64}
@check_bit_operation (.^)(b1, f2) Matrix{Float64}
@check_bit_operation div(b1, f2) Matrix{Float64}
@check_bit_operation div.(b1, f2) Matrix{Float64}
@check_bit_operation mod(b1, f2) Matrix{Float64}

# Number/Matrix
Expand Down Expand Up @@ -695,22 +707,22 @@ end

b2 = trues(n1, n2)
@check_bit_operation (./)(true, b2) Matrix{Float64}
@check_bit_operation div(true, b2) BitMatrix
@check_bit_operation div.(true, b2) BitMatrix
@check_bit_operation mod(true, b2) BitMatrix
@check_bit_operation (./)(false, b2) Matrix{Float64}
@check_bit_operation div(false, b2) BitMatrix
@check_bit_operation div.(false, b2) BitMatrix
@check_bit_operation mod(false, b2) BitMatrix

@check_bit_operation (./)(i1, b2) Matrix{Float64}
@check_bit_operation div(i1, b2) Matrix{Int}
@check_bit_operation div.(i1, b2) Matrix{Int}
@check_bit_operation mod(i1, b2) Matrix{Int}

@check_bit_operation (./)(u1, b2) Matrix{Float64}
@check_bit_operation div(u1, b2) Matrix{UInt8}
@check_bit_operation div.(u1, b2) Matrix{UInt8}
@check_bit_operation mod(u1, b2) Matrix{UInt8}

@check_bit_operation (./)(f1, b2) Matrix{Float64}
@check_bit_operation div(f1, b2) Matrix{Float64}
@check_bit_operation div.(f1, b2) Matrix{Float64}
@check_bit_operation mod(f1, b2) Matrix{Float64}

@check_bit_operation (./)(ci1, b2) Matrix{Complex128}
Expand Down Expand Up @@ -766,7 +778,7 @@ b2 = Array(bitrand(n1,n2))
@check_bit_operation (.*)(false, b1) BitMatrix
@check_bit_operation (./)(b1, true) Matrix{Float64}
@check_bit_operation (./)(b1, false) Matrix{Float64}
@check_bit_operation div(b1, true) BitMatrix
@check_bit_operation div.(b1, true) BitMatrix
@check_bit_operation mod(b1, true) BitMatrix

@check_bit_operation (&)(b1, b2) BitMatrix
Expand All @@ -782,7 +794,7 @@ b2 = Array(bitrand(n1,n2))
@check_bit_operation (.-)(b1, i2) Matrix{Int}
@check_bit_operation (.*)(b1, i2) Matrix{Int}
@check_bit_operation (./)(b1, i2) Matrix{Float64}
@check_bit_operation div(b1, i2) Matrix{Int}
@check_bit_operation div.(b1, i2) Matrix{Int}
@check_bit_operation mod(b1, i2) Matrix{Int}

@check_bit_operation (&)(b1, u2) Matrix{UInt8}
Expand All @@ -792,14 +804,14 @@ b2 = Array(bitrand(n1,n2))
@check_bit_operation (.-)(b1, u2) Matrix{UInt8}
@check_bit_operation (.*)(b1, u2) Matrix{UInt8}
@check_bit_operation (./)(b1, u2) Matrix{Float64}
@check_bit_operation div(b1, u2) Matrix{UInt8}
@check_bit_operation div.(b1, u2) Matrix{UInt8}
@check_bit_operation mod(b1, u2) Matrix{UInt8}

@check_bit_operation (.+)(b1, f2) Matrix{Float64}
@check_bit_operation (.-)(b1, f2) Matrix{Float64}
@check_bit_operation (.*)(b1, f2) Matrix{Float64}
@check_bit_operation (./)(b1, f2) Matrix{Float64}
@check_bit_operation div(b1, f2) Matrix{Float64}
@check_bit_operation div.(b1, f2) Matrix{Float64}
@check_bit_operation mod(b1, f2) Matrix{Float64}

@check_bit_operation (.+)(b1, ci2) Matrix{Complex{Int}}
Expand Down

0 comments on commit 26ea5dd

Please sign in to comment.