#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)
I was trying to make a discord bot that used sympy to solve algebra but I kept running into errors with the implementation above. Can someone please help?
But for the input 10 = 2x
parse_expr
gives a syntax error. How can I use parse_expr
or some similar function to accept this kind of expressions?
Error:
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
To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .
parse_expr() returns one expression. If the text contains more than one expression (separated by semicolons or new lines), an error is issued. On the other hand parse_exprs() can handle multiple expressions. It always returns a list of expressions (compare to base::parse() which returns a base::expression vector).
SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.
The subs() function in SymPy replaces all occurrences of the first parameter with the second. Substitution is the basic operations that must be performed in a mathematical expression. In this way, we can use subs() function in sympy.
parse_expr
allows flexible input via its transformations=
parameter.
The implicit_multiplication_application
allows among others to leave out the *
when a multiplication can be presumed. Very often, people also want ^
for powers, while Python standard uses **
for power and reserves ^
for exclusive or. The transformation convert_xor
takes care of that conversion.
Here is an example:
from sympy.parsing.sympy_parser import parse_expr, standard_transformations, implicit_multiplication_application, convert_xor
transformations = (standard_transformations + (implicit_multiplication_application,) + (convert_xor,))
expr = parse_expr("10tan x^2 + 3xyz + cos theta",
transformations=transformations)
result: 3*x*y*z + cos(theta) + 10*tan(x**2)
The documentation describes many other possible transforms. Care should be taken, because the result is not always what is hoped for, especially when combining transformations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With