Skip to content

Commit

Permalink
Merge pull request #18 from mcabbott/master
Browse files Browse the repository at this point in the history
Faster sum(BroadcastArray)
  • Loading branch information
dlfivefifty authored Jan 24, 2019
2 parents 4559ba2 + 5c99cbb commit 5d9754a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lazybroadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ getindex(B::BroadcastArray{<:Any,1}, kr::AbstractVector{<:Integer}) =

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

# issue 16: sum(b, dims=(1,2,3)) faster than sum(b)
Base._sum(b::BroadcastArray{T,N}, ::Colon) where {T,N} = first(Base._sum(b, ntuple(identity, N)))
Base._prod(b::BroadcastArray{T,N}, ::Colon) where {T,N} = first(Base._prod(b, ntuple(identity, N)))

BroadcastStyle(::Type{<:BroadcastArray{<:Any,N}}) where N = LazyArrayStyle{N}()
BroadcastStyle(L::LazyArrayStyle{N}, ::StaticArrayStyle{N}) where N = L
BroadcastStyle(::StaticArrayStyle{N}, L::LazyArrayStyle{N}) where N = L
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ end
@test C == A .+ 2
D = BroadcastArray(+, A, C)
@test D == A + C

@test sum(B) sum(exp, A)
@test sum(C) sum(A .+ 2)

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

0 comments on commit 5d9754a

Please sign in to comment.