pomp
Inference for partially observed Markov processes
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sobolseq.c File Reference
#include "internal.h"
#include "soboldata.h"
Include dependency graph for sobolseq.c:

Go to the source code of this file.

Data Structures

struct  nlopt_soboldata_s
 

Typedefs

typedef Int32 uint32_t
 
typedef struct nlopt_soboldata_snlopt_sobol
 
typedef struct nlopt_soboldata_s soboldata
 

Functions

static nlopt_sobol nlopt_sobol_create (unsigned sdim)
 
static void nlopt_sobol_destroy (nlopt_sobol s)
 
static void nlopt_sobol_skip (nlopt_sobol s, unsigned n, double *x)
 
static unsigned rightzero32 (uint32_t n)
 
static int sobol_gen (soboldata *sd, double *x)
 
static int sobol_init (soboldata *sd, unsigned sdim)
 
static void sobol_destroy (soboldata *sd)
 
SEXP sobol_sequence (SEXP dim, SEXP length)
 

Typedef Documentation

◆ nlopt_sobol

typedef struct nlopt_soboldata_s* nlopt_sobol

Definition at line 60 of file sobolseq.c.

◆ soboldata

typedef struct nlopt_soboldata_s soboldata

◆ uint32_t

typedef Int32 uint32_t

Definition at line 59 of file sobolseq.c.

Function Documentation

◆ nlopt_sobol_create()

static nlopt_sobol nlopt_sobol_create ( unsigned  sdim)
static

Definition at line 188 of file sobolseq.c.

189{
190 nlopt_sobol s = (nlopt_sobol) R_Calloc(1,soboldata);
191 if (!s) return 0;
192 if (!sobol_init(s, sdim)) { R_Free(s); return NULL; }
193 return s;
194}
static int sobol_init(soboldata *sd, unsigned sdim)
Definition sobolseq.c:122
struct nlopt_soboldata_s * nlopt_sobol
Definition sobolseq.c:60
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nlopt_sobol_destroy()

static void nlopt_sobol_destroy ( nlopt_sobol  s)
static

Definition at line 196 of file sobolseq.c.

197{
198 if (s) {
199 sobol_destroy(s);
200 R_Free(s);
201 }
202}
static void sobol_destroy(soboldata *sd)
Definition sobolseq.c:177
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nlopt_sobol_skip()

static void nlopt_sobol_skip ( nlopt_sobol  s,
unsigned  n,
double *  x 
)
static

Definition at line 225 of file sobolseq.c.

226{
227 if (s) {
228 unsigned k = 1;
229 while (k*2 < n) k *= 2;
230 while (k-- > 0) sobol_gen(s, x);
231 }
232}
static int sobol_gen(soboldata *sd, double *x)
Definition sobolseq.c:96
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rightzero32()

static unsigned rightzero32 ( uint32_t  n)
static

Definition at line 84 of file sobolseq.c.

85{
86 const uint32_t a = 0x05f66a47; /* magic number, found by brute force */
87 static const unsigned decode[32] = {0,1,2,26,23,3,15,27,24,21,19,4,12,16,28,6,31,25,22,14,20,18,11,5,30,13,17,10,29,9,8,7};
88 n = ~n; /* change to rightmost-one problem */
89 n = a * (n & (-n)); /* store in n to make sure mult. is 32 bits */
90 return decode[n >> 27];
91}
Int32 uint32_t
Definition sobolseq.c:59
Here is the caller graph for this function:

◆ sobol_destroy()

static void sobol_destroy ( soboldata sd)
static

Definition at line 177 of file sobolseq.c.

178{
179 R_Free(sd->mdata);
180 R_Free(sd->x);
181 R_Free(sd->b);
182}
unsigned * b
Definition sobolseq.c:70
uint32_t * x
Definition sobolseq.c:69
uint32_t * mdata
Definition sobolseq.c:67
Here is the caller graph for this function:

◆ sobol_gen()

static int sobol_gen ( soboldata sd,
double *  x 
)
static

Definition at line 96 of file sobolseq.c.

97{
98 unsigned c, b, i, sdim;
99
100 if (sd->n == 4294967295U) return 0; /* n == 2^32 - 1 ... we would
101 need to switch to a 64-bit version
102 to generate more terms. */
103 c = rightzero32(sd->n++);
104 sdim = sd->sdim;
105 for (i = 0; i < sdim; ++i) {
106 b = sd->b[i];
107 if (b >= c) {
108 sd->x[i] ^= sd->m[c][i] << (b - c);
109 x[i] = ((double) (sd->x[i])) / (1U << (b+1));
110 }
111 else {
112 sd->x[i] = (sd->x[i] << (c - b)) ^ sd->m[c][i];
113 sd->b[i] = c;
114 x[i] = ((double) (sd->x[i])) / (1U << (c+1));
115 }
116 }
117 return 1;
118}
static unsigned rightzero32(uint32_t n)
Definition sobolseq.c:84
unsigned sdim
Definition sobolseq.c:66
uint32_t * m[32]
Definition sobolseq.c:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sobol_init()

static int sobol_init ( soboldata sd,
unsigned  sdim 
)
static

Definition at line 122 of file sobolseq.c.

123{
124 unsigned i,j;
125
126 if (!sdim || sdim > MAXDIM) return 0;
127
128 sd->mdata = (uint32_t *) R_Calloc(sdim*32,uint32_t);
129 if (!sd->mdata) return 0;
130
131 for (j = 0; j < 32; ++j) {
132 sd->m[j] = sd->mdata + j * sdim;
133 sd->m[j][0] = 1; /* special-case Sobol sequence */
134 }
135 for (i = 1; i < sdim; ++i) {
136 uint32_t a = sobol_a[i-1];
137 unsigned d = 0, k;
138
139 while (a) {
140 ++d;
141 a >>= 1;
142 }
143 d--; /* d is now degree of poly */
144
145 /* set initial values of m from table */
146 for (j = 0; j < d; ++j)
147 sd->m[j][i] = sobol_minit[j][i-1];
148
149 /* fill in remaining values using recurrence */
150 for (j = d; j < 32; ++j) {
151 a = sobol_a[i-1];
152 sd->m[j][i] = sd->m[j - d][i];
153 for (k = 0; k < d; ++k) {
154 sd->m[j][i] ^= ((a & 1) * sd->m[j-d+k][i]) << (d-k);
155 a >>= 1;
156 }
157 }
158 }
159
160 sd->x = (uint32_t *) R_Calloc(sdim,uint32_t);
161 if (!sd->x) { R_Free(sd->mdata); return 0; }
162
163 sd->b = (unsigned *) R_Calloc(sdim,unsigned);
164 if (!sd->b) { R_Free(sd->x); R_Free(sd->mdata); return 0; }
165
166 for (i = 0; i < sdim; ++i) {
167 sd->x[i] = 0;
168 sd->b[i] = 0;
169 }
170
171 sd->n = 0;
172 sd->sdim = sdim;
173
174 return 1;
175}
#define MAXDIM
Definition soboldata.h:34
static const uint32_t sobol_a[MAXDIM-1]
Definition soboldata.h:40
static const uint32_t sobol_minit[MAXDEG+1][MAXDIM-1]
Definition soboldata.h:150
Here is the caller graph for this function:

◆ sobol_sequence()

SEXP sobol_sequence ( SEXP  dim,
SEXP  length 
)

Definition at line 234 of file sobolseq.c.

235{
236 SEXP data;
237 unsigned int d = (unsigned int) INTEGER(dim)[0];
238 unsigned int n = (unsigned int) INTEGER(length)[0];
239 double *dp;
240 unsigned int k;
241 nlopt_sobol s = nlopt_sobol_create((unsigned int) d);
242 if (s==0) err("dimension is too high");
243 PROTECT(data = allocMatrix(REALSXP,d,n)); dp = REAL(data);
244 nlopt_sobol_skip(s,n,dp);
245 for (k = 1; k < n; k++) sobol_gen(s,dp+k*d);
247 UNPROTECT(1);
248 return(data);
249}
#define err(...)
Definition pomp.h:21
static void nlopt_sobol_destroy(nlopt_sobol s)
Definition sobolseq.c:196
static nlopt_sobol nlopt_sobol_create(unsigned sdim)
Definition sobolseq.c:188
static void nlopt_sobol_skip(nlopt_sobol s, unsigned n, double *x)
Definition sobolseq.c:225
Here is the call graph for this function: