【问题标题】:Error "can't multiply sequence by non-int of type 'float'" [closed]错误“不能将序列乘以'float'类型的非整数”[关闭]
【发布时间】:2017-11-28 06:52:29
【问题描述】:

在下面的代码中,当我尝试运行“x01 = rk4(x01,t1[-1],h1,fallParabola)”行时,会弹出一个错误说“不能将序列乘以非整数输入‘浮动’。”我想知道为什么会这样,因为我认为将 numpy 数组中的每个项乘以一个数字就会将数组中的每个项乘以该数字。

**slope1 和 intercept1 已在别处定义,所以这不是问题

def rk4(f,t,h,g):
    k1 = h*g(t,f)
    k2 = h*g(t+0.5*h, f+0.5*k1)
    k3 = h*g(t+0.5*h, f+0.5*k2)
    k4 = h*g(t+h, f+k3)
    return f + k1/6. + k2/3. + k3/3. + k4/6.

def fallParabola(t,f):
    g = 10
    px = f[0]
    py = f[1]
    vx = f[2]
    vy = f[3]
    slope = slope1 * (px-shift1)
    theta = sp.arctan(np.abs(slope))
    acc = np.array([vx,vy,g*sp.sin(theta)*sp.cos(theta), 
        g*sp.sin(theta)*sp.sin(theta)])
    return acc,slope

x01 = np.array([0.0,intercept1,0.0,0.0])
t01 = 0.
px1 = [x01[0],]
py1 = [x01[1],]
vx1 = [x01[2],]
vy1 = [x01[3],]
t1 = [t01,]
h1 = 0.1
while py1[-1] > 0:
    x01 = rk4(x01,t1[-1],h1,fallParabola)
    px1.append(x01[0])
    py1.append(x01[1])
    vx1.append(x01[2])
    vy1.append(x01[3])
    t1.append(t1[-1]+h1)

【问题讨论】:

  • 将您的代码更改为我们可以运行的代码。如前所述,这不是。此外,您可能会丢弃该错误消息中至少 50% 的信息(完整的堆栈跟踪!)。
  • ...请参阅minimal reproducible example 定义以获取有关构建最短代码的更多指导,这些代码可以用来自己查看问题(并测试预期答案)。这看起来应该可以在两行中重现,其中一个是import,另一个是乘法运算。
  • 旁注:看起来你正在乘以一个元组(fallParabola 的返回):h*g(t,f)。但这只是猜测。
  • 我猜遇到了alist*afloat。您正在传递一个列表而不是数组。 [1,2,3]*5 有效,但不像是一个数组。

标签: python numpy


【解决方案1】:

您将 h 乘以一个元组,而不是 Runge-Kutta 定义中的 numpy 数组:

def rk4(f,t,h,g):
    k1 = h*g(t,f)
    k2 = h*g(t+0.5*h, f+0.5*k1)
    k3 = h*g(t+0.5*h, f+0.5*k2)
    k4 = h*g(t+h, f+k3)
    return f + k1/6. + k2/3. + k3/3. + k4/6.

这里 g 是您传入的函数,即 fallParabola() 根据您的定义返回一个元组:

def fallParabola(t,f):
    g = 10
    px = f[0]
    py = f[1]
    vx = f[2]
    vy = f[3]
    slope = slope1 * (px-shift1)
    theta = sp.arctan(np.abs(slope))
    acc = np.array([vx,vy,g*sp.sin(theta)*sp.cos(theta), 
        g*sp.sin(theta)*sp.sin(theta)])
    return acc,slope

您应该修改此定义以返回一个 numpy 数组,以便您可以通过它进行乘法运算:

return np.array([acc, slope])

关于非整数的特定错误消息的原因仅仅是因为您可以将一个元组乘以一个整数,但这不会将元组内的值相乘。首先,元组是不可变的,因此无论如何您都无法更改值,但通常对于序列,乘以整数会重复乘数的序列。例如

>>> tup = (5, 4)
>>> tup*3
(5, 4, 5, 4, 5, 4)

如果你在这里乘以一个浮点数,它当然没有意义,你会得到同样的错误:

>>> tup*3.14
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

顺便说一句,您的 fallParabola() 函数在 IMO 中没有很好地定义。目前,您可以使用全局变量(如slope1shift1),但最好将这些值传递给函数。一般来说,全局变量在 Python 中并不邪恶,但如果一个函数使用了一些参数,最好将这些参数发送进去,这样你就知道它在使用什么。如果您需要随着时间的推移更新slope1 等变量,这提供了一个更简单的界面来执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2010-12-30
    • 2012-10-09
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多