简体   繁体   English

在Sympy中将x / 2打印为1/2 x

[英]Print x/2 as 1/2 x in Sympy

How can I tell Sympy to print x/2 as 1/2 x for example? 我如何告诉Sympy将x / 2打印为1/2 x? Ie print a fraction as 1 over the denominator times the numerator? 即在分母上乘分子的分数等于1?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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