File: generate_values.R

package info (click to toggle)
r-cran-spacefillr 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,472 kB
  • sloc: ansic: 32,867; cpp: 5,657; makefile: 2
file content (339 lines) | stat: -rw-r--r-- 12,101 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#'@title Generate Halton Value (Randomly Initialized)
#'
#'@description Generate a single value from a seeded Halton set.
#'
#'Note: This is much slower than generating the entire set ahead of time.
#'
#'@param i The element of the sequence to extract.
#'@param dim The dimension of the sequence to extract.
#'@param seed Default `0`. The random seed.
#'@return A single numeric value representing the `i`th element in the `dim` dimension.
#'
#'@export
#'@examples
#'#Generate a 3D sample:
#'point3d = c(generate_halton_random_single(10, dim = 1),
#'            generate_halton_random_single(10, dim = 2),
#'            generate_halton_random_single(10, dim = 3))
#'point3d
#'
#'#Change the random seed:
#'#'#Generate a 3D sample
#'point3d_2 = c(generate_halton_random_single(10, dim = 1, seed = 10),
#'              generate_halton_random_single(10, dim = 2, seed = 10),
#'              generate_halton_random_single(10, dim = 3, seed = 10))
#'point3d_2
generate_halton_random_single = function(i, dim, seed = 0) {
  return(rcpp_generate_halton_random_single(i,dim,seed))
}

#'@title Generate Halton Value (Faure Initialized)
#'
#'@description Generate a single value from a seeded Halton set, initialized with a Faure sequence.
#'
#'Note: This is much slower than generating the entire set ahead of time.
#'
#'@param i The element of the sequence to extract.
#'@param dim The dimension of the sequence to extract.
#'@return A single numeric value representing the `i`th element in the `dim` dimension.
#'
#'@export
#'@examples
#'#Generate a 3D sample:
#'point3d = c(generate_halton_faure_single(10, dim = 1),
#'            generate_halton_faure_single(10, dim = 2),
#'            generate_halton_faure_single(10, dim = 3))
#'point3d
generate_halton_faure_single = function(i, dim) {
  return(rcpp_generate_halton_faure_single(i,dim))
}

#'@title Generate Halton Set (Randomly Initialized)
#'
#'@description Generate a set of values from a seeded Halton set.
#'
#'@param n The number of values (per dimension) to extract.
#'@param dim The number of dimensions of the sequence.
#'@param seed Default `0`. The random seed.
#'@return An `n` x `dim` matrix listing all the
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_halton_random_set(n=1000, dim=2)
#'plot(points2d)
#'
#'#Change the seed and extract a separate pair of dimensions
#'points2d = generate_halton_random_set(n=1000, dim=10,seed=2)
#'plot(points2d[,5:6])
#'
#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = matrix(generate_halton_random_set(10000,dim=2),ncol=2)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_halton_random_set = function(n, dim, seed = 0) {
  vals = unlist(rcpp_generate_halton_random_set(n,dim,seed))
  return(matrix(vals, nrow=n,ncol=dim))
}

#'@title Generate Halton Set (Faure Initialized)
#'
#'@description Generate a set of values from a Faure Halton set.
#'
#'@param n The number of values (per dimension) to extract.
#'@param dim The number of dimensions of the sequence.
#'@return An `n` x `dim` matrix listing all the
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_halton_random_set(n=1000, dim=2)
#'plot(points2d)
#'
#'#Extract a separate pair of dimensions
#'points2d = generate_halton_random_set(n=1000, dim=10)
#'plot(points2d[,5:6])
#'
#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = matrix(generate_halton_faure_set(10000,dim=2),ncol=2)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_halton_faure_set = function(n, dim) {
  vals = unlist(rcpp_generate_halton_faure_set(n,dim))
  return(matrix(vals, nrow=n,ncol=dim))
}

#'@title Generate Sobol Set
#'
#'@description Generate a set of values from a Sobol set.
#'
#'Note: the Sobol sequences provided by spacefillr are different than those provided by randtoolbox,
#'as spacefillr's Sobol sequences have better 2D projections
#'(see "Constructing Sobol sequences with better two-dimensional projections" (2012) <doi:10.1137/070709359> S. Joe and F. Y. Kuo).
#'
#'@param n The number of values (per dimension) to extract.
#'@param dim The number of dimensions of the sequence. This has a maximum value of 1024.
#'@param seed Default `0`. The random seed.
#'@return A single numeric value representing the `i`th element in the `dim` dimension.
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_sobol_set(n=1000, dim = 2)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a longer sequence of values from that set
#'points2d = generate_sobol_set(n=1500, dim = 2)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = matrix(generate_sobol_set(10000,dim=2),ncol=2)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_sobol_set = function(n, dim, seed = 0) {
  vals = unlist(rcpp_generate_sobol_set(n, dim, seed))
  return(matrix(vals,ncol=dim))
}

#'@title Generate Owen-scrambled Sobol Set
#'
#'@description Generate a set of values from an Owen-scrambled Sobol set.
#'
#'@param n The number of values (per dimension) to extract.
#'@param dim The number of dimensions of the sequence. This has a maximum value of 21201.
#'@param seed Default `0`. The random seed.
#'@return An `n` x `dim` matrix with all the calculated values from the set.
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_sobol_owen_set(n=1000, dim = 2)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a longer sequence of values from that set
#'points2d = generate_sobol_owen_set(n=1500, dim = 2)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = matrix(generate_sobol_owen_set(10000,dim=2),ncol=2)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_sobol_owen_set = function(n, dim, seed = 0) {
  vals = unlist(rcpp_generate_sobol_owen_set(n, dim, seed))
  return(matrix(vals,ncol=dim))
}


#'@title Generate 2D Progressive Jittered Set
#'
#'@description Generate a set of values from a Progressive Jittered set.
#'
#'@param n The number of 2D values to extract.
#'@param seed Default `0`. The random seed.
#'@return An `n` x `2` matrix with all the calculated values from the set.
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_pj_set(n=1000)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a longer sequence of values from that set
#'points2d = generate_pj_set(n=1500)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a new set by changing the seed
#'points2d = generate_pj_set(n=1500,seed=10)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = generate_pj_set(10000)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_pj_set = function(n, seed = 0) {
  vals = unlist(rcpp_generate_pj_set(n, seed))
  return(matrix(vals, nrow=n,ncol=2,byrow = TRUE))
}

#'@title Generate 2D Progressive Multi-Jittered Set
#'
#'@description Generate a set of values from a Progressive Multi-Jittered set.
#'
#'@param n The number of 2D values to extract.
#'@param seed Default `0`. The random seed.
#'@return An `n` x `2` matrix with all the calculated values from the set.
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_pmj_set(n=1000)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a longer sequence of values from that set
#'points2d = generate_pmj_set(n=1500)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a new set by changing the seed
#'points2d = generate_pmj_set(n=1500,seed=10)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = generate_pj_set(10000)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_pmj_set = function(n, seed = 0) {
  vals = unlist(rcpp_generate_pmj_set(n, seed))
  return(matrix(vals, nrow=n,ncol=2,byrow = TRUE))
}

#'@title Generate 2D Progressive Multi-Jittered (with blue noise) Set
#'
#'@description Generate a set of values from a Progressive Multi-Jittered (with blue noise) set.
#'
#'@param n The number of 2D values to extract.
#'@param seed Default `0`. The random seed.
#'@return An `n` x `2` matrix with all the calculated values from the set.
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_pmjbn_set(n=1000)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a longer sequence of values from that set
#'points2d = generate_pmjbn_set(n=1500)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a new set by changing the seed
#'points2d = generate_pmjbn_set(n=1500,seed=10)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = generate_pmjbn_set(10000)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_pmjbn_set = function(n, seed = 0) {
  vals = unlist(rcpp_generate_pmjbn_set(n, seed))
  return(matrix(vals, nrow=n,ncol=2,byrow=TRUE))
}

#'@title Generate 2D Progressive Multi-Jittered (0, 2) Set
#'
#'@description Generate a set of values from a Progressive Multi-Jittered (0, 2) set.
#'
#'@param n The number of 2D values to extract.
#'@param seed Default `0`. The random seed.
#'@return An `n` x `2` matrix with all the calculated values from the set.
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_pmj02_set(n=1000)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a longer sequence of values from that set
#'points2d = generate_pmj02_set(n=1500)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a new set by changing the seed
#'points2d = generate_pmj02_set(n=1500,seed=10)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = generate_pmj02_set(10000)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_pmj02_set = function(n, seed = 0) {
  vals = unlist(rcpp_generate_pmj02_set(n, seed))
  return(matrix(vals, nrow=n,ncol=2,byrow=TRUE))
}

#'@title Generate 2D Progressive Multi-Jittered (0, 2) (with blue noise) Set
#'
#'@description Generate a set of values from a Progressive Multi-Jittered (0, 2) (with blue noise) set.
#'
#'@param n The number of 2D values to extract.
#'@param seed Default `0`. The random seed.
#'@return An `n` x `2` matrix with all the calculated values from the set.
#'
#'@export
#'@examples
#'#Generate a 2D sample:
#'points2d = generate_pmj02bn_set(n=1000)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a longer sequence of values from that set
#'points2d = generate_pmj02bn_set(n=1500)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Generate a new set by changing the seed
#'points2d = generate_pmj02bn_set(n=1500,seed=10)
#'plot(points2d, xlim=c(0,1),ylim=c(0,1))
#'
#'#Integrate the value of pi by counting the number of randomly generated points that fall
#'#within the unit circle.
#'pointset = generate_pmj02bn_set(10000)
#'
#'pi_estimate = 4*sum(pointset[,1] * pointset[,1] + pointset[,2] * pointset[,2] < 1)/10000
#'pi_estimate
generate_pmj02bn_set = function(n, seed = 0) {
  vals = unlist(rcpp_generate_pmj02bn_set(n,seed))
  return(matrix(vals, nrow=n,ncol=2,byrow=TRUE))
}