Depending on what you want to do with the entered formulas - I would pay attention to two modules: SymPy for analytic and numerical solution of equations, graphing and much more, and NumExpr for fast and safe numerical calculation of the entered formulas with numpy, scipy support , arrays, variables, etc.
Example of using numexpr
examples of using NumExpr with Numpy arrays:
import numpy as np import numexpr as ne In [84]: a, b = np.random.rand(2, 5) In [85]: a Out[85]: array([ 0.05399168, 0.85873638, 0.73317032, 0.17825897, 0.83083985]) In [86]: b Out[86]: array([ 0.19115417, 0.66216767, 0.51021111, 0.5816862 , 0.72958694]) In [87]: ne.evaluate('sin(a)**2 + cos(a)**2') Out[87]: array([ 1., 1., 1., 1., 1.])
matrix multiplication:
In [92]: m1 = np.array([[1,2], [3,4]]) In [93]: m2 = np.array([[10, 10]]) In [94]: m1 Out[94]: array([[1, 2], [3, 4]]) In [95]: m2 Out[95]: array([[10, 10]]) In [96]: ne.evaluate('m1 * m2') Out[96]: array([[10, 20], [30, 40]], dtype=int32)
SymPy usage examples :
from sympy import * x, y, z, t = symbols('xyz t')
open brackets:
In [75]: ((x+y)**2 * (x+1)).expand() Out[75]: x**3 + 2*x**2*y + x**2 + x*y**2 + 2*x*y + y**2
simplify the expression:
In [76]: simplify((x**2 - y**2) / (x - y)**2) Out[76]: (x + y)/(x - y)
equation solution:
In [77]: solve(Eq(x**3 + 2*x**2 + 4*x + 8, 0), x) Out[77]: [-2, -2*I, 2*I]
solving a system of linear equations:
In [78]: solve([Eq(x + 5*y, 2), Eq(-3*x + 6*y, 15)], [x, y]) Out[78]: {x: -3, y: 1}
analytical solution of indefinite integral:
In [79]: integrate(x**2 * cos(x), x) Out[79]: x**2*sin(x) + 2*x*cos(x) - 2*sin(x)
or
In [100]: init_printing(use_unicode=False, wrap_line=False, no_global=True) In [101]: intgrl = Integral(sin(1/x), (x, 0, 1)).transform(x, 1/x) In [102]: intgrl Out[102]: oo / | | sin(x) | ------ dx | 2 | x | / 1
...