How could I solve the error of putting a string in the sympy set solver with an input?
Question:
So I’m trying to create this program where it takes an input (for example x+2=5) and sympy solves that equation. However since I believe that "=" sign will cause an error I tried to cut it out from the input but with this I’m finding my self inputting a string type in the simpy solver. Is there any solution to this?
import math
from sympy import *
class operations():
def __init__(self):
self.operation = input()
def solution(self, *o):
x, y, z = symbols("x y z")
equals = self.operation.split("=",1)[1]
equation = self.operation.split("=")[0]
solution = solveset(Eq(equation, int(equals)), x)
print(solution)
operations().solution()
Answers:
You can use sympify
to convert a string to a symbolic expression, although you will have to remove the equal sign first. In the following code, first I split the string where the equal sign is found, then I convert the two resulting strings to symbolic expressions with sympify
, finally I solve the equation.
def solution(self, *o):
left, right = [sympify(t) for t in self.operation.split("=")]
solution = solveset(left - right) # solve left - right = 0
print(solution)
You can use parse_expr
to parse raw strings. There are fine tuning settings that can be used, but if you are inputting a valid SymPy expressions (or nearly so) on each side of the equal sign then specifying transformations='all'
is a simple way to parse the equation as an equality:
>>> from sympy.parsing import parse_expr
>>> parse_expr('2x=4', transformations='all')
Eq(2*x, 4)
>>> solveset(_)
{2}
So I’m trying to create this program where it takes an input (for example x+2=5) and sympy solves that equation. However since I believe that "=" sign will cause an error I tried to cut it out from the input but with this I’m finding my self inputting a string type in the simpy solver. Is there any solution to this?
import math
from sympy import *
class operations():
def __init__(self):
self.operation = input()
def solution(self, *o):
x, y, z = symbols("x y z")
equals = self.operation.split("=",1)[1]
equation = self.operation.split("=")[0]
solution = solveset(Eq(equation, int(equals)), x)
print(solution)
operations().solution()
You can use sympify
to convert a string to a symbolic expression, although you will have to remove the equal sign first. In the following code, first I split the string where the equal sign is found, then I convert the two resulting strings to symbolic expressions with sympify
, finally I solve the equation.
def solution(self, *o):
left, right = [sympify(t) for t in self.operation.split("=")]
solution = solveset(left - right) # solve left - right = 0
print(solution)
You can use parse_expr
to parse raw strings. There are fine tuning settings that can be used, but if you are inputting a valid SymPy expressions (or nearly so) on each side of the equal sign then specifying transformations='all'
is a simple way to parse the equation as an equality:
>>> from sympy.parsing import parse_expr
>>> parse_expr('2x=4', transformations='all')
Eq(2*x, 4)
>>> solveset(_)
{2}