Solve Linear Equations using eval() in Python
Last Updated :
26 Mar, 2021
Linear equations using one variable of the form a + bx = c + dx can be solved in Python using eval() function. The input type will be a linear equation in the form of a string.
Syntax:
eval(expression, globals=None, locals=None)
Here, we will transform the equation into an expression of real and imaginary numbers such that eval can easily process it.
For example, 5x + 4 = 2x + 10 becomes 5j + 4 - (2j + 10) by shifting all the terms on the right side to the left. We are transforming this equation into a complex equation because eval() is unable to otherwise process the equations. Transforming it into a complex number helps in faster evaluation.
Step 1: We will use the replace() in python to replace "=" with "-(" and replace "x" with "j".
Step 2: The string is then added with "+)" to complete the expression.
Step 3: Then { "j" : 1j} is done to change the equation into a format that can be easily evaluated by the eval() function. In this step all the constant terms are evaluated and the x terms or the imaginary terms as well.
Step 4: Then the evaluated expression is simply broken down into the real and imaginary parts. If the imaginary part exists or the x is true and not zero the answer is printed else if the imaginary part is 0 and the real part is true there is no solution or else there are infinite solutions. Here,
x = 2.000000
is the final solution.
Example 1:
Python3
def solve(equation):
# replacing all the x terms with j
# the imaginary part
s1 = equation.replace('x', 'j')
# shifting the equal sign to start
# an opening bracket
s2 = s1.replace('=', '-(')
# adding the closing bracket to form
# a complete expression
s = s2+')'
# mapping the literal j to the complex j
z = eval(s, {'j': 1j})
real, imag = z.real, -z.imag
# if the imaginary part is true return the
# answer
if imag:
return "x = %f" % (real/imag)
else:
if real:
return "No solution"
else:
return "Infinite solutions"
equation = "2+3x=5x-7"
print(solve(equation))
Example 2:
Python3
def solve(equation):
# replacing all the x terms with j
# the imaginary part
s1 = equation.replace('x', 'j')
# shifting the equal sign to start
# an opening bracket
s2 = s1.replace('=', '-(')
# adding the closing bracket to form
# a complete expression
s = s2+')'
# mapping the literal j to the complex j
z = eval(s, {'j': 1j})
real, imag = z.real, -z.imag
# if the imaginary part is true return the
# answer
if imag:
return "x = %f" % (real/imag)
else:
if real:
return "No solution"
else:
return "Infinite solutions"
equation = "x=x+10"
print(solve(equation))
Example 3:
Python3
def solve(equation):
# replacing all the x terms with j
# the imaginary part
s1 = equation.replace('x', 'j')
# shifting the equal sign to start
# an opening bracket
s2 = s1.replace('=', '-(')
# adding the closing bracket to form
# a complete expression
s = s2+')'
# mapping the literal j to the complex j
z = eval(s, {'j': 1j})
real, imag = z.real, -z.imag
# if the imaginary part is true return the
# answer
if imag:
return "x = %f" % (real/imag)
else:
if real:
return "No solution"
else:
return "Infinite solutions"
equation = "2x=2x"
print(solve(equation))
Similar Reads
Solve Linear Equation and return 3D Graph in Python In this article, we will make the 3D graph by solving the linear equations using Python. Solve Linear Equation in Python Here we are going to create a different variable for assigning the value into a linear equation and then calculate the value by using linalg.solve() methods. Python3 # Python prog
2 min read
Python - Solve the Linear Equation of Multiple Variable Prerequisite: Sympy.solve() In this article, we will discuss how to solve a linear equation having more than one variable. For example, suppose we have two variables in the equations. Equations are as follows: x+y =1 x-y =1 When we solve this equation we get x=1, y=0 as one of the solutions. In Pyth
2 min read
Using User Input to Call Functions - Python input() function allows dynamic interaction with the program. This input can then be used to call specific functions based on the user's choice .Letâs take a simple example to call function based on user's input .Example:Pythondef add(x, y): return x + y # Add def sub(x, y): return x - y # Subtract
2 min read
Evaluate the Mathematical Expressions using Tkinter in Python This article focuses on the evaluation of mathematical expression using the Tkinter and math packages in Python. Tkinter: Python Tkinter is a GUI programming package or built-in package. Tkinter provides the Tk GUI toolkit with a potent object-oriented interface. Python with Tkinter is the fastest
2 min read
Python PyTorch â torch.linalg.solve() Function In this article, we will discuss torch.linalg.solve() method in PyTorch. Example: Let's consider the linear equations : 6x + 3y = 1 3x - 4y = 2 Then M values can be - [[6,3],[3,-4]] and t is [1,2]torch.linalg.solve() Function The torch.linalg.solve() method is used to solve a square system of linear
4 min read