Skip to content

Commit 4976485

Browse files
authored
Merge pull request #20234 from JuliaLang/mh/broadcast_identity
Add `broadcast!(identity, ::Array, ::Array)` specialization
2 parents 48b6198 + ae7aff1 commit 4976485

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

base/broadcast.jl

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ typealias ScalarType Union{Type{Any}, Type{Nullable}}
1515
# fallbacks for some special cases
1616
@inline broadcast(f, x::Number...) = f(x...)
1717
@inline broadcast{N}(f, t::NTuple{N,Any}, ts::Vararg{NTuple{N,Any}}) = map(f, t, ts...)
18+
broadcast!{T,S,N}(::typeof(identity), x::Array{T,N}, y::Array{S,N}) =
19+
size(x) == size(y) ? copy!(x, y) : broadcast_c!(identity, Array, Array, x, y)
1820

1921
# special cases for "X .= ..." (broadcast!) assignments
2022
broadcast!(::typeof(identity), X::AbstractArray, x::Number) = fill!(X, x)

test/broadcast.jl

+9
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,12 @@ end
449449
@test broadcast(==, [1], AbstractArray) == BitArray([false])
450450
@test broadcast(==, 1, AbstractArray) == false
451451
end
452+
453+
# Test that broadcasting identity where the input and output Array shapes do not match
454+
# yields the correct result, not merely a partial copy. See pull request #19895 for discussion.
455+
let N = 5
456+
@test iszero(ones(N, N) .= zeros(N, N))
457+
@test iszero(ones(N, N) .= zeros(N, 1))
458+
@test iszero(ones(N, N) .= zeros(1, N))
459+
@test iszero(ones(N, N) .= zeros(1, 1))
460+
end

0 commit comments

Comments
 (0)