有没有办法在 Python 中定义一个浮点数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29060824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:02:36  来源:igfitidea点击:

Is there a way to define a float array in Python?

pythonarraysnumpyphysicsastronomy

提问by

For my astronomy homework, I need to simulate the elliptical orbit of a planet around a sun. To do this, I need to use a for loop to repeatedly calculate the motion of the planet. However, every time I try to run the program, I get the following error:

对于我的天文学作业,我需要模拟行星围绕太阳的椭圆轨道。为此,我需要使用 for 循环来重复计算行星的运动。但是,每次尝试运行该程序时,都会出现以下错误:

RuntimeWarning: invalid value encountered in power
r=(x**2+y**2)**1.5
Traceback (most recent call last):
File "planetenstelsel3-4.py", line 25, in <module>
ax[i] = a(x[i],y[i])*x[i]
ValueError: cannot convert float NaN to integer

I've done some testing, and I think the problem lies in the fact that the values that are calculated are greater than what fits in an integer, and the arrays are defined as int arrays. So if there was a way do define them as float arrays, maybe it would work. Here is my code:

我已经做了一些测试,我认为问题在于计算出的值大于适合整数的值,并且数组被定义为 int 数组。因此,如果有办法将它们定义为浮点数组,也许它会起作用。这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

dt = 3600 #s
N = 5000
x = np.tile(0, N)
y = np.tile(0, N)
x[0] = 1.496e11 #m
y[0] = 0.0
vx = np.tile(0, N)
vy = np.tile(0, N)
vx[0] = 0.0
vy[0] = 28000 #m/s
ax = np.tile(0, N)
ay = np.tile(0, N)
m1 = 1.988e30 #kg
G = 6.67e-11 #Nm^2kg^-2

def a(x,y):
    r=(x**2+y**2)**1.5
    return (-G*m1)/r

for i in range (0,N):
    r = x[i],y[i]
    ax[i] = a(x[i],y[i])*x[i]
    ay[i] = a(x[i],y[i])*y[i]
    vx[i+1] = vx[i] + ax[i]*dt
    vy[i+1] = vy[i] + ay[i]*dt
    x[i+1] = x[i] + vx[i]*dt 
    y[i+1] = y[i] + vy[i]*dt    
plt.plot(x,y)
plt.show()

The first few lines are just some starting parameters.

前几行只是一些起始参数。

Thanks for the help in advance!

我在这里先向您的帮助表示感谢!

采纳答案by Antti Haapala

When you are doing physics simulations you should definitely use floats for everything. 0is an integer constant in Python, and thus np.tilecreates integer arrays; use 0.0as the argument to np.tileto do floating point arrays; or preferably use the np.zeros(N)instead:

当您进行物理模拟时,您绝对应该对所有内容使用浮点数。0是 Python 中的整数常量,因此np.tile创建整数数组;使用0.0作为参数np.tile做浮点阵列; 或者最好np.zeros(N)改用:

You can check the datatype of any array variable from its dtypemember:

您可以从其dtype成员检查任何数组变量的数据类型:

>>> np.tile(0, 10).dtype
dtype('int64')
>>> np.tile(0.0, 10).dtype
dtype('float64')
>>> np.zeros(10)
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> np.zeros(10).dtype
dtype('float64')

回答by Eric O Lebigot

You need x = np.zeros(N), etc.: this declares the arrays as float arrays.

你需要x = np.zeros(N),等等:这将数组声明为浮点数组。

This is the standard way of putting zeros in an array (np.tile()is convenient for creating a tiling with a fixed array).

这是在数组中放置零的标准方法(np.tile()便于使用固定数组创建平铺)。