Symbol Library
SRToolkit.utils.symbol_library
The SymbolLibrary class for managing the token vocabulary used in symbolic regression expressions.
SymbolLibrary
SymbolLibrary(symbols: Optional[List[str]] = None, num_variables: int = 0, preamble: Optional[List[str]] = None)
A registry of tokens and their properties, used throughout the toolkit to parse, compile, and generate symbolic expressions.
By default, the library uses NumPy for operator and function evaluation. To use a
different backend, pass the required import statements via preamble.
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()
>>> # You can also initialize the library with a list of symbols (listed in SymbolLibrary.default_symbols)
>>> # and the number of variables.
>>> library2 = SymbolLibrary(["+", "*", "sin"], num_variables=2)
>>> len(library2)
5
>>> # Use as a context manager to avoid passing sl explicitly
>>> # with SymbolLibrary.default_symbols(num_variables=2) as sl:
>>> # tree = tokens_to_tree(["X_0", "+", "X_1", "*", "C"])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols
|
Optional[List[str]]
|
Symbols to pre-populate from the default set. |
None
|
num_variables
|
int
|
Number of variable tokens to add, labeled |
0
|
preamble
|
Optional[List[str]]
|
Import statements prepended to compiled expression functions.
Defaults to |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
symbols |
Mapping from token string to its property dict (type, precedence, NumPy function string, LaTeX template). |
Source code in SRToolkit/utils/symbol_library.py
add_symbol
add_symbol(symbol: str, symbol_type: str, precedence: int, np_fn: str, latex_str: Optional[str] = None, cython_id: int = -1)
Add a token to the library with its associated type, precedence, NumPy function string, and LaTeX template.
Symbol types:
"op": binary operator (e.g.+,*)."fn": unary function (e.g.sin,sqrt)."lit": literal with a fixed value (e.g.pi,e)."const": free constant whose value is optimised during parameter estimation (e.g.C). Using a single"const"token is recommended; multiple tokens increase complexity and reduce readability."var": input variable whose values are read from the data arrayX.
If latex_str is omitted, a default template is generated based on the symbol type:
"{} \text{symb} {}" for operators (e.g. + → {} \text{+} {}),
"\text{symb} {}" for functions (e.g. sin → \text{sin} {}),
and "\text{symb}" for all other types.
Note
For best performance, we recommend selecting symbols from the predefined set via the constructor or from_symbol_list rather than adding custom ones. Predefined symbols have C implementations and benefit from Cython acceleration; custom symbols always fall back to Python callables.
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
|
Token string to register. |
required |
symbol_type
|
str
|
One of |
required |
precedence
|
int
|
Operator precedence, used for infix reconstruction and PCFG generation.
For |
required |
np_fn
|
str
|
Python/NumPy expression string used in compiled callables
(e.g. |
required |
latex_str
|
Optional[str]
|
LaTeX template string with |
None
|
cython_id
|
int
|
Integer dispatch ID used by the Cython-based evaluator.
|
-1
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in SRToolkit/utils/symbol_library.py
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 | |
remove_symbol
Remove a token 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
|
Token string to remove. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in SRToolkit/utils/symbol_library.py
get_type
Return the type of a symbol.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_type("x")
'var'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Token to look up. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The type string ( |
Source code in SRToolkit/utils/symbol_library.py
get_precedence
Return the precedence of a symbol.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_precedence("x")
0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Token to look up. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The precedence value if the symbol is in the library, otherwise |
Source code in SRToolkit/utils/symbol_library.py
get_np_fn
Return the NumPy function string for a symbol.
Examples:
>>> library = SymbolLibrary()
>>> library.add_symbol("x", "var", 0, "x")
>>> library.get_np_fn("x")
'x'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Token to look up. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The NumPy function string if the symbol is in the library, otherwise an empty string. |
Source code in SRToolkit/utils/symbol_library.py
get_cython_id
Return the Cython stack machine dispatch ID for a symbol.
Returns -1 for symbols without a C implementation (they will fall
back to a Python callable during Cython evaluation).
Examples:
>>> library = SymbolLibrary.default_symbols()
>>> library.get_cython_id("+")
0
>>> library.get_cython_id("sin")
10
>>> library.get_cython_id("unknown_symbol")
-1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Token to look up. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer dispatch ID, or |
int
|
or has no Cython implementation. |
Source code in SRToolkit/utils/symbol_library.py
get_latex_str
Return the LaTeX template string for a 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
|
Token to look up. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The LaTeX template string if the symbol is in the library, otherwise an empty string. |
Source code in SRToolkit/utils/symbol_library.py
get_symbols_of_type
Return all symbols of a given type.
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 to filter by. One of |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List of token strings matching the requested type. Returns an empty list |
List[str]
|
if no symbols match or if |
Source code in SRToolkit/utils/symbol_library.py
symbols2index
Return a mapping from each token to its index in insertion order.
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]
|
Dict mapping each token string to its zero-based position in the library. |
Source code in SRToolkit/utils/symbol_library.py
from_symbol_list
staticmethod
Create a SymbolLibrary containing only the specified subset of default symbols.
The supported token names are those defined in default_symbols.
Examples:
>>> library = SymbolLibrary.from_symbol_list(["+", "*", "C"], num_variables=2)
>>> len(library.symbols)
5
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols
|
List[str]
|
Token strings to include. Symbols not in the default set are silently ignored. |
required |
num_variables
|
int
|
Number of variable tokens ( |
25
|
Returns:
| Type | Description |
|---|---|
SymbolLibrary
|
A SymbolLibrary restricted to the requested symbols and variables. |
Source code in SRToolkit/utils/symbol_library.py
default_symbols
staticmethod
Return a SymbolLibrary pre-populated with standard mathematical symbols.
Supported tokens:
- Operators (
"op"):+,-,*,/,^ - Functions (
"fn"):u-,sqrt,sin,cos,exp,tan,arcsin,arccos,arctan,sinh,cosh,tanh,floor,ceil,ln,log,^-1,^2,^3,^4,^5 - Literals (
"lit"):pi,e - Free constant (
"const"):C - Variables (
"var"):X_0throughX_{num_variables-1}, mapped to columns of the input array in order.
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_variables
|
int
|
Number of variable tokens to include. Default is |
25
|
Returns:
| Type | Description |
|---|---|
SymbolLibrary
|
A SymbolLibrary populated with the symbols listed above. |
Source code in SRToolkit/utils/symbol_library.py
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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
get_active
staticmethod
Return the currently active SymbolLibrary.
Checks, in order: (1) the context manager stack, (2) the module-level default set via set_default.
Returns:
| Type | Description |
|---|---|
SymbolLibrary
|
The active SymbolLibrary instance. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no library is active and no default has been set. |
Source code in SRToolkit/utils/symbol_library.py
get_or_default
staticmethod
Return the active SymbolLibrary, falling back to default_symbols when nothing is active.
Checks, in order: (1) the context manager stack, (2) the module-level default set via set_default, (3) a freshly constructed default library.
Returns:
| Type | Description |
|---|---|
SymbolLibrary
|
The active or default SymbolLibrary instance. |
Source code in SRToolkit/utils/symbol_library.py
set_default
staticmethod
Set (or clear) a module-level default SymbolLibrary.
The default is used as a fallback by get_active and get_or_default when no context manager is active. It is module-global (not per-thread or per-task) and intended for scripts and notebooks where a single library is used throughout a session.
Pass None to clear the default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sl
|
Optional[SymbolLibrary]
|
Library to set as the module-level default, or |
required |
Source code in SRToolkit/utils/symbol_library.py
to_dict
Serialize the library to a JSON-safe dictionary.
Examples:
>>> library = SymbolLibrary.from_symbol_list(["+"], num_variables=1)
>>> d = library.to_dict()
>>> d["format_version"]
1
>>> d["num_variables"]
1
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary suitable for passing to from_dict. |
Source code in SRToolkit/utils/symbol_library.py
from_dict
staticmethod
Reconstruct a SymbolLibrary from a dictionary produced by to_dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
Dictionary representation of the library, as produced by to_dict. |
required |
Returns:
| Type | Description |
|---|---|
SymbolLibrary
|
The reconstructed SymbolLibrary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in SRToolkit/utils/symbol_library.py
__len__
Return the number of symbols currently in the library.
Examples:
>>> library = SymbolLibrary.default_symbols(5)
>>> len(library)
34
>>> library.add_symbol("a", "lit", 5, "a", "a")
>>> len(library)
35
Returns:
| Type | Description |
|---|---|
int
|
Number of tokens registered in the library. |
Source code in SRToolkit/utils/symbol_library.py
__str__
Return a comma-separated string of all registered token strings.
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
|
All token names joined by |
Source code in SRToolkit/utils/symbol_library.py
__copy__
Return a copy of the library with independent copies of all attributes.
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 new SymbolLibrary instance with deep-copied symbols and preamble. |