在Python使用 eval() 求解线性方程
使用 a + bx = c + dx 形式的一个变量的线性方程可以在Python使用eval()函数求解。输入类型将是一个字符串形式的线性方程。
句法:
eval(expression, globals=None, locals=None)
在这里,我们将方程转换为实数和虚数的表达式,以便 eval 可以轻松处理它。
例如,通过将右侧的所有项向左移动, 5x + 4 = 2x + 10变为5j + 4 – (2j + 10) 。我们正在将这个方程转换为一个复杂的方程,因为 eval() 无法以其他方式处理方程。将其转换为复数有助于更快地进行评估。
步骤1:我们将使用Python的replace()将“=”替换为“-(”,并将“x”替换为“j”。
第 2 步:然后在字符串中添加“+)”以完成表达式。
第 3 步:然后执行 {“j”:1j} 将等式更改为易于由 eval()函数计算的格式。在这一步中,所有的常数项都被评估,x 项或虚项也被评估。
第 4 步:然后将计算出的表达式简单地分解为实部和虚部。如果虚部存在或 x 为真且不为零,则打印答案,否则如果虚部为 0 且实部为真,则没有解,否则有无限解。这里,
x = 2.000000
是最终的解决方案。
示例 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))
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))
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))
输出
x = 4.500000
示例 2:
蟒蛇3
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))
输出
No solution
示例 3:
蟒蛇3
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))
输出
Infinite solutions