0

我写了一个简单的函数

def rect(t):
    field = np.zeros((len(t),3))
    for i in range(len(t)):
        if t[i] >=-10 and t[i] <=10:
            field[i,1] = 1
        else:
            field[i,1] = 0
    return field

并想整合它

from scipy import integrate

def S_elem(t):
    return rect(t)[:,1]*integr

integrate.quad(S_elem, -10, 10)

我收到这个错误

TypeError: object of type 'float' has no len()

我知道我可能不会使用 numpy 数组,但我需要它用于其他目的。如何在不删除 numpy 数组类型的情况下执行集成?

4

1 回答 1

0

您必须将浮点数转换为 numpy 数组:

def rect(t):
    t = np.atleast_1d(t)
    field = np.zeros((len(t), 3))
    field[:, 1] = abs(t) <= 10
    return field
于 2018-09-17T21:24:08.163 回答