如何修改sympy的打印顺序? [英] How to modify sympy's printing order?
问题描述
我正在使用 sympy 编写代数表达式并使用它们执行基本计算.Sympy 没有跟踪变量的顺序,这在打印表达式时可能是一个问题(这个问题已经被提出 此处 和此处,所以这不是重复的).例如
<预><代码>>>>从 sympy 导入 *>>>var("p,a")>>>l=p-1-a;>>>打印(l);-a+p-1然而,sympy 似乎按字母顺序打印变量.有没有办法改变 Python 所指的字母顺序,从而诱使 sympy 以所需的顺序打印变量?欢迎任何其他解决方案!
感谢 smichr 的回答,我写了一个自定义打印机,它可以满足我的需求.我不是一个好的程序员,所以如果你对我的代码有任何建议,我很乐意实现它们.
from sympy import *导入数学导入副本进口藏品导入迭代工具init_printing(use_unicode=True)var("p,a,b,c,d,e,f");ddict=collections.OrderedDict([(p, 1),(a, 2), (b, 3), (c, 4),(d, 5),(e, 6),(f, 7),])from sympy import Basic, Function, Symbol从 sympy.printing.printer 导入打印机从 sympy.printing.latex 导入 print_latex从 sympy.core.basic 导入基本类 MyPrinter(打印机):打印方法 = '_myprinter'def _print_Add(self,expr):expr_args=expr.argsdef new_place(el):如果 el 在 ddict 中:返回 ddict[el]别的:返回 len(ddict)+1def get_place(el):如果 el.is_integer:返回 new_place(el)elif el.is_symbol:返回 new_place(el)elif len(el.args)>0:如果 el.args[len(el.args)-1].is_symbol:返回 new_place(el.args[len(el.args)-1])别的:返回 0别的:返回 0def write_coeff(el):如果 el.is_integer:如果el>0:返回 "+%s" %el别的:返回 "%s" %elelif el.is_symbol:返回 "+%s" %elelif len(el.args)>0:如果 el.args[len(el.args)-1].is_symbol:如果 el.args[0].is_rational:如果 el.args[0]>0:返回 "+%s" %latex(el)别的:返回 "%s" %latex(el)别的:返回 "%s" %latex(el)别的:返回 "%s" %latex(el)别的:返回 "%s" %ellist_place=[get_place(a) for a in expr.args]expr_args=zip(*sorted(zip(list_place,expr_args)))[1]to_print=[write_coeff(a) for a in expr_args]to_print[0]=str(latex(expr_args[0]))return "".join(a for a in to_print)def my_printer(expr):返回 (MyPrinter().doprint(expr))de=-a+p+3+c-b打印(my_printer(de))
I am using sympy to write algebraic expressions and perform basic calculations with them. Sympy does not keep track of the order of the variables which can be a problem when it comes to printing expressions (the issue has already been raised here and here, so this is not a duplicate). e.g.
>>> from sympy import *
>>> var("p,a")
>>> l=p-1-a;
>>> print(l);
-a+p-1
However, sympy seems to print the variables in the alphabetical order. Is there a way to change the alphebetical order Python refers to and thus trick sympy into printing the variables in the desired order? Any other solution is welcome!
Thanks to smichr's answer, I wrote a custom printer which does what I want. I am not a good programmer, so if you have any suggestions to make on my code, I would be happy to implement them.
from sympy import *
import math
import copy
import collections
import itertools
init_printing(use_unicode=True)
var("p,a,b,c,d,e,f");
ddict=collections.OrderedDict([(p, 1),(a, 2), (b, 3), (c, 4),(d, 5),(e, 6),(f, 7),])
from sympy import Basic, Function, Symbol
from sympy.printing.printer import Printer
from sympy.printing.latex import print_latex
from sympy.core.basic import Basic
class MyPrinter(Printer):
printmethod = '_myprinter'
def _print_Add(self,expr):
expr_args=expr.args
def new_place(el):
if el in ddict:
return ddict[el]
else:
return len(ddict)+1
def get_place(el):
if el.is_integer:
return new_place(el)
elif el.is_symbol:
return new_place(el)
elif len(el.args)>0:
if el.args[len(el.args)-1].is_symbol:
return new_place(el.args[len(el.args)-1])
else:
return 0
else:
return 0
def write_coeff(el):
if el.is_integer:
if el>0:
return "+%s" %el
else:
return "%s" %el
elif el.is_symbol:
return "+%s" %el
elif len(el.args)>0:
if el.args[len(el.args)-1].is_symbol:
if el.args[0].is_rational:
if el.args[0]>0:
return "+%s" %latex(el)
else:
return "%s" %latex(el)
else:
return "%s" %latex(el)
else:
return "%s" %latex(el)
else:
return "%s" %el
list_place=[get_place(a) for a in expr.args]
expr_args=zip(*sorted(zip(list_place,expr_args)))[1]
to_print=[write_coeff(a) for a in expr_args]
to_print[0]=str(latex(expr_args[0]))
return "".join(a for a in to_print)
def my_printer(expr):
return (MyPrinter().doprint(expr))
de=-a+p+3+c-b
print(my_printer(de))
这篇关于如何修改sympy的打印顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!