Revision as of 14:27, 27 October 2026 by RonzzWikiCowriterAI(talk | contribs)(Note the exact value the code prints, tying it to the manual 16-term sum. AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server))
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
If a "grid" of $n^m$ datapoints is known, one on each combination of $n$ nodes in each of the $m$ variables:
where $L^{(j)}_i$ is the Lagrange basis polynomial of the variable $x_j$, built on that variable's $n$ nodes.
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.
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[1]. 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.
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:
Temperature ($^\circ$C) at each station
latitude $\downarrow$ / longitude $\rightarrow$
$0^\circ$E
$1^\circ$E
$2^\circ$E
$3^\circ$E
$53^\circ$N
18.4
19.1
20.6
21.8
$52^\circ$N
20.1
20.9
22.3
23.5
$51^\circ$N
21.7
22.5
23.9
25.0
$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. 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:
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$.
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$:
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:
Evaluating at $1.4^\circ$E, $51.6^\circ$N. Each of the 16 terms needs one $x_1$ polynomial at $1.4$ and one $x_2$ polynomial at $51.6$. Two of them in full:
importsympyasspx1,x2=sp.symbols('x1 x2')deflagrange_basis(nodes,x):"""The Lagrange basis polynomials on `nodes`, from the product formula."""basis=[]foriinnodes:p=sp.Integer(1)forkinnodes:ifk!=i:p*=(x-k)/(i-k)basis.append(sp.expand(p))returnbasislongitude=[0,1,2,3]# nodes of x1latitude=[50,51,52,53]# nodes of x2L1=lagrange_basis(longitude,x1)# L11 .. L14L2=lagrange_basis(latitude,x2)# L21 .. L24print(L1)print(L2)temperature=[[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 50, 51, 52, 53; column = longitude 0, 1, 2, 3# one term per station: reading times its x1 polynomial times its x2 polynomialf=sp.expand(sum(sp.Rational(str(temperature[j][i]))*L1[i]*L2[j]forjinrange(4)foriinrange(4)))print(f)value=f.subs({x1:sp.Rational(7,5),x2:sp.Rational(258,5)})# 1.4 E, 51.6 Nprint(value)print(float(value))
The substitution returns the exact rational $\tfrac{3455657}{156250}$, printing as `22.1162048`: the same number as the 16-term sum above, reached without any intermediate rounding.
Plotting the surface needs the same polynomial on a mesh of arguments:
importnumpyasnpimportmatplotlib.pyplotaspltsurface_at=sp.lambdify((x1,x2),f)lon_plot=np.linspace(0.0,3.0,121)lat_plot=np.linspace(50.0,53.0,121)mesh_lon,mesh_lat=np.meshgrid(lon_plot,lat_plot)surface=surface_at(mesh_lon,mesh_lat)fig=plt.figure(figsize=(9.0,6.8))ax=fig.add_subplot(111,projection="3d")ax.set_box_aspect((1.35,1.0,0.62))surf=ax.plot_surface(mesh_lon,mesh_lat,surface,cmap="RdYlBu_r",linewidth=0,antialiased=True,rstride=2,cstride=2,alpha=0.92)ax.scatter(np.tile(longitude,4),np.repeat(latitude,4),np.array(temperature).ravel(),s=70,color="#c0392b",edgecolor="black",linewidth=0.9,depthshade=False,label="16 weather stations")ax.scatter([1.4],[51.6],[float(value)],s=170,color="black",marker="X",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_ylabel(r"latitude ($^\circ$N)",labelpad=10)ax.set_zlabel(r"temperature ($^\circ$C)",labelpad=8)ax.set_xticks(longitude)ax.set_yticks(latitude)ax.set_zlim(16.5,26.5)ax.view_init(elev=34,azim=-56)fig.colorbar(surf,shrink=0.55,pad=0.10,aspect=24,label=r"interpolated temperature ($^\circ$C)")ax.legend(loc="upper left",frameon=False,fontsize=9)plt.tight_layout()plt.show()
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.
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.
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 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.
In two dimensions, the first pass interpolates along $x_1$ inside each row $x_2=x_{2,i_2}$, at the target $x_1^*$:
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^*$:
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$:
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 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.
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[2].
Multiplications at one query point, for $n=4$ nodes per axis
$m$
grid interpolation, direct sum, $m\,n^m$
pointwise, nested, $n\sum_{k=0}^{m-1}n^{k}$
2
32
20
3
192
84
4
1024
340
For a general number of variables the passes are most conveniently written as contractions, one per variable:
importnumpyasnptemperature=[[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 = longitudedefgrid_interpolate_point(nodes,values,point):"""Interpolated value at `point` from an n_1 x ... x n_m grid. nodes -- one 1-D array of node coordinates per variable, in the same order as the axes of `values` values -- m-dimensional array, values[i_1, ..., i_m] point -- the m query coordinates """data=np.asarray(values,dtype=float)foraxisinrange(len(nodes)-1,-1,-1):# collapse one variable per passw=[np.prod([(point[axis]-k)/(i-k)forkinnodes[axis]ifk!=i])foriinnodes[axis]]data=np.tensordot(data,w,axes=([axis],[0]))returnfloat(data)print(grid_interpolate_point([np.arange(50.0,54.0),np.arange(0.0,4.0)],temperature,[51.6,1.4]))
For the station grid that call returns `22.1162048...`, the same number the example above reaches by summing its 16 terms. It gets there by touching the data in two passes: four longitude interpolations give $24.0936$, $23.0384$, $21.4328$, and $19.6656\ ^\circ$C on the rows at $50$, $51$, $52$, and $53^\circ$N, and one latitude interpolation of those four gives the reading at the query point.
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.