简体   繁体   English

追加到NumPy(Python)数组

[英]Appending to NumPy (Python) Array

For starters, I am doing a Runge-Kutta on a three-DOF NumPy array. 首先,我在三自由度NumPy数组上执行Runge-Kutta。 My array looks like this: 我的数组如下所示:

states = [[X], [Vx], [Y], [Vy], [Z], [Vz]] 状态= [[X],[Vx],[Y],[Vy],[Z],[Vz]]

I run my Runge-Kutta, and get my four K values, which I transpose with [newaxis]. 我运行Runge-Kutta,并获取我的四个K值,并使用[newaxis]对其进行了转置。 So when I try to append the new states to my states array as follows: 因此,当我尝试将新状态附加到我的状态数组中时,如下所示:

states = append(states, states[:,i] + (K1.T + 2 * K2.T + 2 * K3.T + K4.T)/6, 1)

where "i" is a counter that starts at 0 and counts up for each iteration. 其中“ i”是一个从0开始并在每次迭代中递增计数的计数器。

However, when I run my code my resulting states array is not two columns of six elements. 但是,当我运行代码时,得到的状态数组不是六个元素的两列。 It appears that I am appending a row vector instead of a column vector to my states array. 看来我在状态数组上附加了行向量而不是列向量。 I ran the code with two elements (X, Vx) in the column, and everything appended just fine (or at least my result made sense). 我在该列中使用两个元素(X,Vx)运行了代码,所有内容都很好地附加了(或者至少使我的结果有意义)。

I have tried forcing the result of my Runge-Kutta to be a column vector, but that messes up my calculation of the K-values. 我试图将Runge-Kutta的结果强制为列向量,但是这弄乱了我对K值的计算。 I have tried variations of my append code, and still have the same result. 我尝试了附加代码的变体,但结果仍然相同。

This is a clone of a Matlab code, and I have been unable to find anything on NumPy arrays and indexing that helps me. 这是Matlab代码的克隆,我无法在NumPy数组和索引上找到任何对我有帮助的东西。

Any help is appreciated. 任何帮助表示赞赏。

Thanks. 谢谢。

UPDATE: states[:,0] = [[0], [2300], [0], [0], [-1600], [500]] - original states[:,1] = [[2300], [2100], [0], [0], [-2100], [450]] - append states = [[0, 2300], [2300, 2100], [0, 0], [0, 0], [-1600, -2100], [500, 450]] - final These are column vectors. 更新: states[:,0] = [[0], [2300], [0], [0], [-1600], [500]] -原始states[:,1] = [[2300], [2100], [0], [0], [-2100], [450]] -附加states = [[0, 2300], [2300, 2100], [0, 0], [0, 0], [-1600, -2100], [500, 450]] -最终这些是列向量。

You should stack them instead of appending them. 您应该堆叠它们而不是appending它们。

Taken from the numpy documentation you should one of the stack methods, for example: np.vstack : numpy文档中获取,您应该使用其中一种堆栈方法,例如: np.vstack

a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
c = np.vstack((a,b))
print(c)
# array([[1, 2, 3],
#        [2, 3, 4]])

or depending on your resulting data there is also np.hstack (stack along first axis) and np.dstack (stack along third axis). 或根据生成的数据,还有np.hstack (沿第一个轴的堆栈)和np.dstack (沿第三个轴的堆栈)。

You should not append arrays, if you can avoid, due to efficiency issues. 由于效率问题,如果可以避免,则不应附加数组。 Appending means changing the allocated memory size, which can run into non-contiguous memory space, hence inefficient allocation or reallocation would be necessary. 追加意味着更改已分配的内存大小,这可能会遇到不连续的内存空间,因此,效率低下的分配或重新分配将是必需的。 These can slow down your program a lot, specially for large arrays. 这些会大大降低您的程序速度,特别是对于大型阵列。

If you are implementing a fixed time-step Runge-Kutta you know beforehand how many points your solution is going to have at time T. It's N = (T-t0)/h+1, where T is the final time, t0 the initial time, and h the time step. 如果您要执行固定的时间步长Runge-Kutta,您将事先知道您的解决方案在时间T会有多少个点。它是N =(T-t0)/ h + 1,其中T是最终时间,t0是初始时间和h时间步长。 You can initialize your array with zeros (using states = np.zeros((N,3)) ) and fill the values as you go, associating the index i to the time t[i] = t0 +i*h . 您可以使用零初始化数组(使用states = np.zeros((N,3)) ),然后随便填充值,将索引i与时间t[i] = t0 +i*h关联。 This would be inside the loop: states[:,i+1] = states[:,i] + RK4_step(states[:,i]) , where RK4_step(states[:,i]) is a function returning an array (column) with your variation of the state values in one step of the Runge-Kutta method. 这将在循环内: states[:,i+1] = states[:,i] + RK4_step(states[:,i]) ,其中RK4_step(states[:,i])是返回数组的函数(列),然后在Runge-Kutta方法的第一步中更改状态值。

Even if your time-step is variable you should still do that, but with nonuniform times t[i] = t0 +i*h . 即使您的时间步长是可变的,您仍然应该这样做,但是时间不均匀t[i] = t0 +i*h

Or, you could use numpy.integrate.ode_int() , which returns the solution of an ODE at the required times. 或者,您可以使用numpy.integrate.ode_int() ,它在需要的时间返回ODE的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM