简体   繁体   English

类型错误:ufunc 的循环不支持没有可调用 sin 方法的 Add 类型的参数 0

[英]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!在您知道自己在做什么之前,不要混合使用sympynumpy


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

pure sympy纯粹的同情

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.

相关问题 TypeError:ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method ufunc 的到期循环中的错误不支持 str 类型的参数 0,该参数没有可调用的日志方法 - Error in due loop of ufunc does not support argument 0 of type str which has no callable log method ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - loop of ufunc does not support argument 0 of type float which has no callable exp method python3 np.exp(matrix1 * matrix2)中的错误-“ufunc循环不支持float类型的参数0,它没有可调用的exp方法” - Error in python3 np.exp(matrix1 * matrix2) - “loop of ufunc does not support argument 0 of type float which has no callable exp method” nplog“TypeError:ufunc循环不支持int类型的参数0”后的PYTHON错误 - PYTHON Error after nplog "TypeError: loop of ufunc does not support argument 0 of type int " 错误类型 ufunc 的错误循环不支持参数 0 pyomo - Error TypeError loop of ufunc does not support argument 0 pyomo “ufunc循环不支持Mul类型的参数0”是什么意思? - What does "loop of ufunc does not support argument 0 of type Mul" mean? 为什么我会收到日志方法的“ufunc 循环不支持 numpy.ndarray 类型的参数 0”错误? - Why do I get the 'loop of ufunc does not support argument 0 of type numpy.ndarray' error for log method? 为什么我会收到 numpy.exp 的“ufunc 循环不支持 int 类型的参数 0”错误? - Why do I get the 'loop of ufunc does not support argument 0 of type int' error for numpy.exp? 尝试集成 function 给出“ufunc 循环不支持 Symbol 类型的参数 0” - Trying to integrate function gives “loop of ufunc does not support argument 0 of type Symbol”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM