用乳胶显示时如何确定多项式的排列? [英] How to determine the arrangement of the polynomial when displaying it with latex?

查看:32
本文介绍了用乳胶显示时如何确定多项式的排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是我的python代码还是乳胶问题,但它会不断在输出中重新排列我的方程式.

I am not sure if it is an issue with my python code or with the latex but it keeps rearranging my equation in the output.

代码:

ddx = '\\frac{{d}}{{dx}}'

f = (a * x ** m) + (b * x ** n) + d
df = sym.diff(f)

df_string = tools.polytex(df)
f_string = tools.polytex(f)

question_stem = f"Find $_\\displaystyle {ddx}\\left({f_string}\\right)$_"

输出:

在这种情况下, a = 9 b = -4 c = 4 m =(-1/2) n = 3 ,我希望输出按变量f的顺序进行.

In this case a = 9, b = -4, c = 4, m = (-1/2), n = 3 and I want the output to be in the order of the variable f.

我尝试将顺序更改为"lex",但是.expand()或mode = equation也无效

I have tried changing the order to 'lex' and that did not work nor did .expand() or mode = equation

推荐答案

StrPrinter 有一个 order 选项.如果将顺序设置为无",然后将未评估的添加"传递给 _print_Add ,则可以得到所需的结果.

There is an order option for the StrPrinter. If you set the order to 'none' and then pass an unevaluated Add to _print_Add you can get the desired result.

>>> from sympy.abc import a,b,c,x,m,n
>>> from sympy import S
>>> oargs = Tuple(a * x ** m, b * x ** n,  c) # in desired order
>>> r = {a: 9, b: -4, c: 4, m: -S.Half, n: 3}
>>> add = Add(*oargs.subs(r).args, evaluate=False) # arg order unchanged
>>> StrPrinter({'order':'none'})._print_Add(add)
9/sqrt(x) - 4*x**3 + 4

这篇关于用乳胶显示时如何确定多项式的排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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