SymPy无法编译方程式

时间:2018-02-27 19:10:18

标签: python sympy

我打算使用SymPy评估一些链衍生物来手工检查我的工作。不幸的是,第一个等式不会编译。有谁知道这里发生了什么:

from sympy import symbols, diff
from mpmath import sinh, atanh, sqrt, power

tau, epsilon = symbols("t e")
sigma = sinh(epsilon * atanh(epsilon * tau / sqrt(1 + power(tau,2))))

引发:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-2b0681b61a82> in <module>()
      7 tau, epsilon = symbols("t e")
----> 8 sigma = sinh(epsilon * atanh(epsilon * tau / sqrt(1 + power(tau,2))))

~[...]/venv/lib/python3.6/site-packages/mpmath/ctx_base.py in power(ctx, x, y)
    427             3.16470269330255923143453723949e+12978188
    428         """
--> 429         return ctx.convert(x) ** ctx.convert(y)
    430 
    431     def _zeta_int(ctx, n):

~[...]/venv/lib/python3.6/site-packages/mpmath/ctx_mp_python.py in convert(ctx, x, strings)
     660         if hasattr(x, '_mpmath_'):
     661             return ctx.convert(x._mpmath_(prec, rounding))
 --> 662         return ctx._convert_fallback(x, strings)
     663 
     664     def isnan(ctx, x):

~[...]/venv/lib/python3.6/site-packages/mpmath/ctx_mp.py in _convert_fallback(ctx, x, strings)
    632             else:
    633                 raise ValueError("can only create mpf from zero-width interval")
--> 634         raise TypeError("cannot create mpf from " + repr(x))
    635 
    636     def mpmathify(ctx, *args, **kwargs):

TypeError: cannot create mpf from t

1 个答案:

答案 0 :(得分:4)

mpmath实际上用于数值计算,而不是符号计算。你正在引入数值函数,而mpmath正确地反对它不知道如何从符号“t”创建一个mpf(mpmath浮点数)。

如果您需要符号,请使用符号:

In [27]: from sympy import symbols, diff

In [28]: from sympy import sinh, atanh, sqrt, Pow

In [29]: tau, epsilon = symbols("t e")
    ...: sigma = sinh(epsilon * atanh(epsilon * tau / sqrt(1 + Pow(tau,2))))
    ...: 

In [30]: sigma
Out[30]: sinh(e*atanh(e*t/sqrt(t**2 + 1)))

In [31]: sigma.diff(tau)
Out[31]: e*(-e*t**2/(t**2 + 1)**(3/2) + e/sqrt(t**2 + 1))*cosh(e*atanh(e*t/sqrt(t**2 + 1)))/(-e**2*t**2/(t**2 + 1) + 1)