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
10 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 |
|
__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']
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
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 |
|
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. |