If you are using the Jupyter notebook you can run

from sympy import init_session
init_session(auto_int_to_Integer=True)

This also works in IPython in the terminal, or you can use isympy -i.
This will automatically wrap integer literals with Integer() so that
1/2 produces Integer(1)/Integer(2).  Just be aware that this could
break non-SymPy things if they don't work properly with Integer, for
example, numpy will create arrays with dtype=object when passed
Integer. You can wrap integers in int() to fix this, like
np.array([int(1)]).

If the issue is only for copy-pasting, I would just use sympify() to
convert the input as a string.

> I know you can always input Rational(3/2), but that seems very clumsy.

You should use Rational(3, 2) with the comma. Rational(3/2) will
convert 3/2 to a float first and then convert it to a rational. That
works fine for 3/2 because 3/2 is exactly representable as a floating
point number, but for others it isn't

>>> Rational(1/3)
6004799503160661/18014398509481984
>>> Rational(1, 3)
1/3

Aaron Meurer

On Mon, Jun 1, 2020 at 5:15 PM David Bailey <[email protected]> wrote:
>
> Dear Group,
>
> It seems unfortunate that Python evaluates fractions such as 1/2 to 0.5, 
> whereas SymPy recognises that in algebraic expressions fractions should 
> rarely be evaluated to a decimal number. This means that if, for example, you 
> input 3*x**2/2, you get a nice algebraic expression, but if you enter
>
>  (3/2)*x**2
>
> you get
>
> 1.5*x^2
>
> This problem is particularly unfortunate if you cut and paste expressions a 
> lot, as I tend to do.
>
> I wonder if any thought has been given to this - I know you can always input 
> Rational(3/2), but that seems very clumsy.
>
> David
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/79efcea3-13ec-d997-5461-8be0536b6431%40dbailey.co.uk.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6JLN-yX0WeYHv257_ckPB2NFXPMskvzj6wpnP7K7qN9sg%40mail.gmail.com.

Reply via email to