From a464c66abc055bb7c55fd4b97dc17295f1b90059 Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Mon, 24 May 2021 10:28:25 -0700 Subject: [PATCH 1/3] stateful iteration --- src/optional/Sobol.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/optional/Sobol.jl b/src/optional/Sobol.jl index 5afcf88..30f1dc3 100644 --- a/src/optional/Sobol.jl +++ b/src/optional/Sobol.jl @@ -7,26 +7,27 @@ export SobolHypercube struct SobolHypercube{k} <: Hypercube{k} seq :: SobolSeq{k} value :: Vector{Float64} - index :: Ref{Int} # start at zero + iter :: Iterators.Stateful{Vector{Float64}, Union{Nothing, Tuple{Float64, Int64}}} function SobolHypercube(k::Int) seq = SobolSeq(k) value = Sobol.next!(seq) - return new{k}(seq, value, 0) + return new{k}(seq, value, Iterators.Stateful(value)) end end export next! -function next!(s::SobolHypercube) - s.index[] = 0 - Sobol.next!(s.seq, s.value) +function next!(ω::SobolHypercube) + Sobol.next!(ω.seq, ω.value) + ω.iter.nextvalstate = iterate(ω.value) + ω.iter.taken = 0 + return ω end -function Base.rand(ω::SobolHypercube{k}) where {k} - ω.value[ω.index[] += 1] -end +Base.rand(ω::SobolHypercube) = popfirst!(ω.iter) + # function makeplot() # ω = SobolHypercube(100) From 2da28aa6fb3422f40fec684ca5dc3db0011ac24a Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Wed, 26 May 2021 09:46:50 -0700 Subject: [PATCH 2/3] AbstractRNG methods --- Project.toml | 1 + src/hypercube.jl | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f36f12a..accfc4f 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ ElasticArrays = "fdbdab4c-e67f-52f5-8c3f-e7b388dad3d4" GeneralizedGenerated = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" NestedTuples = "a734d2a7-8d68-409b-9419-626914d4061d" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Requires = "ae029012-a4dd-5104-9daa-d747884805df" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a" diff --git a/src/hypercube.jl b/src/hypercube.jl index 53583a6..bdc8947 100644 --- a/src/hypercube.jl +++ b/src/hypercube.jl @@ -1,7 +1,8 @@ using GeneralizedGenerated using NestedTuples +using Random: AbstractRNG -abstract type Hypercube{k} end +abstract type Hypercube{k} <: AbstractRNG end struct RandHypercube{T} <: Hypercube{Inf} @@ -12,6 +13,20 @@ function Base.rand(ω::RandHypercube) rand(ω.rng) end +function Base.rand(ω::Hypercube, ::Type{T}) where {T <: Integer} + a = typemin(T) + b = typemax(T) + p = rand(ω) + return b*p + a*(1-p) +end + +function Base.rand(ω::Hypercube, ::Type{T}) where {T <: AbstractFloat} + convert(T, rand(ω)) +end + +function Base.rand(ω::Hypercube, ::Type{T}) where {T <: Complex} + convert(T, rand(ω) + rand(ω)im) +end # TODO: Add a test function NestedTuples.with(m::Module, hcube_nt::NamedTuple{N,Tuple{H}}, tv::TupleVector{T,X}, ex::TypelevelExpr{E}) where {T,X,E, N, H<:Hypercube} From 9fa3e4379f7b8d3f7d19dfe39fbfab3d77e1da3c Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Wed, 26 May 2021 13:27:48 -0700 Subject: [PATCH 3/3] oops missed a `convert` --- src/hypercube.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hypercube.jl b/src/hypercube.jl index bdc8947..8ffb0f8 100644 --- a/src/hypercube.jl +++ b/src/hypercube.jl @@ -1,6 +1,7 @@ using GeneralizedGenerated using NestedTuples using Random: AbstractRNG +using Random abstract type Hypercube{k} <: AbstractRNG end @@ -17,7 +18,7 @@ function Base.rand(ω::Hypercube, ::Type{T}) where {T <: Integer} a = typemin(T) b = typemax(T) p = rand(ω) - return b*p + a*(1-p) + return convert(T, b*p + a*(1-p)) end function Base.rand(ω::Hypercube, ::Type{T}) where {T <: AbstractFloat}