pythmpg.parse_jahn¶
Parser for index symmetrization information in Jahn symbols.
Defines two functions intended to be called externally:
parse_jahn_symbol(), which parses Jahn symbols
into structured instruction blocks describing the needed
symmetrization or antisymmetrization over tensor axes, and
jahn_rank() to return the rank of the tensor.
Notes
Jahn symbols passed to the parser should lack any preceding 'a'
or 'e' characters. Results are cached in the module-level
jahn_dict to avoid redundant parsing of repeated symbols.
- pythmpg.parse_jahn.find_match(text)[source]¶
Find the closing bracket that matches the opening bracket at index 0.
- Parameters:
text (str) – String whose first character is either
'['or'{'.- Returns:
ic – Index of the character immediately after the matching closing bracket (i.e., the length of the balanced sub-string).
- Return type:
int
- Raises:
ParsingError – If the string ends before a matching closing bracket is found.
- pythmpg.parse_jahn.jahn_rank(instructions)[source]¶
Determine the tensor rank from a parsed operation specification.
- Parameters:
instructions (tuple) – Nested instruction specification as returned by
parse_jahn_symbol().- Returns:
rank – Rank of the tensor (zero in the case of scalars).
- Return type:
int
- pythmpg.parse_jahn.list_to_tuple(job, b_text, depth)[source]¶
Recursively parse a block text string into an instruction tuple
Constructs and returns an instruction block of the form
(job, obj_list), whereobj_listis a list of axes or sub-blocks, built by recursively processing any nested bracket groups found inb_text. The globalaxiscounter is also incremented for each tensor axis encountered.- Parameters:
job (str) – Symmetrization mode for this block:
'nosymm','symm', or'asymm'.b_text (str) – Block text string to be parsed, e.g.,
'1[22]1'.depth (int) – Current recursion depth, used for indented debug printing and as a guard against runaway recursion.
- Returns:
result – Parsed instruction block in the form of a tuple
(job, obj_list), or the unwrapped inner tuple when the block reduces to a single sub-tuple under'nosymm'.- Return type:
tuple
- Raises:
ParsingError – If
b_textcontains elements of inconsistent type under a symmetrizing job, an empty list is found, fewer than two sub-blocks appear where multiple are required, or an unexpected object type is encountered.
- pythmpg.parse_jahn.parse_jahn_symbol(jahn, if_print=False)[source]¶
Parse the index symmetrization information in a Jahn symbol.
Converts the Jahn symbol into an instruction set represented as a nested tuple structure specifying which tensor axes are subject to symmetrization or antisymmetrization. Results are cached in
jahn_dictso that repeated calls with the same symbol entail no overhead.- Parameters:
jahn (str) – Jahn symbol to parse, lacking any preceding
'a'or'e'characters.if_print (bool, optional) – If
True, print intermediate parsing steps to stdout. Default isFalse.
- Returns:
instructions – Nested tuple specifying tensor axes and the symmetrization operations to be applied to them. Returns
Noneif the symbol contains illegal characters or if the parser fails.- Return type:
tuple or None
- pythmpg.parse_jahn.parse_string(b_string)[source]¶
Tokenise a block text string into a mixed list of ints and strings.
- Parameters:
b_string (str) – String in Jahn block-text format, e.g.,
'1[{2}{2}]2'.- Returns:
b_list – Mixed list of integers (single-digit axis counts) and strings (bracketed sub-blocks), e.g.,
[1, '[{2}{2}]', 2].- Return type:
list
- Raises:
ParsingError – If an unexpected character is encountered in
b_string.