Skip to content

Commit

Permalink
Fixed infinite indexing with cumsum (#173)
Browse files Browse the repository at this point in the history
* Fixed infinite indexing with cumsum

* Update Project.toml

* Update Project.toml
  • Loading branch information
dlfivefifty authored Apr 25, 2024
1 parent 635b00f commit 99d9671
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
22 changes: 11 additions & 11 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "InfiniteArrays"
uuid = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
version = "0.13.7"
version = "0.13.8"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand All @@ -11,18 +11,10 @@ LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[weakdeps]
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[extensions]
InfiniteArraysDSPExt = "DSP"
InfiniteArraysStatisticsExt = "Statistics"

[compat]
Aqua = "0.8"
ArrayLayouts = "1.0"
BandedMatrices = "0.17.18, 1"
ArrayLayouts = "1.8"
BandedMatrices = "0.17.18, 1.0"
Base64 = "1"
DSP = "0.7"
FillArrays = "1.0"
Expand All @@ -35,6 +27,10 @@ Statistics = "1"
Test = "1"
julia = "1.6"

[extensions]
InfiniteArraysDSPExt = "DSP"
InfiniteArraysStatisticsExt = "Statistics"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
Expand All @@ -47,3 +43,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Test", "BandedMatrices", "LazyBandedMatrices", "Statistics", "SparseArrays", "Base64", "DSP"]

[weakdeps]
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
11 changes: 10 additions & 1 deletion src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ cumsum(r::InfRanges) = RangeCumsum(r)
diff(r::InfRanges) = Fill(step(r),∞)
diff(r::AbstractInfUnitRange{T}) where T = Ones{T}(∞)
Base.@propagate_inbounds getindex(c::RangeCumsum, kr::OneToInf) = RangeCumsum(c.range[kr])
getindex(c::RangeCumsum{<:Any,<:OneToInf}, k::Integer) = k * (k+1) ÷ 2
Base.@propagate_inbounds function getindex(c::RangeCumsum{<:Any,<:OneToInf}, k::Integer)
@boundscheck checkbounds(c, k)
k * (k+1) ÷ 2
end
function union(r1::RangeCumsum{T1, OneToInf{T1}}, r2::RangeCumsum{T2, OneToInf{T2}}) where {T1,T2}
T = promote_type(T1, T2)
RangeCumsum(OneToInf{T}())
Expand All @@ -522,6 +525,12 @@ Base.issorted(r::RangeCumsum{<:Any,<:OneToInf}) = true
Base.sort(r::RangeCumsum{<:Any,<:OneToInf}) = r
Base.sort!(r::RangeCumsum{<:Any,<:OneToInf}) = r

getindex(c::RangeCumsum{<:Any,<:OneToInf}, ::InfiniteCardinal{0}) = last(c)
Base.@propagate_inbounds function getindex(c::RangeCumsum{<:Any,<:AbstractRange}, k::InfiniteCardinal{0})
@boundscheck checkbounds(c, k)
last(c)
end
Base._unsafe_getindex(::IndexStyle, A::RangeCumsum, I::InfiniteCardinal{0}) = last(A)
# vcat

vcat(a::Number, r::InfRanges) = Vcat(a, r)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ end
rs = cumsum(r)
@test sort(rs) === sort!(rs) === rs
@test @inferred((rs -> Val(issorted(rs)))(rs)) isa Val{true}
@test rs[end] ℵ₀
end
end
@testset "in" begin
Expand Down Expand Up @@ -945,6 +946,11 @@ end
@test exp.(c)[1:20] == exp.(c[1:20])
end

@test cumsum(3:4:∞)[end] cumsum(3:4:∞)[∞] cumsum(3:4:∞)[ℵ₀] RealInfinity()
@test cumsum(2:∞)[end] cumsum(2:∞)[∞] cumsum(2:∞)[ℵ₀] cumsum(oneto(∞))[end] cumsum(oneto(∞))[∞] cumsum(oneto(∞))[ℵ₀] ℵ₀

@test_throws BoundsError cumsum(oneto(∞))[-5]

@test cumsum(1:∞)[2:∞][1:5] == cumsum(1:6)[2:end]

@testset "union of cumsum" begin
Expand Down

2 comments on commit 99d9671

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/105596

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.8 -m "<description of version>" 99d967136a80224d42d0926242cab5055d539fea
git push origin v0.13.8

Please sign in to comment.