-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
|
@@ -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) | ||
|
||
|
||
""" | ||
|
@@ -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)) | ||
|
@@ -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) | ||
|
||
|
||
""" | ||
|
@@ -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) | ||
(ys..., acc), acc | ||
end | ||
return ys | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this implementation lead to unrolled code? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
10 ─ return %20
) => NTuple{10,Int64} |
||
|
||
""" | ||
accumulate!(op, B, A; [dims], [init]) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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