【问题标题】:TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin methodTypeError:ufunc 的循环不支持 Add 类型的参数 0,它没有可调用的 sin 方法
【发布时间】:2021-12-13 04:47:44
【问题描述】:
import math
import numpy as np
import matplotlib.pyplot as plt
import simpy as sp
init_printing()

var('x,y,z')

a = math.sqrt(10/7)    
c = (y-math.sqrt(18/7)-1)    
b = np.sin(math.pi/2*c)-z

f = lambda x,y,z: x**2+y**2+z**2-4
g = lambda x, y, z: (9/2)*(x**2+z**2)+y**2-9
h = lambda y, z: np.sqrt(10/7) * np.sin((np.pi/2) * y - np.sqrt(18/7)-1)- z
         
F = [[f(x, y, z)],
     [g(x, y, z)],
     [h(y, z)]]

当我尝试定义函数 h(x) 时,我明白了:

AttributeError                            Traceback (most recent call last)
AttributeError: 'Add' object has no attribute 'sin'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-68-da3a7e28861c> in <module>

         10 a = math.sqrt(10/7)
     11 c = (y-math.sqrt(18/7)-1)
---> 12 b = np.sin((math.pi/2*c))-z
     13 
     14 

TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method

我尝试将函数分成 a、b 和 c 部分,以便更容易发现错误。现在我知道var('x, y, z') 和np.sin()/math.sin() 有问题,但我找不到解决方法。

【问题讨论】:

  • 首先,您的错误出现在第一行中。无需发布其余代码并污染问题。二、var是什么?它在哪里定义? (init_printing() 也未定义,但无论如何我认为这无关紧要。)总的来说,我认为您将 NumPy 和 SimPy 混为一谈,但您的问题中没有足够的信息来提供正确的答案。

标签: python numpy


【解决方案1】:

密切关注变量的type。如有疑问,请检查,不要只是猜测或假设

var('x,y,z')

这些是sympy 符号(虽然我在这里猜测var 在做什么)

a = math.sqrt(10/7)

这必须是一个 python 浮点数,由 math.sqrt 函数生成。

c = (y-math.sqrt(18/7)-1)

假设y 是sympy,那么c 本身就是sympy 表达式,可能是Add。

b = np.sin(math.pi/2*c)-z

math.pi/2*c 是同情的表达。

np.sin 是一个 numpy ufunc,仅适用于 numpy 数组。这意味着它首先会这样做

np.array(math.pi/2*c)

制作单元素对象 dtype 数组。

np.sin 给定一个对象 dtype 数组,将任务传递给该对象的 sin 方法。但是没有人定义sin 方法。它始终是一个函数。

在sympy 表达式上使用np.sin 或许多其他numpy 函数是行不通的。在您知道自己在做什么之前,不要混用 sympy 和 numpy!


In [5]: sp.var('x,y,z')
Out[5]: (x, y, z)
In [6]: y
Out[6]: y
In [7]: a = math.sqrt(10/7)
   ...: c = (y-math.sqrt(18/7)-1)
In [8]: a
Out[8]: 1.1952286093343936
In [9]: c
Out[9]: y - 2.60356745147455
In [10]: type(c)
Out[10]: sympy.core.add.Add
In [11]: math.pi/2*c
Out[11]: 1.5707963267949*y - 4.08967418933897
In [12]: np.array(_)
Out[12]: array(1.5707963267949*y - 4.08967418933897, dtype=object)
In [13]: np.sin(_)
AttributeError: 'Add' object has no attribute 'sin'

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "<ipython-input-13-2b409a55b2a2>", line 1, in <module>
    np.sin(_)
TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method

纯同情

In [14]: a = sp.sqrt(10/7)
    ...: c = (y-sp.sqrt(18/7)-1)
In [15]: c
Out[15]: y - 2.60356745147455
In [16]: sp.pi/2*c
Out[16]: pi*(y - 2.60356745147455)/2
In [17]: sp.sin(sp.pi/2*c)
Out[17]: sin(pi*(y/2 - 1.30178372573727))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2014-03-25
    相关资源
    最近更新 更多