# NAG CL Interfacee01zac (dimn_​grid)

## 1Purpose

e01zac interpolates data at a point in $n$-dimensional space, that is defined by a set of gridded data points. It offers three methods to interpolate the data: Linear Interpolation, Cubic Interpolation and Weighted Average.

## 2Specification

 #include
 void e01zac (Integer d, const Integer narr[], Nag_Boolean uniform, const double axis[], Integer lx, const double v[], const double point[], Nag_Interp method, Integer k, double wf, double *ans, NagError *fail)
The function may be called by the names: e01zac, nag_interp_dimn_grid or nag_nd_grid_interp.

## 3Description

e01zac interpolates an $n$-dimensional point within a set of gridded data points, $\mathbf{Z}=\left\{{z}_{1{j}_{1}},{z}_{2{j}_{2}},\dots ,{z}_{d{j}_{d}}\right\}$, with corresponding data values $\mathbf{F}=\left\{{f}_{1{j}_{1}},{f}_{2{j}_{2}},\dots ,{f}_{d{j}_{d}}\right\}$ where, for the $i$th dimension, ${j}_{i}=1,\dots ,{n}_{i}$ and ${n}_{i}$ is the number of ordinates in the $i$th dimension.
A hypercube of ${\left(2k\right)}^{d}$ data points $\left[{\mathbf{h}}_{1},{\mathbf{h}}_{2},\dots ,{\mathbf{h}}_{{\left(2k\right)}^{d}}\right]\subset \mathbf{Z}$, where ${\mathbf{h}}_{i}=\left({h}_{i1},{h}_{i2},\dots ,{h}_{id}\right)$ and the corresponding data values are $f\left({\mathbf{h}}_{i}\right)\in \mathbf{F}$, around the given point, $\mathbf{x}=\left({x}_{1},{x}_{2},\dots ,{x}_{d}\right)$, is found and then used to interpolate using one of the following three methods.
1. (i)Weighted Average, that is a modification of Shepard's method (Shepard (1968)) as used for scattered data in e01zmc. This method interpolates the data with the weighted mean
 $Q x = ∑ r=1 2kd wr x fr ∑ r=1 2kd wr x ,$
where ${f}_{r}=f\left({\mathbf{h}}_{r}\right)$, ${w}_{r}\left(\mathbf{x}\right)=\frac{1}{D\left(\left|\mathbf{x}-{\mathbf{h}}_{r}\right|\right)}$ and $D\left(\mathbf{y}\right)={y}_{1}^{\rho }+{y}_{2}^{\rho }+\cdots +{y}_{d}^{\rho }$, for a given value of $\rho$.
2. (ii)Linear Interpolation, which takes ${2}^{d}$ surrounding data points ($k=1$) and performs two one-dimensional linear interpolations in each dimension on data points ${\mathbf{h}}_{a}$ and ${\mathbf{h}}_{b}$, reducing the dimension every iteration until it has reached an answer. The formula for linear interpolation in dimension $i$ is simply
 $f = fa + xi-hai fb-fa hbi-hai ,$
where ${f}_{r}=f\left({\mathbf{h}}_{r}\right)$ and ${h}_{ai}<{x}_{i}<{h}_{bi}$.
3. (iii)Cubic Interpolation, based on cubic convolution (Keys (1981)). In a similar way to the Linear Interpolation method, it performs the interpolations in one dimension reducing it each time, however it requires four surrounding data points in each dimension ($k=2$), two in each direction $\left({\mathbf{h}}_{-1},{\mathbf{h}}_{0},{\mathbf{h}}_{1},{\mathbf{h}}_{2}\right)$. The following is used to calculate the one-dimensional interpolant in dimension $i$
 $f = 12 1 t t2 t3 0 2 0 0 -1 0 1 0 2 -5 4 -1 -1 3 -3 1 f-1 f0 f1 f2$
where $t={x}_{i}-{h}_{0i}$ and ${f}_{r}=f\left({\mathbf{h}}_{r}\right)$.

## 4References

Keys R (1981) Cubic Convolution Interpolation for Digital Image Processing IEEE Transactions on Acoutstics, Speech, and Signal Processing Vol ASSP-29 No. 6 1153–1160 http://hmi.stanford.edu/doc/Tech_Notes/HMI-TN-2004-004-Interpolation/Keys_cubic_interp.pdf
Shepard D (1968) A two-dimensional interpolation function for irregularly spaced data Proc. 23rd Nat. Conf. ACM 517–523 Brandon/Systems Press Inc., Princeton

## 5Arguments

1: $\mathbf{d}$Integer Input
On entry: $d$, the number of dimensions.
Constraint: ${\mathbf{d}}\ge 2$.
2: $\mathbf{narr}\left[{\mathbf{d}}\right]$const Integer Input
On entry: the number of data ordinates in each dimension, with ${\mathbf{narr}}\left[\mathit{i}-1\right]={n}_{i}$, for $\mathit{i}=1,2,\dots ,{\mathbf{d}}$.
Constraint: ${\mathbf{narr}}\left[i-1\right]\ge 2$.
3: $\mathbf{uniform}$Nag_Boolean Input
On entry: states whether the data points are uniformly spaced.
${\mathbf{uniform}}=\mathrm{Nag_TRUE}$
The data points are uniformly spaced.
${\mathbf{uniform}}=\mathrm{Nag_FALSE}$
The data points are not uniformly spaced.
Constraint: if ${\mathbf{method}}=\mathrm{Nag_CubicInterp}$, uniform must be Nag_TRUE.
4: $\mathbf{axis}\left[{\mathbf{lx}}\right]$const double Input
On entry: defines the axis. If the data points are uniformly spaced (see argument uniform) axis should contain the start and end of each dimension $\left({x}_{1 1},{x}_{1 {n}_{1}},\dots ,{x}_{d 1},{x}_{d {n}_{d}}\right)$.
If the data points are not uniformly spaced, axis should contain all the data ordinates for each dimension $\left({x}_{1 1},{x}_{1 2},\dots ,{x}_{1 {n}_{1}},\dots ,{x}_{d 1},{x}_{d 2},\dots ,{x}_{d {n}_{d}}\right)$.
Constraint: axis must be strictly increasing in each dimension.
5: $\mathbf{lx}$Integer Input
On entry: the dimension of the array axis.
Constraints:
• if ${\mathbf{uniform}}=\mathrm{Nag_TRUE}$, ${\mathbf{lx}}=2{\mathbf{d}}$;
• if ${\mathbf{uniform}}=\mathrm{Nag_FALSE}$, ${\mathbf{lx}}={\sum }_{i=1}^{{\mathbf{d}}}{\mathbf{narr}}\left[i-1\right]$.
6: $\mathbf{v}\left[\mathit{dim}\right]$const double Input
Note: the dimension, dim, of the array v must be at least $\prod _{\mathit{i}=0}^{{\mathbf{d}}-1}{\mathbf{narr}}\left[\mathit{i}\right]$.
On entry: holds the values of the data points in such an order that the index of a data value with coordinates $\left({z}_{1},{z}_{2},\dots ,{z}_{d}\right)$ is
 $∑ i=1 d zi∏n∈Sin,$
where ${\mathbf{S}}_{i}=\left\{{\mathbf{narr}}\left[l-1\right]:l=1,\dots ,i-1\right\}$ e.g., $\left(\left({x}_{11},{x}_{21},\dots ,{x}_{d1}\right),\left({x}_{12},{x}_{21},\dots ,{x}_{d1}\right),\dots ,\left({x}_{1{n}_{d}},{x}_{21},\dots ,{x}_{d1}\right),\left({x}_{11},{x}_{22},\dots ,{x}_{d1}\right),\left({x}_{12},{x}_{22},\dots ,{x}_{d1}\right),\dots ,\left({x}_{1{n}_{d}},{x}_{2{n}_{d}},\dots ,{x}_{d{n}_{d}}\right)\right)$.
7: $\mathbf{point}\left[{\mathbf{d}}\right]$const double Input
On entry: $\mathbf{x}$, the point at which the data value is to be interpolated.
Constraint: the point must lie inside the limits of the data values in each dimension supplied in axis.
8: $\mathbf{method}$Nag_Interp Input
On entry: the method to be used.
${\mathbf{method}}=\mathrm{Nag_WeightedAverage}$
Weighted Average.
${\mathbf{method}}=\mathrm{Nag_LinearInterp}$
Linear Interpolation.
${\mathbf{method}}=\mathrm{Nag_CubicInterp}$
Cubic Interpolation.
Constraint: ${\mathbf{method}}=\mathrm{Nag_WeightedAverage}$, $\mathrm{Nag_LinearInterp}$ or $\mathrm{Nag_CubicInterp}$.
9: $\mathbf{k}$Integer Input
On entry: if ${\mathbf{method}}=\mathrm{Nag_WeightedAverage}$, k controls the number of data points used in the Weighted Average method, with k points used in each dimension, either side of the interpolation point. The total number of data points used for the interpolation will therefore be ${\left(2{\mathbf{k}}\right)}^{{\mathbf{d}}}$.
If ${\mathbf{method}}\ne \mathrm{Nag_WeightedAverage}$, then k is not referenced and need not be set.
Constraint: if ${\mathbf{method}}=\mathrm{Nag_WeightedAverage}$, ${\mathbf{k}}\ge 1$.
10: $\mathbf{wf}$double Input
On entry: the power used for the weighted average such that a high power will cause closer points to be more heavily weighted.
Constraint: if ${\mathbf{method}}=\mathrm{Nag_WeightedAverage}$, $1.0\le {\mathbf{wf}}\le 15.0$.
11: $\mathbf{ans}$double * Output
On exit: holds the result of the interpolation.
12: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
Cubic Interpolation method does not have enough data surrounding point; interpolation not possible.
On entry, ${\mathbf{d}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{d}}\ge 2$.
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{method}}=\mathrm{Nag_WeightedAverage}$, ${\mathbf{k}}\ge 1$.
On entry, ${\mathbf{lx}}=〈\mathit{\text{value}}〉$, ${\mathbf{d}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{uniform}}=\mathrm{Nag_TRUE}$, ${\mathbf{lx}}=2{\mathbf{d}}$.
On entry, ${\mathbf{lx}}=〈\mathit{\text{value}}〉$, $\sum$narr$=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{uniform}}=\mathrm{Nag_FALSE}$, ${\mathbf{lx}}=\sum {\mathbf{narr}}$.
On entry, ${\mathbf{method}}=\mathrm{Nag_CubicInterp}$ and ${\mathbf{uniform}}=\mathrm{Nag_FALSE}$.
Constraint: if ${\mathbf{method}}=\mathrm{Nag_CubicInterp}$, uniform must be Nag_TRUE.
NE_INT_2
On entry, ${\mathbf{narr}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{narr}}\left[\mathit{i}-1\right]\ge 2$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_NOT_INCREASING
On entry, axis decreases in dimension $〈\mathit{\text{value}}〉$.
Constraint: axis definition must be strictly increasing.
NE_REAL
On entry, ${\mathbf{point}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$ and data range $=\left[〈\mathit{\text{value}}〉,〈\mathit{\text{value}}〉\right]$.
Constraint: point must be within the data range.
On entry, ${\mathbf{wf}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{method}}=\mathrm{Nag_WeightedAverage}$, $1.0\le {\mathbf{wf}}\le 15.0$.
NW_INT
Warning: the size of k has been reduced, due to too few data points around point.

## 7Accuracy

For most data the Cubic Interpolation method will provide the best interpolation but it is data dependent. If the data is linear, the Linear Interpolation method will be best. For noisy data the Weighted Average method is advised with ${\mathbf{wf}}<2.0$ and ${\mathbf{k}}>1$. This will include more data points and give them a greater influence to the answer.

## 8Parallelism and Performance

e01zac is not threaded in any implementation.

None.

## 10Example

This program takes a set of uniform three-dimensional grid data points which come from the function
 $fx= x13- x22+ x3 .$
e01zac then interpolates the data at the point $\left(1.10,0.25,0.75\right)$ using all three methods. The answers and the absolute errors are then printed.

### 10.1Program Text

Program Text (e01zace.c)

### 10.2Program Data

Program Data (e01zace.d)

### 10.3Program Results

Program Results (e01zace.r)