我不确定是我的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)$_"
输出:
在这种情况下,a = 9
,b = -4
,c = 4
,m = (-1/2)
,n = 3
,我希望输出按变量f的顺序排列。
我已经尝试将顺序更改为'lex',但既不起作用,.expand()或mode = equation也没有
以下代码可能适用于您的特定情况:
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