如何将 `\n` 传播到 sympy.latex()
How to propagate `\n` to 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 "
由于您只是从系数列表构建多项式,因此可以简化您的代码。
通常我们想要的是 build/transform/handle 符号化的东西,只有在最后序列化它们并以某种特定格式打印结果
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 的内置函数构建 :
来进一步简化这个过程
import sympy as sym
from sympy import symbols
popt = [-2,1,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_str)
print("tex:", p_tex)
输出:
str: x**2 + x - 2
tex: x^{2} + x - 2
输出是否符合您的预期?
更新:
在详细了解用例后,这里有一个版本,它在表达式的乳胶形式中每隔 N=2 个单项式插入分隔符。
import sympy as sym
from sympy import symbols
popt = [-2,1,1]
x = symbols('x')
p = sym.Poly.from_list(reversed(popt),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_str)
print("tex:",p_tex)
输出:
str: x**2 + x - 2
tex: x^{2} + x
-2
编辑:错误编辑
目标是将具有 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 "
由于您只是从系数列表构建多项式,因此可以简化您的代码。
通常我们想要的是 build/transform/handle 符号化的东西,只有在最后序列化它们并以某种特定格式打印结果
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 的内置函数构建
import sympy as sym
from sympy import symbols
popt = [-2,1,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_str)
print("tex:", p_tex)
输出:
str: x**2 + x - 2
tex: x^{2} + x - 2
输出是否符合您的预期?
更新:
在详细了解用例后,这里有一个版本,它在表达式的乳胶形式中每隔 N=2 个单项式插入分隔符。
import sympy as sym
from sympy import symbols
popt = [-2,1,1]
x = symbols('x')
p = sym.Poly.from_list(reversed(popt),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_str)
print("tex:",p_tex)
输出:
str: x**2 + x - 2
tex: x^{2} + x
-2
编辑:错误编辑