natu.exponents

Contains a class with methods to represent and operate on the product of factors with exponents

class natu.exponents.Exponents(iterable=None, **kwds)

Bases: collections.Counter

Dictionary-like class to track exponents of independent bases

The keys are the bases and the values are the exponents. The supported mathematical operations (addition, subtraction, negation, multiplication, and division) operate on the exponents, not on the bases.

Initialization signatures:

  • Exponents(): Returns an Exponents instance with an empty set of factors

  • Exponents(a=exp1, b=exp2, ...), where exp1 and exp2 are numbers: Returns an Exponents instance that represents the product of symbol ‘a’ raised to the power of exp1 and symbol ‘b’ raised to the power of exp2

  • Exponents(dict(a=exp1, b=exp2, ...)): Returns the same result as the previous signature

  • Exponents({a: exp1, b: exp2, ...}): Returns the same result as the previous signature

  • Exponents(string): Returns an Exponents instance indicated by string

    string must follow the format accepted by the fromstr() constructor.

Formatting

An Exponents instance can be expressed using string format syntax. The format codes are:

  • ‘’ (default):

    • Exponents directly follow the symbols of the bases.
    • ‘*’ indicates multiplication and ‘/’ indicates division.
    • If the denominator contains multiple factors, then they are grouped in parentheses.
  • ‘H’ (HTML):

    • Exponents are written as superscripts (‘<sup>...</sup>’)
    • A non-breaking space (‘&nbsp;’) indicates multiplication and ‘/’ indicates division.
    • If the denominator contains multiple factors, then they are grouped in parentheses.
  • ‘L’ (LaTeX math):

    • Exponents are written as superscripts (‘^...’)
    • Back-to-back factors indicate multiplication and ‘/’ indicates division.
    • If the denominator contains multiple factors, then they are grouped in parentheses.
    • The output must be typeset in LaTeX math mode (e.g., surround it with ‘$...$’).
  • ‘M’ ([Modelica]): Same as the default except ‘.’ indicates multiplication instead of ‘*’

  • ‘U’ (Unicode):

    • Exponents are indicated by Unicode superscripts. Only integer exponents are supported.
    • A space indicates multiplication. Negative exponents are used instead of division.
  • ‘V’ (verbose):

    • Exponents are noted by ‘**’.
    • ‘ * ‘ indicates multiplication and ‘ / ‘ indicates division.
    • If the denominator contains multiple factors, then they are grouped in parentheses.

Examples:

Initialization and representation:

>>> x = Exponents(a=1, b=-1, c=-2)
>>> dict(x)
{'a': 1, 'b': -1, 'c': -2}
>>> str(x)
'a/(b*c2)'
>>> format(x, 'V')
'a / (b * c**2)'
>>> format(x, 'H')
'a&nbsp;b<sup>-1</sup>&nbsp;c<sup>-2</sup>'

The last result displays in HTML as the following: a b-1 c-2.

Addition:

>>> print(x + x)
a2/(b2*c4)

Multiplication:

>>> print(x*2)
a2/(b2*c4)
>>> str(0*x)
''

Division:

>>> print(x/2)
a0.5/(b0.5*c)

Negation:

>>> print(-x)
b*c2/a

Subtraction:

>>> str(x - x)
''

Indexing:

>>> x['a']
1
>>> x['d']
0

Updating:

>>> x['a'] += 1
>>> x['d'] += 2
>>> x['b'] = 2
>>> str(x)
'a2*d2/(b2*c2)'

References

[Modelica]Modelica Specification, version 3.3, p. 235–236 (https://www.modelica.org/documents)
classmethod fromstr(expr)

Create an Exponents instance from a string expression.

Parameters:

  • expr: String expression that indicates the symbols and exponents

    Each symbol may be followed directly by an exponent. If the exponent is not an integer, it may be expressed using a decimal or as a fraction (with ‘/’) enclosed in parentheses. Factors may be multiplied(‘*’), divided (‘/’), or grouped (‘(...)’).

Example:

>>> e = Exponents.fromstr('a/b/(c*d2)')
>>> print(e)
a/(b*c*d2)
update([E, ]**F) → None

Update this Exponents instance from dict/iterable E and F.

If E is present, this does:

for k in E: D[k] = E[k]

Then it does:

for k in F: D[k] = F[k]

Alternatively, E can be a string that is compatible with the fromstr() constructor.

Example:

>>> e = Exponents()
>>> e.update(a=1)
>>> e.update(dict(b=2), c=3)
>>> e.update('1/d')
>>> e 
Exponents({'a': 1, 'b': 2, 'c': 3, 'd': -1})
natu.exponents.split_code(code)

Split a string format code into standard and exponent-related parts.