强制 SymPy 保持术语的顺序 [英] Force SymPy to keep the order of terms

查看:26
本文介绍了强制 SymPy 保持术语的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

from sympy import *
init_printing()

x,y = symbols('x y')
u = Function('u')(x,y)
ux,uy,uxx,uxy,uyy = symbols("u_x u_y u_xx u_xy u_yy")

mainEvaluation = uxx - 2*sin(x)*uxy - (cos(x) ** 2) * uyy - 2*ux + (2 - cos(x) + 2*sin(x) )*uy

print(mainExpression)的输出是

-2*u_x + u_xx - 2*u_xy*sin(x) + u_y*(2*sin(x) - cos(x) + 2) - u_yy*cos(x)**2

问题是:我想要变量的原始顺序.

The problem is: I want the original order of variables.

u_xx - 2*u_xy*sin(x)  - u_yy*cos(x)**2  - 2*u_x + u_y*(2*sin(x) - cos(x) + 2)

所有这些都是在 IPython notebook 中完成的.有什么办法保持秩序吗?

All this is done in IPython notebook. Is there any way to keep order?

推荐答案

遗憾的是,SymPy 不跟踪输入顺序(请参阅我在该问题的评论中链接的另一个问题).您可以定义自己的排序函数,根据需要对表达式进行排序,但无法完全按照输入的顺序排序,因为该信息不会保存.

Sadly, SymPy does not keep track of the input order (see the other question I linked in a comment on the question). You can define your own ordering function that orders expressions however you want, but there's no way to order things exactly as they were input, since that information isn't saved.

这篇关于强制 SymPy 保持术语的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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