sympy 将分数与变量分开 [英] sympy separate fractions from variables

查看:34
本文介绍了sympy 将分数与变量分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 sympy 如何将分数与变量分开

Using sympy how do I keep fractions separate from variables

Mul(Fraction(3,5), Pow(K, Integer(2)))


     2          
  3⋅K         
 ──── 
   5          

to 

               
 3  2      
 ─ K
 5 

我知道这个简化版还不错,但是当我有很大的方程时,它会变得很乱

I know this simplified version is not too bad, but when i have really big equations, it gets messy

推荐答案

我对漂亮打印或 LaTeX 打印不是很熟悉,但我设法想出了一些东西.将 UnevaluatedExpr 放在 Mul 的每个参数中:

I'm not very familiar with pretty printing or LaTeX printing but I managed to come up with something. Put UnevaluatedExpr in each of the arguments of Mul:

from sympy import *
from fractions import Fraction

K = symbols("K")
expr1 = Mul(UnevaluatedExpr(Fraction(3,5)), UnevaluatedExpr(Pow(K, Integer(2))))
expr2 = Mul(UnevaluatedExpr(pi/5), UnevaluatedExpr(Pow(K, Integer(2))))
expr3 = ((UnevaluatedExpr(S(1)*3123456789/512345679) * UnevaluatedExpr(Pow(K, Integer(2)))))
pprint(expr1)
pprint(expr2)
pprint(expr3)

产生:

     2
3/5⋅K 

π  2
─⋅K 
5   

1041152263  2
──────────⋅K 
170781893   

我找不到办法让它为斜线分数3/5打印堆叠分数.不过,更长的分数似乎有效.但是,如果您在 LaTeX 中打印,文档 建议类似 latex(expr1, fold_frac_powers=False) 来纠正这个问题.

I couldn't find a way to make it print a stacked fraction for the slashed fraction 3/5. Longer fractions seem to work though. If you are printing in LaTeX however, the documentation suggests something like latex(expr1, fold_frac_powers=False) to correct this.

太糟糕了,我找不到一个优雅的解决方案,比如将 init_printing(div_stack_symbols=False) 放在文档的顶部.

Too bad I couldn't find an elegant solution like putting init_printing(div_stack_symbols=False) at the top of the document.

这篇关于sympy 将分数与变量分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆