数学方程式作为Python对象(Math Equations as Python Objects)

编程入门 行业动态 更新时间:2024-10-23 10:25:27
数学方程式作为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.

更多推荐

本文发布于:2023-07-16 07:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方程式   对象   数学   Equations   Objects

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!