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

specialize sum for FillArrays #182

Merged
merged 6 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.13.3"
version = "0.13.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
3 changes: 3 additions & 0 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ end
# Cumsum
#########

# These methods are necessary to deal with infinite arrays
sum(x::AbstractFill) = getindex_value(x)*length(x)
sum(f, x::AbstractFill) = length(x) * f(getindex_value(x))
sum(x::Zeros) = getindex_value(x)

cumsum(x::AbstractFill{<:Any,1}) = range(getindex_value(x); step=getindex_value(x),
Expand Down
65 changes: 65 additions & 0 deletions test/infinitearrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Infinite Arrays implementation from
# https://github.com/JuliaLang/julia/blob/master/test/testhelpers/InfiniteArrays.jl
module InfiniteArrays
export OneToInf, Infinity

"""
Infinity()
Represents infinite cardinality. Note that `Infinity <: Integer` to support
being treated as an index.
"""
struct Infinity <: Integer end

Base.:(==)(::Infinity, ::Int) = false
Base.:(==)(::Int, ::Infinity) = false
Base.:(<)(::Int, ::Infinity) = true
Base.:(<)(::Infinity, ::Int) = false
Base.:(≤)(::Int, ::Infinity) = true
Base.:(≤)(::Infinity, ::Int) = false
Base.:(≤)(::Infinity, ::Infinity) = true
Base.:(-)(::Infinity, ::Int) = Infinity()
Base.:(+)(::Infinity, ::Int) = Infinity()
Base.:(:)(::Infinity, ::Infinity) = 1:0

Base.:(+)(::Integer, ::Infinity) = Infinity()
Base.:(+)(::Infinity, ::Integer) = Infinity()
Base.:(*)(::Integer, ::Infinity) = Infinity()
Base.:(*)(::Infinity, ::Integer) = Infinity()

Base.isinf(::Infinity) = true

abstract type AbstractInfUnitRange{T<:Real} <: AbstractUnitRange{T} end
Base.length(r::AbstractInfUnitRange) = Infinity()
Base.size(r::AbstractInfUnitRange) = (Infinity(),)
Base.unitrange(r::AbstractInfUnitRange) = InfUnitRange(r)
Base.last(r::AbstractInfUnitRange) = Infinity()
Base.axes(r::AbstractInfUnitRange) = (OneToInf(),)

"""
OneToInf(n)
Define an `AbstractInfUnitRange` that behaves like `1:∞`, with the added
distinction that the limits are guaranteed (by the type system) to
be 1 and ∞.
"""
struct OneToInf{T<:Integer} <: AbstractInfUnitRange{T} end

OneToInf() = OneToInf{Int}()

Base.axes(r::OneToInf) = (r,)
Base.first(r::OneToInf{T}) where {T} = oneunit(T)
Base.oneto(::Infinity) = OneToInf()

struct InfUnitRange{T<:Real} <: AbstractInfUnitRange{T}
start::T
end
Base.first(r::InfUnitRange) = r.start
InfUnitRange(a::InfUnitRange) = a
InfUnitRange{T}(a::AbstractInfUnitRange) where T<:Real = InfUnitRange{T}(first(a))
InfUnitRange(a::AbstractInfUnitRange{T}) where T<:Real = InfUnitRange{T}(first(a))
unitrange(a::AbstractInfUnitRange) = InfUnitRange(a)
Base.:(:)(start::T, stop::Infinity) where {T<:Integer} = InfUnitRange{T}(start)
function getindex(v::InfUnitRange{T}, i::Integer) where T
@boundscheck i > 0 || Base.throw_boundserror(v, i)
convert(T, first(v) + i - 1)
end
end
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FillArrays, LinearAlgebra, SparseArrays, StaticArrays, Random, Base64, Test, Statistics
import FillArrays: AbstractFill, RectDiagonal, SquareEye

include("infinitearrays.jl")

@testset "fill array constructors and convert" begin
for (Typ, funcs) in ((:Zeros, :zeros), (:Ones, :ones))
@eval begin
Expand Down Expand Up @@ -620,6 +622,13 @@ end
@test diff(Fill(1,10)) ≡ Zeros{Int}(9)
@test diff(Ones{Float64}(10)) ≡ Zeros{Float64}(9)
@test_throws UndefKeywordError cumsum(Fill(1,1,5))

@testset "infinite arrays" begin
A = Ones{Int}((InfiniteArrays.OneToInf(),))
@test isinf(sum(A))
@test sum(A) == length(A)
@test sum(x->x^2, A) == sum(A.^2)
end
end

@testset "Broadcast" begin
Expand Down