Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement accumulate and friends for Tuple #34654

Merged
merged 4 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ New library features

* `isapprox` (or `≈`) now has a one-argument "curried" method `isapprox(x)` which returns a function, like `isequal` (or `==`)` ([#32305]).
* `Ref{NTuple{N,T}}` can be passed to `Ptr{T}`/`Ref{T}` `ccall` signatures ([#34199])
* `accumulate`, `cumsum`, and `cumprod` now support `Tuple` ([#34654]).


Standard library changes
Expand Down
31 changes: 27 additions & 4 deletions base/accumulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ function cumsum(A::AbstractArray{T}; dims::Integer) where T
end

"""
cumsum(x::AbstractVector)
cumsum(itr::Union{AbstractVector,Tuple})

Cumulative sum a vector. See also [`cumsum!`](@ref)
Cumulative sum an iterator. See also [`cumsum!`](@ref)
to use a preallocated output array, both for performance and to control the precision of the
output (e.g. to avoid overflow).

!!! compat "Julia 1.5"
`cumsum` on a tuple requires at least Julia 1.5.

# Examples
```jldoctest
julia> cumsum([1, 1, 1])
Expand All @@ -111,9 +114,13 @@ julia> cumsum([fill(1, 2) for i in 1:3])
[1, 1]
[2, 2]
[3, 3]

julia> cumsum((1, 1, 1))
(1, 2, 3)
```
"""
cumsum(x::AbstractVector) = cumsum(x, dims=1)
cumsum(itr) = accumulate(add_sum, itr)


"""
Expand Down Expand Up @@ -163,12 +170,15 @@ function cumprod(A::AbstractArray; dims::Integer)
end

"""
cumprod(x::AbstractVector)
cumprod(itr::Union{AbstractVector,Tuple})

Cumulative product of a vector. See also
Cumulative product of an iterator. See also
[`cumprod!`](@ref) to use a preallocated output array, both for performance and
to control the precision of the output (e.g. to avoid overflow).

!!! compat "Julia 1.5"
`cumprod` on a tuple requires at least Julia 1.5.

# Examples
```jldoctest
julia> cumprod(fill(1//2, 3))
Expand All @@ -182,9 +192,13 @@ julia> cumprod([fill(1//3, 2, 2) for i in 1:3])
[1//3 1//3; 1//3 1//3]
[2//9 2//9; 2//9 2//9]
[4//27 4//27; 4//27 4//27]

julia> cumprod((1, 2, 1))
(1, 2, 2)
```
"""
cumprod(x::AbstractVector) = cumprod(x, dims=1)
cumprod(itr) = accumulate(mul_prod, itr)


"""
Expand Down Expand Up @@ -247,6 +261,15 @@ function accumulate(op, A; dims::Union{Nothing,Integer}=nothing, kw...)
accumulate!(op, out, A; dims=dims, kw...)
end

function accumulate(op, xs::Tuple; init = _InitialValue())
rf = BottomRF(op)
ys, = foldl(xs; init = ((), init)) do (ys, acc), x
acc = rf(acc, x)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this uses the machinery I added in #33526

julia/base/reduce.jl

Lines 73 to 78 in 3720edf

struct BottomRF{T}
rf::T
end
@inline (op::BottomRF)(::_InitialValue, x) = reduce_first(op.rf, x)
@inline (op::BottomRF)(acc, x) = op.rf(acc, x)

(ys..., acc), acc
end
return ys
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this implementation lead to unrolled code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It const-folds, as shown in the OP. It is also unrolled when the tuple is not constant:

julia> @code_typed optimize=true cumsum(ntuple(identity, 10))
CodeInfo(
1 ── %1  = (getfield)(itr, 1)::Int64%2  = (getfield)(itr, 2)::Int64%3  = (getfield)(itr, 3)::Int64%4  = (getfield)(itr, 4)::Int64%5  = (getfield)(itr, 5)::Int64%6  = (getfield)(itr, 6)::Int64%7  = (getfield)(itr, 7)::Int64%8  = (getfield)(itr, 8)::Int64%9  = (getfield)(itr, 9)::Int64%10 = (getfield)(itr, 10)::Int64%11 = Base.add_int(%1, %2)::Int64%12 = Base.add_int(%11, %3)::Int64%13 = Base.add_int(%12, %4)::Int64%14 = Base.add_int(%13, %5)::Int64%15 = Base.add_int(%14, %6)::Int64%16 = Base.add_int(%15, %7)::Int64%17 = Base.add_int(%16, %8)::Int64%18 = Base.add_int(%17, %9)::Int64%19 = Base.add_int(%18, %10)::Int64%20 = Core.tuple(%1, %11, %12, %13, %14, %15, %16, %17, %18, %19)::NTuple{10,Int64}
└───       goto #3
2 ──       $(Expr(:meta, :nkw, 1))
3 ┄─       goto #4
4 ──       goto #6
5 ──       $(Expr(:meta, :nkw, 1))
6 ┄─       goto #7
7 ──       goto #9
8 ──       $(Expr(:meta, :nkw, 1))
9 ┄─       goto #10
10return %20
) => NTuple{10,Int64}


"""
accumulate!(op, B, A; [dims], [init])

Expand Down
11 changes: 11 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,17 @@ end
end
end

@testset "accumulate" begin
@test @inferred(cumsum(())) == ()
@test @inferred(cumsum((1, 2, 3))) == (1, 3, 6)
@test @inferred(cumprod((1, 2, 3))) == (1, 2, 6)
@test @inferred(accumulate(+, (1, 2, 3); init=10)) == (11, 13, 16)
op(::Nothing, ::Any) = missing
op(::Missing, ::Any) = nothing
@test @inferred(accumulate(op, (1, 2, 3, 4); init = nothing)) ===
(missing, nothing, missing, nothing)
end

@testset "ntuple" begin
nttest1(x::NTuple{n, Int}) where {n} = n
@test nttest1(()) == 0
Expand Down