logo

How to Use Infinite Arrays in Julia 📂Julia

How to Use Infinite Arrays in Julia

Overview

InfiniteArrays.jl is a package that enables the use of arrays of infinite size1, and is, in fact, closely related to Lazy Arrays. Lazy Evaluation refers to the method where the computer knows what needs to be computed but postpones the calculation until it is absolutely necessary. Obviously, computers cannot understand infinity, but this method allows for the implementation of infinite arrays on computers.

Code

julia> using InfiniteArrays

julia> 3141592 < ∞
true

julia> Inf == ∞
true

julia> Inf === ∞
false

Loading InfiniteArrays.jl, you first become able to express infinity with the symbol. Though you can express infinity as Inf without this package, using this symbol allows for a more intuitive use. While they hold the same magnitude of infinity in comparison, they can be differentiated as pointers.

ℵ₀

julia> x = zeros(Int64, ∞);

julia> length(x)
ℵ₀

Creating an infinite array filled with zeros results in a [countably infinite set], hence its size is aleph-zero $\aleph_{0}$.

Can be used ordinarily

julia> x[2] = 3; x[94124843] = 7; x
ℵ₀-element LazyArrays.CachedArray{Int64, 1, Vector{Int64}, Zeros{Int64, 1, Tuple{InfiniteArrays.OneToInf{Int64}}}} with indices OneToInf():
 0
 3
 0
 0
 0
 0
 0
 0
 0
 ⋮

julia> sum(x)
10

Having an infinite array doesn’t mean the interface changes significantly. You can handle it just like any regular array, and it will work as expected.

Complete code

using InfiniteArrays

3141592 < ∞
Inf == ∞
Inf === ∞

x = zeros(Int64, ∞);
length(x)

x[2] = 3; x[94124843] = 7; x
sum(x)

Environment

  • OS: Windows
  • julia: v1.7.3