Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate vectorized div methods in favor of compact broadcast syntax #18607

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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, :&, :|, :xor)
for f in (:+, :-, :mod, :&, :|, :xor)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This change moves div's promotion behavior in the direction of #19669.)

@eval ($f)(A::AbstractArray, B::AbstractArray) =
_elementwise($f, promote_eltype_op($f, A, B), A, B)
end
Expand All @@ -89,7 +89,7 @@ function _elementwise{T}(op, ::Type{T}, A::AbstractArray, B::AbstractArray)
return F
end

for f in (:div, :mod, :rem, :&, :|, :xor, :/, :\, :*, :+, :-)
for f in (:mod, :rem, :&, :|, :xor, :/, :\, :*, :+, :-)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Likewise here, this change moves div's promotion behavior in the direction of #19669.)

if f != :/
@eval function ($f){T}(A::Number, B::AbstractArray{T})
R = promote_op($f, typeof(A), T)
Expand Down
22 changes: 1 addition & 21 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1237,26 +1237,6 @@ end
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
(/)(x::Number, B::BitArray) = (/)(x, Array(B))

function 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)
all(B) || throw(DivideError())
return x ? trues(size(B)) : falses(size(B))
end
function div(x::Number, B::BitArray)
all(B) || throw(DivideError())
y = div(x, true)
return fill(y, size(B))
end

function mod(A::BitArray, B::BitArray)
shp = promote_shape(size(A), size(B))
all(B) || throw(DivideError())
Expand All @@ -1277,7 +1257,7 @@ function mod(x::Number, B::BitArray)
return fill(y, size(B))
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
5 changes: 5 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1168,4 +1168,9 @@ for (dep, f, op) in [(:sumabs!, :sum!, :abs),
end
end

# 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
36 changes: 17 additions & 19 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tc(r1,r2) = false
bitcheck(b::BitArray) = Base._check_bitarray_consistency(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)...)
ret_type ≢ nothing && !isa(r1, ret_type) && @show ret_type, r1
Expand All @@ -22,15 +22,13 @@ function check_bitop(ret_type, func, args...)
@test isequal(r1, ret_type ≡ nothing ? r2 : 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)...)
Expr(:call, :check_bitop_call, esc(ret_type), map(esc, ex.args)...)
end

macro check_bit_operation(ex)
@assert Meta.isexpr(ex, :call)
Expr(:call, :check_bitop, nothing, map(esc,ex.args)...)
Expr(:call, :check_bitop_call, nothing, map(esc, ex.args)...)
end

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

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

Expand Down Expand Up @@ -832,7 +830,7 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(*, b1, i2) Matrix{Int}
@check_bit_operation broadcast(/, b1, i2) Matrix{Float64}
@check_bit_operation broadcast(^, b1, i2) BitMatrix
@check_bit_operation div(b1, i2) Matrix{Int}
@check_bit_operation broadcast(div, b1, i2) Matrix{Int}
@check_bit_operation mod(b1, i2) Matrix{Int}
end

Expand All @@ -843,7 +841,7 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(*, b1, f2) Matrix{Float64}
@check_bit_operation broadcast(/, b1, f2) Matrix{Float64}
@check_bit_operation broadcast(^, b1, f2) Matrix{Float64}
@check_bit_operation div(b1, f2) Matrix{Float64}
@check_bit_operation broadcast(div, b1, f2) Matrix{Float64}
@check_bit_operation mod(b1, f2) Matrix{Float64}
end

Expand Down Expand Up @@ -882,22 +880,22 @@ let b2 = bitrand(n1, n2)

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

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

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

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

@check_bit_operation broadcast(/, ci1, b2) Matrix{Complex128}
Expand Down Expand Up @@ -955,7 +953,7 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(*, false, b1) BitMatrix
@check_bit_operation broadcast(/, b1, true) Matrix{Float64}
@check_bit_operation broadcast(/, b1, false) Matrix{Float64}
@check_bit_operation div(b1, true) BitMatrix
@check_bit_operation broadcast(div, b1, true) BitMatrix
@check_bit_operation mod(b1, true) BitMatrix

@check_bit_operation (&)(b1, b2) BitMatrix
Expand All @@ -971,7 +969,7 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(-, b1, i2) Matrix{Int}
@check_bit_operation broadcast(*, b1, i2) Matrix{Int}
@check_bit_operation broadcast(/, b1, i2) Matrix{Float64}
@check_bit_operation div(b1, i2) Matrix{Int}
@check_bit_operation broadcast(div, b1, i2) Matrix{Int}
@check_bit_operation mod(b1, i2) Matrix{Int}

@check_bit_operation (&)(b1, u2) Matrix{UInt8}
Expand All @@ -981,14 +979,14 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(-, b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(*, b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(/, b1, u2) Matrix{Float64}
@check_bit_operation div(b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(div, b1, u2) Matrix{UInt8}
@check_bit_operation mod(b1, u2) Matrix{UInt8}

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

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