Blame doc/interp.rst

Packit 67cb25
.. index:: interpolation, spline
Packit 67cb25
Packit 67cb25
.. _sec_interpolation:
Packit 67cb25
Packit 67cb25
*************
Packit 67cb25
Interpolation
Packit 67cb25
*************
Packit 67cb25
Packit 67cb25
.. include:: include.rst
Packit 67cb25
Packit 67cb25
This chapter describes functions for performing interpolation.  The
Packit 67cb25
library provides a variety of interpolation methods, including Cubic,
Packit 67cb25
Akima, and Steffen splines.  The interpolation types are interchangeable,
Packit 67cb25
allowing different methods to be used without recompiling.
Packit 67cb25
Interpolations can be defined for both normal and periodic boundary
Packit 67cb25
conditions.  Additional functions are available for computing
Packit 67cb25
derivatives and integrals of interpolating functions. Routines
Packit 67cb25
are provided for interpolating both one and two dimensional datasets.
Packit 67cb25
Packit 67cb25
These interpolation methods produce curves that pass through each
Packit 67cb25
datapoint.  To interpolate noisy data with a smoothing curve see
Packit 67cb25
:ref:`chap_basis-splines`.
Packit 67cb25
Packit 67cb25
The functions described in this section are declared in the header files
Packit 67cb25
:file:`gsl_interp.h` and :file:`gsl_spline.h`.
Packit 67cb25
Packit 67cb25
Introduction to 1D Interpolation
Packit 67cb25
================================
Packit 67cb25
Packit 67cb25
Given a set of data points :math:`(x_1, y_1) \dots (x_n, y_n)` the
Packit 67cb25
routines described in this section compute a continuous interpolating
Packit 67cb25
function :math:`y(x)` such that :math:`y(x_i) = y_i`.  The interpolation
Packit 67cb25
is piecewise smooth, and its behavior at the end-points is determined by
Packit 67cb25
the type of interpolation used.
Packit 67cb25
Packit 67cb25
1D Interpolation Functions
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
The interpolation function for a given dataset is stored in a
Packit 67cb25
:type:`gsl_interp` object.  These are created by the following functions.
Packit 67cb25
Packit 67cb25
.. type:: gsl_interp
Packit 67cb25
Packit 67cb25
   Workspace for 1D interpolation
Packit 67cb25
Packit 67cb25
.. function:: gsl_interp * gsl_interp_alloc (const gsl_interp_type * T, size_t size)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a newly allocated interpolation
Packit 67cb25
   object of type :data:`T` for :data:`size` data-points.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_interp_init (gsl_interp * interp, const double xa[], const double ya[], size_t size)
Packit 67cb25
Packit 67cb25
   This function initializes the interpolation object :data:`interp` for the
Packit 67cb25
   data (:data:`xa`, :data:`ya`) where :data:`xa` and :data:`ya` are arrays of size
Packit 67cb25
   :data:`size`.  The interpolation object (:type:`gsl_interp`) does not save
Packit 67cb25
   the data arrays :data:`xa` and :data:`ya` and only stores the static state
Packit 67cb25
   computed from the data.  The :data:`xa` data array is always assumed to be
Packit 67cb25
   strictly ordered, with increasing :math:`x` values; 
Packit 67cb25
   the behavior for other arrangements is not defined.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_interp_free (gsl_interp * interp)
Packit 67cb25
Packit 67cb25
   This function frees the interpolation object :data:`interp`.
Packit 67cb25
Packit 67cb25
1D Interpolation Types
Packit 67cb25
======================
Packit 67cb25
Packit 67cb25
The interpolation library provides the following interpolation types:
Packit 67cb25
Packit 67cb25
.. type:: gsl_interp_type
Packit 67cb25
Packit 67cb25
   .. index:: linear interpolation
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp_linear
Packit 67cb25
Packit 67cb25
      Linear interpolation.  This interpolation method does not require any
Packit 67cb25
      additional memory.
Packit 67cb25
Packit 67cb25
   .. index:: polynomial interpolation
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp_polynomial
Packit 67cb25
Packit 67cb25
      Polynomial interpolation.  This method should only be used for
Packit 67cb25
      interpolating small numbers of points because polynomial interpolation
Packit 67cb25
      introduces large oscillations, even for well-behaved datasets.  The
Packit 67cb25
      number of terms in the interpolating polynomial is equal to the number
Packit 67cb25
      of points.
Packit 67cb25
Packit 67cb25
   .. index:: cubic splines
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp_cspline
Packit 67cb25
Packit 67cb25
      Cubic spline with natural boundary conditions.  The resulting curve is
Packit 67cb25
      piecewise cubic on each interval, with matching first and second
Packit 67cb25
      derivatives at the supplied data-points.  The second derivative is
Packit 67cb25
      chosen to be zero at the first point and last point.
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp_cspline_periodic
Packit 67cb25
Packit 67cb25
      Cubic spline with periodic boundary conditions.  The resulting curve
Packit 67cb25
      is piecewise cubic on each interval, with matching first and second
Packit 67cb25
      derivatives at the supplied data-points.  The derivatives at the first
Packit 67cb25
      and last points are also matched.  Note that the last point in the
Packit 67cb25
      data must have the same y-value as the first point, otherwise the
Packit 67cb25
      resulting periodic interpolation will have a discontinuity at the
Packit 67cb25
      boundary.
Packit 67cb25
Packit 67cb25
   .. index:: Akima splines
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp_akima
Packit 67cb25
Packit 67cb25
      Non-rounded Akima spline with natural boundary conditions.  This method
Packit 67cb25
      uses the non-rounded corner algorithm of Wodicka.
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp_akima_periodic
Packit 67cb25
Packit 67cb25
      Non-rounded Akima spline with periodic boundary conditions.  This method
Packit 67cb25
      uses the non-rounded corner algorithm of Wodicka.
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp_steffen
Packit 67cb25
Packit 67cb25
      Steffen's method guarantees the monotonicity of the interpolating function
Packit 67cb25
      between the given data points. Therefore, minima and maxima can only occur
Packit 67cb25
      exactly at the data points, and there can never be spurious oscillations
Packit 67cb25
      between data points. The interpolated function is piecewise cubic
Packit 67cb25
      in each interval. The resulting curve and its first derivative
Packit 67cb25
      are guaranteed to be continuous, but the second derivative may be
Packit 67cb25
      discontinuous.
Packit 67cb25
Packit 67cb25
The following related functions are available:
Packit 67cb25
Packit 67cb25
.. function:: const char * gsl_interp_name (const gsl_interp * interp)
Packit 67cb25
Packit 67cb25
   This function returns the name of the interpolation type used by :data:`interp`.
Packit 67cb25
   For example::
Packit 67cb25
Packit 67cb25
      printf ("interp uses '%s' interpolation.\n", gsl_interp_name (interp));
Packit 67cb25
Packit 67cb25
   would print something like::
Packit 67cb25
Packit 67cb25
      interp uses 'cspline' interpolation.
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_interp_min_size (const gsl_interp * interp)
Packit 67cb25
              unsigned int gsl_interp_type_min_size (const gsl_interp_type * T)
Packit 67cb25
Packit 67cb25
   These functions return the minimum number of points required by the
Packit 67cb25
   interpolation object :data:`interp` or interpolation type :data:`T`.  For
Packit 67cb25
   example, Akima spline interpolation requires a minimum of 5 points.
Packit 67cb25
Packit 67cb25
1D Index Look-up and Acceleration
Packit 67cb25
=================================
Packit 67cb25
Packit 67cb25
The state of searches can be stored in a :type:`gsl_interp_accel` object,
Packit 67cb25
which is a kind of iterator for interpolation lookups.
Packit 67cb25
Packit 67cb25
.. type:: gsl_interp_accel
Packit 67cb25
Packit 67cb25
   This workspace stores state variables for interpolation lookups.
Packit 67cb25
   It caches the previous value of an index lookup.  When the subsequent interpolation
Packit 67cb25
   point falls in the same interval its index value can be returned
Packit 67cb25
   immediately.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_interp_bsearch (const double x_array[], double x, size_t index_lo, size_t index_hi)
Packit 67cb25
Packit 67cb25
   This function returns the index :math:`i` of the array :data:`x_array` such
Packit 67cb25
   that :code:`x_array[i] <= x < x_array[i+1]`.  The index is searched for
Packit 67cb25
   in the range [:data:`index_lo`, :data:`index_hi`]. |inlinefn|
Packit 67cb25
Packit 67cb25
.. function:: gsl_interp_accel * gsl_interp_accel_alloc (void)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to an accelerator object, which is a
Packit 67cb25
   kind of iterator for interpolation lookups.  It tracks the state of
Packit 67cb25
   lookups, thus allowing for application of various acceleration
Packit 67cb25
   strategies.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_interp_accel_find (gsl_interp_accel * a, const double x_array[], size_t size, double x)
Packit 67cb25
Packit 67cb25
   This function performs a lookup action on the data array :data:`x_array`
Packit 67cb25
   of size :data:`size`, using the given accelerator :data:`a`.  This is how
Packit 67cb25
   lookups are performed during evaluation of an interpolation.  The
Packit 67cb25
   function returns an index :math:`i` such that :code:`x_array[i] <= x < x_array[i+1]`.
Packit 67cb25
   |inlinefn|
Packit 67cb25
Packit 67cb25
.. function:: int gsl_interp_accel_reset (gsl_interp_accel * acc);
Packit 67cb25
Packit 67cb25
   This function reinitializes the accelerator object :data:`acc`.  It
Packit 67cb25
   should be used when the cached information is no longer
Packit 67cb25
   applicable---for example, when switching to a new dataset.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_interp_accel_free (gsl_interp_accel* acc)
Packit 67cb25
Packit 67cb25
   This function frees the accelerator object :data:`acc`.
Packit 67cb25
Packit 67cb25
1D Evaluation of Interpolating Functions
Packit 67cb25
========================================
Packit 67cb25
Packit 67cb25
.. function::  double gsl_interp_eval (const gsl_interp * interp, const double xa[], const double ya[], double x, gsl_interp_accel * acc)
Packit 67cb25
               int gsl_interp_eval_e (const gsl_interp * interp, const double xa[], const double ya[], double x, gsl_interp_accel * acc, double * y)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value of :data:`y` for a given
Packit 67cb25
   point :data:`x`, using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa` and :data:`ya` and the accelerator :data:`acc`.  When
Packit 67cb25
   :data:`x` is outside the range of :data:`xa`, the error code
Packit 67cb25
   :macro:`GSL_EDOM` is returned with a value of :macro:`GSL_NAN` for
Packit 67cb25
   :data:`y`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp_eval_deriv (const gsl_interp * interp, const double xa[], const double ya[], double x, gsl_interp_accel * acc)
Packit 67cb25
              int gsl_interp_eval_deriv_e (const gsl_interp * interp, const double xa[], const double ya[], double x, gsl_interp_accel * acc, double * d)
Packit 67cb25
Packit 67cb25
   These functions return the derivative :data:`d` of an interpolated
Packit 67cb25
   function for a given point :data:`x`, using the interpolation object
Packit 67cb25
   :data:`interp`, data arrays :data:`xa` and :data:`ya` and the accelerator
Packit 67cb25
   :data:`acc`. 
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp_eval_deriv2 (const gsl_interp * interp, const double xa[], const double ya[], double x, gsl_interp_accel * acc)
Packit 67cb25
              int gsl_interp_eval_deriv2_e (const gsl_interp * interp, const double xa[], const double ya[], double x, gsl_interp_accel * acc, double * d2)
Packit 67cb25
Packit 67cb25
   These functions return the second derivative :data:`d2` of an interpolated
Packit 67cb25
   function for a given point :data:`x`, using the interpolation object
Packit 67cb25
   :data:`interp`, data arrays :data:`xa` and :data:`ya` and the accelerator
Packit 67cb25
   :data:`acc`. 
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp_eval_integ (const gsl_interp * interp, const double xa[], const double ya[], double a, double b, gsl_interp_accel * acc)
Packit 67cb25
              int gsl_interp_eval_integ_e (const gsl_interp * interp, const double xa[], const double ya[], double a, double b, gsl_interp_accel * acc, double * result)
Packit 67cb25
Packit 67cb25
   These functions return the numerical integral :data:`result` of an
Packit 67cb25
   interpolated function over the range [:data:`a`, :data:`b`], using the
Packit 67cb25
   interpolation object :data:`interp`, data arrays :data:`xa` and :data:`ya` and
Packit 67cb25
   the accelerator :data:`acc`.
Packit 67cb25
Packit 67cb25
1D Higher-level Interface
Packit 67cb25
=========================
Packit 67cb25
Packit 67cb25
The functions described in the previous sections required the user to
Packit 67cb25
supply pointers to the :math:`x` and :math:`y` arrays on each call.  The
Packit 67cb25
following functions are equivalent to the corresponding
Packit 67cb25
:type:`gsl_interp` functions but maintain a copy of this data in the
Packit 67cb25
:type:`gsl_spline` object.  This removes the need to pass both :data:`xa`
Packit 67cb25
and :data:`ya` as arguments on each evaluation. These functions are
Packit 67cb25
defined in the header file :file:`gsl_spline.h`.
Packit 67cb25
Packit 67cb25
.. type:: gsl_spline
Packit 67cb25
Packit 67cb25
   This workspace provides a higher level interface for the
Packit 67cb25
   :type:`gsl_interp` object
Packit 67cb25
Packit 67cb25
.. function:: gsl_spline * gsl_spline_alloc (const gsl_interp_type * T, size_t size)
Packit 67cb25
Packit 67cb25
.. function:: int gsl_spline_init (gsl_spline * spline, const double xa[], const double ya[], size_t size)
Packit 67cb25
Packit 67cb25
.. function:: void gsl_spline_free (gsl_spline * spline)
Packit 67cb25
Packit 67cb25
.. function:: const char * gsl_spline_name (const gsl_spline * spline)
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_spline_min_size (const gsl_spline * spline)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline_eval (const gsl_spline * spline, double x, gsl_interp_accel * acc)
Packit 67cb25
              int gsl_spline_eval_e (const gsl_spline * spline, double x, gsl_interp_accel * acc, double * y)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline_eval_deriv (const gsl_spline * spline, double x, gsl_interp_accel * acc)
Packit 67cb25
              int gsl_spline_eval_deriv_e (const gsl_spline * spline, double x, gsl_interp_accel * acc, double * d)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline_eval_deriv2 (const gsl_spline * spline, double x, gsl_interp_accel * acc)
Packit 67cb25
              int gsl_spline_eval_deriv2_e (const gsl_spline * spline, double x, gsl_interp_accel * acc, double * d2)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline_eval_integ (const gsl_spline * spline, double a, double b, gsl_interp_accel * acc)
Packit 67cb25
              int gsl_spline_eval_integ_e (const gsl_spline * spline, double a, double b, gsl_interp_accel * acc, double * result)
Packit 67cb25
Packit 67cb25
1D Interpolation Example Programs
Packit 67cb25
=================================
Packit 67cb25
Packit 67cb25
The following program demonstrates the use of the interpolation and
Packit 67cb25
spline functions.  It computes a cubic spline interpolation of the
Packit 67cb25
10-point dataset :math:`(x_i, y_i)` where :math:`x_i = i + \sin(i)/2` and
Packit 67cb25
:math:`y_i = i + \cos(i^2)` for :math:`i = 0 \dots 9`.
Packit 67cb25
Packit 67cb25
.. include:: examples/interp.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The output is designed to be used with the GNU plotutils
Packit 67cb25
:code:`graph` program::
Packit 67cb25
Packit 67cb25
  $ ./a.out > interp.dat
Packit 67cb25
  $ graph -T ps < interp.dat > interp.ps
Packit 67cb25
Packit 67cb25
.. _fig_interp:
Packit 67cb25
Packit 67cb25
.. figure:: /images/interp.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Cubic spline interpolation
Packit 67cb25
Packit 67cb25
:numref:`fig_interp` shows a smooth interpolation of the original points.  The
Packit 67cb25
interpolation method can be changed simply by varying the first argument of
Packit 67cb25
:func:`gsl_spline_alloc`.
Packit 67cb25
Packit 67cb25
The next program demonstrates a periodic cubic spline with 4 data
Packit 67cb25
points.  Note that the first and last points must be supplied with 
Packit 67cb25
the same y-value for a periodic spline.
Packit 67cb25
Packit 67cb25
.. include:: examples/interpp.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The output can be plotted with GNU :code:`graph`::
Packit 67cb25
Packit 67cb25
  $ ./a.out > interp.dat
Packit 67cb25
  $ graph -T ps < interp.dat > interp.ps
Packit 67cb25
Packit 67cb25
.. _fig_interpp:
Packit 67cb25
Packit 67cb25
.. figure:: /images/interpp.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Periodic cubic spline interpolation
Packit 67cb25
Packit 67cb25
:numref:`fig_interpp` shows a periodic interpolation of the original points. The
Packit 67cb25
slope of the fitted curve is the same at the beginning and end of the
Packit 67cb25
data, and the second derivative is also.
Packit 67cb25
Packit 67cb25
The next program illustrates the difference between the cubic spline,
Packit 67cb25
Akima, and Steffen interpolation types on a difficult dataset.
Packit 67cb25
Packit 67cb25
.. include:: examples/interp_compare.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
.. _fig_interp-compare:
Packit 67cb25
Packit 67cb25
.. figure:: /images/interp_compare.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Comparison of different 1D interpolation methods
Packit 67cb25
Packit 67cb25
The output is shown in :numref:`fig_interp-compare`.
Packit 67cb25
The cubic method exhibits a local maxima between the 6th and 7th data points
Packit 67cb25
and continues oscillating for the rest of the data. Akima also shows a
Packit 67cb25
local maxima but recovers and follows the data well after the 7th grid point.
Packit 67cb25
Steffen preserves monotonicity in all intervals and does not exhibit oscillations,
Packit 67cb25
at the expense of having a discontinuous second derivative.
Packit 67cb25
Packit 67cb25
Introduction to 2D Interpolation
Packit 67cb25
================================
Packit 67cb25
Packit 67cb25
Given a set of :math:`x` coordinates :math:`x_1,...,x_m` and a set of
Packit 67cb25
:math:`y` coordinates :math:`y_1,...,y_n`, each in increasing order,
Packit 67cb25
plus a set of function values :math:`z_{ij}`
Packit 67cb25
for each grid point :math:`(x_i,y_j)`, the routines described in this
Packit 67cb25
section compute a continuous interpolation function :math:`z(x,y)` such
Packit 67cb25
that :math:`z(x_i,y_j) = z_{ij}`.
Packit 67cb25
Packit 67cb25
2D Interpolation Functions
Packit 67cb25
==========================
Packit 67cb25
Packit 67cb25
The interpolation function for a given dataset is stored in a
Packit 67cb25
:type:`gsl_interp2d` object. These are created by the following functions.
Packit 67cb25
Packit 67cb25
.. type:: gsl_interp2d
Packit 67cb25
Packit 67cb25
   Workspace for 2D interpolation
Packit 67cb25
Packit 67cb25
.. function:: gsl_interp2d * gsl_interp2d_alloc (const gsl_interp2d_type * T, const size_t xsize, const size_t ysize)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a newly allocated interpolation
Packit 67cb25
   object of type :data:`T` for :data:`xsize` grid points in the :math:`x`
Packit 67cb25
   direction and :data:`ysize` grid points in the :math:`y` direction.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_interp2d_init (gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const size_t xsize, const size_t ysize)
Packit 67cb25
Packit 67cb25
   This function initializes the interpolation object :data:`interp` for the
Packit 67cb25
   data (:data:`xa`, :data:`ya`, :data:`za`) where :data:`xa` and :data:`ya` are arrays of
Packit 67cb25
   the :math:`x` and :math:`y` grid points of size :data:`xsize` and :data:`ysize`
Packit 67cb25
   respectively, and :data:`za` is an array of function values of size
Packit 67cb25
   :data:`xsize` * :data:`ysize`.  The interpolation object (:type:`gsl_interp2d`) does
Packit 67cb25
   not save the data arrays :data:`xa`, :data:`ya`, and :data:`za` and only stores the
Packit 67cb25
   static state computed from the data. The :data:`xa` and :data:`ya` data arrays
Packit 67cb25
   are always assumed to be strictly ordered, with increasing :math:`x,y` values; 
Packit 67cb25
   the behavior for other arrangements is not defined.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_interp2d_free (gsl_interp2d * interp)
Packit 67cb25
Packit 67cb25
   This function frees the interpolation object :data:`interp`.
Packit 67cb25
Packit 67cb25
2D Interpolation Grids
Packit 67cb25
======================
Packit 67cb25
Packit 67cb25
The 2D interpolation routines access the function values :math:`z_{ij}`
Packit 67cb25
with the following ordering:
Packit 67cb25
Packit 67cb25
.. math:: z_{ij} = za[j*xsize + i]
Packit 67cb25
Packit 67cb25
with :math:`i = 0,...,xsize-1` and :math:`j = 0,...,ysize-1`. However,
Packit 67cb25
for ease of use, the following functions are provided to add and retrieve
Packit 67cb25
elements from the function grid without requiring knowledge of the
Packit 67cb25
internal ordering.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_interp2d_set (const gsl_interp2d * interp, double za[], const size_t i, const size_t j, const double z)
Packit 67cb25
Packit 67cb25
   This function sets the value :math:`z_{ij}` for grid point
Packit 67cb25
   (:data:`i`, :data:`j`) of the array :data:`za` to :data:`z`.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_get (const gsl_interp2d * interp, const double za[], const size_t i, const size_t j)
Packit 67cb25
Packit 67cb25
   This function returns the value :math:`z_{ij}` for grid point
Packit 67cb25
   (:data:`i`, :data:`j`) stored in the array :data:`za`.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_interp2d_idx (const gsl_interp2d * interp, const size_t i, const size_t j)
Packit 67cb25
Packit 67cb25
   This function returns the index corresponding to the grid point
Packit 67cb25
   (:data:`i`, :data:`j`). The index is given by :math:`j*xsize + i`.
Packit 67cb25
Packit 67cb25
2D Interpolation Types
Packit 67cb25
======================
Packit 67cb25
Packit 67cb25
.. type:: gsl_interp2d_type
Packit 67cb25
Packit 67cb25
   The interpolation library provides the following 2D interpolation types:
Packit 67cb25
Packit 67cb25
   .. index:: bilinear interpolation
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp2d_bilinear
Packit 67cb25
Packit 67cb25
      Bilinear interpolation.  This interpolation method does not require any
Packit 67cb25
      additional memory.
Packit 67cb25
Packit 67cb25
   .. index:: bicubic interpolation
Packit 67cb25
Packit 67cb25
   .. var:: gsl_interp2d_bicubic
Packit 67cb25
Packit 67cb25
      Bicubic interpolation.
Packit 67cb25
Packit 67cb25
.. function:: const char * gsl_interp2d_name (const gsl_interp2d * interp)
Packit 67cb25
Packit 67cb25
   This function returns the name of the interpolation type used by :data:`interp`.
Packit 67cb25
   For example::
Packit 67cb25
Packit 67cb25
      printf ("interp uses '%s' interpolation.\n", gsl_interp2d_name (interp));
Packit 67cb25
Packit 67cb25
   would print something like::
Packit 67cb25
Packit 67cb25
      interp uses 'bilinear' interpolation.
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_interp2d_min_size (const gsl_interp2d * interp)
Packit 67cb25
              unsigned int gsl_interp2d_type_min_size (const gsl_interp2d_type * T)
Packit 67cb25
Packit 67cb25
   These functions return the minimum number of points required by the
Packit 67cb25
   interpolation object :data:`interp` or interpolation type :data:`T`.  For
Packit 67cb25
   example, bicubic interpolation requires a minimum of 4 points.
Packit 67cb25
Packit 67cb25
2D Evaluation of Interpolating Functions
Packit 67cb25
========================================
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_eval (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_interp2d_eval_e (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * z)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value of :data:`z` for a given
Packit 67cb25
   point (:data:`x`, :data:`y`), using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa`, :data:`ya`, and :data:`za` and the accelerators :data:`xacc`
Packit 67cb25
   and :data:`yacc`.  When :data:`x` is outside the range of :data:`xa` or :data:`y`
Packit 67cb25
   is outside the range of :data:`ya`, the error code
Packit 67cb25
   :macro:`GSL_EDOM` is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_eval_extrap (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_interp2d_eval_extrap_e (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * z)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value of :data:`z` for a given
Packit 67cb25
   point (:data:`x`, :data:`y`), using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa`, :data:`ya`, and :data:`za` and the accelerators :data:`xacc`
Packit 67cb25
   and :data:`yacc`. The functions perform no bounds checking, so
Packit 67cb25
   when :data:`x` is outside the range of :data:`xa` or :data:`y`
Packit 67cb25
   is outside the range of :data:`ya`, extrapolation is performed.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_eval_deriv_x (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_interp2d_eval_deriv_x_e (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value :data:`d`
Packit 67cb25
   :math:`= \partial z / \partial x` for a given point (:data:`x`, :data:`y`),
Packit 67cb25
   using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa`, :data:`ya`, and :data:`za` and the accelerators :data:`xacc`
Packit 67cb25
   and :data:`yacc`.  When :data:`x` is outside the range of :data:`xa` or :data:`y`
Packit 67cb25
   is outside the range of :data:`ya`, the error code
Packit 67cb25
   :macro:`GSL_EDOM` is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_eval_deriv_y (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_interp2d_eval_deriv_y_e (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value :data:`d`
Packit 67cb25
   :math:`= \partial z / \partial y` for a given point (:data:`x`, :data:`y`),
Packit 67cb25
   using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa`, :data:`ya`, and :data:`za` and the accelerators :data:`xacc`
Packit 67cb25
   and :data:`yacc`.  When :data:`x` is outside the range of :data:`xa` or :data:`y`
Packit 67cb25
   is outside the range of :data:`ya`, the error code
Packit 67cb25
   :macro:`GSL_EDOM` is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_eval_deriv_xx (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_interp2d_eval_deriv_xx_e (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value :data:`d`
Packit 67cb25
   :math:`= \partial^2 z / \partial x^2` for a given point (:data:`x`, :data:`y`),
Packit 67cb25
   using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa`, :data:`ya`, and :data:`za` and the accelerators :data:`xacc`
Packit 67cb25
   and :data:`yacc`.  When :data:`x` is outside the range of :data:`xa` or :data:`y`
Packit 67cb25
   is outside the range of :data:`ya`, the error code
Packit 67cb25
   :macro:`GSL_EDOM` is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_eval_deriv_yy (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_interp2d_eval_deriv_yy_e (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value :data:`d`
Packit 67cb25
   :math:`= \partial^2 z / \partial y^2` for a given point (:data:`x`, :data:`y`),
Packit 67cb25
   using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa`, :data:`ya`, and :data:`za` and the accelerators :data:`xacc`
Packit 67cb25
   and :data:`yacc`.  When :data:`x` is outside the range of :data:`xa` or :data:`y`
Packit 67cb25
   is outside the range of :data:`ya`, the error code
Packit 67cb25
   :macro:`GSL_EDOM` is returned.
Packit 67cb25
Packit 67cb25
.. function:: double gsl_interp2d_eval_deriv_xy (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_interp2d_eval_deriv_xy_e (const gsl_interp2d * interp, const double xa[], const double ya[], const double za[], const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
   These functions return the interpolated value :data:`d`
Packit 67cb25
   :math:`= \partial^2 z / \partial x \partial y` for a given point (:data:`x`, :data:`y`),
Packit 67cb25
   using the interpolation object :data:`interp`, data
Packit 67cb25
   arrays :data:`xa`, :data:`ya`, and :data:`za` and the accelerators :data:`xacc`
Packit 67cb25
   and :data:`yacc`.  When :data:`x` is outside the range of :data:`xa` or :data:`y`
Packit 67cb25
   is outside the range of :data:`ya`, the error code
Packit 67cb25
   :macro:`GSL_EDOM` is returned.
Packit 67cb25
Packit 67cb25
2D Higher-level Interface
Packit 67cb25
=========================
Packit 67cb25
Packit 67cb25
The functions described in the previous sections required the user to
Packit 67cb25
supply pointers to the :math:`x`, :math:`y`, and :math:`z` arrays on each call.
Packit 67cb25
The following functions are equivalent to the corresponding
Packit 67cb25
:code:`gsl_interp2d` functions but maintain a copy of this data in the
Packit 67cb25
:type:`gsl_spline2d` object.  This removes the need to pass :data:`xa`,
Packit 67cb25
:data:`ya`, and :data:`za` as arguments on each evaluation. These functions are
Packit 67cb25
defined in the header file :file:`gsl_spline2d.h`.
Packit 67cb25
Packit 67cb25
.. type:: gsl_spline2d
Packit 67cb25
Packit 67cb25
   This workspace provides a higher level interface for the
Packit 67cb25
   :type:`gsl_interp2d` object
Packit 67cb25
Packit 67cb25
.. function:: gsl_spline2d * gsl_spline2d_alloc (const gsl_interp2d_type * T, size_t xsize, size_t ysize)
Packit 67cb25
Packit 67cb25
.. function:: int gsl_spline2d_init (gsl_spline2d * spline, const double xa[], const double ya[], const double za[], size_t xsize, size_t ysize)
Packit 67cb25
Packit 67cb25
.. function:: void gsl_spline2d_free (gsl_spline2d * spline)
Packit 67cb25
Packit 67cb25
.. function:: const char * gsl_spline2d_name (const gsl_spline2d * spline)
Packit 67cb25
Packit 67cb25
.. function:: unsigned int gsl_spline2d_min_size (const gsl_spline2d * spline)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline2d_eval (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_spline2d_eval_e (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * z)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline2d_eval_deriv_x (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_spline2d_eval_deriv_x_e (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline2d_eval_deriv_y (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_spline2d_eval_deriv_y_e (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline2d_eval_deriv_xx (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_spline2d_eval_deriv_xx_e (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline2d_eval_deriv_yy (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_spline2d_eval_deriv_yy_e (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline2d_eval_deriv_xy (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc)
Packit 67cb25
              int gsl_spline2d_eval_deriv_xy_e (const gsl_spline2d * spline, const double x, const double y, gsl_interp_accel * xacc, gsl_interp_accel * yacc, double * d)
Packit 67cb25
Packit 67cb25
.. function:: int gsl_spline2d_set (const gsl_spline2d * spline, double za[], const size_t i, const size_t j, const double z)
Packit 67cb25
Packit 67cb25
.. function:: double gsl_spline2d_get (const gsl_spline2d * spline, const double za[], const size_t i, const size_t j)
Packit 67cb25
Packit 67cb25
   This function returns the value :math:`z_{ij}` for grid point
Packit 67cb25
   (:data:`i`, :data:`j`) stored in the array :data:`za`.
Packit 67cb25
Packit 67cb25
2D Interpolation Example programs
Packit 67cb25
=================================
Packit 67cb25
Packit 67cb25
The following example performs bilinear interpolation on the unit
Packit 67cb25
square, using :math:`z` values of :math:`(0,1,0.5,1)` going clockwise
Packit 67cb25
around the square.
Packit 67cb25
Packit 67cb25
.. include:: examples/interp2d.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
The results of the interpolation are shown in :numref:`fig_interp2d`,
Packit 67cb25
where the corners are labeled with their fixed :math:`z` values.
Packit 67cb25
Packit 67cb25
.. _fig_interp2d:
Packit 67cb25
Packit 67cb25
.. figure:: /images/interp2d.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   2D interpolation example
Packit 67cb25
Packit 67cb25
References and Further Reading
Packit 67cb25
==============================
Packit 67cb25
Packit 67cb25
Descriptions of the interpolation algorithms and further references can
Packit 67cb25
be found in the following publications:
Packit 67cb25
Packit 67cb25
* C.W. Ueberhuber,
Packit 67cb25
  *Numerical Computation (Volume 1), Chapter 9 "Interpolation"*,
Packit 67cb25
  Springer (1997), ISBN 3-540-62058-3.
Packit 67cb25
Packit 67cb25
* D.M. Young, R.T. Gregory,
Packit 67cb25
  *A Survey of Numerical Mathematics (Volume 1), Chapter 6.8*,
Packit 67cb25
  Dover (1988), ISBN 0-486-65691-8.
Packit 67cb25
Packit 67cb25
* M. Steffen,
Packit 67cb25
  *A simple method for monotonic interpolation in one dimension*,
Packit 67cb25
  Astron. Astrophys. 239, 443-450, 1990.