0

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

  1. How might I replace the string with objects? For example, "x" with an x variable such that I can render the equations in matplotlib?
  2. Are there any existing modules out there that accomplish what I'm trying to do?
Biff
  • 1,009
  • 3
  • 11
  • 20
  • 4
    Look at [`sympy`](http://sympy.org/en/index.html). – Bakuriu Jan 19 '14 at 21:38
  • Are you looking for something like [eval](http://stackoverflow.com/questions/9383740/what-does-pythons-eval-do)? – Mehdi Jan 19 '14 at 21:39
  • @Mehdi Given that the strings are not even remotely valid Python, I think it's safe to say "no". –  Jan 19 '14 at 21:45

1 Answers1

1

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.

Sneftel
  • 40,271
  • 12
  • 71
  • 104