1 /* integration/qmomof.c
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.
22 #include <gsl/gsl_integration.h>
23 #include <gsl/gsl_errno.h>
26 compute_moments (double par, double * cheb);
29 dgtsl (size_t n, double *c, double *d, double *e, double *b);
31 gsl_integration_qawo_table *
32 gsl_integration_qawo_table_alloc (double omega, double L,
33 enum gsl_integration_qawo_enum sine,
36 gsl_integration_qawo_table *t;
41 GSL_ERROR_VAL ("table length n must be positive integer",
45 t = (gsl_integration_qawo_table *)
46 malloc (sizeof (gsl_integration_qawo_table));
50 GSL_ERROR_VAL ("failed to allocate space for qawo_table struct",
54 chebmo = (double *) malloc (25 * n * sizeof (double));
59 GSL_ERROR_VAL ("failed to allocate space for chebmo block",
67 t->par = 0.5 * omega * L;
70 /* precompute the moments */
76 for (i = 0 ; i < t->n; i++)
78 compute_moments (t->par * scale, t->chebmo + 25*i);
87 gsl_integration_qawo_table_set (gsl_integration_qawo_table * t,
88 double omega, double L,
89 enum gsl_integration_qawo_enum sine)
94 t->par = 0.5 * omega * L;
96 /* recompute the moments */
102 for (i = 0 ; i < t->n; i++)
104 compute_moments (t->par * scale, t->chebmo + 25*i);
114 gsl_integration_qawo_table_set_length (gsl_integration_qawo_table * t,
117 /* return immediately if the length is the same as the old length */
122 /* otherwise reset the table and compute the new parameters */
125 t->par = 0.5 * t->omega * L;
127 /* recompute the moments */
133 for (i = 0 ; i < t->n; i++)
135 compute_moments (t->par * scale, t->chebmo + 25*i);
145 gsl_integration_qawo_table_free (gsl_integration_qawo_table * t)
152 compute_moments (double par, double *chebmo)
154 double v[28], d[25], d1[25], d2[25];
156 const size_t noeq = 25;
158 const double par2 = par * par;
159 const double par4 = par2 * par2;
160 const double par22 = par2 + 2.0;
162 const double sinpar = sin (par);
163 const double cospar = cos (par);
167 /* compute the chebyschev moments with respect to cosine */
169 double ac = 8 * cospar;
170 double as = 24 * par * sinpar;
172 v[0] = 2 * sinpar / par;
173 v[1] = (8 * cospar + (2 * par2 - 8) * sinpar / par) / par2;
174 v[2] = (32 * (par2 - 12) * cospar
175 + (2 * ((par2 - 80) * par2 + 192) * sinpar) / par) / par4;
177 if (fabs (par) <= 24)
179 /* compute the moments as the solution of a boundary value
180 problem using the asyptotic expansion as an endpoint */
182 double an2, ass, asap;
186 for (k = 0; k < noeq - 1; k++)
189 d[k] = -2 * (an2 - 4) * (par22 - 2 * an2);
190 d2[k] = (an - 1) * (an - 2) * par2;
191 d1[k + 1] = (an + 3) * (an + 4) * par2;
192 v[k + 3] = as - (an2 - 4) * ac;
198 d[noeq - 1] = -2 * (an2 - 4) * (par22 - 2 * an2);
199 v[noeq + 2] = as - (an2 - 4) * ac;
200 v[3] = v[3] - 56 * par2 * v[2];
203 asap = (((((210 * par2 - 1) * cospar - (105 * par2 - 63) * ass) / an2
204 - (1 - 15 * par2) * cospar + 15 * ass) / an2
205 - cospar + 3 * ass) / an2
207 v[noeq + 2] = v[noeq + 2] - 2 * asap * par2 * (an - 1) * (an - 2);
209 dgtsl (noeq, d1, d, d2, v + 3);
214 /* compute the moments by forward recursion */
218 for (k = 3; k < 13; k++)
220 double an2 = an * an;
221 v[k] = ((an2 - 4) * (2 * (par22 - 2 * an2) * v[k - 1] - ac)
222 + as - par2 * (an + 1) * (an + 2) * v[k - 2])
223 / (par2 * (an - 1) * (an - 2));
229 for (i = 0; i < 13; i++)
231 chebmo[2 * i] = v[i];
234 /* compute the chebyschev moments with respect to sine */
236 v[0] = 2 * (sinpar - par * cospar) / par2;
237 v[1] = (18 - 48 / par2) * sinpar / par2 + (-2 + 48 / par2) * cospar / par;
239 ac = -24 * par * cospar;
242 if (fabs (par) <= 24)
244 /* compute the moments as the solution of a boundary value
245 problem using the asyptotic expansion as an endpoint */
248 double an2, ass, asap;
251 for (k = 0; k < noeq - 1; k++)
254 d[k] = -2 * (an2 - 4) * (par22 - 2 * an2);
255 d2[k] = (an - 1) * (an - 2) * par2;
256 d1[k + 1] = (an + 3) * (an + 4) * par2;
257 v[k + 2] = ac + (an2 - 4) * as;
263 d[noeq - 1] = -2 * (an2 - 4) * (par22 - 2 * an2);
264 v[noeq + 1] = ac + (an2 - 4) * as;
265 v[2] = v[2] - 42 * par2 * v[1];
268 asap = (((((105 * par2 - 63) * ass - (210 * par2 - 1) * sinpar) / an2
269 + (15 * par2 - 1) * sinpar
270 - 15 * ass) / an2 - sinpar - 3 * ass) / an2 - sinpar) / an2;
271 v[noeq + 1] = v[noeq + 1] - 2 * asap * par2 * (an - 1) * (an - 2);
273 dgtsl (noeq, d1, d, d2, v + 2);
278 /* compute the moments by forward recursion */
281 for (k = 2; k < 12; k++)
283 double an2 = an * an;
284 v[k] = ((an2 - 4) * (2 * (par22 - 2 * an2) * v[k - 1] + as)
285 + ac - par2 * (an + 1) * (an + 2) * v[k - 2])
286 / (par2 * (an - 1) * (an - 2));
291 for (i = 0; i < 12; i++)
293 chebmo[2 * i + 1] = v[i];
299 dgtsl (size_t n, double *c, double *d, double *e, double *b)
301 /* solves a tridiagonal matrix A x = b
303 c[1 .. n - 1] subdiagonal of the matrix A
304 d[0 .. n - 1] diagonal of the matrix A
305 e[0 .. n - 2] superdiagonal of the matrix A
307 b[0 .. n - 1] right hand side, replaced by the solution vector x */
328 for (k = 0; k < n - 1; k++)
332 if (fabs (c[k1]) >= fabs (c[k]))
362 double t = -c[k1] / c[k];
364 c[k1] = d[k1] + t * d[k];
365 d[k1] = e[k1] + t * e[k];
367 b[k1] = b[k1] + t * b[k];
378 b[n - 1] = b[n - 1] / c[n - 1];
380 b[n - 2] = (b[n - 2] - d[n - 2] * b[n - 1]) / c[n - 2];
382 for (k = n ; k > 2; k--)
385 b[kb] = (b[kb] - d[kb] * b[kb + 1] - e[kb] * b[kb + 2]) / c[kb];