Classes#

The module contains three fundamental classes: Variable, Operation and Expression.

Variable#

class latexexpr_efficalc.Variable(name, value=None, unit='', format='%.4g', unitFormat='\\mathrm{%s}', exponent=0)#

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 absolute() 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:
  • name (str) – symbolic name of the variable

  • value (float|None) – value of the variable. If value==None, than the Variable is considered as symbolic

  • unit (str) – physical unit of the variable

  • format (str) – python string to be formatted by the numeric value with ‘%’ operation (e.g. ‘%e’, ‘%g’, ‘%.4g’, ‘%.3f’ etc.). See Python string formatting docs for more details.

  • unitFormat (str) –

    python string to be formatted with unit (default is ‘mathrm{%s}’ for non-italic units inside math mode). For no formatting use ‘%s’. See Python string formatting docs for more details.

  • exponent (int) – exponent for scientific representation

>>> 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
__str__()#

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
__float__()#

Returns numeric result of the receiver

Return type:

float

>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print float(v1)
3.45
exponent = 0#

exponent for scientific representation. If 0, then no scientific representation is performed

format = '%.4g  '#

string to be formatted by the numeric value (with ‘%’ operation)

from_expression(expr)#

Copy information from given Expression or Variable. Returns changed receiver

Parameters:

expr (Variable|Expression) – given expression to be copied

Return type:

Variable

is_symbolic()#

Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value

name = ''#

symbolic name

result()#

Returns numeric result of the receiver (its value)

Return type:

float

>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.result()
3.45
str_result(format='', exponent=0)#

Returns string of the result of the receiver (its formatted result)

Parameters:
  • format (str) – how to format result if other than predefined in receiver is required

  • exponent (int) – exponent the returned string if other than predefined in receiver is required

Return type:

str

>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.str_result()
3.45
str_result_with_unit()#

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.str_result_with_unit()
3.45 \ \mathrm{mm}
str_substituted()#

Returns string of numeric representation of receiver (its formatted value)

Return type:

str

>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.str_substituted()
3.45
str_symbolic()#

Returns string of symbolic representation of receiver (its name)

Return type:

str

>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.str_symbolic()
{a_{22}}
to_latex_variable(name, what='float', command='def')#

Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command

Parameters:
  • name (str) – LaTeX name (without initial \ symbol)

  • what (str) – what to include (‘float’ for numeric value, ‘str’ for string value (with possible scientific .10^x), ‘valunit’ for string value + unit , ‘all’ for str(self)’

  • command (str) – LaTeX command to use (without initial \ symbol) [‘def’,’newcommand’,’renewcommand’]

>>> v1 = Variable('a_{22}',3.45,'mm')
>>> print v1.to_latex_variable('AA','float')
\def\AA{3.45}
>>> print v1.to_latex_variable('AA','str','newcommand')
\newcommand{\AA}{3.45}
>>> print v1.to_latex_variable('AA','valunit','renewcommand')
\renewcommand{\AA}{3.45 \ \mathrm{mm}}
>>> print v1.to_latex_variable('AA','all','def')
\def\AA{a_{22} = 3.45 \ \mathrm{mm}}
to_latex_variable_all(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’all’

to_latex_variable_float(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’float’

to_latex_variable_str(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’str’

to_latex_variable_val_unit(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’valunit’

unit = ''#

physical unit

Operation#

class latexexpr_efficalc.Operation(type, *args)#

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(r_brackets(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
__str__()#

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} } }
__float__()#

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
args = []#

argument list subjected to the operation Operation.type

is_symbolic()#

Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value

result()#

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
str_result(format='', exponent=0)#

Returns string of the result of the receiver (its formatted result)

Parameters:
  • format (str) – how to format result if other than predefined in receiver is required

  • exponent (int) – exponent the returned string if other than predefined in receiver is required

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.str_result()
2.14906
str_result_with_unit()#

Returns string of the result of the receiver (its formatted result) ending with its units

str_substituted()#

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.str_substituted()
\frac{ 3.45 + 5.87693 }{ { 434 \cdot 10^{-2} } }
str_symbolic()#

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.str_symbolic()
\frac{ {a_{22}} + {F} }{ {F} }
to_variable(newName='', **kw)#

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

type = None#

arithmetic type of operation

Expression#

class latexexpr_efficalc.Expression(name, operation, unit='', format='%.4g', unitFormat='\\mathrm{%s}', exponent=0)#

Class representing mathematical expression

Parameters:
  • name (str) – symbolic name of the expression

  • operation (Operation|Variable|Expression) – operation of the expression

  • unit (str) – physical unit of the expression

  • format (str) –

    python string to be formatted with ‘%’ operation (e.g. ‘%e’, ‘%g’, ‘%.4g’, ‘%.3f’ etc.). See Python string formatting docs for more details.

  • unitFormat (str) –

    python string to be formatted with unit (default is ‘mathrm{%s}’ for non-italic units inside math mode). For no formatting use ‘%s’. See Python string formatting docs for more details.

  • exponent (int) – exponent for scientific representation

>>> 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',s_brackets(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}
__str__()#

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}
__float__()#

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
exponent = 0#

see Variable.exponent

format = '%.4g'#

see Variable.format

is_symbolic()#

Returns if receiver (or at least one of its sub-components) is purely symbolic variable without specific value

name = ''#

symbolic name of the expression

operation = None#

underlying Operation instance

result()#

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
str_result(format='', exponent=0)#

Returns string of the result of the receiver (its formatted result)

Parameters:
  • format (str) – how to format result if other than predefined in receiver is required

  • exponent (int) – exponent the returned string if other than predefined in receiver is required

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.str_result()
2.14906
str_result_with_unit()#

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.str_result_with_unit()
2.14906 \ \mathrm{mm}
str_substituted()#

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.str_substituted()
2.14906
str_symbolic()#

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.str_symbolic()
{E_2}
to_latex_variable(name, what='float', command='def')#

Returns latex expression converting receiver to LaTeX variable using def, newcommand, or renewcommand LaTeX command

Parameters:
  • name (str) – LaTeX name (without initial \ symbol)

  • what (str) – what to include (‘float’ for numeric value, ‘str’ for string value (with possible scientific .10^x), ‘valunit’ for string value + unit , ‘symb’ for symbolic expression, ‘subst’ for substrituted expression and ‘all’ for str(self)’

  • command (str) – LaTeX command to use (without initial \ symbol) [‘def’,’newcommand’,’renewcommand’]

>>> 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.to_latex_variable('ETWO','float')
\def\ETWO{2.14906332604}
>>> print e2.to_latex_variable('ETWO','str','newcommand')
\newcommand{\ETWO}{2.14906}
>>> print e2.to_latex_variable('ETWO','valunit','renewcommand')
\renewcommand{\ETWO}{2.14906 \ \mathrm{mm}}
>>> print e2.to_latex_variable('ETWO','symb')
\def\ETWO{{E_2}}
>>> print e2.to_latex_variable('ETWO','subst')
\def\ETWO{2.14906}
>>> print e2.to_latex_variable('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}}
to_latex_variable_all(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’all’

to_latex_variable_float(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’float’

to_latex_variable_str(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’str’

to_latex_variable_subst(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’subst’

to_latex_variable_symb(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’symb’

to_latex_variable_val_unit(name, command='def')#

Shortcut for Variable.to_latex_variable() with what=’valunit’

to_variable(newName='')#

Returns new Variable instance with attributes copied from receiver

Parameters:

newName (str) – optional new name of returned variable

Return type:

Variable

unit = ''#

see Variable.unit