Skip to content

Commit b2ae5a5

Browse files
stevengjtkelman
authored andcommitted
broadcast!(f, A, x::Number...) should call f(x...) independently for each A[i] (#19799)
* broadcast!(f, A, x::Number...) should call f(x...) independently for each A[i] * expanded comment for 19799
1 parent 1539061 commit b2ae5a5

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

base/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export broadcast_getindex, broadcast_setindex!, dotview
1616

1717
# special cases for "X .= ..." (broadcast!) assignments
1818
broadcast!(::typeof(identity), X::AbstractArray, x::Number) = fill!(X, x)
19-
broadcast!(f, X::AbstractArray, x::Number...) = fill!(X, f(x...))
19+
broadcast!(f, X::AbstractArray, x::Number...) = (@inbounds for I in eachindex(X); X[I] = f(x...); end; X)
2020
function broadcast!{T,S,N}(::typeof(identity), x::AbstractArray{T,N}, y::AbstractArray{S,N})
2121
check_broadcast_shape(broadcast_indices(x), broadcast_indices(y))
2222
copy!(x, y)

test/broadcast.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,10 @@ end
374374
@test (+).([[0,2], [1,3]], [1,-1]) == [[1,3], [0,2]]
375375
@test (+).([[0,2], [1,3]], Ref{Vector{Int}}([1,-1])) == [[1,1], [2,2]]
376376

377-
# Check that broadcast!(f, A) populates A via independent calls to f (#12277, #19722).
377+
# Check that broadcast!(f, A) populates A via independent calls to f (#12277, #19722),
378+
# and similarly for broadcast!(f, A, numbers...) (#19799).
378379
@test let z = 1; A = broadcast!(() -> z += 1, zeros(2)); A[1] != A[2]; end
380+
@test let z = 1; A = broadcast!(x -> z += x, zeros(2), 1); A[1] != A[2]; end
379381

380382
# broadcasting for custom AbstractArray
381383
immutable Array19745{T,N} <: AbstractArray{T,N}

0 commit comments

Comments
 (0)