![](/img/trans.png)
[英]Python MemoryError in Scipy Radial Basis Function (scipy.interpolate.rbf)
[英]Using Radial Basis Functions to Interpolate a Function on a Sphere
首先,一點背景:
我使用球面諧波作為球體表面上的函數示例,如此圖像中的前球體:
我制作了這些球體中的一個,根據其表面上的點處的諧波函數的值着色。 我首先做了很多點,所以我的功能非常准確。 我稱之為我的fine
球體。
現在我擁有了fine
球體,我在球體上占據了相對較少的點數。 這些是我想要插入的點,訓練數據,我稱之為interp
點。 這是我的interp
點,顏色與他們的值,繪制在我的fine
球體上。
現在,該項目的目標是使用這些interp
點來訓練SciPy徑向基函數以在球體上插入我的函數。 我能夠這樣做:
# Train the interpolation using interp coordinates
rbf = Rbf(interp.phi, interp.theta, harmonic13_coarse)
# The result of the interpolation on fine coordinates
interp_values = rbf(fine.phi, fine.theta)
哪個產生了這個插值,繪制在球體上:
希望通過最后一張圖片,您可以看到我的問題。 注意通過插值的線? 這是因為插值數據具有邊界。 邊界是因為我使用球面坐標([0,pi]和[0,2pi]處的邊界)訓練徑向基函數。
rbf = Rbf(interp.phi, interp.theta, harmonic13_coarse)
我的目標,以及為什么我發布這個問題,是使用球體上數據的x,y,z笛卡爾坐標在球體表面上插入我的函數。 這樣,由於球體沒有邊界,我不會像在球坐標中那樣出現這個邊界誤差。 但是,我只是無法弄清楚如何做到這一點。
我試過簡單地給Rbf函數提供x,y,z坐標和函數的值。
rbf=Rbf(interp.x, interp.y, interp.z, harmonic13_coarse)
interp_values=rbf(fine.x,fine.y,fine.z)
但NumPy卻給我一個奇異矩陣錯誤
numpy.linalg.linalg.LinAlgError: singular matrix
有什么方法可以讓我用笛卡爾坐標給Rbf我的數據站點,每個站點都有函數值,並且它的行為與球面坐標的行為相似但沒有邊界嗎? 從Rbf文檔中,有用於定義不同距離范數的屬性norm
,我是否必須使用球形距離才能使其工作?
我對此很難過。 如果您對沒有球坐標邊界的球體內插函數有任何想法,請告訴我。
這是我的完整代碼:
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy import special
from scipy.interpolate import Rbf
from collections import namedtuple
from mayavi import mlab
# Nice aliases
pi = np.pi
cos = np.cos
sin = np.sin
# Creating a sphere in Cartesian and Sphereical
# Saves coordinates as named tuples
def coordinates(r, n):
phi, theta = np.mgrid[0:pi:n, 0:2 * pi:n]
Coor = namedtuple('Coor', 'r phi theta x y z')
r = r
x = r * sin(phi) * cos(theta)
y = r * sin(phi) * sin(theta)
z = r * cos(phi)
return Coor(r, phi, theta, x, y, z)
# Creating a sphere
# fine is coordinates on a fine grid
# interp is coordinates on coarse grid for training interpolation
fine = coordinates(1, 100j)
interp = coordinates(1, 5j)
# Defining finection to colour sphere
# Here we are using a spherical harmonic
def harmonic(m, n, theta, phi):
return special.sph_harm(m, n, theta, phi).real
norm = colors.Normalize()
# One example of the harmonic function, for testing
harmonic13_fine = harmonic(1, 3, fine.theta, fine.phi)
harmonic13_coarse = harmonic(1, 3, interp.theta, interp.phi)
# Train the interpolation using interp coordinates
rbf = Rbf(interp.phi, interp.theta, harmonic13_coarse)
# The result of the interpolation on fine coordinates
interp_values = rbf(fine.phi, fine.theta)
rbf=Rbf(interp.x, interp.y, interp.z, harmonic13_coarse)
interp_values=rbf(fine.x,fine.y,fine.z)
#Figure of harmoinc function on sphere in fine cordinates
#Points3d showing interpolation training points coloured to their value
mlab.figure()
vmax, vmin = np.max(harmonic13_fine), np.min(harmonic13_fine)
mlab.mesh(fine.x, fine.y, fine.z, scalars=harmonic13_fine, vmax=vmax, vmin=vmin)
mlab.points3d(interp.x, interp.y, interp.z, harmonic13_coarse,
scale_factor=0.1, scale_mode='none', vmax=vmax, vmin=vmin)
#Figure showing results of rbf interpolation
mlab.figure()
vmax, vmin = np.max(harmonic13_fine), np.min(harmonic13_fine)
mlab.mesh(fine.x, fine.y, fine.z, scalars=interp_values)
# mlab.points3d(interp.x, interp.y, interp.z, scalars, scale_factor=0.1, scale_mode='none',vmax=vmax, vmin=vmin)
mlab.show()
您看到的邊界是因為您正在將閉合曲面(S2)映射到開放曲面(R2)。 不管怎樣,你都會有界限。 歧管的局部屬性是兼容的,因此它適用於大多數球體,但不適用於全局 ,你得到一條線。
解決它的方法是使用圖集而不是單個圖表。 地圖集是重疊圖表的集合。 在重疊區域中,您需要定義權重,即每個圖表上從0到1的平滑函數。 (對不起,差異幾何可能不是你期望聽到的)。
如果您不想一直走到這里,您可以注意到您的原始球體有一個赤道,其中方差最小。 然后,您可以旋轉精細球體並使其與線條重合。 它不能解決您的問題,但它肯定可以緩解它。
您可以更改標准距離:
def euclidean_norm(x1, x2):
return np.sqrt( ((x1 - x2)**2).sum(axis=0) )
通過球體距離(例如,參見Python中的這個問題Haversine公式(軸承和兩個GPS點之間的距離) )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.