-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement accumulate and friends (#702)
* Implement accumulate and friends * Run tests for accumulate * Skip inference tests in Julia 1.1 * Update src/mapreduce.jl Co-Authored-By: Chris Foster <chris42f@gmail.com> * Rename: _maybeval -> _maybe_val * Explain how `_map` is used from `_accumulate` * Revert: (push(ys, y), y) This reverts commit 4ca0144. * Comment on why we use `vcat` * Use inference to determine element types * Use reduce_empty in cumsum/cumprod for Array-compatibility * Use reduce_first instead of reduce_empty Co-authored-by: Chris Foster <chris42f@gmail.com>
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using StaticArrays, Test | ||
|
||
@testset "accumulate" begin | ||
@testset "cumsum(::$label)" for (label, T) in [ | ||
# label, T | ||
("SVector", SVector), | ||
("MVector", MVector), | ||
("SizedVector", SizedVector), | ||
] | ||
@testset "$label" for (label, a) in [ | ||
("[1, 2, 3]", T{3}(SA[1, 2, 3])), | ||
("[]", T{0,Int}(())), | ||
] | ||
@test cumsum(a) == cumsum(collect(a)) | ||
@test cumsum(a) isa similar_type(a) | ||
@inferred cumsum(a) | ||
end | ||
@test eltype(cumsum(T{0,Int8}(()))) == eltype(cumsum(Int8[])) | ||
@test eltype(cumsum(T{1,Int8}((1)))) == eltype(cumsum(Int8[1])) | ||
@test eltype(cumsum(T{2,Int8}((1, 2)))) == eltype(cumsum(Int8[1, 2])) | ||
end | ||
|
||
@testset "cumsum(::$label; dims=2)" for (label, T) in [ | ||
# label, T | ||
("SMatrix", SMatrix), | ||
("MMatrix", MMatrix), | ||
("SizedMatrix", SizedMatrix), | ||
] | ||
@testset "$label" for (label, a) in [ | ||
("[1 2; 3 4; 5 6]", T{3,2}(SA[1 2; 3 4; 5 6])), | ||
("0 x 2 matrix", T{0,2,Float64}()), | ||
("2 x 0 matrix", T{2,0,Float64}()), | ||
] | ||
@test cumsum(a; dims = 2) == cumsum(collect(a); dims = 2) | ||
@test cumsum(a; dims = 2) isa similar_type(a) | ||
v"1.1" <= VERSION < v"1.2" && continue | ||
@inferred cumsum(a; dims = Val(2)) | ||
end | ||
end | ||
|
||
@testset "cumsum(a::SArray; dims=$i); ndims(a) = $d" for d in 1:4, i in 1:d | ||
shape = Tuple(1:d) | ||
a = similar_type(SArray, Int, Size(shape))(1:prod(shape)) | ||
@test cumsum(a; dims = i) == cumsum(collect(a); dims = i) | ||
@test cumsum(a; dims = i) isa SArray | ||
v"1.1" <= VERSION < v"1.2" && continue | ||
@inferred cumsum(a; dims = Val(i)) | ||
end | ||
|
||
@testset "cumprod" begin | ||
a = SA[1, 2, 3] | ||
@test cumprod(a)::SArray == cumprod(collect(a)) | ||
@inferred cumprod(a) | ||
|
||
@test eltype(cumsum(SA{Int8}[])) == eltype(cumsum(Int8[])) | ||
@test eltype(cumsum(SA{Int8}[1])) == eltype(cumsum(Int8[1])) | ||
@test eltype(cumsum(SA{Int8}[1, 2])) == eltype(cumsum(Int8[1, 2])) | ||
end | ||
|
||
@testset "empty vector with init" begin | ||
a = SA{Int}[] | ||
right(_, x) = x | ||
@test accumulate(right, a; init = Val(1)) === SA{Int}[] | ||
@inferred accumulate(right, a; init = Val(1)) | ||
end | ||
end |
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