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"
28 #include "set_initial.c"
34 gsl_integration_qawc (gsl_function * f,
35 const double a, const double b, const double c,
36 const double epsabs, const double epsrel,
38 gsl_integration_workspace * workspace,
39 double *result, double *abserr)
42 double result0, abserr0;
45 int roundoff_type1 = 0, roundoff_type2 = 0, error_type = 0;
50 /* Initialize results */
55 if (limit > workspace->limit)
57 GSL_ERROR ("iteration limit exceeds available workspace", GSL_EINVAL) ;
72 initialise (workspace, lower, higher);
74 if (epsabs <= 0 && (epsrel < 50 * GSL_DBL_EPSILON || epsrel < 0.5e-28))
76 GSL_ERROR ("tolerance cannot be acheived with given epsabs and epsrel",
82 GSL_ERROR ("cannot integrate with singularity on endpoint", GSL_EINVAL);
85 /* perform the first integration */
87 qc25c (f, lower, higher, c, &result0, &abserr0, &err_reliable);
89 set_initial_result (workspace, result0, abserr0);
91 /* Test on accuracy, use 0.01 relative error as an extra safety
92 margin on the first iteration (ignored for subsequent iterations) */
94 tolerance = GSL_MAX_DBL (epsabs, epsrel * fabs (result0));
96 if (abserr0 < tolerance && abserr0 < 0.01 * fabs(result0))
98 *result = sign * result0;
105 *result = sign * result0;
108 GSL_ERROR ("a maximum of one iteration was insufficient", GSL_EMAXITER);
118 double a1, b1, a2, b2;
119 double a_i, b_i, r_i, e_i;
120 double area1 = 0, area2 = 0, area12 = 0;
121 double error1 = 0, error2 = 0, error12 = 0;
122 int err_reliable1, err_reliable2;
124 /* Bisect the subinterval with the largest error estimate */
126 retrieve (workspace, &a_i, &b_i, &r_i, &e_i);
129 b1 = 0.5 * (a_i + b_i);
133 if (c > a1 && c <= b1)
135 b1 = 0.5 * (c + b2) ;
138 else if (c > b1 && c < b2)
140 b1 = 0.5 * (a1 + c) ;
144 qc25c (f, a1, b1, c, &area1, &error1, &err_reliable1);
145 qc25c (f, a2, b2, c, &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 = sign * 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);