Expression Tree
SRToolkit.utils.expression_tree
The module containing the expression tree data structure and functions for transforming expressions into trees and back.
Node
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. |
height |
Returns the height of 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. |
to_latex |
SymbolLibrary): Returns a LaTeX representation of the tree rooted at this node. |
__copy__ |
Returns a copy of the expression (tree). |
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 |
|---|---|---|---|
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
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 | |
to_latex
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
height
Returns the height of the tree rooted at this node.
Examples:
Returns:
| Type | Description |
|---|---|
int
|
The height 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
__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 |
|---|---|
Node
|
A copy of the expression (tree). |
Source code in SRToolkit/utils/expression_tree.py
is_float
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
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_0", "+", "X_1", ")"], 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. |
Source code in SRToolkit/utils/expression_tree.py
expr_to_latex
Transforms an expression into 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]]
|
An expression |
required |
symbol_library
|
SymbolLibrary
|
The symbol library. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A LaTeX string representing the expression. |