関数interpolate_3d_data_1p マニュアル

(The documentation of function interpolate_3d_data_1p)

Last Update: 2023/4/27


◆機能・用途(Purpose)

指定した1地点での3次元データの補間値を求める。
Calculate an interpolated value at a specified location from a 3-D data.


◆形式(Format)

#include <3d_data/interpolate.h>
inline double interpolate_3d_data_1p
(const struct _3d_data data,const double x,const double y,const double z)


◆引数(Arguments)

data 補間する前の3次元データ。
The 3-D data before the interpolation.
x 補間値を求めたい地点の\(x\)座標。
The \(x\)-coordinate of the point where the interpolated value is needed.
y 補間値を求めたい地点の\(y\)座標。
The \(y\)-coordinate of the point where the interpolated value is needed.
z 補間値を求めたい地点の\(z\)座標。
The \(z\)-coordinate of the point where the interpolated value is needed.


◆戻り値(Return value)

引数dataで与えた3次元データの位置\((x,y,z)\)における補間値。
The value of the 3-D data given by the argument data at the location \((x,y,z)\).


◆使用例(Example)

struct _3d_data data;
double value=interpolate_3d_data_1p(data,1.2,3.4,5.6);


◆計算式(Formula)

はじめに\((x,y,z)\)がどの座標軸方向に対しても格子点になっていない場合を考え、 点\(x\)を挟む\(x\)軸方向の2つの格子点の座標を\(x_m\), \(x_p\)、 点\(y\)を挟む\(y\)軸方向の2つの格子点の座標を\(y_m\), \(y_p\)、 点\(z\)を挟む\(z\)軸方向の2つの格子点の座標を\(z_m\), \(z_p\) (\(x_m<x<x_p\), \(y_m<y<y_p\), \(z_m<z<z_p\))とする。 3次元データ\(f(x,y,z)\)が \(x_m\leq x\leq x_p\), \(y_m\leq y\leq y_p\), \(z_m\leq z\leq z_p\)の範囲で 線形関数 \[\begin{equation} f(x,y,z)=f_0+\alpha x+\beta y+\gamma z \label{eq.f.linear} \end{equation}\] で近似されるものとして、 \((x,y,z)\)を囲む8つの格子点でのデータに最も良く合う \(f_0\), \(\alpha\), \(\beta\), \(\gamma\)を求める。
First, consider a case where \((x,y,z)\) is not at a grid node for any direction of the coordinate axes. Let \(x_m\) and \(x_p\) be the two grid nodes along the \(x\)-direction that bounds \(x\), \(y_m\) and \(y_p\) be those along the \(y\)-axis direction that bounds \(y\), and \(z_m\) and \(z_p\) be those along the \(z\)-axis direction that bounds \(z\), where \(x_m<x<x_p\), \(y_m<y<y_p\), and \(z_m<z<z_p\). Let the 3-D data \(f(x,y,z)\) be approximated by a linear function (Eq. \ref{eq.f.linear}) in a range \(x_m\leq x\leq x_p\), \(y_m\leq y\leq y_p\), and \(z_m\leq z\leq z_p\). Let us investigate the values of \(f_0\), \(\alpha\), \(\beta\), and \(\gamma\) that best fits the data at the eight grid nodes surrounding \((x,y,z)\).

これを行うには 8つの格子点における3次元データの値と(\ref{eq.f.linear})式での近似値の差を \[\begin{equation} e_{i,j,k}=f(x_i,y_j,z_k)-f_0-\alpha x_i-\beta y_j-\gamma z_k \hspace{0.5em} (i,j,k=m,p) \label{eq.eijk} \end{equation}\] とおいて \[\begin{equation} E=\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}e_{i,j,k}^2 \label{eq.E} \end{equation}\] を最小にする\(f_0\), \(\alpha\), \(\beta\), \(\gamma\)を求めれば良い。 これらは \[\begin{eqnarray} -\frac{1}{2}\PartialDiff{E}{f_0} &=& -\frac{1}{2}\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p} 2e_{i,j,k}\PartialDiff{e_{i,j,k}}{f_0} \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}e_{i,j,k} \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p} \left[f(x_i,y_j,z_k)-f_0-\alpha x_i-\beta y_j-\gamma z_k\right] \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k) \nonumber \\ & & -8f_0 -4\alpha\sum_{i=m,p}x_i -4\beta\sum_{j=m,p}y_j -4\gamma\sum_{k=m,p}z_k \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k) \nonumber \\ & & -8f_0 -4\alpha(x_m+x_p) -4\beta(y_m+y_p) -4\gamma(z_m+z_p) \nonumber \\ &=& 0 \label{eq.Emin.f0} \end{eqnarray}\] \[\begin{eqnarray} -\frac{1}{2}\PartialDiff{E}{\alpha} &=& -\frac{1}{2}\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}2e_{i,j,k} \PartialDiff{e_{i,j,k}}{\alpha} \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}e_{i,j,k}x_i \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p} \left[f(x_i,y_j,z_k)-f_0-\alpha x_i-\beta y_j-\gamma z_k\right]x_i \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)x_i \nonumber \\ & & -4f_0\sum_{i=m,p}x_i -4\alpha\sum_{i=m,p}x_i^2 -2\beta\sum_{i=m,p}\sum_{j=m,p}x_iy_j -2\gamma\sum_{i=m,p}\sum_{k=m,p}x_iz_k \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)x_i \nonumber \\ & & -4f_0(x_m+x_p) -4\alpha(x_m^2+x_p^2) \nonumber \\ & & -2\beta(x_my_m+x_my_p+x_py_m+x_py_p) -2\gamma(x_mz_m+x_mz_p+x_pz_m+x_pz_p) \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)x_i \nonumber \\ & & -4f_0(x_m+x_p) -4\alpha(x_m^2+x_p^2) \nonumber \\ & & -2\beta(x_m+x_p)(y_m+y_p) -2\gamma(x_m+x_p)(z_m+z_p) \nonumber \\ &=& 0 \label{eq.Emin.alpha} \end{eqnarray}\] \[\begin{eqnarray} -\frac{1}{2}\PartialDiff{E}{\beta} &=& -\frac{1}{2}\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p} 2e_{i,j,k}\PartialDiff{e_{i,j,k}}{\beta} \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}e_{i,j,k}y_j \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p} \left[f(x_i,y_j,z_k)-f_0-\alpha x_i-\beta y_j-\gamma z_k\right]y_j \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)y_j \nonumber \\ & & -4f_0\sum_{j=m,p}y_j -2\alpha\sum_{i=m,p}\sum_{j=m,p}x_iy_j -4\beta\sum_{j=m,p}y_j^2 -2\gamma\sum_{j=m,p}\sum_{k=m,p}y_jz_k \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)y_j \nonumber \\ & & -4f_0(y_m+y_p) -2\alpha(x_my_m+x_my_p+x_py_m+x_py_p) \nonumber \\ & & -4\beta(y_m^2+y_p^2) -2\gamma(y_mz_m+y_mz_p+y_pz_m+y_pz_p) \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)y_j \nonumber \\ & & -4f_0(y_m+y_p) -2\alpha(x_m+x_p)(y_m+y_p) \nonumber \\ & & -4\beta(y_m^2+y_p^2) -2\gamma(y_m+y_p)(z_m+z_p) \nonumber \\ &=& 0 \label{eq.Emin.beta} \end{eqnarray}\] \[\begin{eqnarray} -\frac{1}{2}\PartialDiff{E}{\gamma} &=& -\frac{1}{2}\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p} 2e_{i,j,k}\PartialDiff{e_{i,j,k}}{\gamma} \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}e_{i,j,k}z_k \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p} \left[f(x_i,y_j,z_k)-f_0-\alpha x_i-\beta y_j-\gamma z_k\right]z_k \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)z_k \nonumber \\ & & -4f_0\sum_{k=m,p}z_k -2\alpha\sum_{i=m,p}\sum_{k=m,p}x_iz_k -2\beta\sum_{j=m,p}\sum_{k=m,p}y_jz_k -4\gamma\sum_{k=m,p}z_k^2 \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)z_k \nonumber \\ & & -4f_0(z_m+z_p) -2\alpha(x_mz_m+x_mz_p+x_pz_m+x_pz_p) \nonumber \\ & & -2\beta(y_mz_m+y_mz_p+y_pz_m+y_pz_p) -4\gamma(z_m^2+z_p^2) \nonumber \\ &=& \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)z_k \nonumber \\ & & -4f_0(z_m+z_p) -2\alpha(x_m+x_p)(z_m+z_p) \nonumber \\ & & -2\beta(y_m+y_p)(z_m+z_p) -4\gamma(z_m^2+z_p^2) \nonumber \\ &=& 0 \label{eq.Emin.gamma} \end{eqnarray}\] によって求めることができる。
To do this, let \(e_{i,j,k}\) be the difference between the 3-D data values and their approximated values from Eq. (\ref{eq.f.linear}) at the eight grid nodes (Eq. \ref{eq.eijk}). Minimizing the square summation of the differences (Eq. \ref{eq.E}) gives the optimal values of \(f_0\), \(\alpha\), \(\beta\), and \(\gamma\). They are obtained from Eqs. (\ref{eq.Emin.f0})-(\ref{eq.Emin.gamma}).

\(\mbox{(\ref{eq.Emin.f0})}\times(x_m+x_p)- \mbox{(\ref{eq.Emin.alpha})}\times 2\)より \[\begin{eqnarray} & & \sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)(x_m+x_p) -4\alpha(x_m+x_p)^2 \nonumber \\ & & -2\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)x_i +8\alpha(x_m^2+x_p^2) \nonumber \\ &=& 0 \label{eq.alpha.derivation} \end{eqnarray}\] が得られ、これを\(\alpha\)について解けば \[\begin{eqnarray} \alpha &=& \frac{1}{8(x_m^2+x_p^2)-4(x_m+x_p)^2}\left[ 2\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)x_i \right. \nonumber \\ & & \left. -\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)(x_m+x_p) \right] \nonumber \\ &=& \frac{\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)(2x_i-x_m-x_p)} {4(2x_m^2+2x_p^2)-4(x_m^2+2x_mx_p+x_p^2)} \nonumber \\ &=& \frac{\sum_{j=m,p}\sum_{k=m,p}f(x_m,y_j,z_k)(x_m-x_p) +\sum_{j=m,p}\sum_{k=m,p}f(x_p,y_j,z_k)(x_p-x_m)} {4(x_m^2+x_p^2-2x_mx_p)} \nonumber \\ &=& \frac{\sum_{j=m,p}\sum_{k=m,p}f(x_p,y_j,z_k)(x_p-x_m) -\sum_{j=m,p}\sum_{k=m,p}f(x_m,y_j,z_k)(x_p-x_m)} {4(x_p-x_m)^2} \nonumber \\ &=& \frac{\sum_{j=m,p}\sum_{k=m,p}f(x_p,y_j,z_k) -\sum_{j=m,p}\sum_{k=m,p}f(x_m,y_j,z_k)} {4(x_p-x_m)} \nonumber \\ &=& \frac{\frac{\sum_{j=m,p}\sum_{k=m,p}f(x_p,y_j,z_k)}{4} -\frac{\sum_{j=m,p}\sum_{k=m,p}f(x_m,y_j,z_k)}{4}} {x_p-x_m} \label{eq.alpha} \end{eqnarray}\] となる。すなわち\(x=x_m\)の4つの格子点でのデータの平均値と \(x=x_p\)の4つの格子点でのデータの平均値との間の勾配が \(\alpha\)の最適値であることが分かる。 同様にして \[\begin{equation} \beta=\frac{\frac{\sum_{i=m,p}\sum_{k=m,p}f(x_i,y_p,z_k)}{4} -\frac{\sum_{i=m,p}\sum_{k=m,p}f(x_i,y_m,z_k)}{4}} {y_p-y_m} \label{eq.beta} \end{equation}\] \[\begin{equation} \gamma=\frac{\frac{\sum_{i=m,p}\sum_{j=m,p}f(x_i,y_j,z_p)}{4} -\frac{\sum_{i=m,p}\sum_{j=m,p}f(x_i,y_j,z_m)}{4}} {z_p-z_m} \label{eq.gamma} \end{equation}\] も得られる。
An equation of \(\mbox{(\ref{eq.Emin.f0})}\times(x_m+x_p)- \mbox{(\ref{eq.Emin.alpha})}\times 2\) gives (\ref{eq.alpha.derivation}), which can be solved for \(\alpha\) as (\ref{eq.alpha}). This equation indicates that the optimal value of \(\alpha\) is the slope between \(x=x_m\) and \(x=x_p\), where the data of the four grid nodes at each of \(x=x_m\) and \(x=x_p\) are averaged. In the same way, Eqs. (\ref{eq.beta}) and (\ref{eq.gamma}) are obtained.

\(f_0\)を求めるため、(\ref{eq.Emin.f0})式を変形して \[\begin{equation} f_0=\frac{\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)}{8} -\alpha\frac{x_m+x_p}{2} -\beta\frac{y_m+y_p}{2} -\gamma\frac{z_m+z_p}{2} \label{eq.f0} \end{equation}\] を得る。この関係を(\ref{eq.f.linear})に代入すると \[\begin{eqnarray} f(x,y,z) &=& \frac{\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)}{8} \nonumber \\ & & +\alpha\left(x-\frac{x_m+x_p}{2}\right) +\beta\left(y-\frac{y_m+y_p}{2}\right) +\gamma\left(z-\frac{z_m+z_p}{2}\right) \label{eq.f.final} \end{eqnarray}\] となる。この結果から、8つの格子点でのデータの平均値が 中心点での近似値となる場合が最適解であることが分かる。
To calculate \(f_0\), arrange Eq. (\ref{eq.Emin.f0}) as (\ref{eq.f0}). Inserting this relation into (\ref{eq.f.linear}) results in (\ref{eq.f.final}). This result indicates that the solution is optimal when the data averaged over the eight grid nodes is equal to the approximated value at the central point.

最後に、補間を行う地点\((x,y,z)\)が いずれかの座標軸方向に関して格子点上に位置する場合を考える。 例として\(z=z_p\)の場合を考えると(\ref{eq.f.final})式は \[\begin{eqnarray} f(x,y,z) &=& \frac{\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)}{8} \nonumber \\ & & +\alpha\left(x-\frac{x_m+x_p}{2}\right) +\beta\left(y-\frac{y_m+y_p}{2}\right) +\gamma\left(z_p-\frac{z_m+z_p}{2}\right) \nonumber \\ &=& \frac{\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)}{8} \nonumber \\ & & +\alpha\left(x-\frac{x_m+x_p}{2}\right) +\beta\left(y-\frac{y_m+y_p}{2}\right) +\gamma\frac{z_p-z_m}{2} \nonumber \\ &=& \frac{\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)}{8} \nonumber \\ & & +\alpha\left(x-\frac{x_m+x_p}{2}\right) +\beta\left(y-\frac{y_m+y_p}{2}\right) \nonumber \\ & & +\frac{\frac{\sum_{i=m,p}\sum_{j=m,p}f(x_i,y_j,z_p)}{4} -\frac{\sum_{i=m,p}\sum_{j=m,p}f(x_i,y_j,z_m)}{4}} {z_p-z_m} \frac{z_p-z_m}{2} \nonumber \\ &=& \frac{\sum_{i=m,p}\sum_{j=m,p}\sum_{k=m,p}f(x_i,y_j,z_k)}{8} \nonumber \\ & & +\alpha\left(x-\frac{x_m+x_p}{2}\right) +\beta\left(y-\frac{y_m+y_p}{2}\right) \nonumber \\ & & +\frac{\sum_{i=m,p}\sum_{j=m,p}f(x_i,y_j,z_p)}{8} -\frac{\sum_{i=m,p}\sum_{j=m,p}f(x_i,y_j,z_m)}{8} \nonumber \\ &=& \frac{\sum_{i=m,p}\sum_{j=m,p}f(x_i,y_j,z_p)}{4} \nonumber \\ & & +\alpha\left(x-\frac{x_m+x_p}{2}\right) +\beta\left(y-\frac{y_m+y_p}{2}\right) \label{eq.f.2D} \end{eqnarray}\] となる。この結果は\((x,y,z)\)を囲む4つの格子点 \((x_m,y_m,z)\), \((x_m,y_p,z)\), \((x_p,y_m,z)\), \((x_p,y_p,z)\) でのデータの平均値を中心点での近似値とし、 \(x\)方向、\(y\)方向の勾配を(\ref{eq.alpha})(\ref{eq.beta})式にしたがって 計算した値となっている。 このことから、\(z\)方向の変化を考慮するかしないかの違いを除いて (\ref{eq.f.final})と全く同じ考え方で計算できることが分かる。
Finally consider a case where \((x,y,z)\) is on a grid node along either axis direction. When \(z=z_p\), for example, Eq. (\ref{eq.f.final}) reduces to (\ref{eq.f.2D}). This result indicates that the data values averaged over \((x_m,y_m,z)\), \((x_m,y_p,z)\), \((x_p,y_m,z)\), and \((x_p,y_p,z)\) is equal to the approximated value at the central point, and the slopes along \(x\)- and \(y\)-directions are given by Eqs. (\ref{eq.alpha}) and (\ref{eq.beta}). Therefore the procedure is same as that for Eq. (\ref{eq.f.final}) except for a difference of whether the variation along the \(z\)-direction is taken into account or not.