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

stateful iteration #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion src/hypercube.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using GeneralizedGenerated
using NestedTuples
using Random: AbstractRNG
using Random

abstract type Hypercube{k} end
abstract type Hypercube{k} <: AbstractRNG end


struct RandHypercube{T} <: Hypercube{Inf}
Expand All @@ -12,6 +14,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 convert(T, 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}
Expand Down
17 changes: 9 additions & 8 deletions src/optional/Sobol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down