如何将 `\n` 传播到 sympy.latex()

问题描述

目标是将具有 6 个以上参数的多项式格式化为绘图标题。 这是字符串表达式函数的多项式参数,灵感来自this answer,后跟sym.latex()

def func(p_list):
    str_expr = ""       
    for i in range(len(p_list)-1,-1,-1):
        if (i%2 == 0 and i !=len(p_list)):
            str_expr =str_expr + " \n "
        if p_list[i]>0:
            sign = " +"
        else:
            sign = ""
        if i > 1:
            str_expr = str_expr+" + %s*x**%s"%(p_list[i],i)
        if i == 1:
            str_expr = str_expr+" + %s*x"%(p_list[i])
        if i == 0:
            str_expr = str_expr+sign+" %s"%(p_list[i])
        print("str_expr",str_expr)
        return sym.sympify(str_expr)
                    
popt = [-2,1,1] # some toy data
tex = sym.latex(func(popt))
print("tex",tex)

输出:

str_expr
  + -1*x**2 + 1*x
  -2
tex - x^{2} + x - 2 

str_expr 中,\n 中的换行符可见,但在 sympy.latex 输出中,它们不见了。

如何传播此换行符?

编辑:我采用@wsdookadr 答案并对其进行了修改,以便 plt.title 将函数的结果作为参数的文本


def tex_multiline_poly(e,chunk_size=2,separator="\n"):
    tex = ""
    # split into monomials
    print("reversed(e.args)",reversed(e.args))
   
    mono = list(e.args)
    print("mono",mono)
    mono.reverse()
    print("mono",mono)
    # we're going split the list of monomials into chunks of chunk_size
    # serialize each chunk,and insert separators between the chunks
    for i in range(0,len(mono),chunk_size):
        chunk = mono[i:i + chunk_size]
        print("sum(chunk)",sum(chunk))
        print("sym.latex(sum(chunk))",sym.latex(sum(chunk)))
        if i == 0:
            tex += r'$f(x)= %s$'%(sym.latex(sum(chunk)))+separator
        else:
            tex += '$%s$'%(sym.latex(sum(chunk))) + separator
    return tex

popt = est.params
x = sym.symbols('x')
p = sym.Poly.from_list(reversed(popt),gens=x)
tex = tex_multiline_poly(p.as_expr(),chunk_size=2)
plt.title(text=tex)

解决方法

在您的代码中,您为每个偶次幂单项式插入换行符,最后一个除外。

   if (i%2 == 0 and i !=len(p_list)):
       str_expr =str_expr + " \n "

由于您只是根据系数列表构建多项式,因此可以简化您的代码。

通常我们想要的是符号化地构建/转换/处理事物,并且只有在最后将它们序列化并以某种特定格式打印结果

import sympy as sym

x = sym.symbols('x')

def func(p_list):
    expr = 0
    for i in range(len(p_list)-1,-1,-1):
        expr += p_list[i] * (x ** i)
    return sym.sympify(expr)

popt = [-2,1,1]
p = func(popt)
p_tex = sym.latex(p)
p_str = str(p)
print("str:",p_str)
print("tex:",p_tex)

输出:

str: x**2 + x - 2
tex: x^{2} + x - 2

我们可以通过使用 SymPy 的内置函数来构建 the poly from a list of coefficients 来进一步简化:

import sympy as sym
from sympy import symbols

popt = [-2,1]

x = symbols('x')
p = sym.Poly.from_list(reversed(popt),gens=x)
p_tex = sym.latex(p.as_expr())
p_str = str(p.as_expr())
print("str:",p_tex)

输出:

str: x**2 + x - 2
tex: x^{2} + x - 2

输出是否符合您的预期?

更新:

在了解有关用例的更多信息后,这里有一个版本,它在表达式的乳胶形式中每 N=2 个单项式插入分隔符。

import sympy as sym
from sympy import symbols

popt = [-2,gens=x)

def tex_multiline_poly(e,chunk_size=2,separator="\n"):
    tex = ""
    # split into monomials
    mono = list(reversed(e.args))
    # we're going split the list of monomials into chunks of chunk_size
    # serialize each chunk,and insert separators between the chunks
    for i in range(0,len(mono),chunk_size):
        chunk = mono[i:i + chunk_size]
        tex += sym.latex(sum(chunk)) + separator
    return tex

p_tex = tex_multiline_poly(p.as_expr(),chunk_size=2)
p_str = str(p.as_expr())

print("str:",p_tex)

输出:

str: x**2 + x - 2
tex: x^{2} + x
-2

编辑:错误编辑

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...