Skip to content

Commit

Permalink
Infinite random sequences (JuliaArrays#181)
Browse files Browse the repository at this point in the history
* InfRandVector

* Remove export

* prefix

* add prefix to docstring example

* add prefix to docstring example
  • Loading branch information
DanielVandH authored Jun 25, 2024
1 parent c45ae5f commit 8865985
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 15 deletions.
24 changes: 14 additions & 10 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,45 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Infinities = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
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.8"
BandedMatrices = "1.0"
BlockArrays = "1.0"
Base64 = "1"
BlockArrays = "1.0"
DSP = "0.7"
Distributions = "0.25"
FillArrays = "1.0"
Infinities = "0.1.1"
LazyArrays = "2.0.2"
LinearAlgebra = "1.6"
Random = "1"
SparseArrays = "1"
Statistics = "1"
Test = "1"
julia = "1.10"

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

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

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

[weakdeps]
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
test = ["Aqua", "Test", "BandedMatrices", "BlockArrays", "Statistics", "SparseArrays", "Base64", "DSP", "Distributions"]
8 changes: 6 additions & 2 deletions src/InfiniteArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Base: *, +, -, /, <, ==, >, \, ≤, ≥, (:), @propagate_inbounds,
cumsum, dataids, diff, div, eltype, fill, findfirst, first, floatrange, getindex, hcat,
in, ind2sub_rs, intersect, inv, isempty, isinf, issorted, last, length, lt, max,
maximum, minimum, mod, one, ones, parent, parentindices, permutedims, print_matrix, print_matrix_row,
print_matrix_vdots, promote_rule, reinterpret, reshape, reverse, searchsorted,
print_matrix_vdots, promote_rule, rand, reinterpret, reshape, reverse, searchsorted,
searchsortedfirst, searchsortedlast, setindex!, show, show_circular, show_delim_array, sign,
signbit, similar, size, sort, sort!, step, sum, tail,
to_shape, transpose, unaliascopy, union, unitrange_last, unsafe_convert, unsafe_indices, unsafe_length,
Expand Down Expand Up @@ -42,12 +42,15 @@ import FillArrays: AbstractFill, Eye, Fill, Ones, RectDiagonal, Zeros, fill_resh
import Infinities: InfiniteCardinal, Infinity, ∞

import LazyArrays: AbstractCachedVector, ApplyLayout, CachedArray, CachedVector, InvColumnLayout,
LazyArrayStyle, LazyLayout, LazyMatrix, PaddedColumns, _padded_sub_materialize, sub_paddeddata
LazyArrayStyle, LazyLayout, LazyMatrix, PaddedColumns, _padded_sub_materialize, resizedata!,
sub_paddeddata

import LinearAlgebra: AdjOrTrans, HermOrSym, diag, norm, norm1, norm2, normp

import LazyArrays: AbstractPaddedLayout

import Random: default_rng

export ∞, ℵ₀, Hcat, Vcat, Zeros, Ones, Fill, Eye, BroadcastArray, cache
import Base: unitrange, oneto

Expand All @@ -56,6 +59,7 @@ import Base: unitrange, oneto
include("infrange.jl")
include("infarrays.jl")
include("reshapedarray.jl")
include("infrand.jl")

##
# Fill FillArrays
Expand Down
64 changes: 64 additions & 0 deletions src/infrand.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
InfRandVector([rng=default_rng()], [dist=Float64])
Represents a random infinite sequence. The random number generator can be specified
by the first argument `rng`, which defaults to `Random.default_rng()`, and the distribution
to generate from can be specified using the `dist` argument, which defaults to `Float64`.
```julia-repl
julia> using InfiniteArrays
julia> seq = InfiniteArrays.InfRandVector();
julia> seq[1]
0.6947847847152157
julia> seq[1:5]
5-element Vector{Float64}:
0.6947847847152157
0.49859004150722164
0.31559745572937126
0.5338596092137163
0.14792462133894646
julia> using Distributions, Random
julia> seq = InfiniteArrays.InfRandVector(MersenneTwister(123), Normal(0.3, 1.7))
ℵ₀-element InfRandVector{Float64, Normal{Float64}, MersenneTwister} with indices 1:∞:
2.3234553976766703
3.7819055032417075
2.242506534874238
1.0810065546920364
-0.3743544348018791
-0.8300113268258689
1.967645305489507
```
"""
mutable struct InfRandVector{T,D,RNG} <: AbstractCachedVector{T}
const rng::RNG
const dist::D
const data::Vector{T}
datasize::Int
end
function InfRandVector(rng=default_rng(), dist=Float64)
T = typeof(rand(copy(rng), dist))
_rng = copy(rng)
return InfRandVector{T,typeof(dist),typeof(_rng)}(_rng, dist, T[], 0)
end
Base.size(::InfRandVector) = (ℵ₀,)
Base.axes(::InfRandVector) = (1:ℵ₀,)
Base.length(::InfRandVector) = ℵ₀
function resizedata!(seq::InfRandVector, inds)
newlen = maximum(inds)
curlen = length(seq.data)
newlen > curlen || return seq
resize!(seq.data, newlen)
# rand!(seq.rng, view(seq.data, curlen+1:newlen), seq.dist)
# ^ rand() is not actually sequential.. rand(Random.seed!(123), 1000) ≠ (rng = Random.seed!(123); [rand(rng) for _ in 1:1000])
for i in (curlen+1):newlen
seq.data[i] = rand(seq.rng, seq.dist)
end
seq.datasize = newlen
return seq
end
7 changes: 4 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using LinearAlgebra, SparseArrays, InfiniteArrays, Infinities, FillArrays, LazyArrays, Statistics, Test, Base64
using BandedMatrices
using LinearAlgebra, SparseArrays, InfiniteArrays, Infinities, FillArrays, LazyArrays, Random, Statistics, Test, Base64
using BandedMatrices, Distributions
import InfiniteArrays: InfUnitRange, InfStepRange, OneToInf, NotANumber, oneto, unitrange
import LazyArrays: CachedArray, MemoryLayout, LazyLayout, DiagonalLayout, LazyArrayStyle, colsupport, DualLayout
import BandedMatrices: _BandedMatrix, BandedColumns
Expand Down Expand Up @@ -1230,4 +1230,5 @@ end
end

include("test_infconv.jl")
include("test_block.jl")
include("test_block.jl")
include("test_infrand.jl")
32 changes: 32 additions & 0 deletions test/test_infrand.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@testset "InfRandVector" begin
@testset "Default constructor" begin
Random.seed!(123)
seq = InfiniteArrays.InfRandVector()
val = seq[1]
@test seq[1] == val # constant
@test seq[1:10000] == seq[1:10000]
@test seq[1] == val # didn't change after resizing
@inferred seq[1]
Random.seed!(123)
_seq = [rand() for _ in 1:1000]
@test seq[1:1000] == _seq[1:1000]
@test size(seq) == (ℵ₀,)
@test length(seq) == ℵ₀
@test axes(seq) == (1:ℵ₀,)
end

@testset "Providing an RNG and a distribution" begin
rng = MersenneTwister(123)
seq = InfiniteArrays.InfRandVector(rng, Float16)
rng2 = MersenneTwister(123)
@test seq[1:10000] == [rand(rng2, Float16) for _ in 1:10000]
end

@testset "Distributions.jl" begin
dist = Normal(0.3, 1.7) # do Normal{Float32} for example if you want that number type
rng = Xoshiro(5)
seq = InfiniteArrays.InfRandVector(rng, dist)
rng2 = Xoshiro(5)
@test seq[1:100] == [0.3 + 1.7randn(rng2) for _ in 1:100]
end
end

0 comments on commit 8865985

Please sign in to comment.