Skip to content

Commit 2a07f76

Browse files
committed
add init kwarg to sum and prod as well (fix JuliaArrays#1119)
1 parent 23c1b32 commit 2a07f76

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/mapreduce.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,13 @@ reduce(::typeof(hcat), A::StaticArray{<:Tuple,<:StaticVecOrMatLike}) =
284284
# TODO: change to use Base.reduce_empty/Base.reduce_first
285285
@inline iszero(a::StaticArray{<:Tuple,T}) where {T} = reduce((x,y) -> x && iszero(y), a, init=true)
286286

287-
@inline sum(a::StaticArray{<:Tuple,T}; dims=:) where {T} = _reduce(+, a, dims)
288-
@inline sum(f, a::StaticArray{<:Tuple,T}; dims=:) where {T} = _mapreduce(f, +, dims, _InitialValue(), Size(a), a)
289-
@inline sum(f::Union{Function, Type}, a::StaticArray{<:Tuple,T}; dims=:) where {T} = _mapreduce(f, +, dims, _InitialValue(), Size(a), a) # avoid ambiguity
287+
@inline sum(a::StaticArray{<:Tuple,T}; dims=:, init=_IntialialValue()) where {T} = _reduce(+, a, dims, init)
288+
@inline sum(f, a::StaticArray{<:Tuple,T}; dims=:, init=_IntialialValue()) where {T} = _mapreduce(f, +, dims, init, Size(a), a)
289+
@inline sum(f::Union{Function, Type}, a::StaticArray{<:Tuple,T}; dims=:, init=_IntialialValue()) where {T} = _mapreduce(f, +, dims, init, Size(a), a) # avoid ambiguity
290290

291-
@inline prod(a::StaticArray{<:Tuple,T}; dims=:) where {T} = _reduce(*, a, dims)
292-
@inline prod(f, a::StaticArray{<:Tuple,T}; dims=:) where {T} = _mapreduce(f, *, dims, _InitialValue(), Size(a), a)
293-
@inline prod(f::Union{Function, Type}, a::StaticArray{<:Tuple,T}; dims=:) where {T} = _mapreduce(f, *, dims, _InitialValue(), Size(a), a)
291+
@inline prod(a::StaticArray{<:Tuple,T}; dims=:, init=_IntialialValue()) where {T} = _reduce(*, a, dims, init)
292+
@inline prod(f, a::StaticArray{<:Tuple,T}; dims=:, init=_IntialialValue()) where {T} = _mapreduce(f, *, dims, init, Size(a), a)
293+
@inline prod(f::Union{Function, Type}, a::StaticArray{<:Tuple,T}; dims=:, init=_IntialialValue()) where {T} = _mapreduce(f, *, dims, init, Size(a), a)
294294

295295
@inline count(a::StaticArray{<:Tuple,Bool}; dims=:, init=0) = _reduce(+, a, dims, init)
296296
@inline count(f, a::StaticArray; dims=:, init=0) = _mapreduce(x->f(x)::Bool, +, dims, init, Size(a), a)

test/mapreduce.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,22 @@ using Statistics: mean
130130
@test sum(sa, dims=Val(2)) === RSArray2(sum(a, dims=2))
131131
@test sum(abs2, sa; dims=2) === RSArray2(sum(abs2, a, dims=2))
132132
@test sum(abs2, sa; dims=Val(2)) === RSArray2(sum(abs2, a, dims=2))
133+
@test sum(sa, init=2) == sum(a, init=2) == sum(sa) + 2
134+
@test sum(sb, init=2) == sum(b, init=2) == sum(sb) + 2
133135

134136
@test prod(sa) === prod(a)
135137
@test prod(abs2, sa) === prod(abs2, a)
136138
@test prod(sa, dims=Val(2)) === RSArray2(prod(a, dims=2))
137139
@test prod(abs2, sa, dims=Val(2)) === RSArray2(prod(abs2, a, dims=2))
140+
@test prod(sa, init=2) == prod(a, init=2) == 2*prod(sa)
141+
@test prod(sb, init=2) == prod(b, init=2) == 2*prod(sb)
138142

139143
@test count(sb) === count(b)
140-
@test count(sb, init=3) == count(b, init=3) == count(sb) + 3
141144
@test count(x->x>0, sa) === count(x->x>0, a)
142-
@test count(x->x>0, sa, init=-2) == count(x->x>0, a, init=-2) == count(x->x>0, sa) - 2
143145
@test count(sb, dims=Val(2)) === RSArray2(reshape([count(b[i,:,k]) for i = 1:I, k = 1:K], (I,1,K)))
144146
@test count(x->x>0, sa, dims=Val(2)) === RSArray2(reshape([count(x->x>0, a[i,:,k]) for i = 1:I, k = 1:K], (I,1,K)))
147+
@test count(sb, init=3) == count(b, init=3) == count(sb) + 3
148+
@test count(x->x>0, sa, init=-2) == count(x->x>0, a, init=-2) == count(x->x>0, sa) - 2
145149

146150
@test all(sb) === all(b)
147151
@test all(x->x>0, sa) === all(x->x>0, a)

0 commit comments

Comments
 (0)