question about Rational

19 views
Skip to first unread message

Thomas Ligon

unread,
Jan 6, 2021, 12:16:46 PM1/6/21
to sympy
If I use 1/3 in an expression, I get 0.333..., but I know that I can write Rational(1,3) instead. Now I have some expressions, such as
-((s)/(j))*((4*(j-1)*s+4*j**2+4*j-2-4*(s-j+1)*m+m**2)/(2*(4*j**2-1)-4*m+m**2))
where m is a symbol that will ultimately be replaced by a floating-point number, and s and j are integers (positive or negative, but never zero). They are replaced (using subs), and when I substitute s by 3 and j by 1, I get 0.333.... In the formula, I can't write Rational(s,j), because Rational does not accept a symbol as an argument.
Is there a way to do this?

gu...@uwosh.edu

unread,
Jan 6, 2021, 12:31:48 PM1/6/21
to sympy
Thomas,
These look like quantum energy level equations. Have you looked at sympy.physics.quantum to see if they have solved your problem? I seem to remember they have defined some special quantities for quantum numbers that are limited to integer and half-integer values. I make no promises as I have not actually used the package. This is just a recollection from scanning through the documents.

Jonathan

Thomas Ligon

unread,
Jan 6, 2021, 1:27:39 PM1/6/21
to sympy
Jonathan,
thanks for the quick response. This is celestial mechanics, not quantum mechanics, but I will take a look at the package you recommended to see if there is anything I can use. The problem I am looking at is very old, and all solutions created up to now are either approximations or numerical calculations. I am looking to see if I can find an analytical solution for any of the questions involved in the problem, and Sympy is a good way to explore this, even though the questions I am examining are basically pure mathematics and not physics.

Aaron Meurer

unread,
Jan 6, 2021, 3:24:02 PM1/6/21
to sympy
Rational is only needed if you have the division of integer literals,
because SymPy cannot control how Python evaluates 1/3. Once you have a
fraction in an expression, it will stay exact:

>>> (-((s)/(j))*((4*(j-1)*s+4*j**2+4*j-2-4*(s-j+1)*m+m**2)/(2*(4*j**2-1)-4*m+m**2))).subs({s: 3, j: 1})
-3*(m**2 - 12*m + 6)/(m**2 - 4*m + 6)

It's not necessary to use Rational to create a symbolic fraction.
Using / as you have done is the correct way to do that.

It's possible I'm misunderstanding your question because what you
described is not what happens. The problem that you are perhaps having
is that if one of the numbers you substitute is substituted as a
float, then SymPy will evaluate the entire expression as a float.

>>> (-((s)/(j))*((4*(j-1)*s+4*j**2+4*j-2-4*(s-j+1)*m+m**2)/(2*(4*j**2-1)-4*m+m**2))).subs({s: 3, j: 1, m: 1.0})
5.00000000000000

If you want an exact number in your answer then all your numeric
constants should be rational numbers rather than floats.

Aaron Meurer
> --
> 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/aba4d9d8-4533-4649-a9b4-ea0e83116e20n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages