繁体   English   中英

TypeError:无法将表达式转换为浮点数 - 定积分 sympy

[英]TypeError: can't convert expression to float - definite integral sympy

import math

import scipy.integrate

import sympy

m1 = sympy.Symbol('m1')

m2 = sympy.Symbol('m2')

s = sympy.Symbol('s')

T = sympy.Symbol('T')

k = sympy.Symbol('k')

integral = sympy.integrate(((k**2)/8 * math.pi)*(1/(math.sqrt(k**2 + m1**2) * math.sqrt(k**2 + m2**2)))*(1/(math.sqrt(s)-math.sqrt(k**2 + m1**2)-math.sqrt(k**2 + m2**2)))
                         * ((1/(math.exp((math.sqrt(k**2 + m1**2))/T)-1))+(1/(math.exp((math.sqrt(k**2 + m2**2))/T)-1))), (k, 10**-15, math.inf))
print(integral)

为了说明`@Oscar 的评论

In [28]: math.sqrt(s)                                                                    
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-443c32f84854> in <module>
----> 1 math.sqrt(s)

/usr/local/lib/python3.6/dist-packages/sympy/core/expr.py in __float__(self)
    323         if result.is_number and result.as_real_imag()[1]:
    324             raise TypeError("can't convert complex to float")
--> 325         raise TypeError("can't convert expression to float")
    326 
    327     def __complex__(self):

TypeError: can't convert expression to float

您的错误的回溯(您应该已经显示。!)与此类似。

使用sympy.sqrt代替:

In [29]: sqrt(s)                                                                         
Out[29]: √s

math.sqrt (和其他函数)是一个数字运算符。 它需要一个数字,并返回一个。 给它一个Symbol会产生这个错误。

将数字函数与符号函数混合时要小心。 这适用于math以及numpyscipy (您导入但不使用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM