Python Rbf 给出没有重复坐标的奇异矩阵误差,为什么



与RBF插值失败非常相似:LinAlgError:奇异矩阵,但我认为问题是不同的,因为我没有重复的坐标。

玩具示例:

import numpy as np
import scipy.interpolate as interp
coords = (np.array([-1, 0, 1]), np.array([-2, 0, 2]), np.array([-1, 0, 1]))
coords_mesh = np.meshgrid(*coords, indexing="ij")
fn_value = np.power(coords_mesh[0], 2) + coords_mesh[1]*coords_mesh[2]  # F(x, y, z)
coords_array = np.vstack([x.flatten() for x in coords_mesh]).T  # Columns are x, y, z
unique_coords_array = np.vstack({tuple(row) for row in coords_array})
unique_coords_array.shape == coords_array.shape  # True, i.e. no duplicate coords
my_grid_interp = interp.RegularGridInterpolator(points=coords, values=fn_value)
my_grid_interp(np.array([0, 0, 0]))  # Runs without error
my_rbf_interp = interp.Rbf(*[x.flatten() for x in coords_mesh], d=fn_value.flatten())
## Error: numpy.linalg.linalg.LinAlgError: singular matrix -- why?

我错过了什么? 上面的例子使用函数 F(x, y, z) = x^2 + y*z。 我想使用 Rbf 来近似该函数。 据我所知,没有重复的坐标:将unique_coords_arraycoords_array进行比较。

我相信

问题是你的输入:

    my_rbf_interp = interp.Rbf(*[x.flatten() for x in coords_mesh],d=fn_value.flatten())

是否应更改为:

    x,y,z = [x.flatten() for x in coords_mesh]
    my_rbf_interp = interp.Rbf(x,y,z,fn_value.flatten())

它应该有效。我认为您的原始公式是在用于求解的矩阵中重复行,因此与重复项(即奇异矩阵)具有非常相似的问题。

另外,如果您愿意这样做:

    d = fn_value.flatten()
    my_rbf_interp = interp.Rbf(*(x,y,z,d))

它也应该工作。

最新更新