Skip to content

Commit

Permalink
Deprecate manually vectorized mod methods in favor of compact broadca…
Browse files Browse the repository at this point in the history
…st syntax.
  • Loading branch information
Sacha0 committed Dec 31, 2016
1 parent 6306f86 commit 90f5c24
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 50 deletions.
4 changes: 2 additions & 2 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 (:+, :-, :mod, :&, :|)
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 (:mod, :&, :|, :/, :\, :*, :+, :-)
for f in (:&, :|, :/, :\, :*, :+, :-)
if f != :/
@eval ($f){T}(A::Number, B::AbstractArray{T}) = broadcast($f, A, B)
end
Expand Down
34 changes: 0 additions & 34 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1237,40 +1237,6 @@ end
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
(/)(x::Number, B::BitArray) = (/)(x, Array(B))

function mod(A::BitArray, B::BitArray)
shp = promote_shape(size(A), size(B))
all(B) || throw(DivideError())
return falses(shp)
end
mod(A::BitArray, B::Array{Bool}) = mod(A, BitArray(B))
mod(A::Array{Bool}, B::BitArray) = mod(BitArray(A), B)
function mod(B::BitArray, x::Bool)
return x ? falses(size(B)) : throw(DivideError())
end
function mod(x::Bool, B::BitArray)
all(B) || throw(DivideError())
return falses(size(B))
end
function mod(x::Number, B::BitArray)
all(B) || throw(DivideError())
y = mod(x, true)
return fill(y, size(B))
end

for f in (:mod,)
@eval begin
function ($f)(B::BitArray, x::Number)
T = promote_op($f, Bool, typeof(x))
T === Any && return [($f)(b, x) for b in B]
F = Array{T}(size(B))
for i = 1:length(F)
F[i] = ($f)(B[i], x)
end
return F
end
end
end

function (&)(B::BitArray, x::Bool)
x ? copy(B) : falses(size(B))
end
Expand Down
7 changes: 7 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1418,4 +1418,11 @@ end
@deprecate rem(A::Number, B::AbstractArray) rem.(A, B)
@deprecate rem(A::AbstractArray, B::Number) rem.(A, B)

# Deprecate manually vectorized mod methods in favor of compact broadcast syntax
@deprecate mod(B::BitArray, x::Bool) mod.(B, x)
@deprecate mod(x::Bool, B::BitArray) mod.(x, B)
@deprecate mod(A::AbstractArray, B::AbstractArray) mod.(A, B)
@deprecate mod{T}(x::Number, A::AbstractArray{T}) mod.(x, A)
@deprecate mod{T}(A::AbstractArray{T}, x::Number) mod.(A, x)

# End deprecations scheduled for 0.6
28 changes: 14 additions & 14 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,11 @@ let b1 = bitrand(n1, n2)

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

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

# Matrix{Bool}/Matrix{Float64}
Expand All @@ -842,7 +842,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(div, b1, f2) Matrix{Float64}
@check_bit_operation mod(b1, f2) Matrix{Float64}
@check_bit_operation broadcast(mod, b1, f2) Matrix{Float64}
end

# Number/Matrix
Expand Down Expand Up @@ -881,22 +881,22 @@ let b2 = bitrand(n1, n2)
b2 = trues(n1, n2)
@check_bit_operation broadcast(/, true, b2) Matrix{Float64}
@check_bit_operation broadcast(div, true, b2) BitMatrix
@check_bit_operation mod(true, b2) BitMatrix
@check_bit_operation broadcast(mod, true, b2) BitMatrix
@check_bit_operation broadcast(/, false, b2) Matrix{Float64}
@check_bit_operation broadcast(div, false, b2) BitMatrix
@check_bit_operation mod(false, b2) BitMatrix
@check_bit_operation broadcast(mod, false, b2) BitMatrix

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

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

@check_bit_operation broadcast(/, 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(mod, f1, b2) Matrix{Float64}

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

@check_bit_operation (&)(b1, b2) BitMatrix
@check_bit_operation (|)(b1, b2) BitMatrix
Expand All @@ -970,7 +970,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(div, b1, i2) Matrix{Int}
@check_bit_operation mod(b1, i2) Matrix{Int}
@check_bit_operation broadcast(mod, b1, i2) Matrix{Int}

@check_bit_operation (&)(b1, u2) Matrix{UInt8}
@check_bit_operation (|)(b1, u2) Matrix{UInt8}
Expand All @@ -980,14 +980,14 @@ let b1 = bitrand(n1, n2)
@check_bit_operation broadcast(*, b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(/, b1, u2) Matrix{Float64}
@check_bit_operation broadcast(div, b1, u2) Matrix{UInt8}
@check_bit_operation mod(b1, u2) Matrix{UInt8}
@check_bit_operation broadcast(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 broadcast(div, b1, f2) Matrix{Float64}
@check_bit_operation mod(b1, f2) Matrix{Float64}
@check_bit_operation broadcast(mod, b1, f2) Matrix{Float64}

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

0 comments on commit 90f5c24

Please sign in to comment.