Jump to content

Multidimensional interpolation: Difference between revisions

From Wikibase
Restore space after removed comment. AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
Line 3: Line 3:
== Methods ==
== Methods ==


=== Grid interpolation ===
== Grid interpolation ==


If a "grid" of $n^m$ datapoints is known, one on each combination of $n$ nodes in each of the $m$ variables:
=== via Lagrange polynomial in multiple variables ===
 
If a "grid" of $n^m$ datapoints, i.e., values of $y$ at all possible combinations of $n$ particular values in each of the $m$ variables


$$
$$
Line 11: Line 13:
$$
$$


then the interpolant is the multivariable Lagrange polynomial
is known, then a multivariable-version of the Lagrange polynomial can be used to interpolate for $y$.
 
$$
P(x_1,\cdots,x_m)=\sum_{i_1=0}^{n-1}\cdots\sum_{i_m=0}^{n-1}y_{i_1,\cdots,i_m}\prod_{j=1}^{m}L^{(x_j)}_{i_j}(x_j),\qquad L^{(x_j)}_{i_j}(x_j)=\prod_{\substack{k=0\\k\neq i}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i}-x_{j,k}}
$$
 
which is a multi-variable polynomial of degree at most $n-1$ in each variable unique for each set of datapoints.
 
Similar to in the [[Polynomial_interpolation#Lagrange_polynomial|one-dimensional Lagrange polynomial]],


$$
$$
f(x_1,\cdots,x_m)=\sum_{i_1=0}^{n-1}\cdots\sum_{i_m=0}^{n-1}y_{i_1,\cdots,i_m}\prod_{j=1}^{m}L^{(j)}_{i_j}(x_j),\qquad L^{(j)}_{i}(x_j)=\prod_{\substack{k=0\\k\neq i}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i}-x_{j,k}}
L^{(x_j)}_{i_j}(x_j)=
\begin{cases}
1, & x_j=x_{j,i_j}
0, & x_j=x_{j,i_k},\, i_k \in  [0,n-1],\, i_k \in \mathbb{N}  
\end{cases}
$$
$$


where $L^{(j)}_i$ is the [[Polynomial_interpolation#Lagrange_polynomial|Lagrange basis polynomial]] of the variable $x_j$, built on that variable's $n$ nodes.
which garaties that P(x_1,\cdots,x_m)=y(x_1,\cdots,x_m) passes through all datapoints.


Each factor $L^{(j)}_{i_j}(x_j)$ equals one at the node $x_{j,i_j}$ and zero at the other $n-1$ nodes of $x_j$. The product of the $m$ factors is therefore one at the single datapoint $(x_{1,i_1},\cdots,x_{m,i_m})$ and zero at every other datapoint, so each of the $n^m$ terms carries exactly its own reading and vanishes at all the rest, and the sum passes through all $n^m$ datapoints.
<ref>{{#cite:Q2392}}</ref>


The result is the '''tensor product''' interpolant, and it is unique among polynomials of degree at most $n-1$ in each variable: a difference of two of them vanishes at all $n^m$ nodes, and such a difference, read as a polynomial of degree at most $n-1$ in $x_1$, has $n$ roots for every fixed choice of the other variables, so each of its coefficients vanishes at those choices, one variable at a time<ref>{{#cite:Q2392}}</ref>. In the Lagrange basis the coefficients are the readings themselves, so nothing is solved for: the $n^m$ coefficients and the $n^m$ datapoints are the same numbers.
<blockquote>
$P(x_1,\cdots,x_m)$ is also known as the '''tensor product Lagrange interpolant'''.
</blockquote>


{{Hidden example}}
{{Hidden example}}
'''Surface temperature over a square of weather stations.''' Sixteen stations sit on a $4\times4$ grid, spaced $1^\circ$ of longitude apart from west to east and $1^\circ$ of latitude apart from south to north. The readings are synthetic, constructed for this example:
'''Surface temperature over a square of weather stations.'''  
 
Sixteen weather stations sitting in a $4^\circ \times 4^\circ$ grid$ measures surface temperature at various latitudes and longitudes.


{| class="wikitable"
{| class="wikitable"
Line 43: Line 61:
|}
|}


With $m=2$ and $n=4$ the sum has $n^m=16$ terms, one per station. Number the longitude nodes $x_{1,1}=0$, $x_{1,2}=1$, $x_{1,3}=2$, $x_{1,4}=3$ and the latitude nodes $x_{2,1}=50$, $x_{2,2}=51$, $x_{2,3}=52$, $x_{2,4}=53$, so that $i_1=1$ is the column at $0^\circ$E and $i_2=1$ the row at $50^\circ$N, the bottom row of the table:
What is the interpolated temperature at $51.6^\circ \textbf{N}, 1.4^\circ \textbf{E}$?
 
'''Algebraic solution''':
 
Let $x_1$ represent the longitude, $x_2$ represent the latitude, and denote the surface temperature reading at column $i_1$, row $i_2$ by $y_{i_1,i_2}$.
 


$$
$$
Line 49: Line 72:
$$
$$


where $y_{i_1,i_2}$ is the reading in column $i_1$ and row $i_2$, and $L_{ji_j}$ is the general $L^{(j)}_{i_j}$ with the node index counted from $1$ rather than from $0$.
where
 
'''The eight basis polynomials.''' Four are built on the longitude nodes $0,1,2,3$ and are functions of $x_1$; four are built on the latitude nodes $50,51,52,53$ and are functions of $x_2$:


$$
$$
Line 69: Line 90:
$$
$$


'''The polynomial in $x_1$ and $x_2$.''' Substituting the eight polynomials above leaves one term per station, 16 in all, four on each parallel:
Hence


$$
$$
Line 87: Line 108:
$$
$$


Collecting like powers reduces this to 16 monomials, of degree at most 3 in each variable; the large coefficients come from the latitude nodes sitting at 50 to 53 rather than at 0 to 3:
On simplification:


$$
$$
Line 97: Line 118:
$$
$$


'''Evaluating at $1.4^\circ$E, $51.6^\circ$N.''' The point goes into the developed form as it stands, one coordinate for each of $x_1$ and $x_2$:
Substitute in $x_1=1.4,x_2=51.6$,


$$
$$
Line 115: Line 136:
$$
$$


The individual terms are of order $10^4$ and cancel down to $22$, so several digits are lost and the sum has to be taken exactly; the code below does that. The nested route of [[#Pointwise_interpolation|pointwise interpolation]] regroups these same 16 terms into two passes, with numbers no larger than the readings, and reaches the same result with nothing to cancel; the developed form is the harder of the two routes to evaluate, not the easier.
'''Numerical solution''':
 
The same arithmetic in code:


<syntaxhighlight lang="python" copy>
<syntaxhighlight lang="python" copy>
Line 209: Line 228:
</syntaxhighlight>
</syntaxhighlight>


[[File:Bicubic grid interpolation temperature.png|thumb|none|560px|alt=Three-dimensional plot of a gently curving temperature surface over a longitude-latitude grid, with the sixteen station readings marked as red dots and one query point marked with a cross|The interpolating surface through the sixteen station readings, the tensor product of four longitude and four latitude basis polynomials. The query point at $1.4^\circ$E, $51.6^\circ$N is marked with a cross.]]
[[File:Bicubic grid interpolation temperature.png|thumb|none|560px|alt=Three-dimensional plot of a gently curving temperature surface over a longitude-latitude grid, with the sixteen station readings marked as red dots and one query point marked with a cross|The interpolating surface through the sixteen station readings constructed via Lagrange polynomials in two variables. $1.4^\circ$E, $51.6^\circ$N is marked with a cross.]]


The temperature falls from about $26\ ^\circ$C in the south-east to about $18\ ^\circ$C in the north-west, so the surface is dominated by a plane tilted towards the north-west, with a gentler curvature across it.
As observed, the surface is dominated by a plane tilted gently downwards from the south-east towards the north-west, predicting a gentle decrease in temperature along the direction.
{{End hidden}}
{{End hidden}}



Revision as of 16:57, 27 October 2026

Languages: English · français · Esperanto

Multidimensional interpolation is the process of finding a multivariable function $f(x_1,\cdots,x_m)$ that for a definite set of datapoints $\{(x_{1,i},\cdots,x_{m,i},y_i)\mid i\in [0,n], i \in \mathbb{N}\}$ satisfies $f(x_{1,i},\cdots,x_{m,i})=y_i$.

Methods

Grid interpolation

via Lagrange polynomial in multiple variables

If a "grid" of $n^m$ datapoints, i.e., values of $y$ at all possible combinations of $n$ particular values in each of the $m$ variables

$$ \{(x_{1,i_1},\cdots,x_{m,i_m},y_{i_1,\cdots,i_m})\mid i_1,\cdots,i_m \in [0,n-1],\, i_1,\cdots,i_m \in \mathbb{N}\} $$

is known, then a multivariable-version of the Lagrange polynomial can be used to interpolate for $y$.

$$ P(x_1,\cdots,x_m)=\sum_{i_1=0}^{n-1}\cdots\sum_{i_m=0}^{n-1}y_{i_1,\cdots,i_m}\prod_{j=1}^{m}L^{(x_j)}_{i_j}(x_j),\qquad L^{(x_j)}_{i_j}(x_j)=\prod_{\substack{k=0\\k\neq i}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i}-x_{j,k}} $$

which is a multi-variable polynomial of degree at most $n-1$ in each variable unique for each set of datapoints.

Similar to in the one-dimensional Lagrange polynomial,

$$ L^{(x_j)}_{i_j}(x_j)= \begin{cases} 1, & x_j=x_{j,i_j} 0, & x_j=x_{j,i_k},\, i_k \in [0,n-1],\, i_k \in \mathbb{N} \end{cases} $$

which garaties that P(x_1,\cdots,x_m)=y(x_1,\cdots,x_m) passes through all datapoints.

[1]

$P(x_1,\cdots,x_m)$ is also known as the tensor product Lagrange interpolant.

Pointwise interpolation

Pointwise interpolation (or point evaluation) asks only for the value of $f$ at one new point $(x_1^*,\cdots,x_m^*)$ inside the data range, without constructing $f$ anywhere else. It fixes what is to be computed, not how $f$ is defined: the rule of interpolation is free, and only one number is wanted out of it.

One variable at a time. A grid is a family of lines, one along each variable, and each of those lines is an ordinary one-dimensional dataset. A one-dimensional rule applied to the lines of one direction, and then to the lines of the next, reaches the query point. Chasnov states the two-variable case in full: on an $(n+1)\times(n+1)$ grid, first perform $n+1$ one-dimensional interpolations to find the interpolant at the $n+1$ points $(x_0,y^*),\cdots,(x_n,y^*)$, then a single one-dimensional interpolation to find it at $(x^*,y^*)$; MATLAB's interp2 and interp3 address exactly this problem[2].

The rule, named once. Let

$$ \mathcal{I}\big[\{v_k\}_{k=0}^{n-1}\big](t) $$

be the value at $t$ that the chosen rule gives to readings $v_0,\cdots,v_{n-1}$ at nodes $t_0,\cdots,t_{n-1}$. The rule may be piecewise linear, a cubic spline with whatever end condition (cubic spline interpolation), a monotone scheme, or a single polynomial through the whole line; only the arithmetic inside a pass changes.

Two variables. With readings $y_{i_1,i_2}$ on $n$ nodes per axis, a pass along $x_2$ holds $x_1$ at each of its nodes in turn:

$$ y^*_{i_1}=\mathcal{I}\big[\{y_{i_1,i_2}\}_{i_2=0}^{n-1}\big](x_2^*),\qquad \forall i_1\in [0,n-1],\, i_1 \in \mathbb{N} $$

The $n$ values $y^*_{i_1}$ are the interpolant at $(x_{1,i_1},x_2^*)$, i.e., readings on the line $x_2=x_2^*$, so one more pass finishes:

$$ f(x_1^*,x_2^*)\approx\mathcal{I}\big[\{y^*_{i_1}\}_{i_1=0}^{n-1}\big](x_1^*) $$

$m$ variables. Each pass removes one variable, leaving $n^{m-1}$ values, then $n^{m-2}$, down to one, so the passes cost $n^{m-1},n^{m-2},\cdots,n^0$ interpolations whatever the rule, that is $(n^m-1)/(n-1)$ in all; for $n=4$ and $m=2,3,4$ that is $5$, $21$, and $85$. A rule that reads only the nodes bracketing its target needs only the lines that bracket the target, so a local rule costs two readings a line and two lines a pass.

Any order. For a rule linear in its readings, with weights $\varphi_k$ fixed by the nodes and by the target, the two passes give

$$ f(x_1^*,x_2^*)=\sum_{i_1=0}^{n-1}\sum_{i_2=0}^{n-1}\varphi^{(1)}_{i_1}(x_1^*)\,\varphi^{(2)}_{i_2}(x_2^*)\,y_{i_1,i_2} $$

whose products are the same under either nesting, so the order of the passes is free[3].

The station grid again, by hand. Take the sixteen readings of grid interpolation and the query at $1.4^\circ$E, $51.6^\circ$N, and take the rule for a line of unit spacing to be the cubic through its four readings, written in differences:

$$ C(u)=w_0+\Delta w_0\,u+\frac{\Delta^2 w_0}{2}u(u-1)+\frac{\Delta^3 w_0}{6}u(u-1)(u-2) $$

where $u$ is the distance from the first node and $\Delta$ is the forward difference of the line's readings. On four nodes this is what a not-a-knot spline gives as well, a single cubic piece spanning the line. At $u=1.4$ the last two coefficients are $u(u-1)/2=0.28$ and $u(u-1)(u-2)/6=-0.056$, the same on every parallel, so each longitude pass costs three multiplications on top of its reading:

  • $50^\circ$N, $\Delta=(0.8,1.3,1.2)$, $\Delta^2=(0.5,-0.1)$, $\Delta^3=-0.6$: $22.8+1.4(0.8)+0.28(0.5)-0.056(-0.6)=24.0936$
  • $51^\circ$N, $\Delta=(0.8,1.4,1.1)$, $\Delta^2=(0.6,-0.3)$, $\Delta^3=-0.9$: $21.7+1.4(0.8)+0.28(0.6)-0.056(-0.9)=23.0384$
  • $52^\circ$N, $\Delta=(0.8,1.4,1.2)$, $\Delta^2=(0.6,-0.2)$, $\Delta^3=-0.8$: $20.1+1.4(0.8)+0.28(0.6)-0.056(-0.8)=21.4328$
  • $53^\circ$N, $\Delta=(0.7,1.5,1.2)$, $\Delta^2=(0.8,-0.3)$, $\Delta^3=-1.1$: $18.4+1.4(0.7)+0.28(0.8)-0.056(-1.1)=19.6656$

Those four are readings on the line $x_1=1.4$, and the same rule on them at $u=1.6$, where $u(u-1)/2=0.48$ and $u(u-1)(u-2)/6=-0.064$, gives

$$ 24.0936+1.6(-1.0552)+0.48(-0.5504)-0.064(0.3888)=22.1162048 $$

the number grid interpolation reached by summing its 16 monomials, obtained here without forming any of them.

The same passes in code, with the rule passed in as an argument:

import numpy as np
from scipy.interpolate import CubicSpline

temperature = np.array([[22.8, 23.6, 24.9, 26.1],
                        [21.7, 22.5, 23.9, 25.0],
                        [20.1, 20.9, 22.3, 23.5],
                        [18.4, 19.1, 20.6, 21.8]])   # row = latitude, column = longitude

def passes(nodes, values, point, rule):
    """Value at `point` of the grid interpolant, one one-dimensional pass per variable.

    nodes  -- node coordinates of each variable, in the order of the axes of `values`
    values -- m-dimensional array of readings, values[i_1, ..., i_m]
    rule   -- rule(nodes, readings, t), the one-dimensional rule evaluated at t
    """
    data = np.asarray(values, dtype=float)
    for axis, target in enumerate(point):         # one pass per variable, first axis first
        data = np.apply_along_axis(
            lambda line: rule(np.asarray(nodes[axis], dtype=float), line, target), 0, data)
    return float(data)

linear  = lambda nodes, line, t: float(np.interp(t, nodes, line))
spline  = lambda nodes, line, t: float(CubicSpline(nodes, line)(t))
natural = lambda nodes, line, t: float(CubicSpline(nodes, line, bc_type="natural")(t))

latitude, longitude = np.arange(50.0, 54.0), np.arange(0.0, 4.0)
for name, rule in [("piecewise linear", linear), ("natural cubic spline", natural),
                   ("not-a-knot cubic spline", spline)]:
    print(name, passes([latitude, longitude], temperature, [51.6, 1.4], rule))

The spline line prints the value worked out above; the natural end condition gives $22.1157$ instead, and swapping the two variables in nodes and in point alike, with the readings transposed, returns the same three values.

A rule that reads only the nodes bracketing its target does far less of this arithmetic. Piecewise linear passes at the same point touch only the four stations surrounding it: $0.6(22.5)+0.4(23.9)=23.06$ and $0.6(20.9)+0.4(22.3)=21.46$ along the parallels, then $0.4(23.06)+0.6(21.46)=22.10$ along the meridian. That is $0.02\ ^\circ$C from the grid interpolant, but the two are not interchangeable: the bilinear interpolant is merely continuous where cells meet, whereas the grid interpolant is a single polynomial and therefore smooth everywhere.

References