-
Notifications
You must be signed in to change notification settings - Fork 17
Remove static dimension type parameter #42
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,27 +4,26 @@ export SobolSeq, ScaledSobolSeq, next! | |||||||
|
|
||||||||
| include("soboldata.jl") #loads `sobol_a` and `sobol_minit` | ||||||||
|
|
||||||||
| abstract type AbstractSobolSeq{N} end | ||||||||
| abstract type AbstractSobolSeq end | ||||||||
|
|
||||||||
| # N iis the dimension of sequence being generated | ||||||||
| mutable struct SobolSeq{N} <: AbstractSobolSeq{N} | ||||||||
| m::Array{UInt32,2} #array of size (sdim, 32) | ||||||||
| x::Array{UInt32,1} #previous x = x_n, array of length sdim | ||||||||
| b::Array{UInt32,1} #position of fixed point in x[i] is after bit b[i] | ||||||||
| mutable struct SobolSeq{X<:AbstractVector{UInt32}} <: AbstractSobolSeq | ||||||||
| m::Matrix{UInt32} #array of size (sdim, 32) | ||||||||
| x::X #previous x = x_n, array of length sdim | ||||||||
| b::Vector{UInt32} #position of fixed point in x[i] is after bit b[i] | ||||||||
| n::UInt32 #number of x's generated so far | ||||||||
| end | ||||||||
|
|
||||||||
| ndims(s::AbstractSobolSeq{N}) where {N} = N::Int | ||||||||
| Base.ndims(s::SobolSeq) = length(s.x) | ||||||||
|
|
||||||||
| function SobolSeq(N::Int) | ||||||||
| function SobolSeq{X}(N::Integer) where {X<:AbstractVector{UInt32}} | ||||||||
| (N < 0 || N > (length(sobol_a) + 1)) && error("invalid Sobol dimension") | ||||||||
|
|
||||||||
| m = ones(UInt32, (N, 32)) | ||||||||
|
|
||||||||
| #special cases | ||||||||
| N == 0 && return(SobolSeq{0})(m,UInt32[],UInt32[],zero(UInt32)) | ||||||||
| N == 0 && return SobolSeq(m,UInt32[],X(undef,0),zero(UInt32)) | ||||||||
| #special cases 1 | ||||||||
| N == 1 && return(SobolSeq{N}(m,UInt32[0],UInt32[0],zero(UInt32))) | ||||||||
| N == 1 && return SobolSeq(m,UInt32[0],fill!(X(undef,1),zero(UInt32)),zero(UInt32)) | ||||||||
|
|
||||||||
| for i = 2:N | ||||||||
| a = sobol_a[i-1] | ||||||||
|
|
@@ -42,9 +41,9 @@ function SobolSeq(N::Int) | |||||||
| end | ||||||||
| end | ||||||||
| end | ||||||||
| SobolSeq{N}(m,zeros(UInt32,N),zeros(UInt32,N),zero(UInt32)) | ||||||||
| SobolSeq(m,zeros(UInt32,N),fill!(X(undef,N),zero(UInt32)),zero(UInt32)) | ||||||||
| end | ||||||||
| SobolSeq(N::Integer) = SobolSeq(Int(N)) | ||||||||
| SobolSeq(args...) = SobolSeq{Vector{UInt32}}(args...) | ||||||||
|
|
||||||||
| function next!(s::SobolSeq, x::AbstractVector{<:AbstractFloat}) | ||||||||
| length(x) != ndims(s) && throw(BoundsError()) | ||||||||
|
|
@@ -72,7 +71,7 @@ function next!(s::SobolSeq, x::AbstractVector{<:AbstractFloat}) | |||||||
| end | ||||||||
| return x | ||||||||
| end | ||||||||
| next!(s::SobolSeq) = next!(s, Array{Float64,1}(undef, ndims(s))) | ||||||||
| next!(s::SobolSeq) = next!(s, Vector{Float64}(undef, ndims(s))) | ||||||||
|
|
||||||||
| # if we know in advance how many points (n) we want to compute, then | ||||||||
| # adopt a suggestion similar to the Joe and Kuo paper, which in turn | ||||||||
|
|
@@ -95,7 +94,7 @@ function skip!(s::SobolSeq, n::Integer, x; exact=false) | |||||||
| for unused=1:nskip; next!(s,x); end | ||||||||
| return s | ||||||||
| end | ||||||||
| Base.skip(s::SobolSeq, n::Integer; exact=false) = skip!(s, n, Array{Float64,1}(undef, ndims(s)); exact=exact) | ||||||||
| Base.skip(s::SobolSeq, n::Integer; exact=false) = skip!(s, n, Vector{Float64}(undef, ndims(s)); exact=exact) | ||||||||
|
|
||||||||
| function Base.show(io::IO, s::SobolSeq) | ||||||||
| print(io, "$(ndims(s))-dimensional Sobol sequence on [0,1]^$(ndims(s))") | ||||||||
|
|
@@ -115,20 +114,21 @@ Base.IteratorEltype(::Type{<:AbstractSobolSeq}) = Base.HasEltype() | |||||||
|
|
||||||||
| # Convenience wrapper for scaled Sobol sequences | ||||||||
|
|
||||||||
| struct ScaledSobolSeq{N,T} <: AbstractSobolSeq{N} | ||||||||
| s::SobolSeq{N} | ||||||||
| struct ScaledSobolSeq{T,X<:AbstractVector{UInt32}} <: AbstractSobolSeq | ||||||||
| s::SobolSeq{X} | ||||||||
| lb::Vector{T} | ||||||||
| ub::Vector{T} | ||||||||
| function ScaledSobolSeq{N,T}(lb::Vector{T}, ub::Vector{T}) where {N,T} | ||||||||
| length(lb)==length(ub)==N || throw(DimensionMismatch("lb and ub do not have length $N")) | ||||||||
| new(SobolSeq(N), lb, ub) | ||||||||
| function ScaledSobolSeq{T,X}(lb::Vector{T}, ub::Vector{T}) where {T,X<:AbstractVector{UInt32}} | ||||||||
| length(lb)==length(ub) || throw(DimensionMismatch("lb and ub do not have same length")) | ||||||||
| new{T,X}(SobolSeq{X}(length(lb)), lb, ub) | ||||||||
|
||||||||
| new{T,X}(SobolSeq{X}(length(lb)), lb, ub) | |
| x = similar(lb, T) # determine the type of array from lb | |
| new{T,typeof{x}}(x, lb, ub) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if you want to have a much greater level of flexibility, then this is overly restrictive - the bounds could very well be immutable SVectors, but x would have to be its mutable complement.
Uh oh!
There was an error while loading. Please reload this page.