BRL-CAD
sobol.h
Go to the documentation of this file.
1/* S O B O L . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21/*----------------------------------------------------------------------*/
22/** @addtogroup bn_sobol
23 *
24 * @brief
25 * Generation of the Sobol quasi-random low-discrepancy sequence of numbers.
26 *
27 * @par Usage:
28 * @code
29 * double *snums;
30 * struct bn_soboldata *sd = bn_sobol_create(3, time(NULL));
31 * if (sd) {
32 * bn_sobol_skip(s, 4000, snums);
33 * for (i = 0; i < 4000; i++) {
34 * snums = bn_sobol_next(s, NULL, NULL);
35 * printf("x[%d]: %g %g %g", i, snums[0], snums[1], snums[2]);
36 * }
37 * bn_sobol_destroy(sd);
38 * }
39 * @endcode
40 *
41 */
42/** @{ */
43/** @file sobol.h */
44
45#ifndef BN_SOBOL_H
46#define BN_SOBOL_H
47
48#include "common.h"
49#include "bn/defines.h"
50#include "vmath.h"
51
52__BEGIN_DECLS
53
54struct bn_soboldata;
55
56/**
57 * Maximum dimension of Sobol output array
58 */
59#define BN_SOBOL_MAXDIM 1111
60
61/**
62 * Create and initialize an instance of a Sobol sequence data container. If seed
63 * is non-zero the value will be used in initialization, otherwise a default will
64 * be used. User must destroy the returned data with bn_sobol_destroy */
65BN_EXPORT extern struct bn_soboldata * bn_sobol_create(unsigned int sdim, unsigned long seed);
66
67/** Destroy a Sobol data container */
68BN_EXPORT extern void bn_sobol_destroy(struct bn_soboldata *s);
69
70/**
71 * Return the next vector in Sobol sequence, scaled to (lb[i], ub[i]) interval.
72 *
73 * If lb and ub are NULL, x[i] will be in the range (0,1).
74 *
75 * The return vector is read only and is managed internally by bn_sobodata.
76 *
77 * Note: not performing the scale saves some math operations, so NULL lb and ub
78 * are recommend if the required interval for the caller's application happens
79 * to be (0,1).
80 *
81 * Note: If the user attempts to read more than 2^32-1 points from the sequence,
82 * the generator will fall back on pseudo random number generation.
83 */
84BN_EXPORT extern double * bn_sobol_next(struct bn_soboldata *s, const double *lb, const double *ub);
85
86/**
87 * If the caller knows in advance how many numbers (n) they want to compute,
88 * this function supports implementation of the Acworth et al (1998) strategy
89 * of skipping a number of points equal to the largest power of 2 smaller than
90 * n for better performance.
91 *
92 * Joe and Kuo indicate in their notes at
93 * http://web.maths.unsw.edu.au/~fkuo/sobol/ that they are "less persuaded" by
94 * this recommendation, but this function is available for callers who wish to
95 * use it.
96 */
97BN_EXPORT extern void bn_sobol_skip(struct bn_soboldata *s, unsigned n);
98
99/**
100 * @brief
101 * Generate a sample point on a sphere per Marsaglia (1972), using the Sobol
102 * data sequence s to drive the selection.
103 *
104 * The caller is responsible for initializing the bn_sobodata sequence before
105 * generating points.
106 *
107 * TODO: investigate the http://www.dtic.mil/docs/citations/ADA510216
108 * scrambling method to see if basic Sobol sequence can be improved on for
109 * spherical sampling Also relevant: people.sc.fsu.edu/~hcc8471/ssobol.pdf
110 */
111BN_EXPORT extern void bn_sobol_sph_sample(point_t sample, const point_t center, const fastf_t radius, struct bn_soboldata *s);
112
113__END_DECLS
114
115#endif /* BN_SOBOL_H */
116/** @} */
117/*
118 * Local Variables:
119 * mode: C
120 * tab-width: 8
121 * indent-tabs-mode: t
122 * c-file-style: "stroustrup"
123 * End:
124 * ex: shiftwidth=4 tabstop=8
125 */
Header file for the BRL-CAD common definitions.
void bn_sobol_skip(struct bn_soboldata *s, unsigned n)
double * bn_sobol_next(struct bn_soboldata *s, const double *lb, const double *ub)
void bn_sobol_sph_sample(point_t sample, const point_t center, const fastf_t radius, struct bn_soboldata *s)
Generate a sample point on a sphere per Marsaglia (1972), using the Sobol data sequence s to drive th...
struct bn_soboldata * bn_sobol_create(unsigned int sdim, unsigned long seed)
void bn_sobol_destroy(struct bn_soboldata *s)
void float float int * n
Definition: tig.h:74
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:330
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition: vmath.h:351
fundamental vector, matrix, quaternion math macros