Skip to content

Commit 20f5ba0

Browse files
committed
Use broadcast for element wise operators where appropriate
1 parent 951aea0 commit 20f5ba0

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

base/broadcast.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ end
240240
end
241241

242242
# broadcast methods that dispatch on the type found by inference
243-
function _broadcast_t(f, ::Type{Any}, shape, iter, As...)
243+
function broadcast_t(f, ::Type{Any}, shape, iter, As...)
244244
nargs = length(As)
245245
keeps, Idefaults = map_newindexer(shape, As)
246246
st = start(iter)
@@ -250,7 +250,7 @@ function _broadcast_t(f, ::Type{Any}, shape, iter, As...)
250250
B[I] = val
251251
return _broadcast!(f, B, keeps, Idefaults, As, Val{nargs}, iter, st, 1)
252252
end
253-
@inline function _broadcast_t(f, T, shape, iter, As...)
253+
@inline function broadcast_t(f, T, shape, iter, As...)
254254
B = similar(Array{T}, shape)
255255
nargs = length(As)
256256
keeps, Idefaults = map_newindexer(shape, As)
@@ -260,7 +260,8 @@ end
260260

261261
# broadcast method that uses inference to find the type, but preserves abstract
262262
# container types when possible (used by binary elementwise operators)
263-
@inline broadcast_t(f, As...) = broadcast!(f, similar(Array{promote_eltype_op(f, As...)}, broadcast_indices(As...)), As...)
263+
@inline broadcast_elwise_op(f, As...) =
264+
broadcast!(f, similar(Array{promote_eltype_op(f, As...)}, broadcast_indices(As...)), As...)
264265

265266
ftype(f, A) = typeof(a->f(a))
266267
ftype(f, A...) = typeof(a->f(a...))
@@ -272,7 +273,7 @@ ziptype(A, B) = Zip2{Tuple{eltype(A)},Tuple{eltype(B)}}
272273

273274
# broadcast methods that dispatch on the type of the final container
274275
@inline function broadcast_c(f, ::Type{Array}, As...)
275-
S = _default_eltype(Generator{ziptype(As...),ftype(f, As...)})
276+
T = _default_eltype(Generator{ziptype(As...),ftype(f, As...)})
276277
shape = broadcast_indices(As...)
277278
iter = CartesianRange(shape)
278279
if isleaftype(T)
@@ -471,10 +472,10 @@ end
471472
## elementwise operators ##
472473

473474
for op in (:÷, :%, :<<, :>>, :-, :/, :\, ://, :^)
474-
@eval $(Symbol(:., op))(A::AbstractArray, B::AbstractArray) = broadcast_t($op, A, B)
475+
@eval $(Symbol(:., op))(A::AbstractArray, B::AbstractArray) = broadcast_elwise_op($op, A, B)
475476
end
476-
.+(As::AbstractArray...) = broadcast_t(+, As...)
477-
.*(As::AbstractArray...) = broadcast_t(*, As...)
477+
.+(As::AbstractArray...) = broadcast_elwise_op(+, As...)
478+
.*(As::AbstractArray...) = broadcast_elwise_op(*, As...)
478479

479480
# ## element-wise comparison operators returning BitArray ##
480481

base/sparse/sparsematrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ broadcast{Tv1,Ti1,Tv2,Ti2}(f::Function, A_1::SparseMatrixCSC{Tv1,Ti1}, A_2::Spar
17461746
broadcast!(f, spzeros(promote_type(Tv1, Tv2), promote_type(Ti1, Ti2), to_shape(broadcast_indices(A_1, A_2))), A_1, A_2)
17471747

17481748
@inline broadcast_zpreserving!(args...) = broadcast!(args...)
1749-
@inline broadcast_zpreserving(args...) = broadcast(args...)
1749+
@inline broadcast_zpreserving(args...) = Base.Broadcast.broadcast_elwise_op(args...)
17501750
broadcast_zpreserving{Tv1,Ti1,Tv2,Ti2}(f::Function, A_1::SparseMatrixCSC{Tv1,Ti1}, A_2::SparseMatrixCSC{Tv2,Ti2}) =
17511751
broadcast_zpreserving!(f, spzeros(promote_type(Tv1, Tv2), promote_type(Ti1, Ti2), to_shape(broadcast_indices(A_1, A_2))), A_1, A_2)
17521752
broadcast_zpreserving{Tv,Ti}(f::Function, A_1::SparseMatrixCSC{Tv,Ti}, A_2::Union{Array,BitArray,Number}) =

0 commit comments

Comments
 (0)