Sean McVeigh
Sean McVeigh

Reputation: 599

SymPy cannot compile equation

I'd planned on using SymPy to evaluate some chain derivatives to check my work by hand. Unfortunately, the first equation will not compile. Does anyone know what is going on here:

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))))

Raises:

---------------------------------------------------------------------------
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

Upvotes: 1

Views: 149

Answers (1)

DSM
DSM

Reputation: 353329

mpmath is really for numerical calculations, not symbolic ones. You're pulling in numerical functions, and mpmath is rightly objecting it doesn't know how to create an mpf (the mpmath float) from the sympy Symbol "t".

If you want symbolics, use symbolics:

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)

Upvotes: 4

Related Questions