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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/* integration/fixed.c
*
* Copyright (C) 2017 Patrick Alken
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* the code in this module performs fixed-point quadrature calculations for
* integrands and is based on IQPACK */
#include <config.h>
#include <stdlib.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_integration.h>
static int fixed_compute(const double a, const double b, const double alpha, const double beta,
gsl_integration_fixed_workspace * w);
static int imtqlx ( const int n, double d[], double e[], double z[] );
gsl_integration_fixed_workspace *
gsl_integration_fixed_alloc(const gsl_integration_fixed_type * type, const size_t n,
const double a, const double b, const double alpha, const double beta)
{
int status;
gsl_integration_fixed_workspace *w;
/* check inputs */
if (n < 1)
{
GSL_ERROR_VAL ("workspace size n must be at least 1", GSL_EDOM, 0);
}
w = calloc(1, sizeof(gsl_integration_fixed_workspace));
if (w == NULL)
{
GSL_ERROR_VAL ("unable to allocate workspace", GSL_ENOMEM, 0);
}
w->weights = malloc(n * sizeof(double));
if (w->weights == NULL)
{
gsl_integration_fixed_free(w);
GSL_ERROR_VAL ("unable to allocate weights", GSL_ENOMEM, 0);
}
w->x = malloc(n * sizeof(double));
if (w->x == NULL)
{
gsl_integration_fixed_free(w);
GSL_ERROR_VAL ("unable to allocate x", GSL_ENOMEM, 0);
}
w->diag = malloc(n * sizeof(double));
if (w->diag == NULL)
{
gsl_integration_fixed_free(w);
GSL_ERROR_VAL ("unable to allocate diag", GSL_ENOMEM, 0);
}
w->subdiag = malloc(n * sizeof(double));
if (w->subdiag == NULL)
{
gsl_integration_fixed_free(w);
GSL_ERROR_VAL ("unable to allocate subdiag", GSL_ENOMEM, 0);
}
w->n = n;
w->type = type;
/* compute quadrature weights and nodes */
status = fixed_compute(a, b, alpha, beta, w);
if (status)
{
gsl_integration_fixed_free(w);
GSL_ERROR_VAL ("error in integration parameters", GSL_EDOM, 0);
}
return w;
}
void
gsl_integration_fixed_free(gsl_integration_fixed_workspace * w)
{
if (w->weights)
free(w->weights);
if (w->x)
free(w->x);
if (w->diag)
free(w->diag);
if (w->subdiag)
free(w->subdiag);
free(w);
}
size_t
gsl_integration_fixed_n(const gsl_integration_fixed_workspace * w)
{
return w->n;
}
double *
gsl_integration_fixed_nodes(const gsl_integration_fixed_workspace * w)
{
return w->x;
}
double *
gsl_integration_fixed_weights(const gsl_integration_fixed_workspace * w)
{
return w->weights;
}
int
gsl_integration_fixed(const gsl_function * func, double * result,
const gsl_integration_fixed_workspace * w)
{
const size_t n = w->n;
size_t i;
double sum = 0.0;
for (i = 0; i < n; ++i)
{
double fi = GSL_FN_EVAL(func, w->x[i]);
sum += w->weights[i] * fi;
}
*result = sum;
return GSL_SUCCESS;
}
/*
fixed_compute()
Compute quadrature weights and nodes
*/
static int
fixed_compute(const double a, const double b, const double alpha, const double beta,
gsl_integration_fixed_workspace * w)
{
int s;
const size_t n = w->n;
gsl_integration_fixed_params params;
size_t i;
params.a = a;
params.b = b;
params.alpha = alpha;
params.beta = beta;
/* check input parameters */
s = (w->type->check)(n, ¶ms);
if (s)
return s;
/* initialize Jacobi matrix */
s = (w->type->init)(n, w->diag, w->subdiag, ¶ms);
if (s)
return s;
if (params.zemu <= 0.0)
{
GSL_ERROR("zeroth moment must be positive", GSL_EINVAL);
}
for ( i = 0; i < n; i++ )
{
w->x[i] = w->diag[i];
}
w->weights[0] = sqrt (params.zemu);
for ( i = 1; i < n; i++ )
{
w->weights[i] = 0.0;
}
/* diagonalize the Jacobi matrix */
s = imtqlx (n, w->x, w->subdiag, w->weights);
if (s)
return s;
for (i = 0; i < n; i++)
{
w->weights[i] = w->weights[i] * w->weights[i];
}
/*
* The current weights and nodes are valid for a = 0, b = 1.
* Now scale them for arbitrary a,b
*/
{
double p = pow ( params.slp, params.al + params.be + 1.0 );
size_t k;
for ( k = 0; k < n; k++ )
{
w->x[k] = params.shft + params.slp * w->x[k];
w->weights[k] = w->weights[k] * p;
}
}
return GSL_SUCCESS;
}
/******************************************************************************/
/*
Purpose:
IMTQLX diagonalizes a symmetric tridiagonal matrix.
Discussion:
This routine is a slightly modified version of the EISPACK routine to
perform the implicit QL algorithm on a symmetric tridiagonal matrix.
The authors thank the authors of EISPACK for permission to use this
routine.
It has been modified to produce the product Q' * Z, where Z is an input
vector and Q is the orthogonal matrix diagonalizing the input matrix.
The changes consist (essentially) of applying the orthogonal transformations
directly to Z as they are generated.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
11 January 2010
Author:
Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
C version by John Burkardt.
Reference:
Sylvan Elhay, Jaroslav Kautsky,
Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
Interpolatory Quadrature,
ACM Transactions on Mathematical Software,
Volume 13, Number 4, December 1987, pages 399-415.
Roger Martin, James Wilkinson,
The Implicit QL Algorithm,
Numerische Mathematik,
Volume 12, Number 5, December 1968, pages 377-383.
Parameters:
Input, int N, the order of the matrix.
Input/output, double D(N), the diagonal entries of the matrix.
On output, the information in D has been overwritten.
Input/output, double E(N), the subdiagonal entries of the
matrix, in entries E(1) through E(N-1). On output, the information in
E has been overwritten.
Input/output, double Z(N). On input, a vector. On output,
the value of Q' * Z, where Q is the matrix that diagonalizes the
input symmetric tridiagonal matrix.
*/
static int
imtqlx ( const int n, double d[], double e[], double z[] )
{
double b;
double c;
double f;
double g;
int i;
int ii;
int itn = 30;
int j;
int k;
int l;
int m;
int mml;
double p;
double r;
double s;
if ( n == 1 )
{
return GSL_SUCCESS;
}
e[n-1] = 0.0;
for ( l = 1; l <= n; l++ )
{
j = 0;
for ( ; ; )
{
for ( m = l; m <= n; m++ )
{
if ( m == n )
{
break;
}
if ( fabs ( e[m-1] ) <= GSL_DBL_EPSILON * ( fabs ( d[m-1] ) + fabs ( d[m] ) ) )
{
break;
}
}
p = d[l-1];
if ( m == l )
{
break;
}
if ( itn <= j )
{
return GSL_EMAXITER;
}
j = j + 1;
g = ( d[l] - p ) / ( 2.0 * e[l-1] );
r = sqrt ( g * g + 1.0 );
g = d[m-1] - p + e[l-1] / ( g + fabs ( r ) * GSL_SIGN ( g ) );
s = 1.0;
c = 1.0;
p = 0.0;
mml = m - l;
for ( ii = 1; ii <= mml; ii++ )
{
i = m - ii;
f = s * e[i-1];
b = c * e[i-1];
if ( fabs ( g ) <= fabs ( f ) )
{
c = g / f;
r = sqrt ( c * c + 1.0 );
e[i] = f * r;
s = 1.0 / r;
c = c * s;
}
else
{
s = f / g;
r = sqrt ( s * s + 1.0 );
e[i] = g * r;
c = 1.0 / r;
s = s * c;
}
g = d[i] - p;
r = ( d[i-1] - g ) * s + 2.0 * c * b;
p = s * r;
d[i] = g + p;
g = c * r - b;
f = z[i];
z[i] = s * z[i-1] + c * f;
z[i-1] = c * z[i-1] - s * f;
}
d[l-1] = d[l-1] - p;
e[l-1] = g;
e[m-1] = 0.0;
}
}
/*
Sorting.
*/
for ( ii = 2; ii <= m; ii++ )
{
i = ii - 1;
k = i;
p = d[i-1];
for ( j = ii; j <= n; j++ )
{
if ( d[j-1] < p )
{
k = j;
p = d[j-1];
}
}
if ( k != i )
{
d[k-1] = d[i-1];
d[i-1] = p;
p = z[i-1];
z[i-1] = z[k-1];
z[k-1] = p;
}
}
return GSL_SUCCESS;
}
|