Jump to content

Multidimensional interpolation: Difference between revisions

From Wikibase
Rework the example: restate the basis cubics, develop the polynomial in coefficient form along each parallel, then evaluate at (1.4, 51.6). AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
 
(88 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''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$.
'''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 ==


=== Grid interpolation ===
=== via Lagrange polynomial in multiple variables ===


If a "grid" of $n^m$ datapoints is known, one on each combination of $n$ nodes in each of the $m$ 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 11:
$$
$$


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$.


$$
$$
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}}
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_j}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i_j}-x_{j,k}}
$$
$$


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 is a multi-variable polynomial of degree at most $n-1$ in each variable unique for each set of 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.
Similar to the [[Polynomial_interpolation#Lagrange_polynomial|one-dimensional Lagrange polynomial]]s,


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.
$$
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 \neq i_j,\, i_k \in \mathbb{N}
\end{cases}
$$
 
which guarantees that $P(x_1,\cdots,x_m)=y(x_1,\cdots,x_m)$ at all datapoints.<ref>{{#cite:Q2392}}</ref>
 
<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 measure surface temperature at various latitudes and longitudes.


{| class="wikitable"
{| class="wikitable"
Line 30: Line 44:
! latitude $\downarrow$ / longitude $\rightarrow$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
! latitude $\downarrow$ / longitude $\rightarrow$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
|-
|-
! $53^\circ$N
! $50^\circ$N
| 18.4 || 19.1 || 20.6 || 21.8
| 22.8 || 23.6 || 24.9 || 26.1
|-
! $51^\circ$N
| 21.7 || 22.5 || 23.9 || 25.0
|-
|-
! $52^\circ$N
! $52^\circ$N
| 20.1 || 20.9 || 22.3 || 23.5
| 20.1 || 20.9 || 22.3 || 23.5
|-
|-
! $51^\circ$N
! $53^\circ$N
| 21.7 || 22.5 || 23.9 || 25.0
| 18.4 || 19.1 || 20.6 || 21.8
|-
! $50^\circ$N
| 22.8 || 23.6 || 24.9 || 26.1
|}
|}


With $m=2$ and $n=4$ the sum has $n^m=16$ terms, one per station. Write $x_1$ for longitude and $x_2$ for latitude, and set $t=x_2-50$ so that the latitude nodes sit at $0,1,2,3$ like the longitude ones; the table's order is kept with the latitude row first, so $i_2=0$ is the row at $50^\circ$N and $i_1=0$ the column at $0^\circ$E:
What is the interpolated temperature at $51.6^\circ \textbf{N}, 1.4^\circ \textbf{E}$?
 
'''Algebraic solution''':
 
Let $x_1$ represent the latitude, $x_2$ represent the longitude, and denote the surface temperature reading at row $i_1$, column $i_2$ by $y_{i_1,i_2}$.


Then the interpolating multi-variable Lagrange polynomial is
$$
$$
f(x_1,x_2)=\sum_{i_1=0}^{3}\sum_{i_2=0}^{3}y_{i_2,i_1}\,L_{i_1}(x_1)\,L_{i_2}(t)
P(x_1,x_2)=\sum_{i_1=1}^{4}\sum_{i_2=1}^{4}y_{i_1,i_2}\,L^{(x_1)}_{i_1}(x_1)\,L^{(x_2)}_{i_2}(x_2)
$$
$$


'''The basis polynomials.''' The product formula on the nodes $0,1,2,3$ gives four cubics, which serve both variables, in $x_1$ and in $t$:
where


$$
$$
L_0(x)=-\frac{(x-1)(x-2)(x-3)}{6}=-\frac{1}{6}x^3+x^2-\frac{11}{6}x+1,\qquad L_1(x)=\frac{x(x-2)(x-3)}{2}=\frac{1}{2}x^3-\frac{5}{2}x^2+3x
L^{(x_1)}_{1}(x_1)=\frac{(x_1-51)(x_1-52)(x_1-53)}{(50-51)(50-52)(50-53)}=-\frac{1}{6}x_1^3+26x_1^2-\frac{8111}{6}x_1+23426,\\
L^{(x_1)}_{2}(x_1)=\frac{(x_1-50)(x_1-52)(x_1-53)}{(51-50)(51-52)(51-53)}=\frac{1}{2}x_1^3-\frac{155}{2}x_1^2+4003x_1-68900
$$
$$


$$
$$
L_2(x)=-\frac{x(x-1)(x-3)}{2}=-\frac{1}{2}x^3+2x^2-\frac{3}{2}x,\qquad L_3(x)=\frac{x(x-1)(x-2)}{6}=\frac{1}{6}x^3-\frac{1}{2}x^2+\frac{1}{3}x
L^{(x_1)}_{3}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-53)}{(52-50)(52-51)(52-53)}=-\frac{1}{2}x_1^3+77x_1^2-\frac{7903}{2}x_1+67575,\\
L^{(x_1)}_{4}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-52)}{(53-50)(53-51)(53-52)}=\frac{1}{6}x_1^3-\frac{51}{2}x_1^2+\frac{3901}{3}x_1-22100
$$
$$


'''The polynomial, developed along each parallel.''' Collecting the terms of each latitude row turns $f$ into a cubic in $x_1$ whose four coefficients are the latitude cubics:
$$
L^{(x_2)}_{1}(x_2)=\frac{(x_2-1)(x_2-2)(x_2-3)}{(0-1)(0-2)(0-3)}=-\frac{1}{6}x_2^3+x_2^2-\frac{11}{6}x_2+1,\qquad L^{(x_2)}_{2}(x_2)=\frac{x_2(x_2-2)(x_2-3)}{(1-0)(1-2)(1-3)}=\frac{1}{2}x_2^3-\frac{5}{2}x_2^2+3x_2
$$


$$
$$
f(x_1,x_2)=Q_0(x_1)L_0(t)+Q_1(x_1)L_1(t)+Q_2(x_1)L_2(t)+Q_3(x_1)L_3(t),\qquad Q_{i_2}(x_1)=\sum_{i_1=0}^{3}y_{i_2,i_1}L_{i_1}(x_1)
L^{(x_2)}_{3}(x_2)=\frac{x_2(x_2-1)(x_2-3)}{(2-0)(2-1)(2-3)}=-\frac{1}{2}x_2^3+2x_2^2-\frac{3}{2}x_2,\qquad L^{(x_2)}_{4}(x_2)=\frac{x_2(x_2-1)(x_2-2)}{(3-0)(3-1)(3-2)}=\frac{1}{6}x_2^3-\frac{1}{2}x_2^2+\frac{1}{3}x_2
$$
$$


so $Q_{i_2}$ is the cubic through the four readings of the parallel at $x_{2,i_2}$. Carrying out the four sums with the cubics above:
Hence


{| class="wikitable"
$$
|+ The parallel cubics, in expanded form
P(x_1,x_2)=22.8\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{1}(x_2)+23.6\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{2}(x_2)+24.9\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{3}(x_2)+26.1\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{4}(x_2)
! parallel !! $Q_{i_2}(x_1)$
$$
|-
! $50^\circ$N
| $-\frac{1}{10}x_1^3+\frac{11}{20}x_1^2+\frac{7}{20}x_1+\frac{114}{5}$
|-
! $51^\circ$N
| $-\frac{3}{20}x_1^3+\frac{3}{4}x_1^2+\frac{1}{5}x_1+\frac{217}{10}$
|-
! $52^\circ$N
| $-\frac{2}{15}x_1^3+\frac{7}{10}x_1^2+\frac{7}{30}x_1+\frac{201}{10}$
|-
! $53^\circ$N
| $-\frac{11}{60}x_1^3+\frac{19}{20}x_1^2-\frac{1}{15}x_1+\frac{92}{5}$
|}


Each constant term is that parallel's reading at $0^\circ$E, as it must be. In the same way $f$ is a cubic in $t$ whose coefficients are four longitude cubics, so its degree is at most $3$ in each variable.
$$
{}+21.7\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{1}(x_2)+22.5\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{2}(x_2)+23.9\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{3}(x_2)+25.0\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{4}(x_2)
$$


'''Evaluating at $1.4^\circ$E, $51.6^\circ$N.''' Substitute the point into that polynomial, one variable at a time. The outer variable first: $x_1=1.4$ needs three basis values and then the four parallels. For instance
$$
{}+20.1\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{1}(x_2)+20.9\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{2}(x_2)+22.3\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{3}(x_2)+23.5\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{4}(x_2)
$$


$$
$$
L_1(1.4)=\frac{1.4(1.4-2)(1.4-3)}{2}=\frac{1.4\cdot(-0.6)\cdot(-1.6)}{2}=\frac{1.344}{2}=0.672
{}+18.4\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{1}(x_2)+19.1\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{2}(x_2)+20.6\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{3}(x_2)+21.8\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{4}(x_2)
$$
$$


and the same substitution in the other three gives
On simplification:
 
{| class="wikitable"
|+ The basis polynomials at the two arguments
! $i$ !! $L_i(1.4)$ !! $L_i(1.6)$
|-
! 0
| $-0.064$ || $-0.056$
|-
! 1
| $0.672$ || $0.448$
|-
! 2
| $0.448$ || $0.672$
|-
! 3
| $-0.056$ || $-0.064$
|}
 
Each column sums to $1$, as the basis must, since it reproduces constants. Substituting the first column into $Q_{i_2}$ gives the four parallels at $1.4^\circ$E. For the row at $50^\circ$N, the expanded form and the basis values agree:


$$
$$
Q_0(1.4)=-\tfrac{1}{10}(2.744)+\tfrac{11}{20}(1.96)+\tfrac{7}{20}(1.4)+22.8=-0.2744+1.078+0.49+22.8=24.0936
P(x_1,x_2)=-\tfrac{1}{45}x_1^3x_2^3+\tfrac{11}{120}x_1^3x_2^2-\tfrac{31}{360}x_1^3x_2+\tfrac{1}{15}x_1^3+\tfrac{103}{30}x_1^2x_2^3-\tfrac{283}{20}x_1^2x_2^2+\tfrac{199}{15}x_1^2x_2-\tfrac{209}{20}x_1^2
$$
$$


$$
$$
Q_0(1.4)=-0.064\cdot22.8+0.672\cdot23.6+0.448\cdot24.9-0.056\cdot26.1=24.0936
{}-\tfrac{31823}{180}x_1x_2^3+\tfrac{87361}{120}x_1x_2^2-\tfrac{245249}{360}x_1x_2+\tfrac{32657}{60}x_1+\tfrac{45511}{15}x_2^3-\tfrac{62416}{5}x_2^2+\tfrac{174899}{15}x_2-\tfrac{93997}{10}
$$
$$


and the basis values are the quicker route for the other three rows:
Substitute in $x_1=51.6,x_2=1.4$,


$$
$$
Q_1(1.4)=-0.064\cdot21.7+0.672\cdot22.5+0.448\cdot23.9-0.056\cdot25.0=23.0384
y(51.6,1.4)=P(51.6,1.4)=-\tfrac{1}{45}(51.6)^3(1.4)^3+\tfrac{11}{120}(51.6)^3(1.4)^2-\tfrac{31}{360}(51.6)^3(1.4)+\tfrac{1}{15}(51.6)^3
$$
$$


$$
$$
Q_2(1.4)=-0.064\cdot20.1+0.672\cdot20.9+0.448\cdot22.3-0.056\cdot23.5=21.4328
{}+\tfrac{103}{30}(51.6)^2(1.4)^3-\tfrac{283}{20}(51.6)^2(1.4)^2+\tfrac{199}{15}(51.6)^2(1.4)-\tfrac{209}{20}(51.6)^2
$$
$$


$$
$$
Q_3(1.4)=-0.064\cdot18.4+0.672\cdot19.1+0.448\cdot20.6-0.056\cdot21.8=19.6656
{}-\tfrac{31823}{180}(51.6)(1.4)^3+\tfrac{87361}{120}(51.6)(1.4)^2-\tfrac{245249}{360}(51.6)(1.4)+\tfrac{32657}{60}(51.6)
$$
$$
The inner variable is $t=51.6-50=1.6$, whose four basis values are the second column above; for instance $L_1(1.6)=\frac{1.6\cdot(-0.4)\cdot(-1.4)}{2}=\frac{0.896}{2}=0.448$. Substituting both columns into the outer sum of the polynomial:


$$
$$
f(1.4,51.6)=24.0936(-0.056)+23.0384(0.448)+21.4328(0.672)+19.6656(-0.064)=22.1162
{}+\tfrac{45511}{15}(1.4)^3-\tfrac{62416}{5}(1.4)^2+\tfrac{174899}{15}(1.4)-\tfrac{93997}{10}=22.1162
$$
$$


The same arithmetic in code:
'''Numerical solution''':


<syntaxhighlight lang="python" copy>
<syntaxhighlight lang="python" copy>
import numpy as np
import sympy as sp
 
x1, x2 = sp.symbols('x1 x2')
 
def lagrange_basis(nodes, x):
    """The Lagrange basis polynomials on `nodes`, from the product formula."""
    basis = []
    for i in nodes:
        p = sp.Integer(1)
        for k in nodes:
            if k != i:
                p *= (x - k) / (i - k)
        basis.append(sp.expand(p))
    return basis


def lagrange_weights(x_nodes, x):
latitude = [50, 51, 52, 53]                # nodes of x1
    """Weights w_i such that sum(w_i * f(x_i)) interpolates f at x."""
longitude = [0, 1, 2, 3]                    # nodes of x2
    x_nodes = np.asarray(x_nodes, dtype=float)
L1 = lagrange_basis(latitude, x1)           # L^(x1)_1 .. L^(x1)_4
    w = np.ones_like(x_nodes)
L2 = lagrange_basis(longitude, x2)         # L^(x2)_1 .. L^(x2)_4
    for i in range(x_nodes.size):
print(L1)
        for j in range(x_nodes.size):
print(L2)
            if i != j:
                w[i] *= (x - x_nodes[j]) / (x_nodes[i] - x_nodes[j])
    return w


longitude = np.array([0.0, 1.0, 2.0, 3.0])          # degrees east
temperature = [[22.8, 23.6, 24.9, 26.1],
latitude = np.array([50.0, 51.0, 52.0, 53.0])        # degrees north
              [21.7, 22.5, 23.9, 25.0],
temperature = np.array([[22.8, 23.6, 24.9, 26.1],
              [20.1, 20.9, 22.3, 23.5],
                        [21.7, 22.5, 23.9, 25.0],
              [18.4, 19.1, 20.6, 21.8]]   # row = latitude 50, 51, 52, 53; column = longitude 0, 1, 2, 3
                        [20.1, 20.9, 22.3, 23.5],
                        [18.4, 19.1, 20.6, 21.8]])  # row = latitude, column = longitude


print(lagrange_weights(longitude, 1.4) * 125)        # [-8. 84. 56. -7.]
# one term per station: reading times its x1 polynomial times its x2 polynomial
print(lagrange_weights(latitude, 51.6) * 125)       # [-7. 56. 84. -8.]
p = sp.expand(sum(sp.Rational(str(temperature[j][i])) * L1[j] * L2[i]
                  for j in range(4) for i in range(4)))
print(p)


rows = np.array([lagrange_weights(longitude, 1.4) @ temperature[i] for i in range(4)])
value = p.subs({x1: sp.Rational('51.6'), x2: sp.Rational('1.4')})   # 51.6 N, 1.4 E
print(rows)                                         # [24.0936 23.0384 21.4328 19.6656]
print(value)
print(lagrange_weights(latitude, 51.6) @ rows)       # 22.1162...
print(float(value))
</syntaxhighlight>
</syntaxhighlight>


Plotting the whole surface only needs the same weights, taken in pairs:
'''Extra''': To plot the interpolating surface


<syntaxhighlight lang="python" copy>
<syntaxhighlight lang="python" copy>
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
latitude = np.array([50.0, 51.0, 52.0, 53.0])        # nodes of x1
longitude = np.array([0.0, 1.0, 2.0, 3.0])          # nodes of x2
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 L(nodes, x):
    """The Lagrange basis polynomials on `nodes`, evaluated at x."""
    return np.array([np.prod([(x - k) / (i - k) for k in nodes if k != i]) for i in nodes])
def p(x1, x2):
    """2-variable Lagrange polynomial"""
    return (temperature * np.outer(L(latitude, x1), L(longitude, x2))).sum()


lon_plot = np.linspace(0.0, 3.0, 121)
lon_plot = np.linspace(0.0, 3.0, 121)
lat_plot = np.linspace(50.0, 53.0, 121)
lat_plot = np.linspace(50.0, 53.0, 121)
surface = np.array([[lagrange_weights(latitude, y) @ temperature @ lagrange_weights(longitude, x)
surface = np.array([[p(x, y) for y in lon_plot] for x in lat_plot])
                    for x in lon_plot] for y in lat_plot])


fig = plt.figure(figsize=(9.0, 6.8))
fig = plt.figure(figsize=(9.0, 6.8))
Line 190: Line 208:
           s=70, color="#c0392b", edgecolor="black", linewidth=0.9,
           s=70, color="#c0392b", edgecolor="black", linewidth=0.9,
           depthshade=False, label="16 weather stations")
           depthshade=False, label="16 weather stations")
ax.scatter([1.4], [51.6], [22.116], s=170, color="black", marker="X",
ax.scatter([1.4], [51.6], [p(51.6, 1.4)], s=170, color="black", marker="X",
           depthshade=False, label=r"query point (1.4$^\circ$E, 51.6$^\circ$N)")
           depthshade=False, label=r"query point (1.4$^\circ$E, 51.6$^\circ$N)")
ax.set_xlabel(r"longitude ($^\circ$E)", labelpad=10)
ax.set_xlabel(r"longitude ($^\circ$E)", labelpad=10)
Line 206: Line 224:
</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 that direction.
{{End hidden}}
{{End hidden}}


=== Pointwise interpolation ===
== Pointwise interpolation ==
 
Pointwise interpolation estimates the value of $y$ at one particular point, $y(x_1^*,\cdots,x_m^*)$, without constructing a general interpolating function.


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 uses the same grid of $n^m$ datapoints and the same basis polynomials, but regroups the sum one variable at a time instead of writing out its $n^m$ terms.
=== via successive 1D interpolation in each variable ===


In two dimensions, the first pass interpolates along $x_1$ inside each row $x_2=x_{2,i_2}$, at the target $x_1^*$:
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


$$
$$
y^*_{i_2}=\sum_{i_1=0}^{n-1}L^{(1)}_{i_1}(x_1^*)\,y_{i_1,i_2},\qquad \forall i_2\in [0,n-1], i_2 \in \mathbb{N}
\{(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}\}
$$
$$


The $n$ intermediate values $y^*_{i_2}$ sit at the coordinates $(x_1^*,x_{2,i_2})$, i.e., on a one-dimensional grid in $x_2$, so a second pass interpolates them at $x_2^*$:
is known, then first perform $n^{m-1}$ 1D interpolations in $x_1$, fixing $x_2,\cdots,x_m$ at all known values, obtaining $n^{m-1}$ interpolating functions $f^{(x_1)}_{i_2,i_3,\cdots,i_m}(x_1)$
 
Now evaluate each $f^{(x_1)}$ at $x_1=x_1^*$ to obtain $n^{m-1}$ interpolated datapoints


$$
$$
f(x_1^*,x_2^*)=\sum_{i_2=0}^{n-1}L^{(2)}_{i_2}(x_2^*)\,y^*_{i_2}
\{(x_1^*,x_{2,i_2},\cdots,x_{m,i_m},y_{x_1^*,i_2,\cdots,i_m})\mid i_2,\cdots,i_m \in [0,n-1],\, i_2,\cdots,i_m \in \mathbb{N}\}
$$
$$


The first pass costs $n$ one-dimensional interpolations and the second costs $1$. In $m$ dimensions the passes shrink by one variable each time, costing $n^{m-1}, n^{m-2}, \cdots, n^0$:
Then perform $n^{m-2}$ 1D interpolations in $x_2$, fixing $x_3,\cdots,x_m$ at all known values, obtaining $n^{m-2}$ interpolating functions $f^{(x_2)}_{i_3,\cdots,i_m}(x_2)$
 
Now evaluate each $f^{(x_2)}$ at $x_2=x_2^*$ to obtain $n^{m-2}$ interpolated datapoints


$$
$$
\sum_{k=0}^{m-1}n^{k}=\frac{n^{m}-1}{n-1}
\{(x_1^*,x_2^*,x_{3,i_3}, \cdots,x_{m,i_m},y_{x_1^*,x_2^*,i_3,\cdots,i_m})\mid i_3,\cdots,i_m \in [0,n-1],\, i_3,\cdots,i_m \in \mathbb{N}\}
$$
$$


and the passes are exactly the nested form
Then perform $n^{m-3}$ 1D interpolations in $x_3$, $n^{m-4}$ 1D interpolations in $x_4$, $\ldots$, $n$ 1D interpolations in $x_{m-1}$, and finally $1$ 1D interpolation in $x_m$ to obtain one interpolating function $f^{(x_m)}$.
 
Then the estimate of $y$ at $(x_1^*,\cdots,x_m^*)$ is $f^{(x_m)}(x_m^*)$.<ref>{{#cite:Q2421}}</ref>


$$
{{Hidden example}}
f(x_1^*,\cdots,x_m^*)=\sum_{i_m=0}^{n-1}L^{(m)}_{i_m}(x_m^*)\sum_{i_{m-1}=0}^{n-1}L^{(m-1)}_{i_{m-1}}(x_{m-1}^*)\cdots\sum_{i_1=0}^{n-1}L^{(1)}_{i_1}(x_1^*)\,y_{i_1,\cdots,i_m}
'''Surface temperature over a grid of weather stations (particular value by successive 1D interpolations)'''
$$


Each of those weighted sums costs $n$ multiplications, so the nested passes cost $n(n^{m}-1)/(n-1)$ in all. That is fewer than the $m\,n^m$ multiplications needed to sum the $n^m$ terms of [[#Grid_interpolation|grid interpolation]] term by term, because each pass collapses one variable: the first pass does range over the $n^{m-1}$ combinations of known nodes, as a direct sum does, but afterwards one variable is pinned at the target $x_j^*$ and contributes one position instead of $n$. Nothing is thrown away: the same $n^m$ terms are summed either way, and the two counts answer different questions, the value at one point and the whole surface.
Retake the same example as in [[#Grid interpolation]]:


The order of the passes does not matter. Collapsing $x_1$ first and then $x_2$ gives the same result as collapsing $x_2$ first and then $x_1$, because the tensor product sum can be regrouped; every ordering evaluates the same polynomial as [[#Grid_interpolation|grid interpolation]]<ref>{{#cite:Q2391}}</ref>.
Sixteen weather stations sitting in a $4^\circ \times 4^\circ$ grid measure surface temperature at various latitudes and longitudes.


{| class="wikitable"
{| class="wikitable"
|+ Multiplications at one query point, for $n=4$ nodes per axis
|+ Temperature ($^\circ$C) at each station
! $m$ !! grid interpolation, direct sum, $m\,n^m$ !! pointwise, nested, $n\sum_{k=0}^{m-1}n^{k}$
! latitude $\downarrow$ / longitude $\rightarrow$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
|-
! $50^\circ$N
| 22.8 || 23.6 || 24.9 || 26.1
|-
|-
! 2
! $51^\circ$N
| 32 || 20
| 21.7 || 22.5 || 23.9 || 25.0
|-
|-
! 3
! $52^\circ$N
| 192 || 84
| 20.1 || 20.9 || 22.3 || 23.5
|-
|-
! 4
! $53^\circ$N
| 1024 || 340
| 18.4 || 19.1 || 20.6 || 21.8
|}
|}


For a general number of variables the passes are most conveniently written as contractions, one per variable:
What is the interpolated temperature at $51.6^\circ \textbf{N}, 1.4^\circ \textbf{E}$?
 
'''Algebraic solution''':


<syntaxhighlight lang="python" copy>
'''Step 1: interpolate in $x_1$ (latitude).''' Interpolate the four readings of different latitudes with the one-dimensional Lagrange polynomial at each of the four known longitudes, then evaluate the interpolating functions at $x_1^*=51.6$.  
def grid_interpolate_point(axes, values, point):
    """Interpolated value at `point` from an n_1 x ... x n_m grid.


    axes  -- one 1-D array of node coordinates per variable, in the same order
$$
              as the axes of `values`
L^{(x_1)}_{1}(x_1)=\frac{(x_1-51)(x_1-52)(x_1-53)}{(50-51)(50-52)(50-53)},\qquad L^{(x_1)}_{2}(x_1)=\frac{(x_1-50)(x_1-52)(x_1-53)}{(51-50)(51-52)(51-53)}
    values -- m-dimensional array, values[i_1, ..., i_m]
$$
    point  -- the m query coordinates
    """
    data = np.asarray(values, dtype=float)
    for axis in range(len(axes) - 1, -1, -1):        # collapse one variable per pass
        data = np.tensordot(data, lagrange_weights(axes[axis], point[axis]),
                            axes=([axis], [0]))
    return float(data)
</syntaxhighlight>


For the station grid, `grid_interpolate_point([latitude, longitude], temperature, [51.6, 1.4])` returns `22.1162048...`, which is the two-pass calculation of the example above: four longitude interpolations give $24.094$, $23.038$, $21.433$, and $19.666\ ^\circ$C on the rows at $50$, $51$, $52$, and $53^\circ$N, and one latitude interpolation of those four values gives the reading at the query point.
$$
L^{(x_1)}_{3}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-53)}{(52-50)(52-51)(52-53)},\qquad L^{(x_1)}_{4}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-52)}{(53-50)(53-51)(53-52)}
$$


<blockquote>
<blockquote>
Because these readings happen to be close to planar, bilinear interpolation of the four stations surrounding the query point gives $22.100\ ^\circ$C, only $0.02\ ^\circ$C away from the grid interpolant. The two are not interchangeable, however: the bilinear interpolant is merely continuous where cells meet, whereas the grid interpolant is a single polynomial and therefore smooth everywhere.
The four Lagrange basis polynomials in $x_1$, built on the latitude nodes $50,51,52,53^\circ \textbf{N}$, are the same at every longitude.
</blockquote>
</blockquote>
At $x_1^*=51.6$,
$$
L^{(x_1)}_{1}(51.6)=-0.056,\qquad L^{(x_1)}_{2}(51.6)=0.448,\qquad L^{(x_1)}_{3}(51.6)=0.672,\qquad L^{(x_1)}_{4}(51.6)=-0.064
$$
Hence the four interpolated readings
$$
y_{x_1^*,i_2}=\sum_{i_1=1}^{4}y_{i_1,i_2}\,L^{(x_1)}_{i_1}(51.6),\qquad i_2=1,2,3,4
$$
$$
y_{x_1^*,1}=-0.056(22.8)+0.448(21.7)+0.672(20.1)-0.064(18.4)=20.7744,\\
y_{x_1^*,2}=-0.056(23.6)+0.448(22.5)+0.672(20.9)-0.064(19.1)=21.5808,\\
y_{x_1^*,3}=-0.056(24.9)+0.448(23.9)+0.672(22.3)-0.064(20.6)=22.9800,\\
y_{x_1^*,4}=-0.056(26.1)+0.448(25.0)+0.672(23.5)-0.064(21.8)=24.1352
$$
{| class="wikitable"
|+ Interpolated temperature ($^\circ$C) at $51.6^\circ$N
! longitude $x_{2,i_2}$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
|-
! $y_{x_1^*,i_2}$
| 20.7744 || 21.5808 || 22.9800 || 24.1352
|}
'''Step 2: interpolate in $x_2$ (longitude).''' Interpolate the four interpolated readings $y_{51.6^\circ \textbf{N},i_2}$ with a one-dimensional Lagrange polynomial in $x_2$ (longitude$) to obtain the interpolating function $f^{(x_2)}(x_2)$. Evaluate $f$ at $x_2^*=1.4$ for $y(51.6,1.4)$.
Four Lagrange basis polynomials, built on the longitude nodes $0,1,2,3^\circ \textbf{E}$:
$$
L^{(x_2)}_{1}(x_2)=\frac{(x_2-1)(x_2-2)(x_2-3)}{(0-1)(0-2)(0-3)},\qquad L^{(x_2)}_{2}(x_2)=\frac{x_2(x_2-2)(x_2-3)}{(1-0)(1-2)(1-3)}
$$
$$
L^{(x_2)}_{3}(x_2)=\frac{x_2(x_2-1)(x_2-3)}{(2-0)(2-1)(2-3)},\qquad L^{(x_2)}_{4}(x_2)=\frac{x_2(x_2-1)(x_2-2)}{(3-0)(3-1)(3-2)}
$$
At $x_2^*=1.4$,
$$
L^{(x_2)}_{1}(1.4)=-0.064,\qquad L^{(x_2)}_{2}(1.4)=0.672,\qquad L^{(x_2)}_{3}(1.4)=0.448,\qquad L^{(x_2)}_{4}(1.4)=-0.056
$$
$$
y(51.6,1.4)=f^{(x_2)}(1.4)=\sum_{i_2=1}^{4}y_{x_1^*,i_2}\,L^{(x_2)}_{i_2}(1.4)=-0.064(20.7744)+0.672(21.5808)+0.448(22.9800)-0.056(24.1352)
$$
$$
{}=22.1162
$$
which agrees with the value obtained in [[#Grid interpolation]].
{{End hidden}}


== References ==
== References ==


<references/>
<references/>

Latest revision as of 13:27, 28 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$.

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_j}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i_j}-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 the one-dimensional Lagrange polynomials,

$$ 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 \neq i_j,\, i_k \in \mathbb{N} \end{cases} $$

which guarantees that $P(x_1,\cdots,x_m)=y(x_1,\cdots,x_m)$ at all datapoints.[1]

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

Pointwise interpolation

Pointwise interpolation estimates the value of $y$ at one particular point, $y(x_1^*,\cdots,x_m^*)$, without constructing a general interpolating function.

via successive 1D interpolation in each variable

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 first perform $n^{m-1}$ 1D interpolations in $x_1$, fixing $x_2,\cdots,x_m$ at all known values, obtaining $n^{m-1}$ interpolating functions $f^{(x_1)}_{i_2,i_3,\cdots,i_m}(x_1)$

Now evaluate each $f^{(x_1)}$ at $x_1=x_1^*$ to obtain $n^{m-1}$ interpolated datapoints

$$ \{(x_1^*,x_{2,i_2},\cdots,x_{m,i_m},y_{x_1^*,i_2,\cdots,i_m})\mid i_2,\cdots,i_m \in [0,n-1],\, i_2,\cdots,i_m \in \mathbb{N}\} $$

Then perform $n^{m-2}$ 1D interpolations in $x_2$, fixing $x_3,\cdots,x_m$ at all known values, obtaining $n^{m-2}$ interpolating functions $f^{(x_2)}_{i_3,\cdots,i_m}(x_2)$

Now evaluate each $f^{(x_2)}$ at $x_2=x_2^*$ to obtain $n^{m-2}$ interpolated datapoints

$$ \{(x_1^*,x_2^*,x_{3,i_3}, \cdots,x_{m,i_m},y_{x_1^*,x_2^*,i_3,\cdots,i_m})\mid i_3,\cdots,i_m \in [0,n-1],\, i_3,\cdots,i_m \in \mathbb{N}\} $$

Then perform $n^{m-3}$ 1D interpolations in $x_3$, $n^{m-4}$ 1D interpolations in $x_4$, $\ldots$, $n$ 1D interpolations in $x_{m-1}$, and finally $1$ 1D interpolation in $x_m$ to obtain one interpolating function $f^{(x_m)}$.

Then the estimate of $y$ at $(x_1^*,\cdots,x_m^*)$ is $f^{(x_m)}(x_m^*)$.[2]

References