Symbol Library
SRToolkit.utils.symbol_library
This module contains the SymbolLibrary class, which is used for managing symbols and their properties.
SymbolLibrary
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
add_symbol
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
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
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
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
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
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
|
str
|
The symbol to look up. |
required |
Returns:
| Type | Description |
|---|---|
str
|
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
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
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
|
int
|
Number of variables you want. |
25
|
Returns:
| Type | Description |
|---|---|
SymbolLibrary
|
An instance of SymbolLibrary |
Source code in SRToolkit/utils/symbol_library.py
default_symbols
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. |
Source code in SRToolkit/utils/symbol_library.py
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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
to_dict
Creates a dictionary representation of the SymbolLibrary instance.
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing the symbol library's data. |
Source code in SRToolkit/utils/symbol_library.py
from_dict
staticmethod
Creates a SymbolLibrary instance from its dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
the dictionary containing data about the symbol library. |
required |
Returns:
| Type | Description |
|---|---|
SymbolLibrary
|
The SymbolLibrary instance created from the dictionary. |
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. |