3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_integration.h>
27 #include "initialise.c"
34 gsl_integration_qaws (gsl_function * f,
35 const double a, const double b,
36 gsl_integration_qaws_table * t,
37 const double epsabs, const double epsrel,
39 gsl_integration_workspace * workspace,
40 double *result, double *abserr)
43 double result0, abserr0;
46 int roundoff_type1 = 0, roundoff_type2 = 0, error_type = 0;
48 /* Initialize results */
50 initialise (workspace, a, b);
55 if (limit > workspace->limit)
57 GSL_ERROR ("iteration limit exceeds available workspace", GSL_EINVAL) ;
62 GSL_ERROR ("limits must form an ascending sequence, a < b", GSL_EINVAL) ;
65 if (epsabs <= 0 && (epsrel < 50 * GSL_DBL_EPSILON || epsrel < 0.5e-28))
67 GSL_ERROR ("tolerance cannot be acheived with given epsabs and epsrel",
71 /* perform the first integration */
75 double error1, error2;
76 int err_reliable1, err_reliable2;
78 double b1 = 0.5 * (a + b);
82 qc25s (f, a, b, a1, b1, t, &area1, &error1, &err_reliable1);
83 qc25s (f, a, b, a2, b2, t, &area2, &error2, &err_reliable2);
87 append_interval (workspace, a1, b1, area1, error1);
88 append_interval (workspace, a2, b2, area2, error2);
92 append_interval (workspace, a2, b2, area2, error2);
93 append_interval (workspace, a1, b1, area1, error1);
96 result0 = area1 + area2;
97 abserr0 = error1 + error2;
100 /* Test on accuracy */
102 tolerance = GSL_MAX_DBL (epsabs, epsrel * fabs (result0));
104 /* Test on accuracy, use 0.01 relative error as an extra safety
105 margin on the first iteration (ignored for subsequent iterations) */
107 if (abserr0 < tolerance && abserr0 < 0.01 * fabs(result0))
119 GSL_ERROR ("a maximum of one iteration was insufficient", GSL_EMAXITER);
129 double a1, b1, a2, b2;
130 double a_i, b_i, r_i, e_i;
131 double area1 = 0, area2 = 0, area12 = 0;
132 double error1 = 0, error2 = 0, error12 = 0;
133 int err_reliable1, err_reliable2;
135 /* Bisect the subinterval with the largest error estimate */
137 retrieve (workspace, &a_i, &b_i, &r_i, &e_i);
140 b1 = 0.5 * (a_i + b_i);
144 qc25s (f, a, b, a1, b1, t, &area1, &error1, &err_reliable1);
145 qc25s (f, a, b, a2, b2, t, &area2, &error2, &err_reliable2);
147 area12 = area1 + area2;
148 error12 = error1 + error2;
150 errsum += (error12 - e_i);
151 area += area12 - r_i;
153 if (err_reliable1 && err_reliable2)
155 double delta = r_i - area12;
157 if (fabs (delta) <= 1.0e-5 * fabs (area12) && error12 >= 0.99 * e_i)
161 if (iteration >= 10 && error12 > e_i)
167 tolerance = GSL_MAX_DBL (epsabs, epsrel * fabs (area));
169 if (errsum > tolerance)
171 if (roundoff_type1 >= 6 || roundoff_type2 >= 20)
173 error_type = 2; /* round off error */
176 /* set error flag in the case of bad integrand behaviour at
177 a point of the integration range */
179 if (subinterval_too_small (a1, a2, b2))
185 update (workspace, a1, b1, area1, error1, a2, b2, area2, error2);
187 retrieve (workspace, &a_i, &b_i, &r_i, &e_i);
192 while (iteration < limit && !error_type && errsum > tolerance);
194 *result = sum_results (workspace);
197 if (errsum <= tolerance)
201 else if (error_type == 2)
203 GSL_ERROR ("roundoff error prevents tolerance from being achieved",
206 else if (error_type == 3)
208 GSL_ERROR ("bad integrand behavior found in the integration interval",
211 else if (iteration == limit)
213 GSL_ERROR ("maximum number of subdivisions reached", GSL_EMAXITER);
217 GSL_ERROR ("could not integrate function", GSL_EFAILED);