forked from oguri/glafic2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsl_integration.c
149 lines (116 loc) · 3.54 KB
/
gsl_integration.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_integration.h>
#include "glafic.h"
double gsl_qgaus_func(double x, void *p);
static double (*func_qgaus_sav)(double);
double gsl_romberg1_func(double x, void *p);
double gsl_romberg2_func(double x, void *p);
double gsl_romberg3_func(double x, void *p);
static double (*func_romberg1_sav)(double);
static double (*func_romberg2_sav)(double);
static double (*func_romberg3_sav)(double);
/*--------------------------------------------------------------
Gauss integral, fixed points (21 points)
*/
double gsl_qgaus_func(double x, void *p)
{
return (*func_qgaus_sav)(x);
}
double gsl_qgaus(double (*func)(double), double a, double b)
{
double integral, err;
size_t neval;
gsl_function F;
func_qgaus_sav = func;
F.function = &gsl_qgaus_func;
gsl_integration_qng(&F, a, b, TOL_QGAUS, TOL_QGAUS, &integral, &err, &neval);
return integral;
}
/*--------------------------------------------------------------
Romberg integral
*/
double gsl_romberg1_func(double x, void *p)
{
return (*func_romberg1_sav)(x);
}
double gsl_romberg2_func(double x, void *p)
{
return (*func_romberg2_sav)(x);
}
double gsl_romberg3_func(double x, void *p)
{
return (*func_romberg3_sav)(x);
}
double gsl_romberg1(double (*func)(double), double a, double b, double eps)
{
int gslstatus;
double integral;
size_t neval;
gsl_integration_romberg_workspace *workspace = NULL;
gsl_function F;
func_romberg1_sav = func;
F.function = &gsl_romberg1_func;
workspace = gsl_integration_romberg_alloc( GSL_ROMBERG_N );
if(workspace == NULL){
fprintf(stderr, "failed to create workspace in gsl_romberg1\n");
exit(EXIT_FAILURE);
} else {
gslstatus = gsl_integration_romberg(&F, a, b, 0.0, eps, &integral, &neval, workspace);
/* if (gslstatus != GSL_SUCCESS){
fprintf(stderr, "integration failed in gsl_romberg1\n");
exit(EXIT_FAILURE);
} */
}
gsl_integration_romberg_free(workspace);
return integral;
}
double gsl_romberg2(double (*func)(double), double a, double b, double eps)
{
int gslstatus;
double integral;
size_t neval;
gsl_integration_romberg_workspace *workspace = NULL;
gsl_function F;
func_romberg2_sav = func;
F.function = &gsl_romberg2_func;
workspace = gsl_integration_romberg_alloc( GSL_ROMBERG_N );
if(workspace == NULL){
fprintf(stderr, "failed to create workspace in gsl_romberg2\n");
exit(EXIT_FAILURE);
} else {
gslstatus = gsl_integration_romberg(&F, a, b, 0.0, eps, &integral, &neval, workspace);
/* if (gslstatus != GSL_SUCCESS){
fprintf(stderr, "integration failed in gsl_romberg2\n");
exit(EXIT_FAILURE);
} */
}
gsl_integration_romberg_free(workspace);
return integral;
}
double gsl_romberg3(double (*func)(double), double a, double b, double eps)
{
int gslstatus;
double integral;
size_t neval;
gsl_integration_romberg_workspace *workspace = NULL;
gsl_function F;
func_romberg3_sav = func;
F.function = &gsl_romberg3_func;
workspace = gsl_integration_romberg_alloc( GSL_ROMBERG_N );
if(workspace == NULL){
fprintf(stderr, "failed to create workspace in gsl_romberg3\n");
exit(EXIT_FAILURE);
} else {
gslstatus = gsl_integration_romberg(&F, a, b, 0.0, eps, &integral, &neval, workspace);
/* if (gslstatus != GSL_SUCCESS){
fprintf(stderr, "integration failed in gsl_romberg3\n");
exit(EXIT_FAILURE);
} */
}
gsl_integration_romberg_free(workspace);
return integral;
}