Sympy extension#

latexexpr_efficalc.sympy is an extension for LaTeXExpression for symbolic operations (specifically simplify(), expand(), factor(), collect(), cancel(), apart() functions). It requires sympy module. Most of the examples in this documentation is borrowed from sympy documentation.

Note the sympy module has not yet been fully implemented and tested. If you would like to improve the library, please reach out or raise a PR with your proposed improvements.

If sympy is present, it also defines aforementioned methods on Expression and Operation classes, so it is possible to use both simplify() and o.simplify():

>>> import latexexpr_efficalc.sympy as lsympy
>>> v1 = latexexpr_efficalc.Variable('v1',None)
>>> v2 = latexexpr_efficalc.Variable('v2',None)
>>> v3 = latexexpr_efficalc.Variable('v3',1.23)
>>> v4 = latexexpr_efficalc.Variable('v4',4.56)
>>> x = latexexpr_efficalc.Variable('x',None)
>>> e1 = latexexpr_efficalc.Expression('e1',v1+v1+v2+v3+v2+v3-v4)
>>> print e1
e1 = {v1} + {v1} + {v2} + {v3} + {v2} + {v3} - {v4}
>>> print lsympy.simplify(e1)
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> print lsympy.simplify(e1,substituteFloats=True)
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e1.simplify()
>>> print e1
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> e1.simplify(substituteFloats=True)
>>> print e1
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e2 = latexexpr_efficalc.Expression('e2',latexexpr_efficalc.sin(x)**2+latexexpr_efficalc.cos(x)**2)
>>> print lsympy.simplify(e2)
e2 = 1 = 1 \ \mathrm{} = 1 \ \mathrm{}
>>> e3 = latexexpr_efficalc.Expression('e3', (x**3 + x**2 - x - 1) / (x**2 + 2*x + 1) )
>>> print lsympy.simplify(e3)
e3 = {-1} + {x}

Sympy functions#

latexexpr_efficalc.sympy.simplify(arg, substituteFloats=False, **kw)#

Performs simplify operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters:
  • arg (Variable|Operation|Expression) – argument to be processed

  • substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise

  • **kw – keywords for sympy.simplify() function

Return type:

type(arg)

>>> import latexexpr_efficalc.sympy as lsympy
>>> v1 = latexexpr_efficalc.Variable('v1',None)
>>> v2 = latexexpr_efficalc.Variable('v2',None)
>>> v3 = latexexpr_efficalc.Variable('v3',1.23)
>>> v4 = latexexpr_efficalc.Variable('v4',4.56)
>>> x = latexexpr_efficalc.Variable('x',None)
>>> e1 = latexexpr_efficalc.Expression('e1',v1+v1+v2+v3+v2+v3-v4)
>>> print e1
e1 = {v1} + {v1} + {v2} + {v3} + {v2} + {v3} - {v4}
>>> print lsympy.simplify(e1)
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> print lsympy.simplify(e1,substituteFloats=True)
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e1.simplify()
>>> print e1
e1 = \left( - {v4} \right) + {2} \cdot {v1} + {2} \cdot {v2} + {2} \cdot {v3}
>>> e1.simplify(substituteFloats=True)
>>> print e1
e1 = {-2.1} + {2} \cdot {v1} + {2} \cdot {v2}
>>> e2 = latexexpr_efficalc.Expression('e2',latexexpr_efficalc.sin(x)**2+latexexpr_efficalc.cos(x)**2)
>>> print lsympy.simplify(e2)
e2 = 1 = 1 \ \mathrm{} = 1 \ \mathrm{}
>>> e3 = latexexpr_efficalc.Expression('e3', (x**3 + x**2 - x - 1) / (x**2 + 2*x + 1) )
>>> print lsympy.simplify(e3)
e3 = {-1} + {x}
latexexpr_efficalc.sympy.expand(arg, substituteFloats=False, **kw)#

Performs expand operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters:
  • arg (Variable|Operation|Expression) – argument to be processed

  • substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise

  • **kw – keywords for sympy.expand() function

Return type:

type(arg)

>>> import latexexpr_efficalc.sympy as lsympy
>>> x = latexexpr_efficalc.Variable('x',None)
>>> e1 = latexexpr_efficalc.Expression('e1', (x+1)**2 )
>>> print lsympy.expand(e1,substituteFloats=True)
e1 = {1} + {2} \cdot {x} + { {x} }^{ {2} }
>>> e2 = latexexpr_efficalc.Expression('e2', (x+2)*(x-3) )
>>> print lsympy.expand(e2)
e2 = {-6} + \left( - {x} \right) + { {x} }^{ {2} }
>>> e3 = latexexpr_efficalc.Expression('e3', (x+1)*(x-2) - (x-1)*x )
>>> print lsympy.expand(e3)
e3 = -2 = \left( -2 \right) \ \mathrm{} = \left(-2\right) \ \mathrm{}
latexexpr_efficalc.sympy.factor(arg, substituteFloats=False, **kw)#

Performs factor operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters:
  • arg (Variable|Operation|Expression) – argument to be processed

  • substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise

  • **kw – keywords for sympy.factor() function

Return type:

type(arg)

>>> import latexexpr_efficalc.sympy as lsympy
>>> x = latexexpr_efficalc.Variable('x',None)
>>> y = latexexpr_efficalc.Variable('y',None)
>>> z = latexexpr_efficalc.Variable('z',None)
>>> e1 = latexexpr_efficalc.Expression('e1', x**3 - x**2 + x - 1)
>>> print lsympy.factor(e1)
e1 = \left( {1} + { {x} }^{ {2} } \right) \cdot \left( {-1} + {x} \right)
>>> e2 = latexexpr_efficalc.Expression('e2', x**2*z + 4*x*y*z + 4*y**2*z)
>>> print lsympy.factor(e2)
e2 = {z} \cdot { {2} \cdot {y} + {x} }^{ {2} }
latexexpr_efficalc.sympy.collect(arg, syms, substituteFloats=False, **kw)#

Performs collect operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters:
  • arg (Variable|Operation|Expression) – argument to be processed

  • syms (Variable|[Variable]) – variables to be collected

  • substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise

  • **kw – keywords for sympy.collect() function

Return type:

type(arg)

>>> import latexexpr_efficalc.sympy as lsympy
>>> x = latexexpr_efficalc.Variable('x',None)
>>> y = latexexpr_efficalc.Variable('y',None)
>>> z = latexexpr_efficalc.Variable('z',None)
>>> e1 = latexexpr_efficalc.Expression('e1', x*y + x - 3  + 2*x**2 - z*x**2 + x**3)
>>> print lsympy.collect(e1,x)
e1 = {-3} + { {x} }^{ {3} } + {x} \cdot \left( {1} + {y} \right) + { {x} }^{ {2} } \cdot \left( {2} - {z} \right)
latexexpr_efficalc.sympy.cancel(arg, substituteFloats=False, **kw)#

Performs cancel operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters:
  • arg (Variable|Operation|Expression) – argument to be processed

  • substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise

  • **kw – keywords for sympy.cancel() function

Return type:

type(arg)

>>> import latexexpr_efficalc.sympy as lsympy
>>> x = latexexpr_efficalc.Variable('x',None)
>>> y = latexexpr_efficalc.Variable('y',None)
>>> z = latexexpr_efficalc.Variable('z',None)
>>> e1 = latexexpr_efficalc.Expression('e1', (x**2 + 2*x + 1) / (x**2 + x) )
>>> print lsympy.cancel(e1)
e1 = \frac{ {1} }{ {x} } \cdot \left( {1} + {x} \right)
>>> e2 = latexexpr_efficalc.Expression('e2', 1/x + (3*x/2 - 2) / (x - 4) )
>>> print lsympy.cancel(e2)
e2 = \frac{ {1} }{ {2} \cdot { {x} }^{ {2} } + {-8} \cdot {x} } \cdot \left( {-8} + {-2} \cdot {x} + {3} \cdot { {x} }^{ {2} } \right)
>>> e3 = latexexpr_efficalc.Expression('e3', (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2) / (x**2 - 1) )
>>> print lsympy.cancel(e3)
e3 = \frac{ {1} }{ {-1} + {x} } \cdot \left( { {z} }^{ {2} } + {-2} \cdot {y} \cdot {z} + { {y} }^{ {2} } \right)
latexexpr_efficalc.sympy.apart(arg, substituteFloats=False, **kw)#

Performs apart operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)

Parameters:
  • arg (Variable|Operation|Expression) – argument to be processed

  • substituteFloats (bool) – non-symbolic variables are treated as their float values if True, they are left otherwise

  • **kw – keywords for sympy.apart() function

Return type:

type(arg)

>>> import latexexpr_efficalc.sympy as lsympy
>>> x = latexexpr_efficalc.Variable('x',None)
>>> e1 = latexexpr_efficalc.Expression('e1', (4*x**3 + 21*x**2 + 10*x + 12) / (x**4 + 5*x**3 + 5*x**2 + 4*x) )
>>> print lsympy.apart(e1)
e1 = \frac{ {1} }{ {1} + {x} + { {x} }^{ {2} } } \cdot \left( {-1} + {2} \cdot {x} \right) + \left( - \frac{ {1} }{ {4} + {x} } \right) + {3} \cdot \frac{ {1} }{ {x} }