MathJSON Solver is a Python module to numerically evaluate MathJSON expressions, like ["Add", 1, 2]. It is developed by Longenesis to enable numerical evaluation of user provided mathematical expressions in Longenesis digital health products. Its development was inspired by CortexJS Compute Engine.
Please ask questions and share feedback in our Gitter chat https://gitter.im/mathjson-solver/community.
from mathjson_solver import create_solver
parameters = {"x": 2, "y": 3}
expression = ["Add", "x", "y", 4]
solver = create_solver(parameters)
answer = solver(expression)
print(answer)
# 9, because 2+3+4=9Find the full list of supported constructs in the docs/README.md.
A MathJSONException is raised when expression cannot be evaluated. Import MathJSONException to handle it:
from mathjson_solver import create_solver, MathJSONException
solver = create_solver({})
try:
solver(["Divide", 1, 0])
except MathJSONException:
pass
# invoke your own exception logger hereLeft unhandled, the exception will look like MathJSONException("Problem in Divide. ['Divide', 1, 0]. division by zero").
Make sure you have pytest installed. Then cd into project directory and run:
pytest