Skip to content

Commit 8865985

Browse files
authored
Infinite random sequences (#181)
* InfRandVector * Remove export * prefix * add prefix to docstring example * add prefix to docstring example
1 parent c45ae5f commit 8865985

File tree

5 files changed

+120
-15
lines changed

5 files changed

+120
-15
lines changed

Project.toml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,45 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
99
Infinities = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
1010
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
1111
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
12+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1213
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1314

15+
[weakdeps]
16+
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
17+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
18+
19+
[extensions]
20+
InfiniteArraysDSPExt = "DSP"
21+
InfiniteArraysStatisticsExt = "Statistics"
22+
1423
[compat]
1524
Aqua = "0.8"
1625
ArrayLayouts = "1.8"
1726
BandedMatrices = "1.0"
18-
BlockArrays = "1.0"
1927
Base64 = "1"
28+
BlockArrays = "1.0"
2029
DSP = "0.7"
30+
Distributions = "0.25"
2131
FillArrays = "1.0"
2232
Infinities = "0.1.1"
2333
LazyArrays = "2.0.2"
2434
LinearAlgebra = "1.6"
35+
Random = "1"
2536
SparseArrays = "1"
2637
Statistics = "1"
2738
Test = "1"
2839
julia = "1.10"
2940

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

4452
[targets]
45-
test = ["Aqua", "Test", "BandedMatrices", "BlockArrays", "Statistics", "SparseArrays", "Base64", "DSP"]
46-
47-
[weakdeps]
48-
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
49-
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
53+
test = ["Aqua", "Test", "BandedMatrices", "BlockArrays", "Statistics", "SparseArrays", "Base64", "DSP", "Distributions"]

src/InfiniteArrays.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Base: *, +, -, /, <, ==, >, \, ≤, ≥, (:), @propagate_inbounds,
1010
cumsum, dataids, diff, div, eltype, fill, findfirst, first, floatrange, getindex, hcat,
1111
in, ind2sub_rs, intersect, inv, isempty, isinf, issorted, last, length, lt, max,
1212
maximum, minimum, mod, one, ones, parent, parentindices, permutedims, print_matrix, print_matrix_row,
13-
print_matrix_vdots, promote_rule, reinterpret, reshape, reverse, searchsorted,
13+
print_matrix_vdots, promote_rule, rand, reinterpret, reshape, reverse, searchsorted,
1414
searchsortedfirst, searchsortedlast, setindex!, show, show_circular, show_delim_array, sign,
1515
signbit, similar, size, sort, sort!, step, sum, tail,
1616
to_shape, transpose, unaliascopy, union, unitrange_last, unsafe_convert, unsafe_indices, unsafe_length,
@@ -42,12 +42,15 @@ import FillArrays: AbstractFill, Eye, Fill, Ones, RectDiagonal, Zeros, fill_resh
4242
import Infinities: InfiniteCardinal, Infinity, ∞
4343

4444
import LazyArrays: AbstractCachedVector, ApplyLayout, CachedArray, CachedVector, InvColumnLayout,
45-
LazyArrayStyle, LazyLayout, LazyMatrix, PaddedColumns, _padded_sub_materialize, sub_paddeddata
45+
LazyArrayStyle, LazyLayout, LazyMatrix, PaddedColumns, _padded_sub_materialize, resizedata!,
46+
sub_paddeddata
4647

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

4950
import LazyArrays: AbstractPaddedLayout
5051

52+
import Random: default_rng
53+
5154
export ∞, ℵ₀, Hcat, Vcat, Zeros, Ones, Fill, Eye, BroadcastArray, cache
5255
import Base: unitrange, oneto
5356

@@ -56,6 +59,7 @@ import Base: unitrange, oneto
5659
include("infrange.jl")
5760
include("infarrays.jl")
5861
include("reshapedarray.jl")
62+
include("infrand.jl")
5963

6064
##
6165
# Fill FillArrays

src/infrand.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
InfRandVector([rng=default_rng()], [dist=Float64])
3+
4+
Represents a random infinite sequence. The random number generator can be specified
5+
by the first argument `rng`, which defaults to `Random.default_rng()`, and the distribution
6+
to generate from can be specified using the `dist` argument, which defaults to `Float64`.
7+
8+
```julia-repl
9+
julia> using InfiniteArrays
10+
11+
julia> seq = InfiniteArrays.InfRandVector();
12+
13+
julia> seq[1]
14+
0.6947847847152157
15+
16+
julia> seq[1:5]
17+
5-element Vector{Float64}:
18+
0.6947847847152157
19+
0.49859004150722164
20+
0.31559745572937126
21+
0.5338596092137163
22+
0.14792462133894646
23+
24+
julia> using Distributions, Random
25+
26+
julia> seq = InfiniteArrays.InfRandVector(MersenneTwister(123), Normal(0.3, 1.7))
27+
ℵ₀-element InfRandVector{Float64, Normal{Float64}, MersenneTwister} with indices 1:∞:
28+
2.3234553976766703
29+
3.7819055032417075
30+
2.242506534874238
31+
1.0810065546920364
32+
-0.3743544348018791
33+
-0.8300113268258689
34+
1.967645305489507
35+
36+
```
37+
"""
38+
mutable struct InfRandVector{T,D,RNG} <: AbstractCachedVector{T}
39+
const rng::RNG
40+
const dist::D
41+
const data::Vector{T}
42+
datasize::Int
43+
end
44+
function InfRandVector(rng=default_rng(), dist=Float64)
45+
T = typeof(rand(copy(rng), dist))
46+
_rng = copy(rng)
47+
return InfRandVector{T,typeof(dist),typeof(_rng)}(_rng, dist, T[], 0)
48+
end
49+
Base.size(::InfRandVector) = (ℵ₀,)
50+
Base.axes(::InfRandVector) = (1:ℵ₀,)
51+
Base.length(::InfRandVector) = ℵ₀
52+
function resizedata!(seq::InfRandVector, inds)
53+
newlen = maximum(inds)
54+
curlen = length(seq.data)
55+
newlen > curlen || return seq
56+
resize!(seq.data, newlen)
57+
# rand!(seq.rng, view(seq.data, curlen+1:newlen), seq.dist)
58+
# ^ rand() is not actually sequential.. rand(Random.seed!(123), 1000) ≠ (rng = Random.seed!(123); [rand(rng) for _ in 1:1000])
59+
for i in (curlen+1):newlen
60+
seq.data[i] = rand(seq.rng, seq.dist)
61+
end
62+
seq.datasize = newlen
63+
return seq
64+
end

test/runtests.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using LinearAlgebra, SparseArrays, InfiniteArrays, Infinities, FillArrays, LazyArrays, Statistics, Test, Base64
2-
using BandedMatrices
1+
using LinearAlgebra, SparseArrays, InfiniteArrays, Infinities, FillArrays, LazyArrays, Random, Statistics, Test, Base64
2+
using BandedMatrices, Distributions
33
import InfiniteArrays: InfUnitRange, InfStepRange, OneToInf, NotANumber, oneto, unitrange
44
import LazyArrays: CachedArray, MemoryLayout, LazyLayout, DiagonalLayout, LazyArrayStyle, colsupport, DualLayout
55
import BandedMatrices: _BandedMatrix, BandedColumns
@@ -1230,4 +1230,5 @@ end
12301230
end
12311231

12321232
include("test_infconv.jl")
1233-
include("test_block.jl")
1233+
include("test_block.jl")
1234+
include("test_infrand.jl")

test/test_infrand.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@testset "InfRandVector" begin
2+
@testset "Default constructor" begin
3+
Random.seed!(123)
4+
seq = InfiniteArrays.InfRandVector()
5+
val = seq[1]
6+
@test seq[1] == val # constant
7+
@test seq[1:10000] == seq[1:10000]
8+
@test seq[1] == val # didn't change after resizing
9+
@inferred seq[1]
10+
Random.seed!(123)
11+
_seq = [rand() for _ in 1:1000]
12+
@test seq[1:1000] == _seq[1:1000]
13+
@test size(seq) == (ℵ₀,)
14+
@test length(seq) == ℵ₀
15+
@test axes(seq) == (1:ℵ₀,)
16+
end
17+
18+
@testset "Providing an RNG and a distribution" begin
19+
rng = MersenneTwister(123)
20+
seq = InfiniteArrays.InfRandVector(rng, Float16)
21+
rng2 = MersenneTwister(123)
22+
@test seq[1:10000] == [rand(rng2, Float16) for _ in 1:10000]
23+
end
24+
25+
@testset "Distributions.jl" begin
26+
dist = Normal(0.3, 1.7) # do Normal{Float32} for example if you want that number type
27+
rng = Xoshiro(5)
28+
seq = InfiniteArrays.InfRandVector(rng, dist)
29+
rng2 = Xoshiro(5)
30+
@test seq[1:100] == [0.3 + 1.7randn(rng2) for _ in 1:100]
31+
end
32+
end

0 commit comments

Comments
 (0)