Expression Tree
SRToolkit.utils.expression_tree
Binary expression tree (Node) and conversion utilities between token lists, trees, and LaTeX strings.
Node
A node in a binary expression tree.
- Binary operators (
"op") set bothleftandright. - Unary functions (
"fn") set onlyleft;rightisNone. - Leaves (variables, constants, literals, numeric values) have both children as
None.
Examples:
Warning
The second positional argument is right, not left. When passing
children positionally (e.g. Node("+", Node("a"), Node("b"))),
Node("a") becomes the right child and Node("b") the left.
Use keyword arguments to avoid confusion: Node("+", right=Node("a"), left=Node("b")).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Token string stored at this node. |
required |
right
|
Optional[Node]
|
Right operand (binary operators only). |
None
|
left
|
Optional[Node]
|
Left operand (operators and unary functions). |
None
|
Source code in SRToolkit/utils/expression_tree.py
to_list
Transforms the tree rooted at this node into a list of tokens.
Examples:
>>> node = Node("+", Node("X_0"), Node("1"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['1', '+', 'X_0']
>>> node.to_list(notation="postfix")
['1', 'X_0', '+']
>>> node.to_list(notation="prefix")
['+', '1', 'X_0']
>>> node = Node("+", Node("*", Node("X_0"), Node("X_1")), Node("1"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['1', '+', 'X_1', '*', 'X_0']
>>> node.to_list(notation="infix")
['1', '+', '(', 'X_1', '*', 'X_0', ')']
>>> node = Node("sin", None, Node("X_0"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['sin', '(', 'X_0', ')']
>>> node = Node("^2", None, Node("X_0"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['X_0', '^2']
>>> node.to_list()
['(', 'X_0', ')', '^2']
>>> node = Node("*", Node("*", Node("X_0"), Node("X_0")), Node("X_0"))
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols(),notation="infix")
['X_0', '*', '(', 'X_0', '*', 'X_0', ')']
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol_library
|
Optional[SymbolLibrary]
|
Symbol library used to determine token types and precedences
during infix reconstruction. If |
None
|
notation
|
str
|
Output notation: |
'infix'
|
Returns:
| Type | Description |
|---|---|
List[str]
|
Token list representing the subtree rooted at this node. |
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Exception
|
If |
Source code in SRToolkit/utils/expression_tree.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
to_latex
Transforms the tree rooted at this node into a LaTeX expression.
Examples:
>>> node = Node("+", right=Node("X_0"), left=Node("1"))
>>> node.to_latex(symbol_library=SymbolLibrary.default_symbols())
'$1 + X_{0}$'
>>> node = Node("+", right=Node("*", right=Node("X_0"), left=Node("X_1")), left=Node("1"))
>>> print(node.to_latex(symbol_library=SymbolLibrary.default_symbols()))
$1 + X_{1} \cdot X_{0}$
>>> node = Node("sin", None, Node("X_0"))
>>> print(node.to_latex(symbol_library=SymbolLibrary.default_symbols()))
$\sin X_{0}$
>>> node = Node("+", right=Node("*", right=Node("X_0"), left=Node("C")), left=Node("C"))
>>> print(node.to_latex(symbol_library=SymbolLibrary.default_symbols()))
$C_{0} + C_{1} \cdot X_{0}$
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol_library
|
Optional[SymbolLibrary]
|
Symbol library providing LaTeX templates for each token. If None, falls back to the currently active library set via 'with SymbolLibrary(...) as sl:'. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A LaTeX string of the form |
Raises:
| Type | Description |
|---|---|
Exception
|
If the tree contains a token whose type cannot be resolved in
|
Source code in SRToolkit/utils/expression_tree.py
height
Return the height of the subtree rooted at this node.
A single-node tree has height 1.
Examples:
Returns:
| Type | Description |
|---|---|
int
|
Height of the subtree. |
Source code in SRToolkit/utils/expression_tree.py
__len__
Return the number of nodes in the subtree rooted at this node.
Examples:
Returns:
| Type | Description |
|---|---|
int
|
Total node count of the subtree. |
Source code in SRToolkit/utils/expression_tree.py
__str__
Return the expression as a concatenated string using default infix notation that may contain redundant parentheses.
Examples:
Returns:
| Type | Description |
|---|---|
str
|
Concatenated token string with no spaces. |
Source code in SRToolkit/utils/expression_tree.py
__copy__
Return a deep copy of the subtree rooted at this node.
Examples:
>>> node = Node("+", Node("X_0"), Node("1"))
>>> new_node = copy(node)
>>> node.to_list(symbol_library=SymbolLibrary.default_symbols())
['1', '+', 'X_0']
>>> new_node.to_list(symbol_library=SymbolLibrary.default_symbols())
['1', '+', 'X_0']
>>> node == node
True
>>> node == new_node
False
Returns:
| Type | Description |
|---|---|
Node
|
An independent copy of the subtree. |
Source code in SRToolkit/utils/expression_tree.py
is_float
Return True if element can be interpreted as a floating-point number.
Examples:
>>> is_float(1.0)
True
>>> is_float("1.0")
True
>>> is_float("1")
True
>>> is_float(None)
False
>>> is_float("hello")
False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Any
|
Value to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in SRToolkit/utils/expression_tree.py
tokens_to_tree
Parse a token list into an expression tree using the shunting-yard algorithm.
Examples:
>>> tree = tokens_to_tree(["(", "X_0", "+", "X_1", ")"], SymbolLibrary.default_symbols())
>>> len(tree)
3
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
List[str]
|
Token list in infix notation. |
required |
sl
|
Optional[SymbolLibrary]
|
Symbol library used to resolve token types and precedences. If None, falls back to the currently active library set via 'with SymbolLibrary(...) as sl:'. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Node
|
Root Node of the parsed expression tree. |
Raises:
| Type | Description |
|---|---|
Exception
|
If a token is absent from |
Source code in SRToolkit/utils/expression_tree.py
expr_to_latex
Convert an expression to a LaTeX string.
Examples:
>>> expr_to_latex(["(", "X_0", "+", "X_1", ")"], SymbolLibrary.default_symbols())
'$X_{0} + X_{1}$'
>>> expr = Node("+", Node("X_0"), Node("1"))
>>> expr_to_latex(expr, SymbolLibrary.default_symbols())
'$1 + X_{0}$'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Union[Node, List[str]]
|
Expression as a token list or a Node tree. |
required |
symbol_library
|
Optional[SymbolLibrary]
|
Symbol library providing LaTeX templates. If None, falls back to the currently active library set via 'with SymbolLibrary(...) as sl:'. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A LaTeX string of the form |