Symbol Library Module
SRToolkit.utils.symbol_library
This module contains the SymbolLibrary class, which is used for managing symbols and their properties.
SymbolLibrary
Source code in SRToolkit/utils/symbol_library.py
6 7 8 9 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 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 244 245 246 247 248 249 250 251 252 253 254 255 |
|
__init__()
Initializes an instance of the SymbolLibrary class. This class is used for managing symbols and their properties for other functionality in this package.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_type("x")
'var'
>>> library.get_precedence("x")
0
>>> library.get_np_fn("x")
'x'
>>> library.remove_symbol("x")
>>> library = SymbolLibrary.default_symbols()
Attributes:
Name | Type | Description |
---|---|---|
symbols |
dict A dictionary mapping symbols to their properties (type, precedence, numpy function). |
Functions:
Name | Description |
---|---|
add_symbol |
Adds a symbol to the library. |
remove_symbol |
Removes a symbol from the library. |
get_type |
Retrieves the type of a symbol from the library. |
get_precedence |
Returns the precedence of the given symbol. |
get_np_fn |
Returns the numpy function corresponding to the given symbol. |
default_symbols |
Returns a SymbolLibrary with the default symbols. |
Source code in SRToolkit/utils/symbol_library.py
__str__()
Returns a string representation of the SymbolLibrary instance.
This method provides a comma-separated string of all the symbol keys currently stored in the SymbolLibrary.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> str(library)
'x'
>>> library.add_symbol("sin", "fn", 5, "{} = np.sin({})")
>>> str(library)
'x, sin'
Returns:
Type | Description |
---|---|
str
|
A string containing all symbols in the library, separated by commas. |
Source code in SRToolkit/utils/symbol_library.py
__copy__()
Creates a copy of the SymbolLibrary instance.
Examples:
>>> old_symbols = SymbolLibrary()
>>> old_symbols.add_symbol("x", "var", 0, "x")
>>> print(old_symbols)
x
>>> new_symbols = copy.copy(old_symbols)
>>> new_symbols.add_symbol("sin", "fn", 5, "{} = np.sin({})")
>>> print(old_symbols)
x
>>> print(new_symbols)
x, sin
Returns:
Type | Description |
---|---|
SymbolLibrary
|
A copy of the SymbolLibrary instance. |
Source code in SRToolkit/utils/symbol_library.py
add_symbol(symbol, symbol_type, precedence, np_fn)
Adds a symbol to the library. A symbol should have a type, precedence, and numpy function associated with it. Type "op" should be used for symbols operating on two operands, "fn" for symbols operating on one operand, "lit" for constants with a known value (such as pi or e), "const" for constants/parameters without a value that need to be optimized, and "var" for variables whose values are provided as input data.
For example, look at the default_symbols function for the SymbolLibrary class.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.add_symbol("sin", "fn", 5, "np.sin({})")
>>> library.add_symbol("C", "const", 5, "C[{}]")
>>> library.add_symbol("X", "var", 5, "X[:, 0]")
>>> library.add_symbol("pi", "lit", 5, "np.pi")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol to be added to the library. |
required |
symbol_type
|
str
|
The type of the symbol, one of "op" (operator), "fn" (function), "lit" (literal), "const" (constant), or "var" (variable). |
required |
precedence
|
int
|
The precedence of the symbol, used to determine the order of operations. |
required |
np_fn
|
str
|
A string representing the numpy function associated with this symbol. |
required |
Source code in SRToolkit/utils/symbol_library.py
remove_symbol(symbol)
Removes a symbol from the library.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> len(library.symbols)
1
>>> library.remove_symbol("x")
>>> len(library.symbols)
0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol to be removed from the library. |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If the symbol does not exist in the library. |
Source code in SRToolkit/utils/symbol_library.py
get_type(symbol)
Retrieves the type of a symbol from the library.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_type("x")
'var'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol whose type is to be retrieved. |
required |
Returns:
Type | Description |
---|---|
str
|
The type of the symbol if it exists in the library, otherwise an empty string. |
Source code in SRToolkit/utils/symbol_library.py
get_precedence(symbol)
Retrieves the precedence of the given symbol.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_precedence("x")
0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol whose precedence is to be retrieved. |
required |
Returns:
Type | Description |
---|---|
int
|
The precedence of the symbol if it exists in the library, otherwise -1. |
Source code in SRToolkit/utils/symbol_library.py
get_np_fn(symbol)
Returns the numpy function corresponding to the given symbol.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_np_fn("x")
'x'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol to look up. |
required |
Returns:
Type | Description |
---|---|
str
|
The numpy function corresponding to the given symbol, or an empty string if the symbol was not found. |
Source code in SRToolkit/utils/symbol_library.py
default_symbols(num_variables=25)
staticmethod
Creates a SymbolLibrary instance populated with default mathematical symbols.
This method adds a set of predefined symbols to a SymbolLibrary instance, representing common mathematical operations, functions, constants, and optional variables. The symbols include basic arithmetic operations, trigonometric and exponential functions, and mathematical constants like pi and e.
If num_variables is greater than 0, it adds variables labeled 'X_0' to 'X_{num_variables-1}', each associated with a column in a data array X.
Note: The variables in the default_symbols function are added in the predefined order, which is the same order as the columns in the data array X.
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_variables
|
int
|
The number of variables to add to the library (default is 25). |
25
|
Returns:
Type | Description |
---|---|
SymbolLibrary
|
A SymbolLibrary instance populated with default mathematical symbols. |