-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.c
79 lines (59 loc) · 2.34 KB
/
example.c
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
/* example using lintegrate functionality */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_integration.h>
#include <lintegrate.h>
/* create function for integration */
double lintegrand(double x, void *params);
struct intparams {
double mu;
double sig;
};
double lintegrand(double x, void *params){
struct intparams * p = (struct intparams *)params;
double mu = p->mu;
double sig = p->sig;
return -0.5*(mu-x)*(mu-x)/(sig*sig);
}
double integrand(double x, void *params){
struct intparams * p = (struct intparams *)params;
double mu = p->mu;
double sig = p->sig;
return exp(-0.5*(mu-x)*(mu-x)/(sig*sig));
}
int main( int argv, char **argc ){
gsl_function F;
struct intparams params;
gsl_integration_workspace *w = gsl_integration_workspace_alloc (100);
gsl_integration_cquad_workspace *cw = gsl_integration_cquad_workspace_alloc(50);
double qaganswer = 0., qnganswer = 0., cquadanswer = 0., answer = 0.;
double abserr = 0.;
size_t neval = 0;
double minlim = -6.; /* minimum for integration range */
double maxlim = 6.; /* maximum for integration range */
double abstol = 1e-10; /* absolute tolerance */
double reltol = 1e-10; /* relative tolerance */
params.mu = 0.;
params.sig = 1.;
F.function = &lintegrand;
F.params = ¶ms;
/* integrate log of function using QAG */
lintegration_qag(&F, minlim, maxlim, abstol, reltol, 100, GSL_INTEG_GAUSS31, w, &qaganswer, &abserr);
/* integrate log of function using QNG */
lintegration_qng(&F, minlim, maxlim, abstol, reltol, &qnganswer, &abserr, &neval);
/* integrate log of function using CQUAD */
lintegration_cquad(&F, minlim, maxlim, abstol, reltol, cw, &cquadanswer, &abserr, &neval);
/* integrate function using GSL QAG */
F.function = &integrand;
gsl_integration_qag(&F, minlim, maxlim, abstol, reltol, 100, GSL_INTEG_GAUSS31, w, &answer, &abserr);
gsl_integration_workspace_free(w);
gsl_integration_cquad_workspace_free(cw);
fprintf(stdout, "Answer \"lintegrate QAG\" = %.8lf\n", qaganswer);
fprintf(stdout, "Answer \"lintegrate QNG\" = %.8lf\n", qnganswer);
fprintf(stdout, "Answer \"lintegrate CQUAD\" = %.8lf\n", cquadanswer);
fprintf(stdout, "Answer \"gsl_integrate_qag\" = %.8lf\n", log(answer));
fprintf(stdout, "Analytical answer = %.8lf\n", log(sqrt(2.*M_PI)));
return 0;
}