Skip to content

Commit 5d9754a

Browse files
authored
Merge pull request #18 from mcabbott/master
Faster sum(BroadcastArray)
2 parents 4559ba2 + 5c99cbb commit 5d9754a

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/lazybroadcasting.jl

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ getindex(B::BroadcastArray{<:Any,1}, kr::AbstractVector{<:Integer}) =
3737

3838
copy(bc::Broadcasted{<:LazyArrayStyle}) = BroadcastArray(bc)
3939

40+
# issue 16: sum(b, dims=(1,2,3)) faster than sum(b)
41+
Base._sum(b::BroadcastArray{T,N}, ::Colon) where {T,N} = first(Base._sum(b, ntuple(identity, N)))
42+
Base._prod(b::BroadcastArray{T,N}, ::Colon) where {T,N} = first(Base._prod(b, ntuple(identity, N)))
43+
4044
BroadcastStyle(::Type{<:BroadcastArray{<:Any,N}}) where N = LazyArrayStyle{N}()
4145
BroadcastStyle(L::LazyArrayStyle{N}, ::StaticArrayStyle{N}) where N = L
4246
BroadcastStyle(::StaticArrayStyle{N}, L::LazyArrayStyle{N}) where N = L

test/runtests.jl

+3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ end
171171
@test C == A .+ 2
172172
D = BroadcastArray(+, A, C)
173173
@test D == A + C
174+
175+
@test sum(B) sum(exp, A)
176+
@test sum(C) sum(A .+ 2)
174177

175178
x = Vcat([3,4], [1,1,1,1,1], 1:3)
176179
@test x .+ (1:10) isa Vcat

0 commit comments

Comments
 (0)