用乳胶显示时如何确定多项式的排列?

问题描述 投票:0回答:1

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

代码:

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)$_"

输出:

enter image description here

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

我已经尝试将顺序更改为'lex',但既不起作用,.expand()或mode = equation也没有

python latex sympy
1个回答
0
投票
一般来说,这是不可能的,因为SymPy表达式在每次操作时都会重新排序,甚至只是将表达式转换为内部格式也可以。

以下代码可能适用于您的特定情况:

from sympy import * from functools import reduce a, b, c, m, n, x = symbols("a b c m n x") f = (a * x ** m) + (b * x ** n) + c a = 9 b = -4 c = 4 m = -Integer(1)/2 n = 3 repls = ('a', latex(a)), ('+ b', latex(b) if b < 0 else "+"+latex(b)), \ ('+ c', latex(c) if c < 0 else "+"+latex(c)), ('m', latex(m)), ('n', latex(n)) f_tex = reduce(lambda a, kv: a.replace(*kv), repls, latex(f)) # only now the values of the variables are filled into f, to be used in further manipulations f = (a * x ** m) + (b * x ** n) + c

[f_tex中留下以下内容:

9 x^{- \frac{1}{2}} -4 x^{3} 4

© www.soinside.com 2019 - 2024. All rights reserved.