Skip to content

Commit 26ea5dd

Browse files
committed
Deprecate manually vectorized div methods in favor of compact broadcast syntax.
1 parent 47fbd9c commit 26ea5dd

File tree

6 files changed

+57
-32
lines changed

6 files changed

+57
-32
lines changed

base/arraymath.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ promote_array_type{S<:Integer}(::typeof(./), ::Type{S}, ::Type{Bool}, T::Type) =
5252
promote_array_type{S<:Integer}(::typeof(.\), ::Type{S}, ::Type{Bool}, T::Type) = T
5353
promote_array_type{S<:Integer}(F, ::Type{S}, ::Type{Bool}, T::Type) = T
5454

55-
for f in (:+, :-, :div, :mod, :&, :|, :$)
55+
for f in (:+, :-, :mod, :&, :|, :$)
5656
@eval ($f)(A::AbstractArray, B::AbstractArray) =
5757
_elementwise($f, promote_eltype_op($f, A, B), A, B)
5858
end
@@ -68,7 +68,7 @@ function _elementwise{T}(op, ::Type{T}, A::AbstractArray, B::AbstractArray)
6868
return F
6969
end
7070

71-
for f in (:.+, :.-, :.*, :./, :.\, :.^, :, :.%, :.<<, :.>>, :div, :mod, :rem, :&, :|, :$)
71+
for f in (:.+, :.-, :.*, :./, :.\, :.^, :, :.%, :.<<, :.>>, :mod, :rem, :&, :|, :$)
7272
@eval begin
7373
function ($f){T}(A::Number, B::AbstractArray{T})
7474
R = promote_op($f, typeof(A), T)

base/bitarray.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,21 +1128,19 @@ end
11281128
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
11291129
(/)(x::Number, B::BitArray) = (/)(x, Array(B))
11301130

1131-
function div(A::BitArray, B::BitArray)
1131+
function broadcast(::typeof(div), A::BitArray, B::BitArray)
11321132
shp = promote_shape(size(A), size(B))
11331133
all(B) || throw(DivideError())
11341134
return reshape(copy(A), shp)
11351135
end
1136-
div(A::BitArray, B::Array{Bool}) = div(A, BitArray(B))
1137-
div(A::Array{Bool}, B::BitArray) = div(BitArray(A), B)
1138-
function div(B::BitArray, x::Bool)
1139-
return x ? copy(B) : throw(DivideError())
1140-
end
1141-
function div(x::Bool, B::BitArray)
1136+
broadcast(::typeof(div), A::BitArray, B::Array{Bool}) = div.(A, BitArray(B))
1137+
broadcast(::typeof(div), A::Array{Bool}, B::BitArray) = div.(BitArray(A), B)
1138+
broadcast(::typeof(div), B::BitArray, x::Bool) = x ? copy(B) : throw(DivideError())
1139+
function broadcast(::typeof(div), x::Bool, B::BitArray)
11421140
all(B) || throw(DivideError())
11431141
return x ? trues(size(B)) : falses(size(B))
11441142
end
1145-
function div(x::Number, B::BitArray)
1143+
function broadcast(::typeof(div), x::Number, B::BitArray)
11461144
all(B) || throw(DivideError())
11471145
y = div(x, true)
11481146
return fill(y, size(B))
@@ -1167,8 +1165,17 @@ function mod(x::Number, B::BitArray)
11671165
y = mod(x, true)
11681166
return fill(y, size(B))
11691167
end
1168+
function broadcast(::typeof(div), B::BitArray, x::Number)
1169+
T = promote_op(div, Bool, typeof(x))
1170+
T === Any && return [div(b, x) for b in B]
1171+
F = Array{T}(size(B))
1172+
for i = 1:length(F)
1173+
F[i] = div(B[i], x)
1174+
end
1175+
return F
1176+
end
11701177

1171-
for f in (:div, :mod)
1178+
for f in (:mod,)
11721179
@eval begin
11731180
function ($f)(B::BitArray, x::Number)
11741181
T = promote_op($f, Bool, typeof(x))

base/broadcast.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Broadcast
55
using Base.Cartesian
66
using Base: promote_eltype_op, @get!, _msk_end, unsafe_bitgetindex, linearindices, tail, OneTo, to_shape
77
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, , .%, .<<, .>>, .^
8-
export broadcast, broadcast!, bitbroadcast, dotview
8+
import Base: broadcast
9+
export broadcast!, bitbroadcast, dotview
910
export broadcast_getindex, broadcast_setindex!
1011

1112
## Broadcasting utilities ##

base/deprecated.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,4 +1000,9 @@ macro vectorize_2arg(S,f)
10001000
end
10011001
export @vectorize_1arg, @vectorize_2arg
10021002

1003+
# Deprecate manually vectorized div methods in favor of compact broadcast syntax
1004+
@deprecate div(A::Number, B::AbstractArray) div.(A, B)
1005+
@deprecate div(A::AbstractArray, B::Number) div.(A, B)
1006+
@deprecate div(A::AbstractArray, B::AbstractArray) div.(A, B)
1007+
10031008
# End deprecations scheduled for 0.6

base/dft.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ plan_irfft
354354

355355
export fftshift, ifftshift
356356

357-
fftshift(x) = circshift(x, div([size(x)...],2))
357+
fftshift(x) = circshift(x, div.([size(x)...],2))
358358

359359
"""
360360
fftshift(x)
@@ -376,7 +376,7 @@ Swap the first and second halves of the given dimension of array `x`.
376376
"""
377377
fftshift(x,dim)
378378

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

381381
"""
382382
ifftshift(x, [dim])

test/bitarray.jl

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,30 @@ tc(r1,r2) = false
1010
bitcheck(b::BitArray) = length(b.chunks) == 0 || (b.chunks[end] == b.chunks[end] & Base._msk_end(b))
1111
bitcheck(x) = true
1212

13-
function check_bitop(ret_type, func, args...)
13+
function check_bitop_call(ret_type, func, args...)
1414
r1 = func(args...)
1515
r2 = func(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
16+
check_bitop_tests(ret_type, r1, r2)
17+
end
18+
function check_bitop_dotcall(ret_type, func, args...)
19+
r1 = func.(args...)
20+
r2 = func.(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
21+
check_bitop_tests(ret_type, r1, r2)
22+
end
23+
function check_bitop_tests(ret_type, r1, r2)
1624
@test isa(r1, ret_type)
1725
@test tc(r1, r2)
1826
@test isequal(r1, convert(ret_type, r2))
1927
@test bitcheck(r1)
2028
end
21-
2229
macro check_bit_operation(ex, ret_type)
23-
@assert Meta.isexpr(ex, :call)
24-
Expr(:call, :check_bitop, esc(ret_type), map(esc,ex.args)...)
30+
if Meta.isexpr(ex, :call)
31+
Expr(:call, :check_bitop_call, esc(ret_type), map(esc, ex.args)...)
32+
elseif Meta.isexpr(ex, :.)
33+
Expr(:call, :check_bitop_dotcall, esc(ret_type), esc(ex.args[1]), map(esc, ex.args[2].args)...)
34+
else
35+
throw(ArgumentError("first argument to @check_bit_operation must be an expression with head either :call or :. !"))
36+
end
2537
end
2638

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

613625
b2 = trues(n1, n2)
614-
@check_bit_operation div(b1, b2) BitMatrix
626+
@check_bit_operation div.(b1, b2) BitMatrix
615627
@check_bit_operation mod(b1, b2) BitMatrix
616-
@check_bit_operation div(b1,Array(b2)) BitMatrix
628+
@check_bit_operation div.(b1,Array(b2)) BitMatrix
617629
@check_bit_operation mod(b1,Array(b2)) BitMatrix
618-
@check_bit_operation div(Array(b1),b2) BitMatrix
630+
@check_bit_operation div.(Array(b1),b2) BitMatrix
619631
@check_bit_operation mod(Array(b1),b2) BitMatrix
620632

621633
while true
@@ -649,7 +661,7 @@ i2 = rand(1:10, n1, n2)
649661
@check_bit_operation (.*)(b1, i2) Matrix{Int}
650662
@check_bit_operation (./)(b1, i2) Matrix{Float64}
651663
@check_bit_operation (.^)(b1, i2) BitMatrix
652-
@check_bit_operation div(b1, i2) Matrix{Int}
664+
@check_bit_operation div.(b1, i2) Matrix{Int}
653665
@check_bit_operation mod(b1, i2) Matrix{Int}
654666

655667
# Matrix{Bool}/Matrix{Float64}
@@ -658,7 +670,7 @@ f2 = 1.0 .+ rand(n1, n2)
658670
@check_bit_operation (.*)(b1, f2) Matrix{Float64}
659671
@check_bit_operation (./)(b1, f2) Matrix{Float64}
660672
@check_bit_operation (.^)(b1, f2) Matrix{Float64}
661-
@check_bit_operation div(b1, f2) Matrix{Float64}
673+
@check_bit_operation div.(b1, f2) Matrix{Float64}
662674
@check_bit_operation mod(b1, f2) Matrix{Float64}
663675

664676
# Number/Matrix
@@ -695,22 +707,22 @@ end
695707

696708
b2 = trues(n1, n2)
697709
@check_bit_operation (./)(true, b2) Matrix{Float64}
698-
@check_bit_operation div(true, b2) BitMatrix
710+
@check_bit_operation div.(true, b2) BitMatrix
699711
@check_bit_operation mod(true, b2) BitMatrix
700712
@check_bit_operation (./)(false, b2) Matrix{Float64}
701-
@check_bit_operation div(false, b2) BitMatrix
713+
@check_bit_operation div.(false, b2) BitMatrix
702714
@check_bit_operation mod(false, b2) BitMatrix
703715

704716
@check_bit_operation (./)(i1, b2) Matrix{Float64}
705-
@check_bit_operation div(i1, b2) Matrix{Int}
717+
@check_bit_operation div.(i1, b2) Matrix{Int}
706718
@check_bit_operation mod(i1, b2) Matrix{Int}
707719

708720
@check_bit_operation (./)(u1, b2) Matrix{Float64}
709-
@check_bit_operation div(u1, b2) Matrix{UInt8}
721+
@check_bit_operation div.(u1, b2) Matrix{UInt8}
710722
@check_bit_operation mod(u1, b2) Matrix{UInt8}
711723

712724
@check_bit_operation (./)(f1, b2) Matrix{Float64}
713-
@check_bit_operation div(f1, b2) Matrix{Float64}
725+
@check_bit_operation div.(f1, b2) Matrix{Float64}
714726
@check_bit_operation mod(f1, b2) Matrix{Float64}
715727

716728
@check_bit_operation (./)(ci1, b2) Matrix{Complex128}
@@ -766,7 +778,7 @@ b2 = Array(bitrand(n1,n2))
766778
@check_bit_operation (.*)(false, b1) BitMatrix
767779
@check_bit_operation (./)(b1, true) Matrix{Float64}
768780
@check_bit_operation (./)(b1, false) Matrix{Float64}
769-
@check_bit_operation div(b1, true) BitMatrix
781+
@check_bit_operation div.(b1, true) BitMatrix
770782
@check_bit_operation mod(b1, true) BitMatrix
771783

772784
@check_bit_operation (&)(b1, b2) BitMatrix
@@ -782,7 +794,7 @@ b2 = Array(bitrand(n1,n2))
782794
@check_bit_operation (.-)(b1, i2) Matrix{Int}
783795
@check_bit_operation (.*)(b1, i2) Matrix{Int}
784796
@check_bit_operation (./)(b1, i2) Matrix{Float64}
785-
@check_bit_operation div(b1, i2) Matrix{Int}
797+
@check_bit_operation div.(b1, i2) Matrix{Int}
786798
@check_bit_operation mod(b1, i2) Matrix{Int}
787799

788800
@check_bit_operation (&)(b1, u2) Matrix{UInt8}
@@ -792,14 +804,14 @@ b2 = Array(bitrand(n1,n2))
792804
@check_bit_operation (.-)(b1, u2) Matrix{UInt8}
793805
@check_bit_operation (.*)(b1, u2) Matrix{UInt8}
794806
@check_bit_operation (./)(b1, u2) Matrix{Float64}
795-
@check_bit_operation div(b1, u2) Matrix{UInt8}
807+
@check_bit_operation div.(b1, u2) Matrix{UInt8}
796808
@check_bit_operation mod(b1, u2) Matrix{UInt8}
797809

798810
@check_bit_operation (.+)(b1, f2) Matrix{Float64}
799811
@check_bit_operation (.-)(b1, f2) Matrix{Float64}
800812
@check_bit_operation (.*)(b1, f2) Matrix{Float64}
801813
@check_bit_operation (./)(b1, f2) Matrix{Float64}
802-
@check_bit_operation div(b1, f2) Matrix{Float64}
814+
@check_bit_operation div.(b1, f2) Matrix{Float64}
803815
@check_bit_operation mod(b1, f2) Matrix{Float64}
804816

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

0 commit comments

Comments
 (0)