LaTeX Expression is a Python module for easy LaTeX typesetting of algebraic expressions in symbolic form with automatic substitution and result computation, i.e. of the form var = generalExpression = substitutedExpression = result, e.g.
r = 3.0 m
F = 4.0 kN
M = r*F = 3.0*4.0 = 12 kNm
The expression is based on Variable class, representing physical or mathematical variable (with symbolic name, value and unit). Expression has similar meaning, except that instead of value it contains its Operation. Operation contains its type (all basic operations are implemented, see Predefined Operation instance creation) and combine one or more variable(s), expression(s) or other operations. In this way, the hierarchy of operations may be combined in one Expression. Furthermore, where it is reasonable, Python operators are overloaded to make things even more simple and clear.
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1
a_{22} = 3.45 \ \mathrm{mm}
>>> v2 = Variable('F',5.876934835,'kN')
>>> print v2
F = 5.87693 \ \mathrm{kN}
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> print v3
F = { 434 \cdot 10^{-2} } \ \mathrm{kN}
>>> v4 = Variable('F',2.564345,'kN',format='%.4f')
>>> print v4
F = 2.5643 \ \mathrm{kN}
>>> v5 = Variable('F',5.876934835,'kN')
>>> print v5
F = 5.87693 \ \mathrm{kN}
>>> v6 = Variable('F',-6.543,'kN')
>>> o1 = (v1 + SQRT(v2)) / (v3 * v4) + v5
>>> print o1
\frac{ {a_{22}} + \sqrt{ {F} } }{ {F} \cdot {F} } + {F} = \frac{ 3.45 + \sqrt{ 5.87693 } }{ { 434 \cdot 10^{-2} } \cdot 2.5643 } + 5.87693
>>> e1 = Expression('E_1^i',SBRACKETS(o1) - SQR(v6),'kNm')
>>> print e1
E_1^i = \left[ \frac{ {a_{22}} + \sqrt{ {F} } }{ {F} \cdot {F} } + {F} \right] - {F}^2 = \left[ \frac{ 3.45 + \sqrt{ 5.87693 } }{ { 434 \cdot 10^{-2} } \cdot 2.5643 } + 5.87693 \right] - \left( -6.543 \right)^2 = \left(-36.4061\right) \ \mathrm{kNm}
>>> v7 = e1.toVariable()
>>> print v7
E_1^i = \left( -36.4061 \right) \ \mathrm{kNm}
>>> print v7.toLaTeXVariableAll('MYV7')
\def\MYV7{E_1^i = \left( -36.4061 \right) \ \mathrm{kNm}}
>>> v8 = Variable('F',None,'kN')
>>> o4 = v1 + v8
>>> e4 = Expression('E_4',o4,'mF')
>>> print v8
F
>>> print o4
{a_{22}} + {F}
>>> print e4
E_4 = {a_{22}} + {F}
>>> v8.value=2.34
>>> print v8
F = 2.34 \ \mathrm{kN}
>>> print o4
{a_{22}} + {F} = 3.45 + 2.34
>>> print e4
E_4 = {a_{22}} + {F} = 3.45 + 2.34 = 5.79 \ \mathrm{mF}
The module is distributed under GNU LGPL license
To see the module “in action”, visit project home page.
The module contains three fundamental classes: Variable, Operation and Expression.
Class representing mathematical or physical variable, containing information about its symbolic name, value, phyical units and how to format it. It is a fundamental building block of operations and expressions instances.
This class overloads str() method to return expression “name = value unit”, float() to return numeric result (throws exception if value is None) and abs() method.
This class also overloads +,-,*,/ (division, frac{...}{...} in LaTeX), // (divsion, .../... in LaTeX) and ** (power) operators. They can be used with Variable, Expression or Operation instances resulting into new Operation instance.
Parameters: |
|
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1
a_{22} = 3.45 \ \mathrm{mm}
>>> v2 = Variable('F',5.876934835,'kN')
>>> print v2
F = 5.87693 \ \mathrm{kN}
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> print v3
F = { 434 \cdot 10^{-2} } \ \mathrm{kN}
>>> v8 = Variable('F',None,'kN')
>>> print v8
F
Returns string representation of receiver in the form “name = value unit”
Return type: | str |
---|
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> print str(v3)
F = { 434 \cdot 10^{-2} } \ \mathrm{kN}
>>> v8 = Variable('F',None,'kN')
>>> print str(v8)
F
Returns numeric result of the receiver
Return type: | float |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print float(v1)
3.45
symbolic name
physical unit
string to be formatted by the numeric value (with ‘%’ operation)
string to be formatted by physical unit string (with ‘%’ operation)
exponent for scientific representation. If 0, then no scientific representation is performed
numeric value. If value==None, than the Variable is considered as symbolic
Returns string of symbolic representation of receiver (its name)
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.strSymbolic()
{a_{22}}
Returns string of numeric representation of receiver (its formatted value)
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.strSubstituted()
3.45
Returns string of the result of the receiver (its formatted result)
Parameters: |
|
---|---|
Return type: | str |
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.strResult()
3.45
Returns string of the result of the receiver (its formatted result) ending with its units
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.strResultWithUnit()
3.45 \ \mathrm{mm}
Returns numeric result of the receiver (its value)
Return type: | float |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.result()
3.45
Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command
Parameters: |
|
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.toLaTeXVariable('AA','float')
\def\AA{3.45}
>>> print v1.toLaTeXVariable('AA','str','newcommand')
\newcommand{\AA}{3.45}
>>> print v1.toLaTeXVariable('AA','valunit','renewcommand')
\renewcommand{\AA}{3.45 \ \mathrm{mm}}
>>> print v1.toLaTeXVariable('AA','all','def')
\def\AA{a_{22} = 3.45 \ \mathrm{mm}}
Shotcut for Variable.toLaTeXVariable() with what=’float’
Shotcut for Variable.toLaTeXVariable() with what=’str’
Shotcut for Variable.toLaTeXVariable() with what=’valunit’
Shotcut for Variable.toLaTeXVariable() with what=’all’
Copy information from given Expression or Variable. Returns changed receiver
Parameters: | expr (Variable|Expression) – given expression to be copied |
---|---|
Return type: | Variable |
Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value
Class representing mathematical operation applied to one, two or more objects. These objects may be of type Variable, Expression or Operation again, allowing builing a hieararchy of operations. Preferable way of creation of Operation instances is to use predefined functions (see Predefined Operation instance creation) or (where it is possible) standard Python operations +,-,*,/,**.
Parameters: |
|
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> v4 = Variable('F',2.564345,'kN',format='%.4f')
>>> v5 = Variable('F',5.876934835,'kN')
>>> v6 = Variable('F',-6.543,'kN')
>>> v8 = Variable('F',None,'kN')
>>> o3 = (v1+v2)/v3
>>> print o3
\frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } }
>>> o4 = v1 + v8
>>> print o4
{a_{22}} + {F}
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> o2 = MUL(RBRACKETS(e2+v4),v5,v6)
>>> print o2
\left( {E_2} + {F} \right) \cdot {F} \cdot {F} = \left( 2.14906 + 2.5643 \right) \cdot 5.87693 \cdot \left( -6.543 \right)
>>> v8.value=2.34
>>> print o4
{a_{22}} + {F} = 3.45 + 2.34
Returns string representation of receiver in the form “symbolicExpr = substitutedExpr”
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> o3 = (v1+v2)/v3
>>> print str(o3)
\frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } }
Returns numeric result of the receiver
Return type: | float |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> o3 = (v1+v2)/v3
>>> print float(o3)
2.14906332604
arithmetic type of operation
argument list subjected to the operation Operation.type
Returns string of symbolic representation of receiver
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> o3 = (v1+v2)/v3
>>> print o3.strSymbolic()
\frac{ {a_{22}} + {F} }{ {F} }
Returns string of substituted representation of receiver
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> o3 = (v1+v2)/v3
>>> print o3.strSubstituted()
\frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } }
Returns string of the result of the receiver (its formatted result)
Parameters: |
|
---|---|
Return type: | str |
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> o3 = (v1+v2)/v3
>>> print o3.strResult()
2.14906
Returns numeric result of the receiver
Return type: | float |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> o3 = (v1+v2)/v3
>>> print o3.result()
2.14906332604
Returns new Variable instance with attributes copied from receiver
Parameters: | newName (str) – new name of returned variable |
---|---|
Params dict kw: | keyword arguments passed to Variable constructor |
Return type: | Variable |
Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value
Class representing mathematical expression
Parameters: |
|
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> v4 = Variable('F',2.564345,'kN',format='%.4f')
>>> v5 = Variable('F',5.876934835,'kN')
>>> v6 = Variable('F',-6.543,'kN')
>>> v8 = Variable('F',None,'kN')
>>> o1 = (v1 + SQRT(v2)) / (v3 * v4) + v5
>>> e1 = Expression('E_1^i',SBRACKETS(o1) - SQR(v6),'kNm')
>>> print e1
E_1^i = \left[ \frac{ {a_{22}} + \sqrt{ {F} } }{ {F} \cdot {F} } + {F} \right] - {F}^2 = \left[ \frac{ 3.45 + \sqrt{ 5.87693 } }{ { 434 \cdot 10^{-2} } \cdot 2.5643 } + 5.87693 \right] - \left( -6.543 \right)^2 = \left(-36.4061\right) \ \mathrm{kNm}
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2
E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \mathrm{mm}
>>> o4 = v1 + v8
>>> e4 = Expression('E_4',o4,'mF')
>>> print e4
E_4 = {a_{22}} + {F}
>>> v8.value=2.34
>>> print e4
E_4 = {a_{22}} + {F} = 3.45 + 2.34 = 5.79 \ \mathrm{mF}
Returns string representation of receiver in the form “name = symbolicExpr = substitutedExpr = result unit”
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print str(e2)
E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \mathrm{mm}
Returns numeric result of the receiver
Return type: | float |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print float(e2)
2.14906332604
symbolic name of the expression
see Variable.unit
see Variable.format
Returns string of symbolic representation of receiver (its name)
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2.strSymbolic()
{E_2}
Returns string of numeric representation of receiver (its formatted result)
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2.strSubstituted()
2.14906
Returns string of the result of the receiver (its formatted result)
Parameters: |
|
---|---|
Return type: | str |
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2.strResult()
2.14906
Returns string of the result of the receiver (its formatted result) ending with its units
Return type: | str |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2.strResultWithUnit()
2.14906 \ \mathrm{mm}
Returns numeric result of the receiver
Return type: | float |
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2.result()
2.14906332604
Returns new Variable instance with attributes copied from receiver
Parameters: | newName (str) – optional new name of returned variable |
---|---|
Return type: | Variable |
Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command
Parameters: |
|
---|
>>> v1 = Variable('a_{22}',3.45,'mm')
>>> v2 = Variable('F',5.876934835,'kN')
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2.toLaTeXVariable('ETWO','float')
\def\ETWO{2.14906332604}
>>> print e2.toLaTeXVariable('ETWO','str','newcommand')
\newcommand{\ETWO}{2.14906}
>>> print e2.toLaTeXVariable('ETWO','valunit','renewcommand')
\renewcommand{\ETWO}{2.14906 \ \mathrm{mm}}
>>> print e2.toLaTeXVariable('ETWO','symb')
\def\ETWO{{E_2}}
>>> print e2.toLaTeXVariable('ETWO','subst')
\def\ETWO{2.14906}
>>> print e2.toLaTeXVariable('ETWO','all','def')
\def\ETWO{E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } } = 2.14906 \ \mathrm{mm}}
Shotcut for Variable.toLaTeXVariable() with what=’float’
Shotcut for Variable.toLaTeXVariable() with what=’str’
Shotcut for Variable.toLaTeXVariable() with what=’valunit’
Shotcut for Variable.toLaTeXVariable() with what=’symb’
Shotcut for Variable.toLaTeXVariable() with what=’subst’
Shotcut for Variable.toLaTeXVariable() with what=’all’
Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value
Preferable way to create new Operation instances. For each supported operation type there exists corresponding “constructor”. All such “constructors” return new Operation instance.
Returns addition Operation instance
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 or more objects for summation ( arg0 + arg1 + ... + argLast) |
---|
Returns subtraction Operation instance
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for subtraction ( arg0 - arg1) |
---|
Returns multiplication Operation instance
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 or more objects for multiplication ( arg0 * arg1 * ... * argLast ) |
---|
Returns division Operation instance (in LaTeX marked by \frac{...}{...})
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for division ( arg0 / arg1) |
---|
Returns division Operation instance (in LaTeX marked by .../...
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for division ( arg0 / arg1) |
---|
Returns negation Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for negation ( -arg0) |
---|
Returns the “positivition” (which does nothing actually) Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 object |
---|
Returns absolute value Operation instance
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 1 objects for absolute value ( |arg0| ) |
---|
Returns max Operation instance
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for max ( max(arg0,arg1,...,argN) ) |
---|
Returns min Operation instance
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for min ( min(arg0,arg1,...,argN) ) |
---|
Returns power Operation instance
Parameters: | args (Variable(s)|Expression(s)|Operation(s)) – 2 objects for power ( arg0 ^ arg1) |
---|
Returns square Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for square ( arg ^ 2) |
---|
Returns square root Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for square root ( sqrt(arg) ) |
---|
Returns sinus Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for sinus ( sin(arg) ) |
---|
Returns cosinus Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for cosinus ( cos(arg) ) |
---|
Returns tangent Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for tangent ( tan(arg) ) |
---|
Returns hyperbolic sinus Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for hyperbolic sinus ( sin(arg) ) |
---|
Returns hyperbolic cosinus Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for hyperbolic cosinus ( cos(arg) ) |
---|
Returns hyperbolic tangent Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for hyperbolic tangent ( tan(arg) ) |
---|
Returns exp Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for exp ( exp(arg)=e^arg ) |
---|
Returns logarithm Operation instance
Parameters: | args (Variable|Expression|Operation) – 2 objects for logarithm ( log_arg0(arg1) = ln(arg1)/ln(arg0) ) |
---|
Returns natural logarithm Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for natural logarithm ( ln(arg) ) |
---|
Returns decadic logarithm Operation instance
Parameters: | args (Variable|Expression|Operation) – 1 objects for decadic logarithm ( log_10(arg) ) |
---|
Returns round brackets Operation instance (wrapes passed argument to round brackets)
Parameters: | args (Variable|Expression|Operation) – 1 objects for round brackets ( (arg) ) |
---|
Alias for RBRACKETS()
Returns square brackets Operation instance (wrapes passed argument to square brackets)
Parameters: | args (Variable|Expression|Operation) – 1 objects for square brackets ( [arg] ) |
---|
Returns curly brackets Operation instance (wrapes passed argument to curly brackets)
Parameters: | args (Variable|Expression|Operation) – 1 objects for curly brackets ( {arg} ) |
---|
Returns angle brackets Operation instance (wrapes passed argument to angle brackets)
Parameters: | args (Variable|Expression|Operation) – 1 objects for angle brackets ( ⟨arg⟩ ) |
---|
Some predefined Variable instances which might be useful in constructing different expressions.
Variable instance representing 1
Variable instance representing 2
Variable instance representing Euler number
Variable instance representing pi
Some other useful functions and other module members.
Module exception class
Saves globally defined variables from current session into a file. This simplifies working within one LaTeX document, but several python sessions
Parameters: |
|
---|
Loads saved variables form a file into global namespace
Parameters: |
|
---|
Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command
Parameters: |
|
---|
>>> n,s = 'varName','some string content of the variable'
>>> print toLaTeXVariable(n,s)
\def\varName{some string content of the variable}
>>> print toLaTeXVariable(n,s,'newcommand')
\newcommand{\varName}{some string content of the variable}
>>> print toLaTeXVariable(n,s,'renewcommand')
\renewcommand{\varName}{some string content of the variable}
latexexpr.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.
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.sympy as lsympy
>>> v1 = latexexpr.Variable('v1',None)
>>> v2 = latexexpr.Variable('v2',None)
>>> v3 = latexexpr.Variable('v3',1.23)
>>> v4 = latexexpr.Variable('v4',4.56)
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.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.Expression('e2',latexexpr.SIN(x)**2+latexexpr.COS(x)**2)
>>> print lsympy.simplify(e2)
e2 = 1 = 1 \ \mathrm{} = 1 \ \mathrm{}
>>> e3 = latexexpr.Expression('e3', (x**3 + x**2 - x - 1) / (x**2 + 2*x + 1) )
>>> print lsympy.simplify(e3)
e3 = {-1} + {x}
Performs simplify operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)
Parameters: |
|
---|---|
Return type: | type(arg) |
>>> import latexexpr.sympy as lsympy
>>> v1 = latexexpr.Variable('v1',None)
>>> v2 = latexexpr.Variable('v2',None)
>>> v3 = latexexpr.Variable('v3',1.23)
>>> v4 = latexexpr.Variable('v4',4.56)
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.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.Expression('e2',latexexpr.SIN(x)**2+latexexpr.COS(x)**2)
>>> print lsympy.simplify(e2)
e2 = 1 = 1 \ \mathrm{} = 1 \ \mathrm{}
>>> e3 = latexexpr.Expression('e3', (x**3 + x**2 - x - 1) / (x**2 + 2*x + 1) )
>>> print lsympy.simplify(e3)
e3 = {-1} + {x}
Performs expand operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)
Parameters: |
|
---|---|
Return type: | type(arg) |
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.Expression('e1', (x+1)**2 )
>>> print lsympy.expand(e1,substituteFloats=True)
e1 = {1} + {2} \cdot {x} + { {x} }^{ {2} }
>>> e2 = latexexpr.Expression('e2', (x+2)*(x-3) )
>>> print lsympy.expand(e2)
e2 = {-6} + \left( - {x} \right) + { {x} }^{ {2} }
>>> e3 = latexexpr.Expression('e3', (x+1)*(x-2) - (x-1)*x )
>>> print lsympy.expand(e3)
e3 = -2 = \left( -2 \right) \ \mathrm{} = \left(-2\right) \ \mathrm{}
Performs factor operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)
Parameters: |
|
---|---|
Return type: | type(arg) |
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> y = latexexpr.Variable('y',None)
>>> z = latexexpr.Variable('z',None)
>>> e1 = latexexpr.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.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} }
Performs collect operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)
Parameters: |
|
---|---|
Return type: | type(arg) |
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> y = latexexpr.Variable('y',None)
>>> z = latexexpr.Variable('z',None)
>>> e1 = latexexpr.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)
Performs cancel operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)
Parameters: |
|
---|---|
Return type: | type(arg) |
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> y = latexexpr.Variable('y',None)
>>> z = latexexpr.Variable('z',None)
>>> e1 = latexexpr.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.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.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)
Performs apart operation on arg. Symbolic variables are left symbolic, but variables with values are treated as the values (!)
Parameters: |
|
---|---|
Return type: | type(arg) |
>>> import latexexpr.sympy as lsympy
>>> x = latexexpr.Variable('x',None)
>>> e1 = latexexpr.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} }