Rational with Symbol

20 views
Skip to first unread message

Nico Schlömer

unread,
Oct 4, 2017, 5:04:43 AM10/4/17
to sympy
I have a function that takes in an integer value and returns a symbolic fraction of it, e.g.,
```
def r(n):
    return sympy.Rational(n, 261)
```
I would like to be able to provide integers and sympy.Symbols alike, but the latter fails with
```
TypeError: invalid input: n
```
-- Right, one can simply use `n/261`.

Since Rationals appear quite frequently in my code, I would like to avoid constructions like
```
x = n/261 if isinstance(n, sympy.Atom) else sympy.Rational(n, 261)
```
(where sympy.Atom wouldn't even cover cases like `2n+1` in either numerator or denominator).

Is there a way to support integer and symbolic fractions from one interface?

Cheers,
Nico

Kalevi Suominen

unread,
Oct 4, 2017, 7:00:32 AM10/4/17
to sympy

You could probably use  n/sympy.Integer(261),  or briefly  n/S(261)  where  S = sympy.sympify.

Kalevi Suominen
Reply all
Reply to author
Forward
0 new messages