This is gsl-ref.info, produced by Makeinfo version 3.12h from
gsl-ref.texi.

INFO-DIR-SECTION Scientific software
START-INFO-DIR-ENTRY
* gsl-ref: (gsl-ref).                   GNU Scientific Library - Reference
END-INFO-DIR-ENTRY

   This file documents the GNU Scientific Library.

   Copyright (C) 1996, 1997, 1998, 1999 The GSL Project.

   Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

   Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided that
the entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

   Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that this permission notice may be stated in a
translation approved by the Foundation.


File: gsl-ref.info,  Node: Mixed-radix FFT routines for real data,  Next: FFT References and Further Reading,  Prev: Radix-2 FFT routines for real data,  Up: Fast Fourier Transforms

Mixed-radix FFT routines for real data
======================================

   This section describes mixed-radix FFT algorithms for real data.  The
mixed-radix functions work for FFTs of any length.  They are a
reimplementation of the real-FFT routines in the Fortran FFTPACK library
by Paul Swarztrauber.  The theory behind the algorithm is explained in
the article `Fast Mixed-Radix Real Fourier Transforms' by Clive
Temperton.  The routines here use the same indexing scheme and basic
algorithms as FFTPACK.

   The functions use the FFTPACK storage convention for half-complex
sequences.  In this convention the half-complex transform of a real
sequence is stored in with frequencies in increasing order, starting at
zero, with the real and imaginary parts of each frequency in neighboring
locations.  When a value is known to be real the imaginary part is not
stored.  The imaginary part of the zero-frequency component is never
stored.  It is known to be zero (since the zero frequency component is
simply the sum of the input data (all real)).  For a sequence of even
length the imaginary part of the frequency n/2 is not stored either,
since the symmetry z_k = z_{N-k}^* implies that this is purely real too.

   The storage scheme is best shown by some examples.  The table below
shows the output for an odd-length sequence, n=5.  The two columns give
the correspondence between the 5 values in the half-complex sequence
returned by `gsl_fft_real_transform', HALFCOMPLEX[] and the values
COMPLEX[] that would be returned if the same real input sequence were
passed to `gsl_fft_complex_backward' as a complex sequence (with
imaginary parts set to `0'),

     complex[0].real  =  halfcomplex[0]
     complex[0].imag  =  0
     complex[1].real  =  halfcomplex[1]
     complex[1].imag  =  halfcomplex[2]
     complex[2].real  =  halfcomplex[3]
     complex[2].imag  =  halfcomplex[4]
     complex[3].real  =  halfcomplex[3]
     complex[3].imag  = -halfcomplex[4]
     complex[4].real  =  halfcomplex[1]
     complex[4].imag  = -halfcomplex[2]

The upper elements of the COMPLEX array, `complex[3]' and `complex[4]'
are filled in using the symmetry condition.  The imaginary part of the
zero-frequency term `complex[0].imag' is known to be zero by the
symmetry.

   The next table shows the output for an even-length sequence, n=5 In
the even case both the there are two values which are purely real,

     complex[0].real  =  halfcomplex[0]
     complex[0].imag  =  0
     complex[1].real  =  halfcomplex[1]
     complex[1].imag  =  halfcomplex[2]
     complex[2].real  =  halfcomplex[3]
     complex[2].imag  =  halfcomplex[4]
     complex[3].real  =  halfcomplex[5]
     complex[3].imag  =  0
     complex[4].real  =  halfcomplex[3]
     complex[4].imag  = -halfcomplex[4]
     complex[5].real  =  halfcomplex[1]
     complex[5].imag  = -halfcomplex[2]

The upper elements of the COMPLEX array, `complex[4]' and `complex[5]'
are be filled in using the symmetry condition.  Both `complex[0].imag'
and `complex[3].imag' are known to be zero.

   All these functions are declared in the header files
`gsl_fft_real.h' and `gsl_fft_halfcomplex.h'.

 - Function: gsl_fft_real_wavetable * gsl_fft_real_alloc (size_t N)
 - Function: gsl_fft_halfcomplex_wavetable * gsl_fft_halfcomplex_alloc
          (size_t N)
     These functions allocate space for a wavetable struct, a scratch
     area of size N real elements and a trigonometric lookup table, of
     size n/2 complex elements.  The relevant components of the
     wavetable are modified to point to the newly allocated memory.

     The functions initialize WAVETABLE.  They first select a
     factorization of the length N into the implemented subtransforms,
     storing the details of the factorization in WAVETABLE.

     Using this factorization they then prepare a trigonometric lookup
     table in the memory previously allocated by `gsl_fft_real_alloc' or
     `gsl_fft_halfcomplex_alloc'.  The wavetable is computed using
     direct calls to `sin' and `cos', for accuracy.  It could be
     computed faster using recursion relations if necessary.  If an
     application performs many FFTs of the same length then computing
     the wavetable is a one-off overhead which does not affect the final
     throughput.

     The wavetable structure can be used repeatedly for any transform
     of the same length.  The table is not modified by calls to any of
     the other FFT functions.  The same wavetable can be used for both
     forward and backward (or inverse) transforms of a given length.

     These functions return a pointer to the newly allocated struct if
     no errors were detected, and `0' in the case of error.  The
     following `gsl_errno' conditions are defined for these functions:

    `GSL_ENOMEM'
          The requested memory could not be allocated (`malloc' returned
          a null pointer).

    `GSL_EDOM'
          The length of the data N is not a positive integer (i.e. N is
          zero).

    `GSL_EFACTOR'
          The length N could not be factorized (this shouldn't happen).

    `GSL_EFAILED'
          A failure was detected in the wavetable generation.  This
          could be caused by an inconsistency in a user-supplied
          WAVETABLE structure.


 - Function: void gsl_fft_real_free (gsl_fft_real_wavetable * WAVETABLE)
 - Function: void gsl_fft_halfcomplex_free
          (gsl_fft_halfcomplex_wavetable * WAVETABLE)
     These functions free the blocks of memory previously allocated by
     `gsl_fft_real_alloc' or `gsl_fft_halfcomplex_alloc' and pointed to
     by the components of WAVETABLE.

     The wavetable should be freed if no further FFTs of the same
     length will be needed.

 - Function: int gsl_fft_real_transform (double DATA[], size_t STRIDE,
          size_t N, const gsl_fft_real_wavetable * WAVETABLE)
 - Function: int gsl_fft_halfcomplex_transform (double DATA[], size_t
          STRIDE, size_t N, const gsl_fft_halfcomplex_wavetable *
          WAVETABLE)
     These functions compute the FFT of DATA, a real or half-complex
     array of length N, using a mixed radix decimation-in-frequency
     algorithm.  For `gsl_fft_real_transform' DATA is an array of
     time-ordered real data.  For `gsl_fft_halfcomplex_transform' DATA
     contains fourier coefficients in the half-complex ordering
     described above.

     The caller must supply a WAVETABLE containing the chosen
     factorization, trigonometric lookup tables and scratch area.  The
     wavetable can be easily prepared using the function
     `gsl_fft_real_alloc' or `gsl_fft_halfcomplex_alloc'.

     There is no restriction on the length N.  Efficient modules are
     provided for subtransforms of length 2, 3, 4 and 5.  Any remaining
     factors are computed with a slow, O(n^2), general-n module.

     The functions return a value of `0' if no errors were detected,
     and `-1' in the case of error.  The following `gsl_errno'
     conditions are defined for these functions:

    `GSL_EDOM'
          The length of the data N is not a positive integer (i.e. N is
          zero).

    `GSL_EINVAL'
          The length of the data N and the length used to compute the
          given WAVETABLE do not match.

 - Function: int gsl_fft_real_unpack (const double REAL_COEFFICIENT[],
          gsl_complex_packed_array COMPLEX_COEFFICIENT[], size_t
          STRIDE, size_t N)
     This function converts a single real array, REAL_COEFFICIENT into
     an equivalent complex array, COMPLEX_COEFFICIENT, (with imaginary
     part set to zero), suitable for `gsl_fft_complex' routines.  The
     algorithm for the conversion is simply,

          for (i = 0; i < n; i++)
              {
                complex_coefficient[i].real = real_coefficient[i];
                complex_coefficient[i].imag = 0.0;
              }

     The function returns a value of `0' if no errors were detected, and
     `-1' in the case of error.  There is only one `gsl_errno'
     condition defined for this function:

    `GSL_EDOM'
          The length of the data N is not a positive integer (i.e. N is
          zero).

 - Function: int gsl_fft_halfcomplex_unpack (const double
          HALFCOMPLEX_COEFFICIENT[], gsl_complex_packed_array
          COMPLEX_COEFFICIENT[], size_t STRIDE, size_t N)
     This function converts HALFCOMPLEX_COEFFICIENT, an array of
     half-complex coefficients as returned by `gsl_fft_real_transform',
     into an ordinary complex array, COMPLEX_COEFFICIENT.  It fills in
     the complex array using the symmetry z_k = z_{N-k}^* to
     reconstruct the redundant elements.  The algorithm for the
     conversion is,

          complex_coefficient[0].real = halfcomplex_coefficient[0];
          complex_coefficient[0].imag = 0.0;
          
          for (i = 1; i < n - i; i++)
            {
              const double hc_real = halfcomplex_coefficient[2 * i - 1];
              const double hc_imag = halfcomplex_coefficient[2 * i];
              complex_coefficient[i].real = hc_real;
              complex_coefficient[i].imag = hc_imag;
              complex_coefficient[n - i].real = hc_real;
              complex_coefficient[n - i].imag = -hc_imag;
            }
           if (i == n - i)
            {
              complex_coefficient[i].real = halfcomplex_coefficient[n - 1];
              complex_coefficient[i].imag = 0.0;
            }

     The function returns a value of `0' if no errors were detected, and
     `-1' in the case of error.  There is only one `gsl_errno'
     condition defined for this function:

    `GSL_EDOM'
          The length of the data N is not a positive integer (i.e. N is
          zero).

   Here is an example program using `gsl_fft_real_transform' and
`gsl_fft_halfcomplex_inverse'.  It generates a real signal in the shape
of a square pulse.  The pulse is fourier transformed to frequency
space, and all but the lowest ten frequency components are removed from
the array of fourier coefficients returned by `gsl_fft_real_transform'.

   The remaining fourier coefficients are transformed back to the
time-domain, to give a filtered version of the square pulse.  Since
fourier coefficients are stored using the half-complex symmetry both
positive and negative frequencies are removed and the final filtered
signal is also real.

     #include <stdio.h>
     #include <math.h>
     #include <gsl/gsl_errno.h>
     #include <gsl/gsl_fft_real.h>
     #include <gsl/gsl_fft_halfcomplex.h>
     
     int main ()
     {
       int i, n = 100;
       double data[n];
     
       gsl_fft_real_wavetable * real_wavetable;
       gsl_fft_halfcomplex_wavetable * halfcomplex_wavetable;
     
       for (i = 0; i < n; i++)
         {
           data[i] = 0.0;
         }
     
       for (i = n / 3; i < 2 * n / 3; i++)
         {
           data[i] = 1.0;
         }
     
       for (i = 0; i < n; i++)
         {
           printf ("%d: %e\n", i, data[i]);
         }
       printf ("\n");
     
       real_wavetable = gsl_fft_real_alloc (n);
     
       gsl_fft_real_transform (data, 1, n, real_wavetable);
       gsl_fft_real_free (real_wavetable);
     
       for (i = 11; i < n; i++)
         {
           data[i] = 0;
         }
     
       halfcomplex_wavetable = gsl_fft_halfcomplex_alloc (n);
     
       gsl_fft_halfcomplex_inverse (data, 1, n, halfcomplex_wavetable);
       gsl_fft_halfcomplex_free (halfcomplex_wavetable);
     
       for (i = 0; i < n; i++)
         {
           printf ("%d: %e\n", i, data[i]);
         }
     }


File: gsl-ref.info,  Node: FFT References and Further Reading,  Prev: Mixed-radix FFT routines for real data,  Up: Fast Fourier Transforms

References and Further Reading
==============================

   For serious FFT work we recommend the use of the dedicated FFTW
library by Frigo and Johnson.  The FFTW library is self-optimizing -- it
automatically tunes itself for each hardware platform in order to
achieve maximum performance.  It is available under the GNU GPL.

     FFTW Website, <http://www.fftw.org/>

A good starting point for learning more about the FFT is the review
article `Fast Fourier Transforms: A Tutorial Review and A State of the
Art' by Duhamel and Vetterli,

     P. Duhamel and M. Vetterli.  Fast fourier transforms: A tutorial
     review and a state of the art.  `Signal Processing', 19:259-299,
     1990.

To find out about the algorithms used in the GSL routines you may want
to consult the latex document `GSL FFT Algorithms' (it is included in
GSL, as `doc/fftalgorithms.tex').  This has general information on FFTs
and explicit derivations of the implementation for each routine.  There
are also references to the relevant literature.  For convenience some
of the more important references are reproduced below.

   There are several introductory books on the FFT with example
programs, such as `The Fast Fourier Transform' by Brigham and `DFT/FFT
and Convolution Algorithms' by Burrus and Parks,

     E. Oran Brigham.  `The Fast Fourier Transform'.  Prentice Hall,
     1974.

     C. S. Burrus and T. W. Parks.  `DFT/FFT and Convolution
     Algorithms'.  Wiley, 1984.

Both these introductory books cover the radix-2 FFT in some detail.
The mixed-radix algorithm at the heart of the FFTPACK routines is
reviewed in Clive Temperton's paper,

     Clive Temperton.  Self-sorting mixed-radix fast fourier transforms.
     `Journal of Computational Physics', 52(1):1-23, 1983.

The derivation of FFTs for real-valued data is explained in the
following two articles,

     Henrik V. Sorenson, Douglas L. Jones, Michael T. Heideman, and C.
     Sidney Burrus.  Real-valued fast fourier transform algorithms.
     `IEEE Transactions on Acoustics, Speech, and Signal Processing',
     ASSP-35(6):849-863, 1987.

     Clive Temperton.  Fast mixed-radix real fourier transforms.
     `Journal of Computational Physics', 52:340-350, 1983.

In 1979 the IEEE published a compendium of carefully-reviewed Fortran
FFT programs in `Programs for Digital Signal Processing'.  It is a
useful reference for implementations of many different FFT algorithms,

     Digital Signal Processing Committee and IEEE Acoustics, Speech,
     and Signal Processing Committee, editors.  `Programs for Digital
     Signal Processing'.  IEEE Press, 1979.

There is also an annotated bibliography of papers on the FFT and related
topics by Burrus,

     C. S. Burrus.  Notes on the FFT.

The notes are available from
<http://www-dsp.rice.edu/res/fft/fftnote.asc>.


File: gsl-ref.info,  Node: Discrete Hankel Transforms,  Next: Numerical Integration,  Prev: Fast Fourier Transforms,  Up: Top

Discrete Hankel Transforms
**************************

   This chapter describes functions for performing Discrete Hankel
Transforms (DHTs).

 - Function: gsl_dht_transform * gsl_dht_transform_new (size_t SIZE,
          double NU, double XMAX)

 - Function: void gsl_dht_transform_free (gsl_dht_transform * T)

 - Function: double gsl_dht_transform_x_sample (const gsl_dht_transform
          * T, int N)

 - Function: double gsl_dht_transform_k_sample (const gsl_dht_transform
          * T, int N)

 - Function: int gsl_dht_transform_apply (const gsl_dht_transform * T,
          double * F_IN, double * F_OUT)

 - Function: int gsl_dht_transform_recalc_impl (gsl_dht_transform * T,
          double NU, double XMAX)
 - Function: int gsl_dht_transform_recalc_e (gsl_dht_transform * T,
          double NU, double XMAX)


File: gsl-ref.info,  Node: Numerical Integration,  Next: Monte Carlo Integration,  Prev: Discrete Hankel Transforms,  Up: Top

Numerical Integration
*********************

   This chapter describes routines for performing numerical integration
(quadrature) of a function in one dimension.  There are routines for
adaptive and non-adaptive integration of general functions, with
specialised routines for specific cases.  These include integration over
infinite and semi-infinite ranges, singular integrals, including
logarithmic singularities, computation of Cauchy principal values and
oscillatory integrals.  The library reimplements the algorithms used in
QUADPACK, a numerical integration package written by Piessens,
Doncker-Kapenga, Uberhuber and Kahaner.  Fortran code for QUADPACK is
available on Netlib.

   Each algorithm computes an approximation to a definite integral of
the form,

     I = \int_a^b f(x) w(x) dx

where w(x) is a weight function (for general integrands w(x)=1).  The
user provides absolute and relative error bounds (epsabs, epsrel) which
specify the following accuracy requirement,

     |RESULT - I|  <= max(epsabs, epsrel |I|)

where RESULT is the numerical approximation obtained by the algorithm.
The algorithms attempt to estimate the absolute error ABSERR = |RESULT
- I| in such a way that the following inequality holds,

     |RESULT - I| <= ABSERR <= max(epsabs, epsrel |I|)

The routines will fail to converge if the error bounds are too
stringent, but always return the best approximation obtained up to that
stage.

   The algorithms in QUADPACK use a naming convention based on the
following letters,

     `Q' - quadrature routine
     
     `N' - non-adaptive integrator
     `A' - adaptive integrator
     
     `G' - general integrand (user-defined)
     `W' - weight function with integrand
     
     `S' - singularities can be more readily integrated
     `P' - points of special difficulty can be supplied
     `I' - infinite range of integration
     `O' - oscillatory weight function, cos or sin
     `F' - Fourier integral
     `C' - Cauchy principal value

The algorithms are built on pairs of quadrature rules, a higher order
rule and a lower order rule.  The higher order rule is used to compute
the best approximation to an integral over a small range.  The
difference between the higher order and lower order results gives an
estimate of the error in the approximation.

   The algorithms for general functions (without a weight function) are
based on Gauss-Kronrod rules. A Gauss-Kronrod rule begins with a
classical Gaussian quadrature rule of order m.  This is extended with
additional points between each of the abscissae to give a higher order
Kronrod rule of order 2m+1.  The Kronrod rule is efficient because it
reuses existing function evaluations from the Gaussian rule.  The
higher order Kronrod rule is used as the best approximation to the
integral, and the difference between the two rules is used as an
estimate of the error in the approximation.

   For integrands with weight functions the algorithms use
Clenshaw-Curtis quadrature rules.  A Clenshaw-Curtis rule begins with
an n-th order Chebyschev polynomial approximation to the integrand.
This polynomial can be integrated exactly to give an approximation to
the integral of the original function.  The Chebyschev expansion can be
extended to higher orders to improve the approximation.  The presence of
singularities (or other behavior) in the integrand can cause slow
convergence in the Chebyschev approximation.  The modified
Clenshaw-Curtis rules used in QUADPACK separate out several common
weight functions which cause slow convergence.  These weight functions
are integrated analytically against the Chebyschev polynomials to
precompute "modified Chebyschev moments".  Combining the moments with
the Chebyschev approximation to the function gives the desired
integral.  The use of analytic integration for the singular part of the
function allows exact cancellations and substantially improves the
overall convergence behavior of the integration.

* Menu:

* QNG non-adaptive Gauss-Kronrod integration::
* QAG adaptive integration::
* QAGS adaptive integration with singularities::
* QAGP adaptive integration with known singular points::
* QAGI adaptive integration on infinite intervals::
* QAWC adaptive integration for Cauchy principal values::
* QAWS adaptive integration for singular functions::
* QAWO adaptive integration for oscillatory functions::
* QAWF adaptive integration for Fourier integrals::
* Numerical integration error codes::
* Numerical integration examples::
* Numerical integration References and Further Reading::


File: gsl-ref.info,  Node: QNG non-adaptive Gauss-Kronrod integration,  Next: QAG adaptive integration,  Up: Numerical Integration

QNG non-adaptive Gauss-Kronrod integration
==========================================

   The QNG algorithm is non-adaptive procedure which uses fixed
Gauss-Kronrod abscissae to sample the integrand at a maximum of 87
points.  It is provided for fast integration of smooth functions.

 - Function: int gsl_integration_qng (const gsl_function *F, double A,
          double B, double EPSABS, double EPSREL, double * RESULT,
          double * ABSERR, size_t * NEVAL)
     This function applies the Gauss-Kronrod 10-point, 21-point,
     43-point and 87-point integration rules in succession until an
     estimate of the integral of f over (a,b) is achieved within the
     desired absolute and relative error limits, EPSABS and EPSREL.  The
     function returns the final approximation, RESULT, an estimate of
     the absolute error, ABSERR and the number of function evaluations
     used, NEVAL.  The Gauss-Kronrod rules are designed in such a way
     that each rule uses all the results of its predecessors, in order
     to minimise the total number of function evaluations.


File: gsl-ref.info,  Node: QAG adaptive integration,  Next: QAGS adaptive integration with singularities,  Prev: QNG non-adaptive Gauss-Kronrod integration,  Up: Numerical Integration

QAG adaptive integration
========================

   The QAG algorithm is a simple adaptive integration procedure.  The
integration region is divided into subintervals, and on each iteration
the subinterval with the largest estimated error is bisected.  This
reduces the overall error rapidly, as the subintervals become
concentrated around local difficulties in the integrand.  These
subintervals are managed by a `gsl_integration_workspace' struct, which
handles the memory for the subinterval ranges, results and error
estimates.

 - Function: gsl_integration_workspace *
          gsl_integration_workspace_alloc (const size_t N)
     This function allocates a workspace sufficient to hold N double
     precision intervals, their integration results and error estimates.

 - Function: void gsl_integration_workspace_free
          (gsl_integration_workspace * W)
     This function frees the memory associated with the workspace W.

 - Function: int gsl_integration_qag (const gsl_function *F, double A,
          double B, double EPSABS, double EPSREL, size_t LIMIT, int
          KEY, gsl_integration_workspace * WORKSPACE, double * RESULT,
          double * ABSERR)
     This function applies an integration rule adaptively until an
     estimate of the integral of f over (a,b) is achieved within the
     desired absolute and relative error limits, EPSABS and EPSREL.
     The function returns the final approximation, RESULT, and an
     estimate of the absolute error, ABSERR.  The integration rule is
     determined by the value of KEY, which should be chosen from the
     following symbolic names,

          GSL_INTEG_GAUSS15  (key = 1)
          GSL_INTEG_GAUSS21  (key = 2)
          GSL_INTEG_GAUSS31  (key = 3)
          GSL_INTEG_GAUSS41  (key = 4)
          GSL_INTEG_GAUSS51  (key = 5)
          GSL_INTEG_GAUSS61  (key = 6)

     corresponding to the 15, 21, 31, 41, 51 and 61 point Gauss-Kronrod
     rules.  The higher-order rules give better accuracy for smooth
     functions, while lower-order rules save time when the function
     contains local difficulties, such as discontinuities.

     On each iteration the adaptive integration strategy bisects the
     interval with the largest error estimate.  The subintervals and
     their results are stored in the memory provided by WORKSPACE.  The
     maximum number of subintervals is given by LIMIT, which may not
     exceed the allocated size of the workspace.


File: gsl-ref.info,  Node: QAGS adaptive integration with singularities,  Next: QAGP adaptive integration with known singular points,  Prev: QAG adaptive integration,  Up: Numerical Integration

QAGS adaptive integration with singularities
============================================

   The presence of an integrable singularity in the integration region
causes an adaptive routine to concentrate new subintervals around the
singularity.  As the subintervals decrease in size the successive
approximations to the integral converge in a limiting fashion.  This
approach to the limit can be accelerated using an extrapolation
procedure.  The QAGS algorithm combines adaptive bisection with the Wynn
epsilon-algorithm to speed up the integration of many types of
integrable singularities.

 - Function: int gsl_integration_qags (const gsl_function * F, double
          A, double B, double EPSABS, double EPSREL, size_t LIMIT,
          gsl_integration_workspace * WORKSPACE, double *RESULT, double
          *ABSERR)
     This function applies the Gauss-Kronrod 21-point integration rule
     adaptively until an estimate of the integral of f over (a,b) is
     achieved within the desired absolute and relative error limits,
     EPSABS and EPSREL.  The results are extrapolated using the
     epsilon-algorithm, which accelerates the convergence of the
     integral in the presence of discontinuities and integrable
     singularities.  The function returns the final approximation from
     the extrapolation, RESULT, and an estimate of the absolute error,
     ABSERR.  The subintervals and their results are stored in the
     memory provided by WORKSPACE.  The maximum number of subintervals
     is given by LIMIT, which may not exceed the allocated size of the
     workspace.



File: gsl-ref.info,  Node: QAGP adaptive integration with known singular points,  Next: QAGI adaptive integration on infinite intervals,  Prev: QAGS adaptive integration with singularities,  Up: Numerical Integration

QAGP adaptive integration with known singular points
====================================================

 - Function: int gsl_integration_qagp (const gsl_function * F, double
          *PTS, size_t NPTS, double EPSABS, double EPSREL, size_t
          LIMIT, gsl_integration_workspace * WORKSPACE, double *RESULT,
          double *ABSERR)
     This function applies the adaptive integration algorithm QAGS
     taking account of the user-supplied locations of singular points.
     The array PTS of length NPTS should contain the endpoints of the
     integration ranges defined by the integration region and locations
     of the singularities.  For example, to integrate over the region
     (a,b) with break-points at x_1, x_2, x_3 (where a < x_1 < x_2 <
     x_3 < b) the following PTS array should be used

          pts[0] = a
          pts[1] = x_1
          pts[2] = x_2
          pts[3] = x_3
          pts[4] = b

     with NPTS = 5.

     If you know the locations of the singular points in the integration
     region then this routine will be faster than `QAGS'.



File: gsl-ref.info,  Node: QAGI adaptive integration on infinite intervals,  Next: QAWC adaptive integration for Cauchy principal values,  Prev: QAGP adaptive integration with known singular points,  Up: Numerical Integration

QAGI adaptive integration on infinite intervals
===============================================

 - Function: int gsl_integration_qagi (gsl_function * F, double EPSABS,
          double EPSREL, size_t LIMIT, gsl_integration_workspace *
          WORKSPACE, double *RESULT, double *ABSERR)
     This function computes the integral of the function F over the
     infinite interval (-\infty,+\infty).  The integral is mapped onto
     the interval (0,1] using the transformation x = (1-t)/t,

          \int_{-\infty}^{+\infty} dx f(x) =
               \int_0^1 dt (f((1-t)/t) + f((-1+t)/t))/t^2

     and then integrated using the QAGS algorithm.  The normal 21-point
     Gauss-Kronrod rule of QAGS is replaced by a 15-point rule, because
     the transformation can generate an integrable singularity at the
     origin.  In this case a lower-order rule is more efficient.

 - Function: int gsl_integration_qagiu (gsl_function * F, double A,
          double EPSABS, double EPSREL, size_t LIMIT,
          gsl_integration_workspace * WORKSPACE, double *RESULT, double
          *ABSERR)
     This function computes the integral of the function F over the
     semi-infinite interval (a,+\infty).  The integral is mapped onto
     the interval (0,1] using the transformation x = a + (1-t)/t,

          \int_{a}^{+\infty} dx f(x) =
               \int_0^1 dt f(a + (1-t)/t)/t^2

     and then integrated using the QAGS algorithm.

 - Function: int gsl_integration_qagil (gsl_function * F, double B,
          double EPSABS, double EPSREL, size_t LIMIT,
          gsl_integration_workspace * WORKSPACE, double *RESULT, double
          *ABSERR)
     This function computes the integral of the function F over the
     semi-infinite interval (-\infty,b).  The integral is mapped onto
     the region (0,1] using the transformation x = b - (1-t)/t,

          \int_{+\infty}^{b} dx f(x) =
               \int_0^1 dt f(b - (1-t)/t)/t^2

     and then integrated using the QAGS algorithm.


File: gsl-ref.info,  Node: QAWC adaptive integration for Cauchy principal values,  Next: QAWS adaptive integration for singular functions,  Prev: QAGI adaptive integration on infinite intervals,  Up: Numerical Integration

QAWC adaptive integration for Cauchy principal values
=====================================================

 - Function: int gsl_integration_qawc (gsl_function *F, double A,
          double B, double C, double EPSABS, double EPSREL, size_t
          LIMIT, gsl_integration_workspace * WORKSPACE, double *
          RESULT, double * ABSERR)
     This function computes the Cauchy principal value of the integral
     of f over (a,b), with a singularity at C,

          I = \int_a^b dx f(x) / (x - c)

     The adaptive bisection algorithm of QAG is used, with
     modifications to ensure that subdivisions do not occur at the
     singular point x = c.  When a subinterval contains the point x = c
     or is close to it then a special 25-point modified Clenshaw-Curtis
     rule is used to control the singularity.  Further away from the
     singularity the algorithm uses an ordinary 15-point Gauss-Kronrod
     integration rule.



File: gsl-ref.info,  Node: QAWS adaptive integration for singular functions,  Next: QAWO adaptive integration for oscillatory functions,  Prev: QAWC adaptive integration for Cauchy principal values,  Up: Numerical Integration

QAWS adaptive integration for singular functions
================================================

   The QAWS algorithm is designed for integrands with
algebraic-logarithmic singularities at the end-points of an integration
region.  In order to work efficiently the algorithm requires a
precomputed table of Chebyschev moments.

 - Function: gsl_integration_qaws_table *
gsl_integration_qaws_table_alloc (double ALPHA, double BETA, int MU,
          int NU)
     This function allocates space for a `gsl_integration_qaws_table'
     struct and associated workspace describing a singular weight
     function W(x) with the parameters (\alpha, \beta, \mu, \nu),

          W(x) = (x-a)^alpha (b-x)^beta log^mu (x-a) log^nu (b-x)

     where \alpha < -1, \beta < -1, and \mu = 0, 1, \nu = 0, 1.  The
     weight function can take four different forms depending on the
     values of \mu and \nu,

          W(x) = (x-a)^alpha (b-x)^beta                   (mu = 0, nu = 0)
          W(x) = (x-a)^alpha (b-x)^beta log(x-a)          (mu = 1, nu = 0)
          W(x) = (x-a)^alpha (b-x)^beta log(b-x)          (mu = 0, nu = 1)
          W(x) = (x-a)^alpha (b-x)^beta log(x-a) log(b-x) (mu = 1, nu = 1)

     The singular points (a,b) do not have to be specified until the
     integral is computed, where they are the endpoints of the
     integration range.

     The function returns a pointer to the newly allocated
     `gsl_integration_qaws_table' if no errors were detected, and 0 in
     the case of error.

 - Function: int gsl_integration_qaws_table_set
          (gsl_integration_qaws_table * T, double ALPHA, double BETA,
          int MU, int NU)
     This function modifies the parameters (\alpha, \beta, \mu, \nu) of
     an existing `gsl_integration_qaws_table' struct R.

 - Function: void gsl_integration_qaws_table_free
          (gsl_integration_qaws_table * T)
     This function frees all the memory associated with the
     `gsl_integration_qaws_table' struct T.

 - Function: int gsl_integration_qaws (gsl_function * F, const double
          A, const double B, gsl_integration_qaws_table * T, const
          double EPSABS, const double EPSREL, const size_t LIMIT,
          gsl_integration_workspace * WORKSPACE, double *RESULT, double
          *ABSERR)
     This function computes the integral of the function f(x) over the
     interval (a,b) with the singular weight function (x-a)^\alpha
     (b-x)^\beta log^\mu (x-a) log^\nu (b-x).  The parameters of the
     weight function (\alpha, \beta, \mu, \nu) are taken from the table
     T.

          I = \int_a^b dx f(x) (x-a)^alpha (b-x)^beta log^mu (x-a) log^nu (b-x)

     The adaptive bisection algorithm of QAG is used.  When a
     subinterval contains one of the endpoints then a special 25-point
     modified Clenshaw-Curtis rule is used to control the
     singularities.  For subintervals which do not include the
     endpoints an ordinary 15-point Gauss-Kronrod integration rule is
     used.



File: gsl-ref.info,  Node: QAWO adaptive integration for oscillatory functions,  Next: QAWF adaptive integration for Fourier integrals,  Prev: QAWS adaptive integration for singular functions,  Up: Numerical Integration

QAWO adaptive integration for oscillatory functions
===================================================

   The QAWO algorithm is designed for integrands with an oscillatory
factor, \sin(\omega x) or \cos(\omega x).  In order to work efficiently
the algorithm requires a table of Chebyschev moments which is updated
as the algorithm proceeds.

 - Function: gsl_integration_qawo_table *
gsl_integration_qawo_table_alloc (double OMEGA, double L, enum
          gsl_integration_qawo_enum SINE, size_t N)
     This function allocates space for a `gsl_integration_qawo_table'
     struct and its associated workspace describing a sine or cosine
     weight function W(x) with the parameters (\omega, L),

          W(x) = sin(omega x)
          W(x) = cos(omega x)

     The parameter L must be the length of the interval over which the
     function will be integrated L = b - a.  The choice of sine or
     cosine is made with the parameter SINE which should be chosen from
     one of the two following symbolic values:

          GSL_INTEG_COSINE
          GSL_INTEG_SINE

     The `gsl_integration_qawo_table' acts as a cache for trigonometric
     coefficients computed in the integration process.  The parameter N
     determines the number of levels of coefficients that can be
     stored.  Each level corresponds to one bisection of the interval
     L, so that N levels are sufficient for subintervals down to the
     length L/2^n.


 - Function: int gsl_integration_qawo_table_set
          (gsl_integration_qawo_table * T, double OMEGA, double L, enum
          gsl_integration_qawo_enum SINE)
     This function changes the parameters OMEGA, L and SINE of the
     existing workspace T.

 - Function: int gsl_integration_qawo_table_set_length
          (gsl_integration_qawo_table * T, double L)
     This function allows the length parameter L of the workspace T to
     be changed.

 - Function: void gsl_integration_qawo_table_free
          (gsl_integration_qawo_table * T)
     This function frees all the memory associated with the workspace T.

 - Function: int gsl_integration_qawo (gsl_function * F, const double
          A, const double EPSABS, const double EPSREL, const size_t
          LIMIT, gsl_integration_workspace * WORKSPACE,
          gsl_integration_qawo_table * WF, double *RESULT, double
          *ABSERR)
     This function uses an adaptive algorithm to compute the integral of
     f over (a,b) with the weight function \sin(\omega x) or
     \cos(\omega x) defined by the table WF.

          I = \int_a^b dx f(x) sin(omega x)
          I = \int_a^b dx f(x) cos(omega x)

     The results are extrapolated using the epsilon-algorithm to
     accelerate the convergence of the integral.  The function returns
     the final approximation from the extrapolation, RESULT, and an
     estimate of the absolute error, ABSERR.  The subintervals and
     their results are stored in the memory provided by WORKSPACE.  The
     maximum number of subintervals is given by LIMIT, which may not
     exceed the allocated size of the workspace.

     Those subintervals with "large" widths d, d\omega > 4 are computed
     using a 25-point Clenshaw-Curtis integration rule, which handles
     the oscillatory behavior.  Subintervals with a "small" width
     d\omega < 4 are computed using a 15-point Gauss-Kronrod
     integration.



File: gsl-ref.info,  Node: QAWF adaptive integration for Fourier integrals,  Next: Numerical integration error codes,  Prev: QAWO adaptive integration for oscillatory functions,  Up: Numerical Integration

QAWF adaptive integration for Fourier integrals
===============================================

 - Function: int gsl_integration_qawf (gsl_function * F, const double
          A, const double EPSABS, const size_t LIMIT,
          gsl_integration_workspace * WORKSPACE,
          gsl_integration_workspace * CYCLE_WORKSPACE,
          gsl_integration_qawo_table * WF, double *RESULT, double
          *ABSERR)
     This function attempts to compute a Fourier integral of the
     function F over the semi-infinite interval [a,+\infty).

          I = \int_a^{+\infty} dx f(x) sin(omega x)
          I = \int_a^{+\infty} dx f(x) cos(omega x)

     The parameter \omega is taken from the table WF (the length L can
     take any value, since it is overridden by this function to a value
     appropriate for the fourier integration).  The integral is computed
     using the QAWO algorithm over each of the subintervals,

          C_1 = [a, a + c]
          C_2 = [a + c, a + 2 c]
          ... = ...
          C_k = [a + (k-1) c, a + k c]

     where c = (2 floor(|\omega|) + 1) \pi/|\omega|.  The width c is
     chosen to cover an odd number of periods so that the contributions
     from the intervals alternate in sign and are monotonically
     decreasing when F is positive and monotonically decreasing.  The
     sum of this sequence of contributions is accelerated using the
     epsilon-algorithm.

     This function works to an overall absolute tolerance of ABSERR.
     The following strategy is used: on each interval C_k the algorithm
     tries to achieve the tolerance

          TOL_k = u_k abserr

     where u_k = (1 - p)p^{k-1} and p = 9/10.  The sum of the geometric
     series of contributions from each interval gives an overall
     tolerance of ABSERR.

     If the integration of a subinterval leads to difficulties then the
     accuracy requirement for subsequent intervals is relaxed,

          TOL_k = u_k max(abserr, max_{i<k}{E_i})

     where E_k is the estimated error on the interval C_k.

     The subintervals and their results are stored in the memory
     provided by WORKSPACE.  The maximum number of subintervals is
     given by LIMIT, which may not exceed the allocated size of the
     workspace.  The integration over each subinterval uses the memory
     provided by CYCLE_WORKSPACE as scratch space for the QAWO
     algorithm.



File: gsl-ref.info,  Node: Numerical integration error codes,  Next: Numerical integration examples,  Prev: QAWF adaptive integration for Fourier integrals,  Up: Numerical Integration

Error codes
===========

   In addition to the standard error codes for invalid arguments the
functions can return the following values,

`GSL_EMAXITER'
     the maximum number of subdivisions was exceeded.

`GSL_EROUND'
     cannot reach tolerance because of roundoff error, or roundoff
     error was detected in the extrapolation table.

`GSL_ESING'
     a non-integrable singularity or other bad integrand behavior was
     found in the integration interval.

`GSL_EDIVERGE'
     the integral is divergent, or slowly convergent.


File: gsl-ref.info,  Node: Numerical integration examples,  Next: Numerical integration References and Further Reading,  Prev: Numerical integration error codes,  Up: Numerical Integration

Examples
========

   The integrator `QAGS' will handle a large class of definite
integrals.  For example, consider the following integral, which has a
algebraic-logarithmic singularity at the origin,

     \int_0^1 x^{-1/2} log(x) dx = -4

The program below computes this integral to a relative accuracy bound of
`1e-7'.

     #include <stdio.h>
     #include <math.h>
     #include <gsl/gsl_integration.h>
     
     double f (double x, void * params) {
       double alpha = *(double *) params ;
       double f = log(alpha*x) / sqrt(x);
       return f ;
     }
     
     int
     main ()
     {
       gsl_integration_workspace * w = gsl_integration_workspace_alloc(1000);
     
       double result, error;
       double expected = -4.0;
       double alpha = 1.0;
     
       gsl_function F;
       F.function = &f;
       F.params = &alpha;
     
       gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000, w, &result, &error);
     
       printf("result          = % .18f\n", result);
       printf("exact result    = % .18f\n", expected);
       printf("estimated error = % .18f\n", error);
       printf("actual error    = % .18f\n",  result - expected);
       printf("intervals =  %d\n", w->size);
     }

The results show that the desired accuracy is achieved after 8
subdivisions.  In fact, the extrapolation procedure used by `QAGS'
produces an accuracy of almost twice as many digits.

     bash$ ./a.out
     result          = -3.999999999999973799
     exact result    = -4.000000000000000000
     estimated error =  0.000000000000246025
     actual error    =  0.000000000000026201
     intervals =  8

The error estimate returned by the extrapolation procedure is larger
than the actual error, giving a margin of safety of one order of
magnitude.


File: gsl-ref.info,  Node: Numerical integration References and Further Reading,  Prev: Numerical integration examples,  Up: Numerical Integration

References and Further Reading
==============================

The following book is the definitive reference for QUADPACK, and was
written by the original authors.  It provides descriptions of the
algorithms, program listings, test programs and examples.  It also
includes useful advice on numerical integration and many references to
the numerical integration literature used in developing QUADPACK.

     R. Piessens, E. de Doncker-Kapenga, C.W. Uberhuber, D.K. Kahaner.
     `QUADPACK A subroutine package for automatic integration' Springer
     Verlag, 1983.


File: gsl-ref.info,  Node: Monte Carlo Integration,  Next: Ordinary Differential Equations (ODEs),  Prev: Numerical Integration,  Up: Top

Monte Carlo Integration
***********************

* Menu:

* Algorithms::
* Interface::
* Example::
* The Future::

   This chapter describes the Monte Carlo integration routines in the
library.  At the moment the algorithms implemented are: plain
non-adaptive, Monte Carlo, VEGAS (following Peter Lepage) and MISER
(following Numerical recipes).  We will dispense with an introduction
and simply describe the algorithms and interfaces.  Note that the
documentation (as well as the code) is still evolving - the code at a
faster rate.


File: gsl-ref.info,  Node: Algorithms,  Next: Interface,  Up: Monte Carlo Integration

Algorithms
==========

PLAIN (or Simple) Monte Carlo
-----------------------------

   We begin by establishing some notation.  Let I(f) denote the
integral of the function f (for convenience, we take I(1)=1) We will
write the Monte Carlo estimate of the integral as E(f; N) for the

     E(f; N) = {1 \over N } \sum_i^N f(x_i).

for N randomly distributed points x_i Similarly, we will denote the
variance of f by

     \sigma^2(f) = I(f^2) - I(f)^2

and the variance of the estimate by

     Var(f; N) = E(f^2; N) - (E(f; N))^2.

   The fundamental point of Monte Carlo integration is that for large N,

     E(f; N) - I(f) \approx {\sigma^2(f)\over N }

and furthermore, by the same argument,

     \sigma^2(f) \approx Var(f; N)

   That's all there is to simple Monte Carlo.

MISER
-----

   The starting point of all stratified sampling techniques is the
observation that for two disjoint regions a and b with Monte Carlo
estimates of the integral E_a(f) and E_b(f) of the integral of f in
those regions and variances \sigma_a^2(f) and \sigma_b^2(f), the
variance Var(f) of the combined estimate E(f) = {1\over 2} E_a(f) +
{1\over 2} E_b(f) is given by

     Var(f) = {\sigma_a^2(f) \over 4 N_a} + {\sigma_b^2(f) \over 4 N_b}

   This variance is minimized (subject to the constrain that N_a + N_b
is fixed) by choosing (assuming one can) N_a s.t.

     {N_a \over N_a+N_b } = { \sigma_a \over \sigma_a + \sigma_b }

   For such a choice, the variance is given by

     Var(f) =  { (\sigma_a + \sigma_b)^2 \over 4 N }

   In words, the variance of the estimate (and hence the error) is
minimized by concentrating points in regions where the variance of the
function f is large.

   The most straightforward stratified sampling routine divides a
hyper-cubic integration region into sub-cubes along the coordinate axes.
This is simple to do, but gets out of hand for large numbers of
dimensions d because the number of sub-cubes grows like K^d (where K is
the number of sub-divisions along the axis).

   What MISER does (and other recursive stratified samplers) is to
bisect the hypercube along one coordinate axis.  The direction is
chosen by examining all possible bisections (d of them) and picking the
one that gives the best combined variance.  The same procedure is then
done for each of the two half-spaces (choosing N_a and N_b as described
above), and so on.  Since by assumption one does not know the variance
Var_L(f) in the various sub-regions, it is estimated using some
fraction of the total number of points alloted to the estimate.

VEGAS
-----

   Vegas takes another approach to obtaining good results, namely it
tries to sample points from the probability distribution described by
the function f.  More precisely, suppose we estimate the integral of f
with points distributed according to a probability distribution
described by the function g.  If we call the new estimate E_g(f; N) we
have

     E_g(f; N) = E(f/g; N)

   and similarly

     Var_g(f; N) = Var(f/g; N)

   It is clear from this that if g = |f|/I(|f|) then the variance
V_g(f; N) vanishes.  This it was what Vegas attempts to do.  It makes
several passes, histograming f, and each time using the histogram to
define the sampling distribution for the next pass.  This basic idea
again has the problem that the number of histogram bins grows like K^d.
The compromise that Vegas (ie, Peter Lepage) adopts is to assume that
g factors: g(x_1, x_2, ...) = g_1(x_1) g_2(x_2) ...  so that the number
of bins required is only d.K.  In this case, it is not hard to show
that the optimal distribution is

     g_1(x_1) = [\int dx_1 ... dx_n {f^2(x_1,...,x_n)\over g_2(x_2)... g_n(x_n) } ]^{(1/2)}

   Lepage's Vegas is actually a bit fancier than this, combining
stratified sampling and importance sampling.  The integration region is
divided into a number of "boxes", with each box getting in fixed number
of points (the goal is 2).  Each box can then have a fractional number
of bins, but if bins/box is less than two, Vegas switches to a kind
variance reduction (rather than importance sampling).

