SymPy自动处理表达式 [英] SymPy automatically processes expressions

查看:177
本文介绍了SymPy自动处理表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用SymPy将表达式转换为乳胶(然后由Matplotlib呈现).例如

I have been using SymPy to convert expressions into latex (to then be rendered by Matplotlib). e.g.

from sympy import latex, sympify
from sympy.abc import x

str = '2*x + 3*x'

TeX = latex(sympify(str))

问题在于它会自动处理表达式,因此2 * x + 3 * x自动变为5 * x等;这不是我想要的(不要问!).

The problem is that it automatically processes the expression, so 2*x + 3*x automatically becomes 5*x etc; which is not what I want (don't ask!).

推荐答案

Sympy的Add类处理符号的添加.您可以提供一个关键字参数来停止术语的自动收集.

Sympy's Add class handles the addition of symbols. You can provide a keyword argument to stop the automatic collection of terms.

from sympy import Add
from sympy.abc import x

eq = Add(2*x, 3*x, evaluate=False)

# this will print: 2*x + 3*x
print eq

根据对phimuemue的评论的回复,这可能并不是您想要的.

This may not be exactly what you want based on your reply to phimuemue's comment.

这篇关于SymPy自动处理表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆