简体   繁体   中英

Print x/2 as 1/2 x in Sympy

How can I tell Sympy to print x/2 as 1/2 x for example? Ie print a fraction as 1 over the denominator times the numerator?

To do this properly you would have to write your own printer, I believe. To do this hackishly you might try:

mul = x/2
a, b = mul.as_two_terms()
if a.is_Rational and not a.is_Integer:
    print (Mul(Symbol(str(a.p))/a.q, b, evaluate=False))
else:
    print (mul)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM