错误“无法将序列乘以'float'类型的非整数" [英] Error "can't multiply sequence by non-int of type 'float'"

查看:830
本文介绍了错误“无法将序列乘以'float'类型的非整数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,当我尝试运行"x01 = rk4(x01,t1 [-1],h1,fallParabola)"行时,弹出错误消息,提示无法将序列与非整数相乘输入"float".我想知道为什么会这样,因为我认为将numpy数组中的每一项乘以一个数字再乘以该数字即可.

In the following code, when I try to run the line "x01 = rk4(x01,t1[-1],h1,fallParabola)," an error pops up saying "can't multiply sequence by non-int of type 'float'." I am wondering why this is, as I thought that multiplying every term in a numpy array by a number multiplied every term in the array by that number.

** slope1和intercept1已在其他位置定义,所以这不是问题

**slope1 and intercept1 are already defined elsewhere, so that is not an issue

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)

推荐答案

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

You're multiplying h by a tuple, and not a numpy array in your Runge-Kutta definition:

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()是根据您的定义返回的元组:

Here g is the function you're passing in, which is fallParabola() which by your definition returns a tuple:

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数组,以便可以对其进行乘法运算:

You should modify this definition to return a numpy array so you can multiply through it:

return np.array([acc, slope])


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


The reason for the specific error message about non-int is simply because you can multiply a tuple by an integer, but this doesn't multiply the values inside the tuple. First, a tuple is immutable so you can't change the values anyways, but in general for sequences, multiplying by an integer repeats the sequence by the multiplier. For e.g.

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

如果您在此处乘以浮点数,那当然是没有意义的,并且您将得到与您相同的错误:

If you multiply here by a float, it doesn't make sense of course, and you get the same error you've got:

>>> 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这样的变量,那么这样做会提供一个更简单的界面.


Also as an aside, your fallParabola() function is not defined well IMO. Currently you have it taking global variables (like slope1, shift1) but it would be best to pass these values into the function. Generally global variables are not evil in Python, but if a function uses some parameters, it's best practice to send those parameters in so you know what it's using. And if you need to update the variables like slope1 as time goes on, this provides an easier interface to do so.

这篇关于错误“无法将序列乘以'float'类型的非整数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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