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
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|
__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", "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
__len__()
Returns the number of symbols currently stored in the SymbolLibrary.
Examples:
>>> library = SymbolLibrary.default_symbols(5)
>>> len(library)
34
>>> library.add_symbol("a", "lit", 5, "a", "a")
>>> len(library)
35
Returns Number of symbols currently stored in the SymbolLibrary.
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", "x")
>>> str(library)
'x'
>>> library.add_symbol("sin", "fn", 5, "{} = np.sin({})", r"\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", "x")
>>> print(old_symbols)
x
>>> new_symbols = copy.copy(old_symbols)
>>> new_symbols.add_symbol("sin", "fn", 5, "{} = np.sin({})", r"\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, latex_str=None)
Adds a symbol to the library. A symbol should have a type, precedence, a numpy function, and a LaTeX template 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.
We recommend you use a single token of "const" type as using multiple might lead to more work, errors, and less readability.
If the argument 'latex_str' is ommited, a default LaTeX template will be generated for the symbol. In case of symbol 'symb', the default template will be '{} \text{symb} {}' for an operator,'\text{symb} {}' for a function, and '\text{symb}' otherwise.
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({})", r"\sin {}")
>>> library.add_symbol("C", "const", 5, "C[{}]", r"c_{}")
>>> library.add_symbol("X_0", "var", 5, "X[:, 0]", r"X_0")
>>> library.add_symbol("pi", "lit", 5, "np.pi", r"\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 |
latex_str
|
str
|
A string that represents how the symbol is written in LaTeX |
None
|
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
get_latex_str(symbol)
Returns the LaTeX template for the corresponding symbol.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x", "test")
>>> library.get_latex_str("x")
'test'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
The symbol to look up. |
required |
Returns:
Type | Description |
---|---|
The LaTeX template for the corresponding symbol, or an empty string if the symbol was not found. |
Source code in SRToolkit/utils/symbol_library.py
get_symbols_of_type(symbol_type)
Returns a list of symbols with the requested type ("op", "fn", "var", "const", "lit").
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.add_symbol("y", "var", 0, "y")
>>> library.get_symbols_of_type("var")
['x', 'y']
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol_type
|
str
|
Type of symbols you want to get. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
A list of symbols with the requested type |
Source code in SRToolkit/utils/symbol_library.py
symbols2index()
Generates a dictionary mapping symbols to their indices in the symbol list.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.add_symbol("y", "var", 0, "y")
>>> print(library.symbols2index())
{'x': 0, 'y': 1}
>>> library.remove_symbol("x")
>>> print(library.symbols2index())
{'y': 0}
Returns:
Type | Description |
---|---|
Dict[str, int]
|
A dictionary mapping symbols to their indices in the symbol list. |
Source code in SRToolkit/utils/symbol_library.py
from_symbol_list(symbols, num_variables=25)
staticmethod
Creates an instance of SymbolLibrary from a list of symbols and number of variables. The list of currently supported symbols (by default) can be seen in the SymbolLibrary.default_symbols() function.
Examples:
>>> library = SymbolLibrary().from_symbol_list(["+", "*", "C"], num_variables=2)
>>> len(library.symbols)
5
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbols
|
List[str]
|
List of symbols you want. |
required |
num_variables
|
Number of variables you want. |
25
|
Returns:
Type | Description |
---|---|
An instance of SymbolLibrary |
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.
By default, we currently support the following symbols: "+", "-", "*", "/", "^", "u-" (unary minus), "sqrt", "sin", "cos", "exp", "tan", "arcsin", "arccos", "arctan", "sinh", "cosh", "tanh", "floor", "ceil", "ln", "log", "^-1", "^2", "^3", "^4", "^5", "pi", "e", "C" (unknown constant).
Notes: 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. |