AttributeError:"Mul"对象没有属性"sqrt" [英] AttributeError: 'Mul' object has no attribute 'sqrt'
问题描述
我收到标题中指出的错误.完整错误:
I am receiving the error stated in the title. Full error:
MaxD = Cone*np.sqrt(SymsX/np.pi)*np.exp((-SymsX/(k*T))) #Define Maxwellian distribution function
AttributeError: 'Mul' object has no attribute 'sqrt'
这是代码:
from sympy.interactive import printing
printing.init_printing(use_latex = True)
import numpy as np
from sympy import Eq, dsolve, Function, Symbol, symbols
import sympy as sp
EpNaut = 8.854187E-12
u0 = 1.256E-6
k = 1/(4*np.pi*EpNaut)
NumGen = 1000 #How many solution points user wants to generate between 0 and maxen (Higher # the more accurate)
T = 1000 #Temperature in (K)
MaxEn = 7*T*k #Max energy in system
Cone = 2/((k*T)**(3/2)) #Constant infront of the Maxwellian distribution function
SymsX = sp.Symbol('SymsX')
MaxD = Function('MaxD')
PFunction = Function('PFunction')
MaxD = Cone*np.sqrt(SymsX/np.pi)*np.exp((-SymsX/(k*T))) #Define Maxwellian distribution function
PFunction = sp.integrate(MaxD) #Integrate function to get probability-error function
print(PFunction)
我还有一个问题.我有时会看到示例使用从...导入...".为什么是这样?仅仅导入整个库是否就足够了?是因为使用import命令实际上并没有导入整个库,而是实际上只是最基本的功能?
I also have an additional question. I sometimes see examples use "from ... import ...". Why is this? Shouldn't just importing the entire library be enough? Is it because using the import command doesn't actually import the entire library but really just the most basic functions?
推荐答案
在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数组:
np.sqrt
has to first convert its input into a numpy array:
In [10]: np.array(SymsX/np.pi)
Out[10]: array(0.318309886183791*SymsX, dtype=object)
这是一个对象dtype数组,不是普通的数字数组.给定这样的数组,q numpy ufunc
尝试将操作委派给元素方法.例如(0.31*SymsX).sqrt()
This is an object dtype array, not a normal numeric one. Given such an array, q numpy ufunc
tries to delegate the action to a element method. e.g. (0.31*SymsX).sqrt()
乘法和加法可与此对象数组一起使用:
Multiply and addition do work with this object array:
In [11]: 2*_
Out[11]: 0.636619772367581⋅SymsX
In [12]: _ + __
Out[12]: 0.954929658551372⋅SymsX
这些工作是因为sympy
对象具有正确的加法和乘法方法:
These work because the sympy
object has the right add and multiply methods:
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
和numpy
一起使用的最佳工具.查找其文档.
The sympy.lambdify
is the best tool for using sympy
and numpy
together. Look up its docs.
在这种情况下,可以使用以下方法将SymsX/pi
表达式转换为numpy表达式:
In this case the SymsX/pi
expression can be converted into a numpy expression with:
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
这篇关于AttributeError:"Mul"对象没有属性"sqrt"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!