gpt4 book ai didi

python - SymPy 无法编译方程

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:06 25 4
gpt4 key购买 nike

我计划使用 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

最佳答案

mpmath 真正用于数值计算,而不是符号计算。您正在引入数值函数,并且 mpmath 正确地反对它不知道如何从 sympy 符号“t”创建 mpf(mpmath float )。

如果你想要符号,使用符号:

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)

关于python - SymPy 无法编译方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com