我没有正确理解 Latex2sympy 和 sympy.symbols() 之间的交互。
如果我运行该程序
将 sympy 导入为 sym 像这样导入 scipy.optimize 从 Latex2sympy2 导入 Latex2sympy,latex2latex
s_c,t_h = sym.symbols("sigma_c T_H")
exp1 = s_c+t_h 打印(exp1) exp2 = Latex2sympy(" s_c+t_h") 打印(exp2)
我明白了
T_H + sigma_c s_c + t_h
虽然我预计 exp1 和 exp2 是相同的。我不明白什么以及如何使 exp2 等于 T_H + sigma_C ?
谢谢你
latex2sympy 将乳胶作为输入,所以你需要类似的东西:
s_c,t_h = sym.symbols("sigma_c T_H" )
f = s_c + t_h # This is a sympy function class.
_latex = sym.latex(f) # This is now a string representing the latex of f.
f2 = latex2sympy(_latex) # Takes the latex string as an input, but could be any latex string.
print(f"""
f: {f}
f2: {f2}
""")
注意:像我一样使用反引号将代码块包装在突出显示的代码中。三个反引号,后跟语言 (py),然后是您的代码,然后三个反引号将其关闭。如果您希望代码像
const this = true
那样内联显示,则可以在任一侧使用一个反引号。