1

I'm writing now a pet project in which I want to draw a plot in the terminal window. The idea is that user print an equation, like x = y + 1, or 3 * x = y. The question is how can I transfer this string equation to an equation in Python.

For instance: I have an array, for x values like:

x_arr = [1, 2, 3, 4, 5]

And then I want to put it into the user's equation to get y values. or vise versa (vise versa, put y values to get x values)

Thought to create a lot of if conditions and use for loop to detect every character but it seems something like a sandbox. Is there a more elegant way?

Ali_Sh
  • 2,667
  • 3
  • 43
  • 66
Serhei
  • 87
  • 1
  • 7
  • 1
    You want to draw equations using ∫ or √ or ∑ in a terminal, i.e. in text format? Or you want to plot a graph in a terminal using ASCII art? – Thomas Weller Jun 22 '22 at 08:15
  • 2
    Take a look on [sympy](https://docs.sympy.org/latest/index.html). – Olvin Roght Jun 22 '22 at 08:16
  • You can use [`eval`](https://docs.python.org/3/library/functions.html#eval). While it will evaluate all kinds of inputs beyond math equations, that doesn't really matter if this code is only ran at the users' platforms. – a_guest Jun 22 '22 at 08:17
  • To me it seems that you have multiple problems at once and you should tackle each one individually. – Thomas Weller Jun 22 '22 at 08:18
  • @ThomasWeller, yes, I want to plot graph in a terminal usin ASCII art. I understand that there will be more problems with complex equations, by I do it step by step. Now I want just to print points on a plot which are in the equation – Serhei Jun 22 '22 at 08:20
  • @a_guest seems interesting, thnks! Will try it! – Serhei Jun 22 '22 at 08:24
  • You can also take a look at [the plotext project](https://github.com/piccolomo/plotext) for inspiration. – a_guest Jun 22 '22 at 08:25
  • @OlvinRoght thks! Will watch it too – Serhei Jun 22 '22 at 08:27
  • @a_guest, yea, saw it. Quite interesting! – Serhei Jun 22 '22 at 08:28

2 Answers2

2

You can do it using sympy library.

      from sympy import *
      from sympy.solvers import solve
      x,y = symbols('x,y')
      eq = '3*x=y*2'
      parts = [x.strip() for x in eq.split('=')]
      final_eq = parts[1]+'-({})'.format(parts[0])
      equation = Eq(eval(parts[0]),eval(parts[1]))
      x_array = [1,2,3,4,5]
      for x1 in x_array:
            print('given x={}, y={}'.format(x1,solve(equation.subs(x,x1),y)))

Ouput

      given x=1, y=[3/2]
      given x=2, y=[3]
      given x=3, y=[9/2]
      given x=4, y=[6]
      given x=5, y=[15/2]
Mehul Gupta
  • 1,829
  • 3
  • 17
  • 33
  • Wouldn't it be more easy with: `equation = "x = y + 1"` `x_arr = []` `y_arr = [1,2,3]` `x_part, y_part = equation.lower().replace(" ", "").split("=")` `for i in y_arr:` `y = i` `x_arr.append(eval(y_part))` `print(x_arr)` – Serhei Jun 22 '22 at 09:33
  • 1
    This would be a hard coded solution for a particular type of equations (like x=m*y+c). Won't work for eqs like 3*x=y or 2+x=y+5 – Mehul Gupta Jun 22 '22 at 09:39
0

Use eval for evaluating the textual commands.

https://www.programiz.com/python-programming/methods/built-in/eval

Chandan Kumar
  • 1,066
  • 10
  • 16