[英]TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method
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)]]
As I try to define the fuction h(x)
I get this:当我尝试定义函数
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
I tried to separete the function in parts a, b and c so the error could be more easy to find.我试图将 a、b 和 c 部分中的函数分开,以便更容易找到错误。 Now I know I have some problem with the
var('x, y, z')
and the np.sin()/math.sin()
, but I can't find how to fix it.现在我知道
var('x, y, z')
和np.sin()/math.sin()
有一些问题,但我找不到如何解决它。
Pay close attention to the type
of the variables.密切注意变量的
type
。 When in doubt, check, don't just guess or assume如有疑问,请检查,不要只是猜测或假设
var('x,y,z')
these are sympy
symbols (though here I'm guessing what var
is doing)这些是
sympy
符号(虽然我在这里猜测var
在做什么)
a = math.sqrt(10/7)
this must be a python float, produced by the math.sqrt
function.这必须是由
math.sqrt
函数生成的 python 浮点数。
c = (y-math.sqrt(18/7)-1)
Assuming y
is sympy, then c
itself is a sympy
expression, probably an Add
.假设
y
是 sympy,那么c
本身就是一个sympy
表达式,可能是一个Add
。
b = np.sin(math.pi/2*c)-z
math.pi/2*c
is sympy expression. math.pi/2*c
是 sympy 表达式。
np.sin
is a numpy ufunc
, and only works with numpy arrays. np.sin
是numpy的ufunc
,只有用numpy的阵列工作。 That means it first does这意味着它首先会
np.array(math.pi/2*c)
making a single element object dtype array.制作单个元素对象 dtype 数组。
np.sin
given an object dtype array, passes the task to the sin
method of that object. np.sin
给定一个对象np.sin
数组,将任务传递给该对象的sin
方法。 But noone defines a sin
method.但是没有人定义
sin
方法。 It's always a function.它始终是一个函数。
Using np.sin
, or many other numpy
functions on sympy
expressions just does not work.在
sympy
表达式上使用np.sin
或许多其他numpy
函数是行不通的。 Don't mix sympy
and numpy
until you know what you are doing!在您知道自己在做什么之前,不要混合使用
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.