Skip to content

Commit 8c85a8c

Browse files
committed
Deprecate manually vectorized div methods in favor of compact broadcast syntax.
1 parent 44d7677 commit 8c85a8c

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
lines changed

base/arraymath.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ promote_array_type{S<:Integer}(::typeof(/), ::Type{S}, ::Type{Bool}, T::Type) =
6666
promote_array_type{S<:Integer}(::typeof(\), ::Type{S}, ::Type{Bool}, T::Type) = T
6767
promote_array_type{S<:Integer}(F, ::Type{S}, ::Type{Bool}, T::Type) = T
6868

69-
for f in (:+, :-, :div, :mod, :&, :|, :xor)
69+
for f in (:+, :-, :mod, :&, :|, :xor)
7070
@eval ($f)(A::AbstractArray, B::AbstractArray) =
7171
_elementwise($f, promote_eltype_op($f, A, B), A, B)
7272
end
@@ -89,7 +89,7 @@ function _elementwise{T}(op, ::Type{T}, A::AbstractArray, B::AbstractArray)
8989
return F
9090
end
9191

92-
for f in (:div, :mod, :rem, :&, :|, :xor, :/, :\, :*, :+, :-)
92+
for f in (:mod, :rem, :&, :|, :xor, :/, :\, :*, :+, :-)
9393
if f != :/
9494
@eval function ($f){T}(A::Number, B::AbstractArray{T})
9595
R = promote_op($f, typeof(A), T)

base/bitarray.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,21 +1237,19 @@ end
12371237
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
12381238
(/)(x::Number, B::BitArray) = (/)(x, Array(B))
12391239

1240-
function div(A::BitArray, B::BitArray)
1240+
function broadcast(::typeof(div), A::BitArray, B::BitArray)
12411241
shp = promote_shape(size(A), size(B))
12421242
all(B) || throw(DivideError())
12431243
return reshape(copy(A), shp)
12441244
end
1245-
div(A::BitArray, B::Array{Bool}) = div(A, BitArray(B))
1246-
div(A::Array{Bool}, B::BitArray) = div(BitArray(A), B)
1247-
function div(B::BitArray, x::Bool)
1248-
return x ? copy(B) : throw(DivideError())
1249-
end
1250-
function div(x::Bool, B::BitArray)
1245+
broadcast(::typeof(div), A::BitArray, B::Array{Bool}) = div.(A, BitArray(B))
1246+
broadcast(::typeof(div), A::Array{Bool}, B::BitArray) = div.(BitArray(A), B)
1247+
broadcast(::typeof(div), B::BitArray, x::Bool) = x ? copy(B) : throw(DivideError())
1248+
function broadcast(::typeof(div), x::Bool, B::BitArray)
12511249
all(B) || throw(DivideError())
12521250
return x ? trues(size(B)) : falses(size(B))
12531251
end
1254-
function div(x::Number, B::BitArray)
1252+
function broadcast(::typeof(div), x::Number, B::BitArray)
12551253
all(B) || throw(DivideError())
12561254
y = div(x, true)
12571255
return fill(y, size(B))
@@ -1276,8 +1274,17 @@ function mod(x::Number, B::BitArray)
12761274
y = mod(x, true)
12771275
return fill(y, size(B))
12781276
end
1277+
function broadcast(::typeof(div), B::BitArray, x::Number)
1278+
T = promote_op(div, Bool, typeof(x))
1279+
T === Any && return [div(b, x) for b in B]
1280+
F = Array{T}(size(B))
1281+
for i = 1:length(F)
1282+
F[i] = div(B[i], x)
1283+
end
1284+
return F
1285+
end
12791286

1280-
for f in (:div, :mod)
1287+
for f in (:mod,)
12811288
@eval begin
12821289
function ($f)(B::BitArray, x::Number)
12831290
T = promote_op($f, Bool, typeof(x))

base/deprecated.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,4 +1168,9 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs),
11681168
end
11691169
end
11701170

1171+
# Deprecate manually vectorized div methods in favor of compact broadcast syntax
1172+
@deprecate div(A::Number, B::AbstractArray) div.(A, B)
1173+
@deprecate div(A::AbstractArray, B::Number) div.(A, B)
1174+
@deprecate div(A::AbstractArray, B::AbstractArray) div.(A, B)
1175+
11711176
# 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: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,41 @@ tc(r1,r2) = false
1313
bitcheck(b::BitArray) = Base._check_bitarray_consistency(b)
1414
bitcheck(x) = true
1515

16-
function check_bitop(ret_type, func, args...)
16+
function check_bitop_call(ret_type, func, args...)
1717
r1 = func(args...)
1818
r2 = func(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
19+
check_bitop_tests(ret_type, r1, r2)
20+
end
21+
function check_bitop_dotcall(ret_type, func, args...)
22+
r1 = func.(args...)
23+
r2 = func.(map(x->(isa(x, BitArray) ? Array(x) : x), args)...)
24+
check_bitop_tests(ret_type, r1, r2)
25+
end
26+
function check_bitop_tests(ret_type, r1, r2)
1927
ret_type nothing && !isa(r1, ret_type) && @show ret_type, r1
2028
ret_type nothing && @test isa(r1, ret_type)
2129
@test tc(r1, r2)
2230
@test isequal(r1, ret_type nothing ? r2 : convert(ret_type, r2))
2331
@test bitcheck(r1)
2432
end
25-
2633
macro check_bit_operation(ex, ret_type)
27-
@assert Meta.isexpr(ex, :call)
28-
Expr(:call, :check_bitop, esc(ret_type), map(esc,ex.args)...)
34+
if Meta.isexpr(ex, :call)
35+
Expr(:call, :check_bitop_call, esc(ret_type), map(esc, ex.args)...)
36+
elseif Meta.isexpr(ex, :.)
37+
Expr(:call, :check_bitop_dotcall, esc(ret_type), esc(ex.args[1]), map(esc, ex.args[2].args)...)
38+
else
39+
throw(ArgumentError("first argument to @check_bit_operation must be an expression with head either :call or :. !"))
40+
end
2941
end
3042

3143
macro check_bit_operation(ex)
32-
@assert Meta.isexpr(ex, :call)
33-
Expr(:call, :check_bitop, nothing, map(esc,ex.args)...)
44+
if Meta.isexpr(ex, :call)
45+
Expr(:call, :check_bitop_call, nothing, map(esc, ex.args)...)
46+
elseif Meta.isexpr(ex, :.)
47+
Expr(:call, :check_bitop_dotcall, nothing, esc(ex.args[1]), map(esc, ex.args[2].args)...)
48+
else
49+
throw(ArgumentError("first argument to @check_bit_operation must be an expression with head either :call or :. !"))
50+
end
3451
end
3552

3653
let t0 = time()
@@ -793,11 +810,11 @@ let b1 = bitrand(n1, n2)
793810
@check_bit_operation (/)(b1,1) Matrix{Float64}
794811

795812
b2 = trues(n1, n2)
796-
@check_bit_operation div(b1, b2) BitMatrix
813+
@check_bit_operation broadcast(div, b1, b2) BitMatrix
797814
@check_bit_operation mod(b1, b2) BitMatrix
798-
@check_bit_operation div(b1,Array(b2)) BitMatrix
815+
@check_bit_operation broadcast(div, b1, Array(b2)) BitMatrix
799816
@check_bit_operation mod(b1,Array(b2)) BitMatrix
800-
@check_bit_operation div(Array(b1),b2) BitMatrix
817+
@check_bit_operation broadcast(div, Array(b1), b2) BitMatrix
801818
@check_bit_operation mod(Array(b1),b2) BitMatrix
802819
end
803820

@@ -832,7 +849,7 @@ let b1 = bitrand(n1, n2)
832849
@check_bit_operation broadcast(*, b1, i2) Matrix{Int}
833850
@check_bit_operation broadcast(/, b1, i2) Matrix{Float64}
834851
@check_bit_operation broadcast(^, b1, i2) BitMatrix
835-
@check_bit_operation div(b1, i2) Matrix{Int}
852+
@check_bit_operation broadcast(div, b1, i2) Matrix{Int}
836853
@check_bit_operation mod(b1, i2) Matrix{Int}
837854
end
838855

@@ -843,7 +860,7 @@ let b1 = bitrand(n1, n2)
843860
@check_bit_operation broadcast(*, b1, f2) Matrix{Float64}
844861
@check_bit_operation broadcast(/, b1, f2) Matrix{Float64}
845862
@check_bit_operation broadcast(^, b1, f2) Matrix{Float64}
846-
@check_bit_operation div(b1, f2) Matrix{Float64}
863+
@check_bit_operation broadcast(div, b1, f2) Matrix{Float64}
847864
@check_bit_operation mod(b1, f2) Matrix{Float64}
848865
end
849866

@@ -882,22 +899,22 @@ let b2 = bitrand(n1, n2)
882899

883900
b2 = trues(n1, n2)
884901
@check_bit_operation broadcast(/, true, b2) Matrix{Float64}
885-
@check_bit_operation div(true, b2) BitMatrix
902+
@check_bit_operation broadcast(div, true, b2) BitMatrix
886903
@check_bit_operation mod(true, b2) BitMatrix
887904
@check_bit_operation broadcast(/, false, b2) Matrix{Float64}
888-
@check_bit_operation div(false, b2) BitMatrix
905+
@check_bit_operation broadcast(div, false, b2) BitMatrix
889906
@check_bit_operation mod(false, b2) BitMatrix
890907

891908
@check_bit_operation broadcast(/, i1, b2) Matrix{Float64}
892-
@check_bit_operation div(i1, b2) Matrix{Int}
909+
@check_bit_operation broadcast(div, i1, b2) Matrix{Int}
893910
@check_bit_operation mod(i1, b2) Matrix{Int}
894911

895912
@check_bit_operation broadcast(/, u1, b2) Matrix{Float64}
896-
@check_bit_operation div(u1, b2) Matrix{UInt8}
913+
@check_bit_operation broadcast(div, u1, b2) Matrix{UInt8}
897914
@check_bit_operation mod(u1, b2) Matrix{UInt8}
898915

899916
@check_bit_operation broadcast(/, f1, b2) Matrix{Float64}
900-
@check_bit_operation div(f1, b2) Matrix{Float64}
917+
@check_bit_operation broadcast(div, f1, b2) Matrix{Float64}
901918
@check_bit_operation mod(f1, b2) Matrix{Float64}
902919

903920
@check_bit_operation broadcast(/, ci1, b2) Matrix{Complex128}
@@ -955,7 +972,7 @@ let b1 = bitrand(n1, n2)
955972
@check_bit_operation broadcast(*, false, b1) BitMatrix
956973
@check_bit_operation broadcast(/, b1, true) Matrix{Float64}
957974
@check_bit_operation broadcast(/, b1, false) Matrix{Float64}
958-
@check_bit_operation div(b1, true) BitMatrix
975+
@check_bit_operation broadcast(div, b1, true) BitMatrix
959976
@check_bit_operation mod(b1, true) BitMatrix
960977

961978
@check_bit_operation (&)(b1, b2) BitMatrix
@@ -971,7 +988,7 @@ let b1 = bitrand(n1, n2)
971988
@check_bit_operation broadcast(-, b1, i2) Matrix{Int}
972989
@check_bit_operation broadcast(*, b1, i2) Matrix{Int}
973990
@check_bit_operation broadcast(/, b1, i2) Matrix{Float64}
974-
@check_bit_operation div(b1, i2) Matrix{Int}
991+
@check_bit_operation broadcast(div, b1, i2) Matrix{Int}
975992
@check_bit_operation mod(b1, i2) Matrix{Int}
976993

977994
@check_bit_operation (&)(b1, u2) Matrix{UInt8}
@@ -981,14 +998,14 @@ let b1 = bitrand(n1, n2)
981998
@check_bit_operation broadcast(-, b1, u2) Matrix{UInt8}
982999
@check_bit_operation broadcast(*, b1, u2) Matrix{UInt8}
9831000
@check_bit_operation broadcast(/, b1, u2) Matrix{Float64}
984-
@check_bit_operation div(b1, u2) Matrix{UInt8}
1001+
@check_bit_operation broadcast(div, b1, u2) Matrix{UInt8}
9851002
@check_bit_operation mod(b1, u2) Matrix{UInt8}
9861003

9871004
@check_bit_operation broadcast(+, b1, f2) Matrix{Float64}
9881005
@check_bit_operation broadcast(-, b1, f2) Matrix{Float64}
9891006
@check_bit_operation broadcast(*, b1, f2) Matrix{Float64}
9901007
@check_bit_operation broadcast(/, b1, f2) Matrix{Float64}
991-
@check_bit_operation div(b1, f2) Matrix{Float64}
1008+
@check_bit_operation broadcast(div, b1, f2) Matrix{Float64}
9921009
@check_bit_operation mod(b1, f2) Matrix{Float64}
9931010

9941011
@check_bit_operation broadcast(+, b1, ci2) Matrix{Complex{Int}}

0 commit comments

Comments
 (0)