Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Sympy:如何解析表达式,如';2x';?_Python 3.x_Sympy - Fatal编程技术网

Python 3.x Sympy:如何解析表达式,如';2x';?

Python 3.x Sympy:如何解析表达式,如';2x';?,python-3.x,sympy,Python 3.x,Sympy,parse_expr允许通过其参数进行灵活的输入 隐式乘法应用程序允许在可以假定乘法时省略*。很多时候,人们也希望使用^作为电源,而Python标准使用**作为电源,保留^作为异或。转换convert\uxor负责该转换 以下是一个例子: 从sympy.parsing.sympy\u解析器导入解析表达式、标准转换、隐式乘法应用程序、转换异或 转换=(标准转换+(隐式乘法应用程序,)+(转换异或,) expr=parse_expr(“10tan x^2+3xyz+cosθ”, 转换=转换) 结果

parse_expr
允许通过其参数进行灵活的输入

隐式乘法应用程序
允许在可以假定乘法时省略
*
。很多时候,人们也希望使用
^
作为电源,而Python标准使用
**
作为电源,保留
^
作为异或。转换
convert\uxor
负责该转换

以下是一个例子:

从sympy.parsing.sympy\u解析器导入解析表达式、标准转换、隐式乘法应用程序、转换异或
转换=(标准转换+(隐式乘法应用程序,)+(转换异或,)
expr=parse_expr(“10tan x^2+3xyz+cosθ”,
转换=转换)
结果:
3*x*y*z+cos(θ)+10*tan(x**2)


描述了许多其他可能的转换。应该小心,因为结果并不总是期望的,特别是在组合转换时。

必须有一个
sympy
明白了这一点。由于Python的限制,
2x
无效,即使带有
符号
x
。它必须是
2*x
            #split the equation into 2 parts using the = sign as the divider, parse, and turn into an equation sympy can understand
            equation = Eq(parse_expr(<input string>.split("=")[0]), parse_expr(<input string>.split("=")[1]))
            answers = solve(equation)
            #check for answers and send them if there are any
            if answers.len == 0:
                response = "There are no solutions!"
            else:
                response = "The answers are "
                for answer in answers:
                    response = response + answer + ", "
                response = response[:-2]
        await self.client.send(response, message.channel)
  File "C:\Users\RealAwesomeness\Documents\Github\amber\amber\plugins/algebra.py", line 19, in respond
    equation = Eq(parse_expr(c[2].split("=")[0]),parse_expr(c[2].split("=")[1]))
  File "C:\Users\RealAwesomeness\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sympy\parsing\sympy_parser.py", line 1008, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "C:\Users\RealAwesomeness\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sympy\parsing\sympy_parser.py", line 902, in eval_expr
    expr = eval(
  File "<string>", line 1
    Integer (2 )Symbol ('x' )
                ^
SyntaxError: invalid syntax