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 phisical 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.88 \ \mathrm{kN}
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> print v3
F = { 434.00 \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.88 \ \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.88 } }{ { 434.00 \cdot 10^{-2} } \cdot 2.5643 } + 5.88
>>> 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.88 } }{ { 434.00 \cdot 10^{-2} } \cdot 2.5643 } + 5.88 \right] - \left( -6.54 \right)^2 = \left(-36.41\right) \ \mathrm{kNm}
>>> v7 = e1.toVariable()
>>> print v7
E_1^i = \left( -36.41 \right) \ \mathrm{kNm}
>>> print v7.toLaTeXVariableAll('MYV7')
\def\MYV7{E_1^i = \left( -36.41 \right) \ \mathrm{kNm}}
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”.
This class also overloads +,-,*,/ (division, frac{...}{...} in LaTeX) and // (divsion, .../... in LaTeX) operators. They can be used with Variable, Expression or Operation instances resulting into new Operation instance.
Parameters: |
|
---|---|
Variables: |
|
>>> 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.88 \ \mathrm{kN}
>>> v3 = Variable('F',4.34,'kN',exponent=-2)
>>> print v3
F = { 434.00 \cdot 10^{-2} } \ \mathrm{kN}
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|int |
---|
>>> 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 hard copy of receiver
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')
>>> o3 = (v1+v2)/v3
>>> print o3
\frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.88 }{ { 434.00 \cdot 10^{-2} } }
>>> 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.15 + 2.5643 \right) \cdot 5.88 \cdot \left( -6.54 \right)
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.88 }{ { 434.00 \cdot 10^{-2} } }
Returns string of the result of the receiver (its formatted result)
param str format: how to format result if other than predefined in receiver is required param int exponent: exponent the returned string if other than predefined in receiver is required rtype: str
%s
Returns numeric result of the receiver
Return type: | float|int |
---|
>>> 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 |
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')
>>> 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.88 } }{ { 434.00 \cdot 10^{-2} } \cdot 2.5643 } + 5.88 \right] - \left( -6.54 \right)^2 = \left(-36.41\right) \ \mathrm{kNm}
>>> e2 = Expression('E_2',(v1+v2)/v3,'mm')
>>> print e2
E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.88 }{ { 434.00 \cdot 10^{-2} } } = 2.15 \ \mathrm{mm}
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.15
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.15
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.15 \ \mathrm{mm}
Returns numeric result of the receiver
Return type: | float|int |
---|
>>> 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.15}
>>> print e2.toLaTeXVariable('ETWO','valunit','renewcommand')
\renewcommand{\ETWO}{2.15 \ \mathrm{mm}}
>>> print e2.toLaTeXVariable('ETWO','symb')
\def\ETWO{{E_2}}
>>> print e2.toLaTeXVariable('ETWO','subst')
\def\ETWO{2.15}
>>> print e2.toLaTeXVariable('ETWO','all','def')
\def\ETWO{E_2 = \frac{ {a_{22}} + {F} }{ {F} } = \frac{ 3.45 + 5.88 }{ { 434.00 \cdot 10^{-2} } } = 2.15 \ \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’
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}