Expression Tree Module
SRToolkit.utils.expression_tree
The module containing the expression tree data structure and functions for transforming expressions into trees and back.
Node
Source code in SRToolkit/utils/expression_tree.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
__init__(symbol=None, right=None, left=None)
Initializes a Node object. We assume that nodes containing functions have only one child node, i.e. right is None.
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol string stored in this node. |
None
|
right
|
Node
|
The right child of this node. |
None
|
left
|
Node
|
The left child of this node. |
None
|
Functions:
Name | Description |
---|---|
__len__ |
Returns the number of nodes in the tree rooted at this node. |
__str__ |
Returns a string representation of the tree rooted at this node. |
to_list |
str = "infix", symbol_library: SymbolLibrary = None): Returns a list representation of the tree rooted at this node. |
Source code in SRToolkit/utils/expression_tree.py
__len__()
Returns the number of nodes in the tree rooted at this node.
Examples:
Returns:
Type | Description |
---|---|
int
|
The number of nodes in the tree rooted at this node. |
Source code in SRToolkit/utils/expression_tree.py
__str__()
Returns a string representation of the tree rooted at this node.
Examples:
Returns:
Type | Description |
---|---|
str
|
A string representation of the tree rooted at this node. |
Source code in SRToolkit/utils/expression_tree.py
to_list(notation='infix', symbol_library=None)
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(notation="infix", symbol_library=SymbolLibrary.default_symbols())
['X_0', '*', '(', 'X_0', '*', 'X_0', ')']
Parameters:
Name | Type | Description | Default |
---|---|---|---|
notation
|
str
|
The notation to use for the resulting list of tokens. One of "prefix", "postfix", or "infix". |
'infix'
|
symbol_library
|
SymbolLibrary
|
The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree. |
None
|
Returns:
Type | Description |
---|---|
List[str]
|
A list of tokens representing the tree rooted at this node in the specified notation. |
Raises:
Type | Description |
---|---|
Exception
|
If the notation is not one of "prefix", "postfix", or "infix" or if a symbol is not in the symbol library. |
Notes
If the notation is "infix" and the symbol library is not provided, then the resulting list of tokens may contain unnecessary parentheses or have other issues.
Source code in SRToolkit/utils/expression_tree.py
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 160 |
|
to_latex(symbol_library)
Transforms the tree rooted at this node into a LaTeX expression.
Examples:
>>> node = Node("+", Node("X_0"), Node("1"))
>>> node.to_latex(symbol_library=SymbolLibrary.default_symbols())
'$1 + X_{0}$'
>>> node = Node("+", Node("*", Node("X_0"), Node("X_1")), 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("+", Node("*", Node("X_0"), Node("C")), 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
|
SymbolLibrary
|
The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree. |
required |
Returns:
Type | Description |
---|---|
str
|
A latex string representing the tree rooted at this node. |
Raises:
Type | Description |
---|---|
Exception
|
If the notation is not one of "prefix", "postfix", or "infix" or if a symbol is not in the symbol library. |
Source code in SRToolkit/utils/expression_tree.py
__copy__()
Creates a copy of the expression (usefull for manipulating expressions).
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 |
---|---|
A copy of the expression (tree). |
Source code in SRToolkit/utils/expression_tree.py
is_float(element)
Checks if a given element is a float.
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element
|
any
|
The element to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the element is a float, False otherwise. |
Source code in SRToolkit/utils/expression_tree.py
tokens_to_tree(tokens, sl)
Converts a list of tokens to a tree data structure. Throws an exception if the expression is invalid (check syntax and that all symbols are in the symbol library correctly defined).
Examples:
>>> tree = tokens_to_tree(["(", "x", "+", "y", ")"], SymbolLibrary.default_symbols())
>>> len(tree)
3
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokens
|
List[str]
|
The list of tokens to convert. |
required |
sl
|
SymbolLibrary
|
The symbol library to use when parsing the tokens. |
required |
Returns:
Type | Description |
---|---|
Node
|
The root of the expression tree data structure. |
Raises:
Type | Description |
---|---|
Exception
|
If the expression is invalid. Usually this means that a symbol is not in the symbol library or that there is a syntactic error in the expression. |