插复数整个阵列 [英] Interpolate whole arrays of complex numbers

查看:211
本文介绍了插复数整个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司拥有一批2维np.arrays(所有大小相同的)含有复数。他们每个人都属于一个位置在4维空间。这些职位是稀疏和不规则分布(拉丁超立方体被precise)。
我想在同样的4维空间插值这个数据到其他点。

I have a number of 2-dimensional np.arrays (all of equal size) containing complex numbers. Each of them belongs to one position in a 4-dimensional space. Those positions are sparse and distributed irregularly (a latin hypercube to be precise). I would like to interpolate this data to other points in the same 4-dimensional space.

我能成功地做到了简单的数字,即使用 sklearn.kriging() scipy.interpolate.Rbf()(或其它):

I can successfully do this for simple numbers, using either sklearn.kriging(), scipy.interpolate.Rbf() (or others):

# arrayof co-ordinates: 2 4D sets
X = np.array([[1.0, 0.0, 0.0, 0.0],\
              [0.0, 1.0, 0.0, 0.0]])

# two numbers, one for each of the points above 
Y = np.array([1,\
              0])

# define the type of gaussian process I want
kriging = gp.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=4.0,\
            corr='linear', normalize=True, nugget=0.00001, optimizer='fmin_cobyla')

# train the model on the data
kmodel = kriging.fit(X,Y)

# interpolate
kmodel.predict(np.array([0.5, 0.5, 0.0, 0.0]))
# returns: array([ 0.5])

如果我尝试使用阵列(或只是复数)数据,这不起作用:

If I try to use arrays (or just complex numbers) as data, this doesn't work:

# two arrays of complex numbers, instead of the numbers 
Y = np.array([[1+1j, -1-1j],\
              [0+0j,  0+0j]])

kmodel = kriging.fit(X,Y)
# returns: ValueError: The number of features in X (X.shape[1] = 1) should match the sample size used for fit() which is 4.

这是显而易见的,因为文档字符串为 kriging.fit()明确指出,它需要ñ标量的数组,每个每个元素之一,X的第一维

This is obvious since the docstring for kriging.fit() clearly states that it needs an array of n scalars, one per each element in the first dimension of X.

一种解决方案是分解阵列Y中成单个数字,这些成实部和虚部,使各那些的一个单独的内插,然后再次把它们放在一起。我可以循环的正确组合和一些艺术性做到这一点,但如果有一个方法,这将是很好(如 scipy.interpolate ),可以处理整个NP。数组,而不是标量值。

One solution is to decompose the arrays in Y into individual numbers, those into real and imaginary parts, make a separate interpolation of each of those and then put them together again. I can do this with the right combination of loops and some artistry but it would be nice if there was a method (e.g. in scipy.interpolate) that could handle an entire np.array instead of scalar values.

我不是固定在一个特定的算法(还),所以我很乐意知道任何能够使用复数阵列的变量来进行插值。因为 - 正如我所说的 - 有空间很少,不规则的点(和无网格内插的),简单的线性插值不会做,当然,

I'm not fixed on a specific algorithm (yet), so I'd be happy to know about any that can use arrays of complex numbers as the "variable" to be interpolated. Since -- as I said -- there are few and irregular points in space (and no grid to interpolate on), simple linear interpolation won't do, of course.

推荐答案

有看着复数两种方式:


  1. 直角坐标形式(A + BI)和

  2. 极地/ Euler格式(A * EXP(我* PHI))

当你说你想要两个极坐标之间进行插值,你想相对于实/虚部(1)进行插值,或相对于数字的幅度和相位(2)?

When you say you want to interpolate between two polar coordinates, do you want to interpolate with respect to the real/imaginary components (1), or with respect to the number's magnitude and phase (2)?

您可以打破的东西分解成实部和虚部,

You CAN break things down into real and imaginary components,

X = 2 * 5j
X_real = np.real(X)
X_imag = np.imag(X)

# Interpolate the X_real and X_imag

# Reconstruct X
X2 = X_real + 1j * X_imag

然而,使用涉及到复杂的数字,如数字滤波器设计真实生活中的应用,则经常要在极性/指数形式编号,以正常工作。

However, With real-life applications that involve complex numbers, such as digital filter design, you quite often want to work with numbers in Polar/exponential form.

。因此,而不是插值np.real()和np.imag()组件,您可能需要把它分解成幅度和放大器;使用np.abs()阶段和角度或< A HREF =htt​​p://docs.scipy.org/doc/numpy/reference/generated/numpy.arctan2.html#numpy.arctan2相对=nofollow> Arctan2 ,并分别插值。你可以这样做,例如,试图插值傅立叶变换的数字滤波器的时候。

Therefore instead of interpolating the np.real() and np.imag() components, you may want to break it down into magnitude & phase using np.abs() and Angle or Arctan2, and interpolate separately. You might do this, for example, when trying to interpolate the Fourier Transform of a digital filter.

Y = 1+2j
mag = np.abs(Y)
phase = np.angle(Y)

该插补值可以被转换回使用欧拉公式络合物(笛卡尔)编号

The interpolated values can be converted back into complex (Cartesian) numbers using the Eulers formula

# Complex number
y = mag * np.exp( 1j * phase)

# Or if you want the real and imaginary complex components separately,
realPart, imagPart = mag * np.cos(phase) , mag * np.sin(phase)

根据你在做什么,这让你与你使用插值方法一些真正的灵活性。

Depending on what you're doing, this gives you some real flexibility with the interpolation methods you use.

这篇关于插复数整个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆