Expressing exponents as fractions (not decimals)

80 views
Skip to first unread message

Fritz Sonnichsen

unread,
Oct 26, 2023, 10:16:53 PM10/26/23
to sympy
In this simple string below, the result is a decimal. I have fooled a lot with the documentation but cannot see how to get my output as fractions instead of decimals. (In fact I thought a CAS would default to this). 
What is the simplest way to stop the 1-1/4 rendering to 1.25 (e.g. I want 5/4 here)? A global setting would be nice if one exists!

Thanks
Fritz

P=(m*w)*(m*w)**(1/4)
x = collect(P,m*w)
print("P is:    " + str(x))

P is:    (m*w)**1.25

gu...@uwosh.edu

unread,
Oct 27, 2023, 9:17:11 AM10/27/23
to sympy
Within an IPython session this can be achieved with `sympy.interactive.session.enable_automatic_int_sympification(shell)`. See the documentation: https://docs.sympy.org/latest/modules/interactive.html#module-sympy.interactive.

An alternative is to use Algebra_with_Sympy, which defaults to your desired behavior, but has a relatively simple way to turn the feature on and off.

I think these provide the behavior you looking for.

Chris Smith

unread,
Oct 27, 2023, 1:58:14 PM10/27/23
to sympy
Enter the exponent as `(S(1)/4)` or the power as `root(m*w, 4)`.

/c

On Thursday, October 26, 2023 at 9:16:53 PM UTC-5 sonn...@gmail.com wrote:

Fritz Sonnichsen

unread,
Oct 27, 2023, 9:59:08 PM10/27/23
to sy...@googlegroups.com
OK--thank you to both of you for the advice. I think Jonathan's method is inclined towards interactive use but allows global settings. Chris's method works for my inline code methods (but wish they had global variables there!). 
Much appreciated
Fritz

--
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 sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/7c900e18-3cfc-4b69-93c4-dc81a76a6108n%40googlegroups.com.

Sangyub Lee

unread,
Nov 15, 2023, 7:31:05 AM11/15/23
to sympy
>  In fact I thought a CAS would default to this
> Chris's method works for my inline code methods (but wish they had global variables there!). 

Division of numeric literals is an hardcoded behavior of Python interpreter, and a hardcoded behavior of Python language itself.
So it is not possible to work around that for SymPy.
(other than modifying the IPython interpreter, or parsing strings like sympy_parser)
Reply all
Reply to author
Forward
0 new messages