add mathematical solver engine based on sympy

This commit is contained in:
2025-01-02 00:45:03 +01:00
parent fd7e3d5235
commit fe9c738891
7 changed files with 410 additions and 18 deletions

View File

@@ -3,19 +3,16 @@ import tool_helper
import tests.helper as helper
def test_tool_function_decorator_if_clean_tool_list():
""" tests for the tool list to be empty. NOT strictly nessesary,
but I want to be warned if this is not the case anymore. Could be not the intention """
start_len = len(tool_helper.tool_list)
assert start_len == 0
def test_tool_function_decorator():
# get length before adding tools
start_len = len(tool_helper.tool_list)
# add tools like it would be a decorator
tool_helper.tool(helper.tool_dummy)
tool_helper.tool(helper.tool_dummy2)
res = tool_helper.tool(helper.tool_dummy)
assert res == helper.tool_dummy # decorator should return the function itself, so it is usable just in case.
res = tool_helper.tool(helper.tool_dummy2)
assert res == helper.tool_dummy2 # decorator should return the function itself, so it is usable just in case.
# get length after adding tools
end_len = len(tool_helper.tool_list)
@@ -28,3 +25,4 @@ def test_tool_function_decorator():

View File

@@ -0,0 +1,57 @@
import pytest
import tool_functions
def test_math_evaluate_1():
result = tool_functions.math_evaluate("1+2*pi")
assert result == "1 + 2*pi = 7.28318530717959"
def test_math_evaluate_2a():
result = tool_functions.math_evaluate("2**4")
assert result == "2**4 = 16"
def test_math_evaluate_2b():
""" test that ^ notation is also working, original sympy cannot do this """
result = tool_functions.math_evaluate("2^4")
assert result == "2**4 = 16"
def test_math_evaluate_3():
result = tool_functions.math_evaluate("Integral(exp(-x**2), (x, -oo, oo))")
assert result == "Integral(exp(-x**2), (x, -oo, oo)) = sqrt(pi) = 1.77245385090552"
def test_math_evaluate_4():
result = tool_functions.math_evaluate("(2**x)**2")
assert result == "(2**x)**2 = 2**(2*x) = 2.0**(2*x)"
def test_math_evaluate_5():
result = tool_functions.math_evaluate("sin(pi/2) + cos(0)")
assert result == "sin(pi/2) + cos(0) = 2"
def test_math_solver_1():
result = tool_functions.math_evaluate("solve x = 1 for x")
assert result == "solved x = 1 for x = {1}"
def test_math_solver_2():
result = tool_functions.math_evaluate("solve (x + 1)*(x - 1) = 1 for x")
assert result == "solved (x + 1)*(x - 1*1) = 1 for x = {-sqrt(2), sqrt(2)}"
def test_math_solver_3a():
result = tool_functions.math_evaluate("solve 2*x + 3*y = 7 and x - y = 1 for x, y")
assert result == "solved equation system for x=2 and y=1"
def test_math_solver_3b():
result = tool_functions.math_evaluate("solve 2*x + 3*y = 7, x - y = 1 for x and y")
assert result == "solved equation system for x=2 and y=1"
def test_math_solver_4():
result = tool_functions.math_evaluate("solve 2*x**3 + 3*y = 7 and x - y = 1 for x, y")
assert result == "solved equation system for x=~1.421 and y=~0.421"