|
Packit |
67cb25 |
/* monte/test.c
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000 Michael Booth
|
|
Packit |
67cb25 |
* Copyright (C) 2009 Michael Booth
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
67cb25 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
67cb25 |
* the Free Software Foundation; either version 3 of the License, or (at
|
|
Packit |
67cb25 |
* your option) any later version.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is distributed in the hope that it will be useful, but
|
|
Packit |
67cb25 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
67cb25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
67cb25 |
* General Public License for more details.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
67cb25 |
* along with this program; if not, write to the Free Software
|
|
Packit |
67cb25 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <config.h>
|
|
Packit |
67cb25 |
#include <stdlib.h>
|
|
Packit |
67cb25 |
#include <math.h>
|
|
Packit |
67cb25 |
#include <stdio.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <gsl/gsl_math.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_errno.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_rng.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_test.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_ieee_utils.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <gsl/gsl_rng.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_monte_plain.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_monte_miser.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_monte_vegas.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#define CONSTANT
|
|
Packit |
67cb25 |
#define STEP
|
|
Packit |
67cb25 |
#define PRODUCT
|
|
Packit |
67cb25 |
#define GAUSSIAN
|
|
Packit |
67cb25 |
#define DBLGAUSSIAN
|
|
Packit |
67cb25 |
#define TSUDA
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#define PLAIN
|
|
Packit |
67cb25 |
#define MISER
|
|
Packit |
67cb25 |
#define VEGAS
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
double xl[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
Packit |
67cb25 |
double xu[11] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
|
|
Packit |
67cb25 |
double xu2[11] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
|
|
Packit |
67cb25 |
double xu3[2] = { GSL_DBL_MAX, GSL_DBL_MAX };
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
double fconst (double x[], size_t d, void *params);
|
|
Packit |
67cb25 |
double fstep (double x[], size_t d, void *params);
|
|
Packit |
67cb25 |
double f0 (double x[], size_t d, void *params);
|
|
Packit |
67cb25 |
double f1 (double x[], size_t d, void *params);
|
|
Packit |
67cb25 |
double f2 (double x[], size_t d, void *params);
|
|
Packit |
67cb25 |
double f3 (double x[], size_t d, void *params);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
void my_error_handler (const char *reason, const char *file,
|
|
Packit |
67cb25 |
int line, int err);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
struct problem {
|
|
Packit |
67cb25 |
gsl_monte_function * f;
|
|
Packit |
67cb25 |
double * xl;
|
|
Packit |
67cb25 |
double * xu;
|
|
Packit |
67cb25 |
size_t dim;
|
|
Packit |
67cb25 |
size_t calls;
|
|
Packit |
67cb25 |
double expected_result;
|
|
Packit |
67cb25 |
double expected_error;
|
|
Packit |
67cb25 |
char * description;
|
|
Packit |
67cb25 |
} ;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_monte_function
|
|
Packit |
67cb25 |
make_function (double (*f)(double *, size_t, void *), size_t d, void * p);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_monte_function
|
|
Packit |
67cb25 |
make_function (double (*f)(double *, size_t, void *), size_t d, void * p)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_monte_function f_new;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
f_new.f = f;
|
|
Packit |
67cb25 |
f_new.dim = d;
|
|
Packit |
67cb25 |
f_new.params = p;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return f_new;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
void
|
|
Packit |
67cb25 |
add (struct problem * problems, int * n,
|
|
Packit |
67cb25 |
gsl_monte_function * f, double xl[], double xu[], size_t dim, size_t calls,
|
|
Packit |
67cb25 |
double result, double err, char * description);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
void
|
|
Packit |
67cb25 |
add (struct problem * problems, int * n,
|
|
Packit |
67cb25 |
gsl_monte_function * f, double xl[], double xu[], size_t dim, size_t calls,
|
|
Packit |
67cb25 |
double result, double err, char * description)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
int i = *n;
|
|
Packit |
67cb25 |
problems[i].f = f;
|
|
Packit |
67cb25 |
problems[i].xl = xl;
|
|
Packit |
67cb25 |
problems[i].xu = xu;
|
|
Packit |
67cb25 |
problems[i].dim = dim;
|
|
Packit |
67cb25 |
problems[i].calls = calls;
|
|
Packit |
67cb25 |
problems[i].expected_result = result;
|
|
Packit |
67cb25 |
problems[i].expected_error = err;
|
|
Packit |
67cb25 |
problems[i].description = description;
|
|
Packit |
67cb25 |
(*n)++;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#define TRIALS 10
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int
|
|
Packit |
67cb25 |
main (void)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double result[TRIALS], error[TRIALS];
|
|
Packit |
67cb25 |
double a = 0.1;
|
|
Packit |
67cb25 |
double c = (1.0 + sqrt (10.0)) / 9.0;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_monte_function Fc = make_function(&fconst, 0, 0);
|
|
Packit |
67cb25 |
gsl_monte_function Fs = make_function(&fstep, 0, 0);
|
|
Packit |
67cb25 |
gsl_monte_function F0 = make_function(&f0, 0, &a);
|
|
Packit |
67cb25 |
gsl_monte_function F1 = make_function(&f1, 0, &a);
|
|
Packit |
67cb25 |
gsl_monte_function F2 = make_function(&f2, 0, &a);
|
|
Packit |
67cb25 |
gsl_monte_function F3 = make_function(&f3, 0, &c);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* The relationship between the variance of the function itself, the
|
|
Packit |
67cb25 |
error on the integral and the number of calls is,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
sigma = sqrt(variance/N)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the variance is the <(f - <f>)^2> where <.> denotes the
|
|
Packit |
67cb25 |
volume average (integral over the integration region divided by
|
|
Packit |
67cb25 |
the volume) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int n = 0;
|
|
Packit |
67cb25 |
struct problem * I;
|
|
Packit |
67cb25 |
struct problem problems[256];
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef CONSTANT
|
|
Packit |
67cb25 |
/* variance(Fc) = 0 */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 1, 1000, 1.0, 0.0, "constant, 1d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 2, 1000, 1.0, 0.0, "constant, 2d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 3, 1000, 1.0, 0.0, "constant, 3d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 4, 1000, 1.0, 0.0, "constant, 4d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 5, 1000, 1.0, 0.0, "constant, 5d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 6, 1000, 1.0, 0.0, "constant, 6d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 7, 1000, 1.0, 0.0, "constant, 7d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 8, 1000, 1.0, 0.0, "constant, 8d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 9, 1000, 1.0, 0.0, "constant, 9d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fc, xl, xu, 10, 1000, 1.0, 0.0, "constant, 10d");
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef STEP
|
|
Packit |
67cb25 |
/* variance(Fs) = 0.4/sqrt(1000) */
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 1, 100000, 0.8, 1.264e-3, "step, 1d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 2, 100000, 0.8, 1.264e-3, "step, 2d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 3, 100000, 0.8, 1.264e-3, "step, 3d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 4, 100000, 0.8, 1.264e-3, "step, 4d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 5, 100000, 0.8, 1.264e-3, "step, 5d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 6, 100000, 0.8, 1.264e-3, "step, 6d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 7, 100000, 0.8, 1.264e-3, "step, 7d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 8, 100000, 0.8, 1.264e-3, "step, 8d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 9, 100000, 0.8, 1.264e-3, "step, 9d");
|
|
Packit |
67cb25 |
add(problems,&n, &Fs, xl, xu, 10, 100000, 0.8, 1.264e-3, "step, 10d");
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef PRODUCT
|
|
Packit |
67cb25 |
/* variance(F0) = (4/3)^d - 1 */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 1, 3333, 1.0, 0.01, "product, 1d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 2, 7777, 1.0, 0.01, "product, 2d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 3, 13703, 1.0, 0.01, "product, 3d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 4, 21604, 1.0, 0.01, "product, 4d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 5, 32139, 1.0, 0.01, "product, 5d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 6, 46186, 1.0, 0.01, "product, 6d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 7, 64915, 1.0, 0.01, "product, 7d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 8, 89887, 1.0, 0.01, "product, 8d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 9, 123182, 1.0, 0.01, "product, 9d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F0, xl, xu, 10, 167577, 1.0, 0.01, "product, 10d" );
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef GAUSSIAN
|
|
Packit |
67cb25 |
/* variance(F1) = (1/(a sqrt(2 pi)))^d - 1 */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 1, 298, 1.0, 0.1, "gaussian, 1d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 2, 1492, 1.0, 0.1, "gaussian, 2d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 3, 6249, 1.0, 0.1, "gaussian, 3d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 4, 25230, 1.0, 0.1, "gaussian, 4d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 5, 100953, 1.0, 0.1, "gaussian, 5d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 6, 44782, 1.0, 0.3, "gaussian, 6d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 7, 178690, 1.0, 0.3, "gaussian, 7d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 8, 712904, 1.0, 0.3, "gaussian, 8d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 9, 2844109, 1.0, 0.3, "gaussian, 9d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F1, xl, xu, 10, 11346390, 1.0, 0.3, "gaussian, 10d" );
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef DBLGAUSSIAN
|
|
Packit |
67cb25 |
/* variance(F2) = 0.5 * (1/(a sqrt(2 pi)))^d - 1 */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 1, 9947, 1.0, 0.01, "double gaussian, 1d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 2, 695, 1.0, 0.1, "double gaussian, 2d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 3, 3074, 1.0, 0.1, "double gaussian, 3d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 4, 12565, 1.0, 0.1, "double gaussian, 4d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 5, 50426, 1.0, 0.1, "double gaussian, 5d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 6, 201472, 1.0, 0.1, "double gaussian, 6d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 7, 804056, 1.0, 0.1, "double gaussian, 7d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 8, 356446, 1.0, 0.3, "double gaussian, 8d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 9, 1422049, 1.0, 0.3, "double gaussian, 9d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F2, xl, xu, 10, 5673189, 1.0, 0.3, "double gaussian, 10d" );
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef TSUDA
|
|
Packit |
67cb25 |
/* variance(F3) = ((c^2 + c + 1/3)/(c(c+1)))^d - 1 */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 1, 4928, 1.0, 0.01, "tsuda function, 1d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 2, 12285, 1.0, 0.01, "tsuda function, 2d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 3, 23268, 1.0, 0.01, "tsuda function, 3d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 4, 39664, 1.0, 0.01, "tsuda function, 4d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 5, 64141, 1.0, 0.01, "tsuda function, 5d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 6, 100680, 1.0, 0.01, "tsuda function, 6d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 7, 155227, 1.0, 0.01, "tsuda function, 7d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 8, 236657, 1.0, 0.01, "tsuda function, 8d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 9, 358219, 1.0, 0.01, "tsuda function, 9d" );
|
|
Packit |
67cb25 |
add(problems,&n, &F3, xl, xu, 10, 539690, 1.0, 0.01, "tsuda function, 10d" );
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
add(problems,&n, 0, 0, 0, 0, 0, 0, 0, 0 );
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* gsl_set_error_handler (&my_error_handler); */
|
|
Packit |
67cb25 |
gsl_ieee_env_setup ();
|
|
Packit |
67cb25 |
gsl_rng_env_setup ();
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef A
|
|
Packit |
67cb25 |
printf ("testing allocation/input checks\n");
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
status = gsl_monte_plain_validate (s, xl, xu3, 1, 1);
|
|
Packit |
67cb25 |
gsl_test (status != 0, "error if limits too large");
|
|
Packit |
67cb25 |
status = gsl_monte_plain_validate (s, xl, xu, 0, 10);
|
|
Packit |
67cb25 |
gsl_test (status != 0, "error if num_dim = 0");
|
|
Packit |
67cb25 |
status = gsl_monte_plain_validate (s, xl, xu, 1, 0);
|
|
Packit |
67cb25 |
gsl_test (status != 0, "error if calls = 0");
|
|
Packit |
67cb25 |
status = gsl_monte_plain_validate (s, xu, xl, 1, 10);
|
|
Packit |
67cb25 |
gsl_test (status != 0, "error if xu < xl");
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef PLAIN
|
|
Packit |
67cb25 |
#define NAME "plain"
|
|
Packit |
67cb25 |
#define MONTE_STATE gsl_monte_plain_state
|
|
Packit |
67cb25 |
#define MONTE_ALLOC gsl_monte_plain_alloc
|
|
Packit |
67cb25 |
#define MONTE_INTEGRATE gsl_monte_plain_integrate
|
|
Packit |
67cb25 |
#define MONTE_FREE gsl_monte_plain_free
|
|
Packit |
67cb25 |
#define MONTE_SPEEDUP 1
|
|
Packit |
67cb25 |
#define MONTE_ERROR_TEST(err,expected) gsl_test_factor(err,expected, 5.0, NAME ", %s, abserr[%d]", I->description, i)
|
|
Packit |
67cb25 |
#include "test_main.c"
|
|
Packit |
67cb25 |
#undef NAME
|
|
Packit |
67cb25 |
#undef MONTE_STATE
|
|
Packit |
67cb25 |
#undef MONTE_ALLOC
|
|
Packit |
67cb25 |
#undef MONTE_INTEGRATE
|
|
Packit |
67cb25 |
#undef MONTE_FREE
|
|
Packit |
67cb25 |
#undef MONTE_ERROR_TEST
|
|
Packit |
67cb25 |
#undef MONTE_SPEEDUP
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef MISER
|
|
Packit |
67cb25 |
#define NAME "miser"
|
|
Packit |
67cb25 |
#define MONTE_STATE gsl_monte_miser_state
|
|
Packit |
67cb25 |
#define MONTE_ALLOC gsl_monte_miser_alloc
|
|
Packit |
67cb25 |
#define MONTE_INTEGRATE gsl_monte_miser_integrate
|
|
Packit |
67cb25 |
#define MONTE_FREE gsl_monte_miser_free
|
|
Packit |
67cb25 |
#define MONTE_SPEEDUP 2
|
|
Packit |
67cb25 |
#define MONTE_ERROR_TEST(err,expected) gsl_test(err > 5.0 * expected, NAME ", %s, abserr[%d] (obs %g vs plain %g)", I->description, i, err, expected)
|
|
Packit |
67cb25 |
#include "test_main.c"
|
|
Packit |
67cb25 |
#undef NAME
|
|
Packit |
67cb25 |
#undef MONTE_STATE
|
|
Packit |
67cb25 |
#undef MONTE_ALLOC
|
|
Packit |
67cb25 |
#undef MONTE_INTEGRATE
|
|
Packit |
67cb25 |
#undef MONTE_FREE
|
|
Packit |
67cb25 |
#undef MONTE_ERROR_TEST
|
|
Packit |
67cb25 |
#undef MONTE_SPEEDUP
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef MISER
|
|
Packit |
67cb25 |
#define NAME "miser(params)"
|
|
Packit |
67cb25 |
#define MONTE_STATE gsl_monte_miser_state
|
|
Packit |
67cb25 |
#define MONTE_ALLOC gsl_monte_miser_alloc
|
|
Packit |
67cb25 |
#define MONTE_PARAMS gsl_monte_miser_params
|
|
Packit |
67cb25 |
#define MONTE_INTEGRATE(f,xl,xu,dim,calls,r,s,res,err) { gsl_monte_miser_params_get(s, ¶ms) ; params.alpha = 1.5 ; gsl_monte_miser_params_set(s, ¶ms) ; gsl_monte_miser_integrate(f,xl,xu,dim,calls,r,s,res,err); }
|
|
Packit |
67cb25 |
#define MONTE_FREE gsl_monte_miser_free
|
|
Packit |
67cb25 |
#define MONTE_SPEEDUP 2
|
|
Packit |
67cb25 |
#define MONTE_ERROR_TEST(err,expected) gsl_test(err > 5.0 * expected, NAME ", %s, abserr[%d] (obs %g vs plain %g)", I->description, i, err, expected)
|
|
Packit |
67cb25 |
#include "test_main.c"
|
|
Packit |
67cb25 |
#undef NAME
|
|
Packit |
67cb25 |
#undef MONTE_STATE
|
|
Packit |
67cb25 |
#undef MONTE_ALLOC
|
|
Packit |
67cb25 |
#undef MONTE_PARAMS
|
|
Packit |
67cb25 |
#undef MONTE_INTEGRATE
|
|
Packit |
67cb25 |
#undef MONTE_FREE
|
|
Packit |
67cb25 |
#undef MONTE_ERROR_TEST
|
|
Packit |
67cb25 |
#undef MONTE_SPEEDUP
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef MISER
|
|
Packit |
67cb25 |
#define NAME "miser(params)"
|
|
Packit |
67cb25 |
#define MONTE_STATE gsl_monte_miser_state
|
|
Packit |
67cb25 |
#define MONTE_ALLOC gsl_monte_miser_alloc
|
|
Packit |
67cb25 |
#define MONTE_PARAMS gsl_monte_miser_params
|
|
Packit |
67cb25 |
#define MONTE_INTEGRATE(f,xl,xu,dim,calls,r,s,res,err) { gsl_monte_miser_params_get(s, ¶ms) ; params.alpha = 1.5 ; gsl_monte_miser_params_set(s, ¶ms) ; gsl_monte_miser_integrate(f,xl,xu,dim,calls,r,s,res,err); }
|
|
Packit |
67cb25 |
#define MONTE_FREE gsl_monte_miser_free
|
|
Packit |
67cb25 |
#define MONTE_SPEEDUP 2
|
|
Packit |
67cb25 |
#define MONTE_ERROR_TEST(err,expected) gsl_test(err > 5.0 * expected, NAME ", %s, abserr[%d] (obs %g vs plain %g)", I->description, i, err, expected)
|
|
Packit |
67cb25 |
#include "test_main.c"
|
|
Packit |
67cb25 |
#undef NAME
|
|
Packit |
67cb25 |
#undef MONTE_STATE
|
|
Packit |
67cb25 |
#undef MONTE_ALLOC
|
|
Packit |
67cb25 |
#undef MONTE_PARAMS
|
|
Packit |
67cb25 |
#undef MONTE_INTEGRATE
|
|
Packit |
67cb25 |
#undef MONTE_FREE
|
|
Packit |
67cb25 |
#undef MONTE_ERROR_TEST
|
|
Packit |
67cb25 |
#undef MONTE_SPEEDUP
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef VEGAS
|
|
Packit |
67cb25 |
#define NAME "vegas"
|
|
Packit |
67cb25 |
#define MONTE_STATE gsl_monte_vegas_state
|
|
Packit |
67cb25 |
#define MONTE_ALLOC gsl_monte_vegas_alloc
|
|
Packit |
67cb25 |
#define MONTE_INTEGRATE(f,xl,xu,dim,calls,r,s,res,err) { gsl_monte_vegas_integrate(f,xl,xu,dim,calls,r,s,res,err) ; }
|
|
Packit |
67cb25 |
#define MONTE_FREE gsl_monte_vegas_free
|
|
Packit |
67cb25 |
#define MONTE_SPEEDUP 3
|
|
Packit |
67cb25 |
#define MONTE_ERROR_TEST(err,expected) gsl_test(err > 3.0 * (expected == 0 ? 1.0/(I->calls/MONTE_SPEEDUP) : expected), NAME ", %s, abserr[%d] (obs %g vs exp %g)", I->description, i, err, expected) ; gsl_test(gsl_monte_vegas_chisq(s) < 0, NAME " returns valid chisq (%g)", gsl_monte_vegas_chisq(s))
|
|
Packit |
67cb25 |
#include "test_main.c"
|
|
Packit |
67cb25 |
#undef NAME
|
|
Packit |
67cb25 |
#undef MONTE_STATE
|
|
Packit |
67cb25 |
#undef MONTE_ALLOC
|
|
Packit |
67cb25 |
#undef MONTE_INTEGRATE
|
|
Packit |
67cb25 |
#undef MONTE_FREE
|
|
Packit |
67cb25 |
#undef MONTE_ERROR_TEST
|
|
Packit |
67cb25 |
#undef MONTE_SPEEDUP
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef VEGAS
|
|
Packit |
67cb25 |
#define NAME "vegas(warm)"
|
|
Packit |
67cb25 |
#define MONTE_STATE gsl_monte_vegas_state
|
|
Packit |
67cb25 |
#define MONTE_ALLOC gsl_monte_vegas_alloc
|
|
Packit |
67cb25 |
#define MONTE_INTEGRATE(f,xl,xu,dim,calls,r,s,res,err) { gsl_monte_vegas_integrate(f,xl,xu,dim,calls,r,s,res,err) ; gsl_monte_vegas_integrate(f,xl,xu,dim,calls,r,s,res,err); }
|
|
Packit |
67cb25 |
#define MONTE_FREE gsl_monte_vegas_free
|
|
Packit |
67cb25 |
#define MONTE_SPEEDUP 3
|
|
Packit |
67cb25 |
#define MONTE_ERROR_TEST(err,expected) gsl_test(err > 3.0 * (expected == 0 ? 1.0/(I->calls/MONTE_SPEEDUP) : expected), NAME ", %s, abserr[%d] (obs %g vs exp %g)", I->description, i, err, expected); gsl_test(gsl_monte_vegas_chisq(s) < 0, NAME " returns valid chisq (%g)", gsl_monte_vegas_chisq(s))
|
|
Packit |
67cb25 |
#include "test_main.c"
|
|
Packit |
67cb25 |
#undef NAME
|
|
Packit |
67cb25 |
#undef MONTE_STATE
|
|
Packit |
67cb25 |
#undef MONTE_ALLOC
|
|
Packit |
67cb25 |
#undef MONTE_INTEGRATE
|
|
Packit |
67cb25 |
#undef MONTE_FREE
|
|
Packit |
67cb25 |
#undef MONTE_ERROR_TEST
|
|
Packit |
67cb25 |
#undef MONTE_SPEEDUP
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef VEGAS
|
|
Packit |
67cb25 |
#define NAME "vegas(params)"
|
|
Packit |
67cb25 |
#define MONTE_STATE gsl_monte_vegas_state
|
|
Packit |
67cb25 |
#define MONTE_ALLOC gsl_monte_vegas_alloc
|
|
Packit |
67cb25 |
#define MONTE_PARAMS gsl_monte_vegas_params
|
|
Packit |
67cb25 |
#define MONTE_INTEGRATE(f,xl,xu,dim,calls,r,s,res,err) { gsl_monte_vegas_params_get(s, ¶ms) ; params.alpha = 2 ; params.iterations = 3 ; gsl_monte_vegas_params_set(s, ¶ms) ; gsl_monte_vegas_integrate(f,xl,xu,dim,calls,r,s,res,err); }
|
|
Packit |
67cb25 |
#define MONTE_FREE gsl_monte_vegas_free
|
|
Packit |
67cb25 |
#define MONTE_SPEEDUP 3
|
|
Packit |
67cb25 |
#define MONTE_ERROR_TEST(err,expected) gsl_test(err > 3.0 * (expected == 0 ? 1.0/(I->calls/MONTE_SPEEDUP) : expected), NAME ", %s, abserr[%d] (obs %g vs exp %g)", I->description, i, err, expected); gsl_test(gsl_monte_vegas_chisq(s) < 0, NAME " returns valid chisq (%g)", gsl_monte_vegas_chisq(s))
|
|
Packit |
67cb25 |
#include "test_main.c"
|
|
Packit |
67cb25 |
#undef NAME
|
|
Packit |
67cb25 |
#undef MONTE_STATE
|
|
Packit |
67cb25 |
#undef MONTE_ALLOC
|
|
Packit |
67cb25 |
#undef MONTE_PARAMS
|
|
Packit |
67cb25 |
#undef MONTE_INTEGRATE
|
|
Packit |
67cb25 |
#undef MONTE_FREE
|
|
Packit |
67cb25 |
#undef MONTE_ERROR_TEST
|
|
Packit |
67cb25 |
#undef MONTE_SPEEDUP
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
exit (gsl_test_summary ());
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Simple constant function */
|
|
Packit |
67cb25 |
double
|
|
Packit |
67cb25 |
fconst (double x[], size_t num_dim, void *params)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
return 1;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Step-type (pulse) function */
|
|
Packit |
67cb25 |
double
|
|
Packit |
67cb25 |
fstep (double x[], size_t num_dim, void *params)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
return (x[0] > 0.1 && x[0] < 0.9) ? 1 : 0;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Simple product function */
|
|
Packit |
67cb25 |
double
|
|
Packit |
67cb25 |
f0 (double x[], size_t num_dim, void *params)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double prod = 1.0;
|
|
Packit |
67cb25 |
unsigned int i;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (i = 0; i < num_dim; ++i)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
prod *= 2.0 * x[i];
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return prod;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Gaussian centered at 1/2. */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
double
|
|
Packit |
67cb25 |
f1 (double x[], size_t num_dim, void *params)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double a = *(double *)params;
|
|
Packit |
67cb25 |
double sum = 0.;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
unsigned int i;
|
|
Packit |
67cb25 |
for (i = 0; i < num_dim; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double dx = x[i] - 0.5;
|
|
Packit |
67cb25 |
sum += dx * dx;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
return (pow (M_2_SQRTPI / (2. * a), (double) num_dim) *
|
|
Packit |
67cb25 |
exp (-sum / (a * a)));
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* double gaussian */
|
|
Packit |
67cb25 |
double
|
|
Packit |
67cb25 |
f2 (double x[], size_t num_dim, void *params)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double a = *(double *)params;
|
|
Packit |
67cb25 |
double sum1 = 0.;
|
|
Packit |
67cb25 |
double sum2 = 0.;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
unsigned int i;
|
|
Packit |
67cb25 |
for (i = 0; i < num_dim; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double dx1 = x[i] - 1. / 3.;
|
|
Packit |
67cb25 |
double dx2 = x[i] - 2. / 3.;
|
|
Packit |
67cb25 |
sum1 += dx1 * dx1;
|
|
Packit |
67cb25 |
sum2 += dx2 * dx2;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
return 0.5 * pow (M_2_SQRTPI / (2. * a), num_dim)
|
|
Packit |
67cb25 |
* (exp (-sum1 / (a * a)) + exp (-sum2 / (a * a)));
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Tsuda's example */
|
|
Packit |
67cb25 |
double
|
|
Packit |
67cb25 |
f3 (double x[], size_t num_dim, void *params)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double c = *(double *)params;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
double prod = 1.;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
unsigned int i;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (i = 0; i < num_dim; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
prod *= c / (c + 1) * pow((c + 1) / (c + x[i]), 2.0);
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
return prod;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
void
|
|
Packit |
67cb25 |
my_error_handler (const char *reason, const char *file, int line, int err)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
if (0)
|
|
Packit |
67cb25 |
printf ("(caught [%s:%d: %s (%d)])\n", file, line, reason, err);
|
|
Packit |
67cb25 |
}
|