Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当用乳胶显示多项式时,如何确定多项式的排列?_Python_Latex_Sympy - Fatal编程技术网

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

Python 当用乳胶显示多项式时,如何确定多项式的排列?,python,latex,sympy,Python,Latex,Sympy,我不确定这是我的python代码还是latex的问题,但它在输出中不断地重新排列我的等式 代码: 输出: 在这种情况下,a=9,b=-4,c=4,m=-1/2,n=3,我希望输出的顺序是变量f 我尝试过将顺序更改为“lex”,但这不起作用。expand或mode=equation通常情况下,这是不可能的,因为每次操作都会对SymPy表达式进行重新排序,甚至只是将表达式转换为内部格式 以下是一些可能适用于您的特定情况的代码: from sympy import * from functools i

我不确定这是我的python代码还是latex的问题,但它在输出中不断地重新排列我的等式

代码:

输出:

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


我尝试过将顺序更改为“lex”,但这不起作用。expand或mode=equation

通常情况下,这是不可能的,因为每次操作都会对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 
StrPrinter有一个订购选项。如果将订单设置为“无”,然后将未计算的添加传递到“打印添加”,则可以获得所需的结果

>>> 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

你能解释一下tools.polytex的确切定义吗?
9 x^{- \frac{1}{2}} -4 x^{3} 4
>>> 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