sympy returns TypeError when putting irrational in a fraction

Viewed 281

I have tried to put this in sympy, but it doesn't work:

import sympy
sympy.init_printing(use_unicode=True)

sympy.Rational(3, sympy.sqrt(3))

It returns to me the following:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-25b0450ea0d0> in <module>()
      1 root = sympy.sqrt(3)
      2 
----> 3 sympy.Rational(3, sympy.sqrt(3))

~\Anaconda3\lib\site-packages\sympy\core\numbers.py in __new__(cls, p, q, gcd)
   1488         else:
   1489             p = Rational(p)
-> 1490             q = Rational(q)
   1491 
   1492         if isinstance(q, Rational):

~\Anaconda3\lib\site-packages\sympy\core\numbers.py in __new__(cls, p, q, gcd)
   1483 
   1484             if not isinstance(p, SYMPY_INTS + (Rational,)):
-> 1485                 raise TypeError('invalid input: %s' % p)
   1486             q = q or S.One
   1487             gcd = 1

TypeError: invalid input: sqrt(3)

Why does this happen? How can I put an irrational number inside a fraction in sympy, then?

1 Answers

To create the ratio, use 3/sqrt(3); to print it as such requires the use of unevaluated expressions: (Mul(3,Pow(sqrt(3),-1, evaluate=False),evaluate=False).

Related