Skip to content

Commit

Permalink
Use broadcast for some binary array operations
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Dec 29, 2016
1 parent fc63178 commit a2e34c1
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,10 @@ promote_array_type{S<:Integer}(::typeof(\), ::Type{S}, ::Type{Bool}, T::Type) =
promote_array_type{S<:Integer}(F, ::Type{S}, ::Type{Bool}, T::Type) = T

for f in (:+, :-, :div, :mod, :&, :|)
@eval ($f)(A::AbstractArray, B::AbstractArray) =
_elementwise($f, promote_eltype_op($f, A, B), A, B)
end
function _elementwise(op, ::Type{Any}, A::AbstractArray, B::AbstractArray)
promote_shape(A, B) # check size compatibility
return broadcast(op, A, B)
end
function _elementwise{T}(op, ::Type{T}, A::AbstractArray, B::AbstractArray)
F = similar(A, T, promote_shape(A, B))
RF, RA, RB = eachindex(F), eachindex(A), eachindex(B)
if RF == RA == RB
for i in RA
@inbounds F[i] = op(A[i], B[i])
end
else
for (iF, iA, iB) in zip(RF, RA, RB)
@inbounds F[iF] = op(A[iA], B[iB])
end
@eval function ($f)(A::AbstractArray, B::AbstractArray)
promote_shape(A, B) # check size compatibility
broadcast($f, A, B)
end
return F
end

for f in (:div, :mod, :rem, :&, :|, :/, :\, :*, :+, :-)
Expand Down

0 comments on commit a2e34c1

Please sign in to comment.