数学方程式作为Python对象(Math Equations as Python Objects)
python
我正在尝试开发一个包含已定义函数集(或列表)的程序,如下所示:
x = ["f1(x)=x^2", "f2(x)=2x+1", ...]之后,我可以使用matplotlib并渲染它们等。
问题
我怎么能用对象替换字符串? 例如,带有x变量的"x"使得我可以在matplotlib中渲染方程式? 是否有任何现有的模块可以完成我想要做的事情?I'm trying to develop a program that contains a set (or list) of defined functions, such as follows:
x = ["f1(x)=x^2", "f2(x)=2x+1", ...]Afterward, I can use matplotlib and render them, etc.
Questions
How might I replace the string with objects? For example, "x" with an x variable such that I can render the equations in matplotlib? Are there any existing modules out there that accomplish what I'm trying to do?最满意答案
这得看情况。 如果您只想评估函数(绘制它们或其他),那么只需将它们声明为函数:
X = { "f1": lambda x: x**2, "f2": lambda x: 2*x+1 }如果你想象征性地操作它们,比如找到它们的衍生物,反转它们等,那么你需要像SAGE或sympy这样的东西,它们将它们表示为符号表达式而不是python字节码。
Well, it depends. If all you want to do is evaluate the functions (to plot them or whatever), then just declare them as functions:
X = { "f1": lambda x: x**2, "f2": lambda x: 2*x+1 }If you want to operate on them symbolically, such as to find their derivatives, invert them, etc., then you'll need something like SAGE or sympy, which represents them as symbolic expressions instead of as python bytecode.
更多推荐
发布评论