SF_INTERPOLATE

Description

SciFortran module for data interpolation

Quick access

Types:

finter_type, finter2d_type

Routines:

cinter(), cubic_spline(), delete_finter(), delete_finter2d(), finter(), finter2d(), init_finter(), init_finter2d(), linear_spline(), poly_spline()

Used modules

  • interpolate_nr

Types

type  sf_interpolate/finter_type

This derived type stores the tabulated data of a 1-dimensional function and the order of the local polynomial interpolation, and is used by finter and cinter. It is initialized with init_finter and released with delete_finter. The components are not meant to be set directly.

Type fields:
  • x (•) [real, allocatable] – grid points, strictly increasing

  • f (•) [real, allocatable] – function values on the grid (real part for a complex function)

  • g (•) [real, allocatable] – imaginary part of the function values (allocated only for complex data)

  • imin [integer] – index of the first grid point used (set to 1 by init_finter)

  • imax [integer] – index of the last grid point used (set to size(X) by init_finter)

  • n [integer] – order of the local polynomial interpolation

  • status [logical] – T if the object has been initialized

type  sf_interpolate/finter2d_type

This derived type stores the tabulated data of a 2-dimensional function on a regular grid and the order of the local polynomial interpolation, and is used by finter2d. It is initialized with init_finter2d and released with delete_finter2d. The components are not meant to be set directly.

Type fields:
  • x (•) [real, allocatable] – grid points along x, strictly increasing

  • y (•) [real, allocatable] – grid points along y, strictly increasing

  • f (•, •) [real, allocatable] – function values on the grid, F(i,j)=F(X(i),Y(j))

  • n [integer] – order of the local polynomial interpolation

  • imin [integer] – index of the first grid point used along x (set to 1 by init_finter2d)

  • imax [integer] – index of the last grid point used along x (set to size(X) by init_finter2d)

  • jmin [integer] – index of the first grid point used along y (set to 1 by init_finter2d)

  • jmax [integer] – index of the last grid point used along y (set to size(Y) by init_finter2d)

  • status [logical] – T if the object has been initialized

Subroutines and functions

interface  sf_interpolate/linear_spline(xin, fin, xout, fout, yin, yout)

This subroutine evaluates the piecewise linear interpolation of a real or complex function tabulated on a grid. The specific procedures cover

  • a function of one variable, evaluated at a single point (scalar Xout) or at an array of points

  • a function of two variables on a regular grid, evaluated at a single point or on the grid Xout \(\times\) Yout

In 1 dimension the interpolation is carried out by the routine interp_linear. The grid Xin must be strictly increasing, otherwise the program stops. Points outside the grid are extrapolated linearly. If the input and output grids have the same size and are identical the message msg: test_grid_equlity: Fout=Fin. Exit. is printed, and the result is the same.

In 2 dimensions the interpolation is bilinear, see http://en.wikipedia.org/wiki/Bilinear_interpolation. The points must lie inside the input grid, extrapolation is not available.

Parameters:
  • xin (•) [real, in,required] – input grid points (x axis in 2D), strictly increasing

  • fin (various shapes) [real/complex] – function values: F(Xin(i)) in 1D, F(Xin(i),Yin(j)) in 2D

  • xout (various shapes) [real] – points where to interpolate (x axis in 2D): scalar or array

  • fout (various shapes) [real/complex]

  • yin (•) [real, in,required] – input grid points along y (2D), strictly increasing

  • yout (various shapes) [real] – points along y where to interpolate (2D): scalar or array

interface  sf_interpolate/poly_spline(xin, fin, xout, fout, yin, yout[, n])

This subroutine evaluates the polynomial interpolation of a real or complex function tabulated on a grid, using the local polynomial of order N built by finter (1 dimension) or finter2d (2 dimensions). The specific procedures cover

  • a function of one variable, evaluated at a single point (scalar Xout) or at an array of points

  • a function of two variables on a regular grid, evaluated at a single point or on the grid Xout \(\times\) Yout

At each point the interpolating polynomial is built on a window of \(N+2\) neighbouring grid points (\((N+2)^2\) in 2 dimensions) using polint (polin2). The window is shifted to stay inside the grid at its borders, so points outside the grid are extrapolated with the polynomial of the first or last window. The grid must have at least \(N+2\) points along each direction, and orders \(N>20\) are not recommended.

Parameters:
  • xin (•) [real, in,required] – input grid points (x axis in 2D), strictly increasing

  • fin (various shapes) [real/complex] – function values: F(Xin(i)) in 1D, F(Xin(i),Yin(j)) in 2D

  • xout (various shapes) [real] – points where to interpolate (x axis in 2D): scalar or array

  • fout (various shapes) [real/complex]

  • yin (•) [real, in,required] – input grid points along y (2D), strictly increasing

  • yout (various shapes) [real] – points along y where to interpolate (2D): scalar or array

Options:

n [integer] – order of the local polynomial interpolation (default 5)

interface  sf_interpolate/cubic_spline(xin, fin, xout, fout)

This subroutine evaluates the cubic spline interpolation of a real or complex function of one variable tabulated on the grid Xin, at a single point (scalar Xout) or at an array of points. It is an interface to the routines CUBSPL and PPVALU of C. de Boor, called with the not-a-knot condition at both ends, that is the first two and the last two cubic pieces coincide. This routine works well with imaginary time Green’s functions. The grid Xin must be strictly increasing and have at least 2 points. Points outside the grid are extrapolated with the first or last cubic piece. If the input and output grids have the same size and are identical the message msg: test_grid_equlity: Fout=Fin. Exit. is printed, and the result is the same.

Parameters:
  • xin (•) [real, in,required] – input grid points (x axis in 2D), strictly increasing

  • fin (size(xin)) [real/complex] – function values: F(Xin(i)) in 1D, F(Xin(i),Yin(j)) in 2D

  • xout (various shapes) [real] – points where to interpolate (x axis in 2D): scalar or array

  • fout (various shapes) [real/complex]

interface  sf_interpolate/init_finter(func, xin, fin, n)

This subroutine initializes a finter_type object with the tabulated data of a real or complex function of one variable, so that it can be interpolated with finter (real) or cinter (complex). The data are copied, func does not keep a reference to Xin and Fin. If func was already initialized it is first released with delete_finter. In the complex case the real and imaginary parts are stored separately, in func%F and func%G.

Parameters:
  • func [finter_type] – finter_type object to initialize (previous content is released)

  • xin (•) [real, in,required] – function values on the grid, F(Xin(i)): real or complex

  • fin (size(xin)) [real/complex]

  • n [integer] – order of the local polynomial interpolation (window of N+2 grid points)

subroutine  sf_interpolate/delete_finter(func)

This subroutine releases the memory of the finter_type object func and resets it to the uninitialized state: Imin=Imax=N=0 and status=.false..

Parameters:

func [finter_type]

function  sf_interpolate/finter(func, x)

This function evaluates at the point x the real function stored in the finter_type object func. It finds the grid interval containing x with locate and interpolates with polint on a window of \(N+2\) grid points around it, with N=func%N. The window is shifted to stay inside the grid, so points outside the grid are extrapolated with the polynomial of the first or last window. If func was initialized with a complex function, the real part is interpolated.

Parameters:
Result:

finter [real]

function  sf_interpolate/cinter(func, x)

This function is the complex counterpart of finter. It evaluates at the point x the complex function stored in the finter_type object func, interpolating separately the real and the imaginary parts, stored in func%F and func%G. The object must have been initialized with a complex function, otherwise func%G is not allocated.

Parameters:
Result:

cinter [complex]

subroutine  sf_interpolate/init_finter2d(func, xin, yin, fin, n)

This subroutine initializes a finter2d_type object with the tabulated data of a real function of two variables on the regular grid Xin \(\times\) Yin, so that it can be interpolated with finter2d. The data are copied, func does not keep a reference to Xin, Yin and Fin. If func was already initialized its memory is first released.

Parameters:
  • func [finter2d_type]

  • xin (•) [real, in,required]

  • yin (•) [real, in,required]

  • fin (size(xin), size(yin)) [real]

  • n [integer]

subroutine  sf_interpolate/delete_finter2d(func)

This subroutine releases the memory of the finter2d_type object func and resets it to the uninitialized state: Imin=Imax=Jmin=Jmax=N=0 and status=.false..

Parameters:

func [finter2d_type]

function  sf_interpolate/finter2d(func, x, y)

This function evaluates at the point (x, y) the function stored in the finter2d_type object func. It finds the grid cell containing the point with locate along each direction and interpolates with polin2 on a window of \((N+2) \times (N+2)\) grid points around it, with N=func%N. The window is shifted to stay inside the grid, so points outside the grid are extrapolated with the polynomial of the first or last window.

Parameters:
Result:

finter2d [real]