python - 如何解决 "Invalid use of Function(<built-in function abs>)"

标签 python python-3.x numba

我正在尝试计算重力。结果应该是一个列表,但是出现了以下错误:

numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function abs>) with argument(s) of type(s): (array(float64, 1d, C))

@jit(nopython = True)
def gravity_calculator(x, y, h, dx, dy, p):
    calculated_gravity = np.array([])
    for i in range(len(x)):
        cal = 0
        for j in range(len(x)):
            x1 = abs((x[j] - x[i])+0.000001)
            x2 = x1 + dx
            y1 = abs((y[j]-y[i])+0.000001)
            y2 = y1 + dy
            t1 = np.log((y2 + np.sqrt((x2 ** 2) + (y2 ** 2))) / (y2 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))))
            t2 = np.log((y1 + np.sqrt((x2 ** 2) + (y1 ** 2))) / (y1 + np.sqrt((x2 ** 2) + (y1 ** 2) + (h[j] ** 2))))
            t3 = np.log((y2 + np.sqrt((x1 ** 2) + (y2 ** 2))) / (y2 + np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))))
            t4 = np.log((y1 + np.sqrt((x1 ** 2) + (y1 ** 2))) / (y1 + np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))))
            t5 = np.log((x2 + np.sqrt((x2 ** 2) + (y2 ** 2))) / (x2 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))))
            t6 = np.log((x1 + np.sqrt((x1 ** 2) + (y2 ** 2))) / (x1 + np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))))
            t7 = np.log((x2 + np.sqrt((x2 ** 2) + (y1 ** 2))) / (x2 + np.sqrt((x2 ** 2) + (y1 ** 2) + (h[j] ** 2))))
            t8 = np.log((x1 + np.sqrt((x1 ** 2) + (y1 ** 2))) / (x1 + np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))))
            t9 = np.arcsin(((y2 ** 2) + (h[j] ** 2) + y2 * np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))) / (
                        (y2 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))) * np.sqrt((y2 ** 2) + (h[j] ** 2))))
            t10 = np.arcsin(((y2 ** 2) + (h[j] ** 2) + y2 * np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))) / (
                        (y2 + np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))) * np.sqrt((y2 ** 2) + (h[j] ** 2))))
            t11 = np.arcsin(((y1 ** 2) + (h[j] ** 2) + y1 * np.sqrt((x2 ** 2) + (y1 ** 2) + (h[j] ** 2))) / (
                        (y1 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))) * np.sqrt((y1 ** 2) + (h[j] ** 2))))
            t12 = np.arcsin(((y1 ** 2) + (h[j] ** 2) + y1 * np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))) / (
                        (y1 + np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))) * np.sqrt((y1 ** 2) + (h[j] ** 2))))
            G = (x2 * (t1 - t2) - x1 * (t3 - t4) + y2 * (t5 - t6) - y1 * (t7 - t8) + h[j] * (t9 - t10 - t11 + t12))
            cal = cal +(p * G)
        calc = cal * 0.00667
        np.append(calculated_gravity,calc)
    return calculated_gravity

result = gravity_calculator(xi,yi,initial_depth,dx,dy,-0.4)
print(result)

最佳答案

abs() 仅采用一个参数,即要返回其绝对值的数字。参数可以是整数、 float 或复数。
尝试:

np.absolute()

关于python - 如何解决 "Invalid use of Function(<built-in function abs>)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56984179/

相关文章:

python - 实现非阻塞等待的最佳方式?

python - 值错误:must start with a character '-'

python - 在 Python 与 Matlab 中减去 3D numpy 数组

python - 使用 numba jit,Python 的段间距离

python - 在 Python/Numba 中访问数组会产生奇怪的结果

python - @njit 在组合数组方面的工作问题

python - 无法使用 tkinter 取消绑定(bind)函数

Python:何时使用线程与多处理

python - Keras, tensorflow 在 Sublime Text 和spyder 中导入错误,但在命令行中工作

python - 在 SQLAlchemy 模型字段上接受多种类型