AttributeError:“Mul”对象没有属性“sqrt”

2024-10-02 00:35:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到标题中所述的错误。完全错误:

MaxD = Cone*np.sqrt(SymsX/np.pi)*np.exp((-SymsX/(k*T))) #Define Maxwellian distribution function

AttributeError: 'Mul' object has no attribute 'sqrt'

代码如下:

^{2}$

我还有另外一个问题。我有时看到例子用“from。。。导入。为什么会这样?仅仅导入整个库就够了吗?是不是因为使用import命令实际上并没有导入整个库,而只导入最基本的函数?在


Tags: 标题object错误nppifunctionsqrtdistribution
2条回答

isympy会话中:

In [1]: import numpy as np                                                      

In [3]: SymsX = Symbol('SymsX')                                                 

In [5]: SymsX/np.pi                 # symbol * float                                                             
Out[5]: 0.318309886183791⋅SymsX

In [6]: SymsX/pi                    # symbol * symbol                            
Out[6]: 
SymsX
─────
  π  

In [7]: sqrt(SymsX/pi)             # sympy sqrt                           
Out[7]: 
  _______
╲╱ SymsX 
─────────
    √π   

In [8]: np.sqrt(SymsX/pi)          # numeric sqrt                                 
                                     -
AttributeError                            Traceback (most recent call last)
AttributeError: 'Mul' object has no attribute 'sqrt'

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

TypeError                                 Traceback (most recent call last)
<ipython-input-8-27f855f6b3e2> in <module>
  > 1 np.sqrt(SymsX/pi)

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

np.sqrt必须首先将其输入转换为numpy数组:

^{pr2}$

这是一个对象数据类型数组,不是普通的数值数组。给定这样的数组,qnumpyufunc尝试将操作委托给element方法。e、 g.(0.31*SymsX).sqrt()

乘法和加法可用于此对象数组:

In [11]: 2*_                                                                    
Out[11]: 0.636619772367581⋅SymsX

In [12]: _ + __                                                                 
Out[12]: 0.954929658551372⋅SymsX

因为sympy对象具有正确的加法和乘法方法:

In [14]: Out[5].__add__                                                         
Out[14]: <bound method Expr.__add__ of 0.318309886183791*SymsX>

In [15]: Out[5]+2*Out[5]                                                        
Out[15]: 0.954929658551372⋅SymsX

===

sympy.lambdify是同时使用sympy和{}的最佳工具。查一下它的文件。在

在这种情况下,SymsX/pi表达式可以转换为numpy表达式,其中:

In [18]: lambdify(SymsX, Out[5],'numpy')                                        
Out[18]: <function _lambdifygenerated(SymsX)>

In [19]: _(23)            # evaluate with `SymsX=23`:                                                                  
Out[19]: 7.321127382227194

In [20]: 23/np.pi                                                               
Out[20]: 7.321127382227186

In [21]: np.sqrt(_19)        # np.sqrt now works on the number                            
Out[21]: 2.7057581899030065

===

sympy中的相同评估:

In [23]: expr = sqrt(SymsX/pi)                                                  

In [24]: expr                                                                   
Out[24]: 
  _______
╲╱ SymsX 
─────────
    √π   

In [25]: expr.subs(SymsX, 23)                                                   
Out[25]: 
√23
───
 √π

In [27]: _.evalf()                                                              
Out[27]: 2.70575818990300

在新的isympy会话中:

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at https://docs.sympy.org/1.4/


In [1]: EpNaut = 8.854187E-12 
   ...: u0 = 1.256E-6 
   ...: k = 1/(4*pi*EpNaut) 
   ...: NumGen = 1000  
   ...: T = 1000  
   ...: MaxEn = 7*T*k  
   ...: Cone = 2/((k*T)**(3/2)) 
   ...:  
   ...: SymsX = Symbol('SymsX') 
   ...: MaxD = Function('MaxD') 
   ...: PFunction = Function('PFunction') 
   ...: MaxD = Cone*sqrt(SymsX/pi)*exp((-SymsX/(k*T))) #Define Maxwellian distri
   ...: bution function 
   ...: PFunction = integrate(MaxD) #Integrate function to get probability-error
   ...:  function 
   ...:                                                                         

结果是:

^{pr2}$

SymsX仍然是一个符号,因此这些是sympy表达式,而不是数字。在

相关问题 更多 >

    热门问题