3
<title>Monte Carlo Interface - GNU Scientific Library -- Reference Manual</title>
4
<meta http-equiv="Content-Type" content="text/html">
5
<meta name="description" content="GNU Scientific Library -- Reference Manual">
6
<meta name="generator" content="makeinfo 4.8">
7
<link title="Top" rel="start" href="index.html#Top">
8
<link rel="up" href="Monte-Carlo-Integration.html#Monte-Carlo-Integration" title="Monte Carlo Integration">
9
<link rel="next" href="PLAIN-Monte-Carlo.html#PLAIN-Monte-Carlo" title="PLAIN Monte Carlo">
10
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 The GSL Team.
14
Permission is granted to copy, distribute and/or modify this document
15
under the terms of the GNU Free Documentation License, Version 1.2 or
16
any later version published by the Free Software Foundation; with the
17
Invariant Sections being ``GNU General Public License'' and ``Free Software
18
Needs Free Documentation'', the Front-Cover text being ``A GNU Manual'',
19
and with the Back-Cover Text being (a) (see below). A copy of the
20
license is included in the section entitled ``GNU Free Documentation
23
(a) The Back-Cover Text is: ``You have freedom to copy and modify this
24
GNU Manual, like GNU software.''-->
25
<meta http-equiv="Content-Style-Type" content="text/css">
26
<style type="text/css"><!--
27
pre.display { font-family:inherit }
28
pre.format { font-family:inherit }
29
pre.smalldisplay { font-family:inherit; font-size:smaller }
30
pre.smallformat { font-family:inherit; font-size:smaller }
31
pre.smallexample { font-size:smaller }
32
pre.smalllisp { font-size:smaller }
33
span.sc { font-variant:small-caps }
34
span.roman { font-family:serif; font-weight:normal; }
35
span.sansserif { font-family:sans-serif; font-weight:normal; }
41
<a name="Monte-Carlo-Interface"></a>
42
Next: <a rel="next" accesskey="n" href="PLAIN-Monte-Carlo.html#PLAIN-Monte-Carlo">PLAIN Monte Carlo</a>,
43
Up: <a rel="up" accesskey="u" href="Monte-Carlo-Integration.html#Monte-Carlo-Integration">Monte Carlo Integration</a>
47
<h3 class="section">23.1 Interface</h3>
49
<p>All of the Monte Carlo integration routines use the same general form of
50
interface. There is an allocator to allocate memory for control
51
variables and workspace, a routine to initialize those control
52
variables, the integrator itself, and a function to free the space when
55
<p>Each integration function requires a random number generator to be
56
supplied, and returns an estimate of the integral and its standard
57
deviation. The accuracy of the result is determined by the number of
58
function calls specified by the user. If a known level of accuracy is
59
required this can be achieved by calling the integrator several times
60
and averaging the individual results until the desired accuracy is
63
<p>Random sample points used within the Monte Carlo routines are always
64
chosen strictly within the integration region, so that endpoint
65
singularities are automatically avoided.
67
<p>The function to be integrated has its own datatype, defined in the
68
header file <samp><span class="file">gsl_monte.h</span></samp>.
71
— Data Type: <b>gsl_monte_function</b><var><a name="index-gsl_005fmonte_005ffunction-1870"></a></var><br>
73
<p>This data type defines a general function with parameters for Monte
77
<dt><code>double (* f) (double * </code><var>x</var><code>, size_t </code><var>dim</var><code>, void * </code><var>params</var><code>)</code><dd>this function should return the value
78
<!-- {$f(x,\hbox{\it params})$} -->
79
f(x,params) for the argument <var>x</var> and parameters <var>params</var>,
80
where <var>x</var> is an array of size <var>dim</var> giving the coordinates of
81
the point where the function is to be evaluated.
83
<br><dt><code>size_t dim</code><dd>the number of dimensions for <var>x</var>.
85
<br><dt><code>void * params</code><dd>a pointer to the parameters of the function.
87
</p></blockquote></div>
89
<p class="noindent">Here is an example for a quadratic function in two dimensions,
90
with a = 3, b = 2, c = 1. The following code
91
defines a <code>gsl_monte_function</code> <code>F</code> which you could pass to an
94
<pre class="example"> struct my_f_params { double a; double b; double c; };
97
my_f (double x[], size_t dim, void * p) {
98
struct my_f_params * fp = (struct my_f_params *)p;
102
fprintf (stderr, "error: dim != 2");
106
return fp->a * x[0] * x[0]
107
+ fp->b * x[0] * x[1]
108
+ fp->c * x[1] * x[1];
111
gsl_monte_function F;
112
struct my_f_params params = { 3.0, 2.0, 1.0 };
116
F.params = &params;
118
<p class="noindent">The function f(x) can be evaluated using the following macro,
120
<pre class="example"> #define GSL_MONTE_FN_EVAL(F,x)
121
(*((F)->f))(x,(F)->dim,(F)->params)