ArrayInterpolation - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


CurveFitting

  

ArrayInterpolation

  

n-dimensional data interpolation (table lookup)

 

Calling Sequence

Parameters

Description

Options

Examples

Calling Sequence

ArrayInterpolation(xdata, ydata, xvalues, options)

ArrayInterpolation(xydata, xvalues, options)

Parameters

xdata

-

a list, Array, DataFrame, DataSeries, Vector, or Matrix containing the independent coordinate(s) of each of the data points, given in one of several possible forms

ydata

-

a list, Array, DataFrame, DataSeries, or Vector containing the dependent coordinate of each of the data points

xydata

-

alternate input; a list, Array, DataFrame, or Matrix containing both the dependent and independent coordinates of each of the data points

xvalues

-

a numeric value, list, Vector, or Array containing the independent coordinate(s) of one or more points whose dependent coordinate will be approximated using interpolation

options

-

(optional) equation(s) of the form keyword = value, where keyword is one of method, degree, endpoints, knots, uniform, verify, extrapolate, or container.

Description

• 

The ArrayInterpolation command takes a finite set of distinct data points given by xdata and ydata (or xydata), and interpolates to approximate the y-values corresponding to the points given in xvalues.  It considers an interpolant function f such that fx=y for all respective pairs x,y in xdata and ydata (or xydata). Such a function can be constructed using one of various methods (see below).  It then computes and returns fxi for all xi in xvalues.

• 

The focus of the ArrayInterpolation command is the performance of quick and efficient data resampling and table lookup.  To actually compute and return interpolants, functions such as CurveFitting[Spline] and CurveFitting[RationalInterpolation] can be used instead.

• 

The ArrayInterpolation function can interpolate numeric data in n dimensions, where n is any positive integer.

• 

The list of independent coordinates of the data points, given in xdata, can be input in a number of different ways.  xdata can be:

– 

(preferred if n=1) a Vector, DataSeries, list, or one-dimensional Array of strictly increasing x-coordinates. The data set will then have size a1, where a1 is the length of xdata.

– 

(preferred if 1<n) a list of n Vectors, lists, or one-dimensional Arrays, one for each dimension of the data.  The jth Vector, list, or Array in the input must contain, in increasing order, all of the possible jth coordinates of the data points.  In this case, the block of data points will be assumed to lie on an a1 by a2 by ... by an grid, where aj is the length of the jth Vector or Array in the input.  The pth coordinate of the data point at index &lsqb;j1, j2, ..., jn&rsqb; (where 1jiai) will be equal to the apth element of the pth Array in the input.

– 

a list of n Arrays of size a1 by a2 by ... by an, where the jth array contains the jth independent coordinate of each of the a1 by a2 by ... by an data points.  The coordinates must form a proper "grid" of values, and must be sorted in strictly increasing order along each dimension.  More formally, opp&comma;xdata&lsqb;j1, j2, ..., jn] - opp&comma;xdata&lsqb;k1, k2, ..., kn&rsqb; must be zero if jp=kp, and must be positive if kp<jp.

  

The preferred methods minimize memory usage and execution time by avoiding unnecessary storage and verification of redundant data.  In all cases, xdata must contain real values of type numeric.

• 

The list of dependent coordinates of the data points, given in ydata, must be input as an Array (or a Matrix, Vector, or list for appropriate values of n) of size a1 by a2 by ... by an, so that the value of ydata&lsqb;j1, j2, ..., jn&rsqb; corresponds to the element in xdata of index &lsqb;j1, j2, ..., jn&rsqb;. Values in ydata may be complex, but must be of type complex(extended_numeric). The output will not contain complex values unless ydata does.

• 

As an alternate form of input, a single structure xydata containing all coordinates of the data points can be entered.  It can be formatted in one of the following ways:

– 

an Array, DataFrame, or Matrix of size a1 by a2 by ... by an by (n+1), giving the independent and dependent coordinate(s) of each of a1 by a2 by ... by an data points as an ordered (n+1)-tuple.  The first n elements in each (n+1)-tuple represent the independent coordinates of each point, and must adhere to the same restrictions as above (a proper "grid" must be formed, and the independent coordinates must be sorted in strictly increasing order along each dimension).  The n+1st coordinate in each (n+1)-tuple then represents the dependent coordinate of the respective data point.

– 

a list of n+1 Arrays, Vectors, Matrices, or lists of size a1 by a2 by ... by an, where the jth array contains the jth independent coordinate of each of the a1 by a2 by ... by an data points for 1jn, and the n+1st Array contains the dependent coordinates of each point.  As above, the independent coordinates must adhere to certain restrictions (a proper "grid" must be formed, and the independent coordinates must be sorted in strictly increasing order along each dimension).

  

For multidimensional data, these methods are not recommended, since space is wasted storing the full grid of independent coordinates instead of a list of all the possible coordinates in each dimension. In both cases, the values representing independent coordinates must be of type numeric, but the values representing dependent coordinates can be of type complex(extended_numeric).

• 

The list of values to interpolate at, given in xvalues, may be input in one of the following formats:

– 

for one-dimensional data, a single numeric value, or a Vector, list, or one-dimensional Array of numeric values can be input.  The output will be returned in a format matching the format of the input.

– 

for multidimensional data, an Array or Matrix of size u1 by u2 by ... by uk by n of numeric values can be input.  It must contain the n coordinates of each of u1 by u2 by ... by uk values to interpolate at, with the value of xvalues&lsqb;j1, j2, ..., jk, p&rsqb; giving the pth coordinate of the respective point.  The output will be returned in an array of size u1 by u2 by ... by uk containing the interpolated results.

– 

alternatively, a list of n Vectors, lists, or one-dimensional Arrays can be input. The jth Vector, list, or Array in the input will be assumed to contain all of the possible jth coordinates of the values to interpolate at.  In this case, interpolation will be performed on an a1 by a2 by ... by an block of points, where aj is the length of the jth Vector or Array in the input.  The output will then be returned in a Vector, Matrix, list, or Array of size a1 by a2 by ... by an.

• 

If any of the data points in xvalues lie outside the rectangular bounding box specified by the input, then extrapolation will be performed to approximate their corresponding y-values.  The method by which extrapolation is performed can be controlled by using option extrapolate; see below.

• 

This routine has separate numeric methods for handling hardware and software floats.  The decision about which routine to use can be controlled by setting the UseHardwareFloats environment variable.  If UseHardwareFloats remains unset, then hardware floats are used if and only if DigitsevalhfDigits, in which case all software floats in the input will be converted to hardware floats.

• 

Only computations involving numeric floating-point data are supported by this routine. If the input does not contain floating-point data, an error will be thrown.

• 

For optimal performance, all rtables in the input should be Fortran order with rectangular storage (the default). Otherwise, a conversion will take place.  All rtables in the output will be Fortran order rtables with rectangular storage.

• 

This function is part of the CurveFitting package, so it can be used in the short form ArrayInterpolation(..) only after executing the command with(CurveFitting).  However, it can always be accessed through the long form of the command by using CurveFitting[ArrayInterpolation](..).

Options

• 

If the option method = <name> is given, then one of the following interpolation methods are used to compute the interpolant f and evaluate fxi for each point xi in xvalues:

– 

method = nearest: Perform nearest neighbor interpolation.  Given a point xi in xvalues, fxi is defined to be y, where x&comma;y is the data point such that the Euclidean distance xxi is minimized.

– 

method = lowest: Perform lowest neighbor interpolation.  Given a point xi in xvalues, fxi is defined to be y, wherex&comma;y is the data point such that xix is non-negative in all coordinates, but the Euclidean distance xxi is minimized.

– 

method = highest: Perform highest neighbor interpolation.  Given a point xi in xvalues, fxi is defined to be y, where x&comma;y is the data point such that xxi is non-negative in all coordinates, but the Euclidean distance xxi is minimized.

– 

method = linear: Perform n-dimensional linear interpolation (lerping).  In the one-dimensional case, f is a piecewise-linear function passing through each data point x&comma;y in the input.  In the multidimensional case, f is the tensor product of n such piecewise linear functions, one for each dimension.  fxi is computed by performing linear interpolation along the first dimension, then along the second dimension, and so on.

– 

method = cubic: Perform piecewise cubic Hermite interpolation. In the 1-dimensional case, f is a piecewise-cubic function passing through each data pointx&comma;y in the input. In this case, fx = fix if x lies in the interval xi&comma;xi+1, where each fi is a cubic polynomial such that fixi=yi and fixi+1=yi+1 for all data points xi&comma;yi in the input (where i ranges from 0 to k). The coefficients of the functions fi are determined locally by assigning slopes si to each data point xi and solving for the unique cubic function fix determined by the additional constraints that fi'xi=siandfi'xi+1=si+1.  This forces f to be continuously differentiable (C1).  The si themselves are computed using Bessel's method: si is the slope at xi of the parabola passing through xi1,yi1, xi,yi, and xi+1,yi+1. In the multidimensional case, f is the tensor product of n such spline functions, one for each dimension.

– 

method = spline: Perform spline interpolation.  By default, natural cubic spline interpolation is used. In the 1-dimensional case, f is a piecewise-cubic function passing through each data pointx&comma;y in the input. In this case, fx = fix if x lies in the interval xi&comma;xi+1, where each fi is a cubic polynomial such that fixi=yi and fixi+1=yi+1 for all data points xi&comma;yi in the input (where i ranges from 0 to k).  The coefficients of the functions fi are selected such that f is twice continuously differentiable (C2), that is,  fi'xi+1=fi+1'xi+1and fi''xi+1=fi+1''xi+1.  In addition, the "natural" condition of the spline specifies that f''x0=0 and f''xk=0. In the multidimensional case, f is the tensor product of n such spline functions, one for each dimension. Using method=spline will produce a smoother interpolant than method=cubic (C2 instead of C1), but is more expensive to set up and more prone to numerical instability because each segment of the spline is determined globally by the positions of all other points in the data set.

  

method=linear is used by default.

• 

If the options degree=d and endpoints=e are given, where d is a positive integer and e is one of natural, notaknot, or periodic, then spline interpolation will be performed using the provided degree and endpoint conditions.  See Spline Continuity and End Conditions for details. These options only affect the result if method=spline is used.  In the multidimensional case, the same degree and endpoint conditions are used for the splines generated in each dimension. The defaults are degree=3 and endpoints=natural, in which case natural cubic spline interpolation will be performed.

• 

If splines of an even degree are being used, the option knots=data forces the use of a spline function where the spline knots are positioned on the nodes.  See Spline Continuity and End Conditions for details.  The default method, knots=default, defines the spline knots at the midpoints of the nodes when even degree splines are used.  This option has no effect when other methods are used.

• 

If the option uniform=true is given, then ArrayInterpolation assumes that the data points are sampled over a grid of uniformly spaced points in each dimension. In other words, if ai,j is the jth possible coordinate in the ith dimension, then di=ai,jai,j1 is assumed to be constant over all possible j, given any fixed value of i. This gives a considerable speedup when the input contains uniform data, since it allows ArrayInterpolation to use a fast lookup algorithm when evaluating the interpolant at the specified points. The default is uniform=false, in which case ArrayInterpolation uses a slower but more general binary search algorithm to perform interpolation.  Using the uniform=true option with non-uniform data may produce incorrect results.

• 

If the option verify=false is given, then ArrayInterpolation skips the various checks it performs to ensure correctly formatted input.  This can decrease the time required to solve large problems, but will prevent the function from detecting any errors in the input.  If the input is improperly sorted, contains Arrays indexed from values other than 1, contains non-rectangular or C order rtables, or is otherwise formatted incorrectly, ArrayInterpolation may return incorrect results or throw an unexpected error.

• 

If the option extrapolate=e is given, where e is of type extended_numeric or truefalse, then one of the following possible extrapolation methods will be used to compute fx if x lies outside the bounding box specified by the input:

– 

extrapolate = true: Perform extrapolation using the closest valid branch of the interpolating function. In the case of method=lowest and method=highest, this is not be defined for some points, in which case undefined will be returned.

– 

extrapolate = false: Do not extrapolate.  An error will be thrown if any point in xvalues lies outside the bounding box specified by the input.

– 

extrapolate = e, where e is of type extended_numeric : Define fx to be e if x lies outside the bounding box specified by the input.  e is commonly zero or undefined.

  

extrapolate=true is used by default.

• 

If the option container=c is given, where c is an appropriately sized and typed rtable, then the result is written into c. c must be of the correct size and datatype to match the output of the routine. If ydata (and thus c) have only real values, then with this option, no additional memory is allocated to store the result; this is a programmer-level feature that can be used to reduce memory usage and decrease the time spent by Maple's garbage collector. If nonreal values are present, then the output is still copied into c, but there are no memory savings.  The default is container=false, in which case Maple creates and returns a new rtable containing the result.

Examples

withCurveFitting&colon;

withplots&colon;

An introductory example.  Suppose a signal is sampled several times over a given interval of time:

Times0.&comma;0.01&comma;0.02&comma;0.03&comma;0.04&comma;0.05&comma;0.06&comma;0.07&comma;0.08&comma;0.09&comma;0.10&colon;

Amplitudes0.6&comma;0.&comma;0.4&comma;0.6&comma;0.3&comma;0.1&comma;0.2&comma;0.&comma;0.1&comma;0.3&comma;0.6&colon;

pointplotTimes&comma;Amplitudes

ArrayInterpolation to resample the data at a higher sampling frequency:

NewTimesseq0.001i&comma;i=0..100&colon;

NewAmplitudesArrayInterpolationTimes&comma;Amplitudes&comma;NewTimes&colon;

pointplotNewTimes&comma;NewAmplitudes

Use a cubic spline to achieve a smoother, more realistic resampling of the data:

NewAmplitudesArrayInterpolationTimes&comma;Amplitudes&comma;NewTimes&comma;method=spline&colon;

pointplotNewTimes&comma;NewAmplitudes

Try again, using a spline that assumes the data is sampled from a periodic waveform:

NewAmplitudesArrayInterpolationTimes&comma;Amplitudes&comma;NewTimes&comma;method=spline&comma;endpoints=periodic&colon;

pointplotNewTimes&comma;NewAmplitudes

A two-dimensional example: a tiny grayscale image stored in a Matrix:

Rangesseq1..10&comma;seq1..16&colon;

ImgevalfMatrix255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;208&comma;93&comma;22&comma;0&comma;16&comma;98&comma;210&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;196&comma;16&comma;0&comma;0&comma;0&comma;0&comma;0&comma;19&comma;200&comma;255&comma;255&comma;54&comma;54&comma;212&comma;255&comma;255&comma;61&comma;34&comma;156&comma;231&comma;255&comma;230&comma;154&comma;26&comma;58&comma;255&comma;255&comma;61&comma;0&comma;59&comma;255&comma;255&comma;5&comma;205&comma;255&comma;255&comma;255&comma;255&comma;255&comma;196&comma;7&comma;255&comma;255&comma;230&comma;91&comma;7&comma;255&comma;255&comma;20&comma;235&comma;255&comma;255&comma;255&comma;255&comma;255&comma;235&comma;36&comma;255&comma;255&comma;255&comma;233&comma;32&comma;255&comma;255&comma;125&comma;129&comma;255&comma;255&comma;255&comma;255&comma;255&comma;133&comma;186&comma;255&comma;255&comma;217&comma;89&comma;147&comma;255&comma;255&comma;255&comma;125&comma;50&comma;91&comma;99&comma;93&comma;40&comma;22&comma;121&comma;93&comma;58&comma;10&comma;140&comma;255&comma;255&comma;255&comma;255&comma;255&comma;216&comma;131&comma;75&comma;14&comma;5&comma;7&comma;12&comma;59&comma;121&comma;214&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&comma;255&colon;

listdensityplotImg&comma;style=PATCHNOGRID&comma;axes=none&comma;range=0..255

Upsample it to a larger image using bilinear interpolation:

NewRangesseq3..52&comma;seq3..825.0&colon;

NewImgArrayInterpolationRanges&comma;Img&comma;NewRanges&comma;extrapolate=255&colon;

listdensityplotNewImg&comma;style=PATCHNOGRID&comma;axes=none&comma;range=0..255

Try again, using bicubic interpolation instead for a smoother fit:

NewImgArrayInterpolationRanges&comma;Img&comma;NewRanges&comma;extrapolate=255&comma;method=cubic&colon;

listdensityplotNewImg&comma;style=PATCHNOGRID&comma;axes=none&comma;range=0..255

A non-uniform multidimensional example.  Create some 3-D mesh structures to pass through a given set of points defined by a mathematical function:

fi&comma;j3sini23j2&colon;

plot3df&comma;0..10&comma;0..10&comma;axes=normal

Define a non-uniform grid of points, and sample f over them:

vArray0&comma;1.5&comma;3.5&comma;5&comma;6&comma;8&comma;9&colon;

wArray0&comma;3&comma;5&comma;6&comma;6.5&comma;7.5&colon;

yMatrix7&comma;6&comma;a&comma;bevalffva&comma;wb&colon;

Plot the data so far:

pointplot3dseqseqvi&comma;wj&comma;yi,j&comma;j=1..6&comma;i=1..7&comma;axes=normal&comma;symbol=sphere

Create a finer mesh to interpolate over:

a1Matrix50&comma;50&comma;i&comma;ji5&colon;

a2Matrix50&comma;50&comma;i&comma;jj5&colon;

AArrayToolsConcatenate3&comma;a1&comma;a2&colon;

Linear interpolation produces a quick approximation to f:

BArrayInterpolationv&comma;w&comma;y&comma;A&comma;method=linear&colon;

matrixplotMatrixB&comma;axes=normal

Nearest-neighbor interpolation can also be used for quick lookup purposes:

BArrayInterpolationv&comma;w&comma;y&comma;A&comma;method=nearest&colon;

matrixplotMatrixB&comma;axes=normal

Spline interpolation produces a smoother approximation to the original function f:

BArrayInterpolationv&comma;w&comma;y&comma;A&comma;method=spline&colon;

matrixplotMatrixB&comma;axes=normal

Increasing the degree of the spline approximation can increase the smoothness of the result, but results in a longer computation time, greater numerical instability, and can cause large oscillations around the edges of a data set:

BArrayInterpolationv&comma;w&comma;y&comma;A&comma;method=spline&comma;degree=5&colon;

matrixplotMatrixB&comma;axes=normal

Array interpolation of real x values and complex y values.

fxsinx+I

fxsinx+I

(1)

vxseq3..3&comma;0.5&colon;

vyseqevalffi&comma;i=3..3&comma;0.5&colon;

Plot of the points:

complexplotvy&comma;style=point&comma;scaling=constrained

x points to interpolate over.

Aseqi&comma;i=evalfπ..evalfπ&comma;numelems=100&colon;

BArrayInterpolationvx&comma;vy&comma;A&comma;method=spline&colon;

complexplotconvertB&comma;list&comma;scaling=constrained

A complex multidimensional example.  Create some 3-D mesh structures to pass through a given Set of points defined by a mathematical function:

fi&comma;jsqrti+sqrtj&colon;

Define a non-uniform grid of points, and sample f over them:

vArray0&comma;2&comma;4&comma;5&comma;8&comma;10&comma;12&colon;

wArray6&comma;5&comma;4&comma;3&comma;2&comma;1&colon;

yMatrix7&comma;6&comma;a&comma;bevalffva&comma;wb&colon;

Create a finer mesh to interpolate over:

a1Matrix50&comma;50&comma;i&comma;ji5&colon;

a2Matrix50&comma;50&comma;i&comma;jj5&colon;

AArrayToolsConcatenate3&comma;a1&comma;a2&colon;

Linear interpolation produces a quick approximation to f:

BArrayInterpolationv&comma;w&comma;y&comma;A&comma;method=spline&colon;

complexplotconvertB&comma;list&comma;axes=normal

Another format for the last example

xdataMatrix7&comma;6&comma;i&comma;jvi&comma;Matrix7&comma;6&comma;i&comma;jwj&colon;

BArrayInterpolationxdata&comma;y&comma;A&comma;method=linear&colon;

A 5 dimensional example

v1Array10&comma;9&comma;8&comma;7&colon;

v2Array1&comma;0&comma;1&comma;2&comma;3&colon;

v3Array3&comma;2&comma;0&comma;6&colon;

v4Array2&comma;3&comma;4&comma;5&comma;6&comma;7&colon;

fi&comma;j&comma;h&comma;kksqrti+sqrthj&colon;

yArray1..4&comma;1..5&comma;1..4&comma;1..6&comma;i&comma;j&comma;h&comma;kevalffv1i&comma;v2j&comma;v3h&comma;v4k&colon;

AArrayseqi&comma;i=10..7&comma;0.2&comma;Arrayseqi&comma;i=1..3&comma;0.2&comma;Arrayseqi&comma;i=3..6&comma;1.5&comma;Arrayseqi&comma;i=2..7&comma;0.4&colon;

BArrayInterpolationv1&comma;v2&comma;v3&comma;v4&comma;y&comma;A&colon;

Finally, a large example to illustrate a few tips for increasing the speed of computations:

AevalfVectorseq0..50000&colon;

BevalfVectorseqi2&comma;i=0..50000&colon;

CevalfVectorseq0..2000004&colon;

timeArrayInterpolationA&comma;B&comma;C

0.066

(2)

On such a large one-dimensional example, a significant portion of the execution time is spent verifying the integrity of the input data.  Disabling this verification will produce a significant speedup in the execution time of the routine, but will produce incorrect results if the input is not correctly formatted or sorted:

timeArrayInterpolationA&comma;B&comma;C&comma;verify=false

0.018

(3)

Asserting that the data is uniform allows a faster lookup method to be used:

timeArrayInterpolationA&comma;B&comma;C&comma;verify=false&comma;uniform=true

0.012

(4)

Cubic interpolation takes longer than the default, linear method:

timeArrayInterpolationA&comma;B&comma;C&comma;verify=false&comma;uniform=true&comma;method=cubic

0.015

(5)

See Also

CurveFitting

CurveFitting[PolynomialInterpolation]

CurveFitting[RationalInterpolation]

CurveFitting[Spline]

CurveFitting[SplineConditions]

CurveFitting[ThieleInterpolation]