11__precompile__ ()
22
33module Sobol
4- using Compat
5-
6- import Base: ndims, skip, next, start, done, show
7- @compat import Base. show
4+ using Compat, Compat. Random
85export SobolSeq, ScaledSobolSeq, next!
96
107include (" soboldata.jl" ) # loads `sobol_a` and `sobol_minit`
118
9+ abstract type AbstractSobolSeq{N} end
10+
1211# N iis the dimension of sequence being generated
13- type SobolSeq{N}
12+ mutable struct SobolSeq{N} <: AbstractSobolSeq {N}
1413 m:: Array{UInt32,2} # array of size (sdim, 32)
1514 x:: Array{UInt32,1} # previous x = x_n, array of length sdim
1615 b:: Array{UInt32,1} # position of fixed point in x[i] is after bit b[i]
1716 n:: UInt32 # number of x's generated so far
1817end
1918
20- ndims {N} (s:: SobolSeq {N} ) = N:: Int
19+ ndims (s:: AbstractSobolSeq {N} ) where {N} = N:: Int
2120
2221function SobolSeq (N:: Int )
2322 (N < 0 || N > (length (sobol_a) + 1 )) && error (" invalid Sobol dimension" )
@@ -47,11 +46,12 @@ function SobolSeq(N::Int)
4746 end
4847 SobolSeq {N} (m,zeros (UInt32,N),zeros (UInt32,N),zero (UInt32))
4948end
49+ SobolSeq (N:: Integer ) = SobolSeq (Int (N))
5050
5151# 1/2^m for m = 1:32
52- const scale2m = @compat exp2 .(- (1 : 32 ))
52+ const scale2m = exp2 .(- (1 : 32 ))
5353
54- function next! {T<:AbstractFloat} (s:: SobolSeq , x:: AbstractVector{T } )
54+ function next! (s:: SobolSeq , x:: AbstractVector{<:AbstractFloat } )
5555 length (x) != ndims (s) && throw (BoundsError ())
5656
5757 if s. n == typemax (s. n)
@@ -76,7 +76,7 @@ function next!{T<:AbstractFloat}(s::SobolSeq, x::AbstractVector{T})
7676 end
7777 return x
7878end
79- next (s:: SobolSeq ) = next! (s, Array {Float64,1} (ndims (s)))
79+ next! (s:: SobolSeq ) = next! (s, Array {Float64,1} (undef, ndims (s)))
8080
8181# if we know in advance how many points (n) we want to compute, then
8282# adopt the suggestion of the Joe and Kuo paper, which in turn
@@ -87,63 +87,55 @@ function skip!(s::SobolSeq, n::Integer, x)
8787 for unused= 1 : nskip; next! (s,x); end
8888 return nothing
8989end
90- skip (s:: SobolSeq , n:: Integer ) = skip! (s, n, Array {Float64,1} (ndims (s)))
90+ Base . skip (s:: SobolSeq , n:: Integer ) = skip! (s, n, Array {Float64,1} (ndims (s)))
9191
92- function show (io:: IO , s:: SobolSeq )
92+ function Base . show (io:: IO , s:: SobolSeq )
9393 print (io, " $(ndims (s)) -dimensional Sobol sequence on [0,1]^$(ndims (s)) " )
9494end
95- @compat function show (io:: IO , :: MIME"text/html" , s:: SobolSeq )
95+ function Base . show (io:: IO , :: MIME"text/html" , s:: SobolSeq )
9696 print (io, " $(ndims (s)) -dimensional Sobol sequence on [0,1]<sup>$(ndims (s)) </sup>" )
9797end
9898
9999# Make an iterator so that we can do "for x in SobolSeq(...)".
100100# Technically, the Sobol sequence ends after 2^32-1 points, but it
101101# falls back on pseudorandom numbers after this. In practice, one is
102102# unlikely to reach that point.
103- start (s:: SobolSeq ) = s
104- next (s:: SobolSeq , s_:: SobolSeq ) = (next (s), s_)
105- done (s:: SobolSeq , s_:: SobolSeq ) = false
106- Base. eltype {N} (:: Type{SobolSeq{N}} ) = Vector{Float64}
107- if VERSION >= v " 0.5.0-dev+3305" # Julia #15123
108- Base. iteratorsize {N} (:: Type{SobolSeq{N}} ) = Base. IsInfinite ()
109- Base. iteratoreltype {N} (:: Type{SobolSeq{N}} ) = Base. HasEltype ()
110- end
103+ Base. start (s:: AbstractSobolSeq ) = nothing
104+ Base. next (s:: AbstractSobolSeq , state) = (next! (s), state)
105+ Base. done (s:: AbstractSobolSeq , state) = false
106+ Base. eltype (:: Type{<:AbstractSobolSeq} ) = Vector{Float64}
107+ Compat. IteratorSize (:: Type{<:AbstractSobolSeq} ) = Base. IsInfinite ()
108+ Compat. IteratorEltype (:: Type{<:AbstractSobolSeq} ) = Base. HasEltype ()
111109
112110# Convenience wrapper for scaled Sobol sequences
113111
114- type ScaledSobolSeq
115- s:: SobolSeq
112+ struct ScaledSobolSeq{N} <: AbstractSobolSeq{N}
113+ s:: SobolSeq{N}
116114 lb:: Vector{Float64}
117115 ub:: Vector{Float64}
118- ScaledSobolSeq (N :: Integer , lb:: Vector{Float64} , ub:: Vector{Float64} ) =
119- new (SobolSeq (N), lb, ub)
116+ ScaledSobolSeq (lb:: Vector{Float64} , ub:: Vector{Float64} ) =
117+ new {N} (SobolSeq (N), lb, ub)
120118end
121119SobolSeq (N:: Integer , lb, ub) =
122- ScaledSobolSeq (N, copy! (Array {Float64,1} ( N),lb), copy! (Array {Float64,1} ( N),ub))
120+ ScaledSobolSeq {Int(N)} ( copy! (Vector {Float64} (undef, N), lb), copy! (Vector {Float64} (undef, N), ub))
123121
124- ndims (s:: ScaledSobolSeq ) = ndims (s. s)
125-
126- function next! (s:: SobolSeq , x:: AbstractVector{Float64} ,
127- lb:: AbstractVector , ub:: AbstractVector )
122+ function next! (s:: SobolSeq , x:: AbstractVector{<:AbstractFloat} ,
123+ lb:: AbstractVector , ub:: AbstractVector )
128124 length (x) < ndims (s) && throw (BoundsError ())
129125 next! (s,x)
130126 for i= 1 : ndims (s)
131127 x[i] = lb[i] + (ub[i]- lb[i]) * x[i]
132128 end
133129 return x
134130end
135- next {N} (s:: SobolSeq{N} , lb:: AbstractVector , ub:: AbstractVector ) = next! (s, Array {Float64,1} (N), lb, ub)
136-
137- next! (s:: ScaledSobolSeq , x:: AbstractVector{Float64} ) = next! (s. s, x, s. lb, s. ub)
138- next (s:: ScaledSobolSeq ) = next! (s, Array {Float64,1} (ndims (s)))
131+ next! (s:: SobolSeq{N} , lb:: AbstractVector , ub:: AbstractVector ) where {N} = next! (s, Vector {Float64} (undef, N), lb, ub)
139132
140- start (s:: ScaledSobolSeq ) = s
141- next (s:: ScaledSobolSeq , s_:: ScaledSobolSeq ) = (next (s), s_)
142- done (s:: ScaledSobolSeq , s_:: ScaledSobolSeq ) = false
133+ next! (s:: ScaledSobolSeq , x:: AbstractVector{<:AbstractFloat} ) = next! (s. s, x, s. lb, s. ub)
134+ Base. next (s:: ScaledSobolSeq ) = next! (s, Vector {Float64} (undef, ndims (s)))
143135
144- skip (s:: ScaledSobolSeq , n) = skip (s. s, n)
136+ Base . skip (s:: ScaledSobolSeq , n) = skip (s. s, n)
145137
146- function show (io:: IO , s:: ScaledSobolSeq )
138+ function Base . show (io:: IO , s:: ScaledSobolSeq )
147139 lb = s. lb; ub = s. ub
148140 N = ndims (s)
149141 print (io, " $N -dimensional scaled Sobol sequence on [$(lb[1 ]) ,$(ub[1 ]) ]" )
@@ -164,7 +156,7 @@ function show(io::IO, s::ScaledSobolSeq)
164156 end
165157end
166158
167- @compat function show (io:: IO , :: MIME"text/html" , s:: ScaledSobolSeq )
159+ function Base . show (io:: IO , :: MIME"text/html" , s:: ScaledSobolSeq )
168160 N = ndims (s)
169161 lb = s. lb; ub = s. ub
170162 print (io, " $N -dimensional scaled Sobol sequence on [$(lb[1 ]) ,$(ub[1 ]) ]" )
0 commit comments