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
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)
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: "{} \text{symb} {}"
for operators, "\text{symb} {}" for functions, and "\text{symb}" otherwise.
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. |
required |
np_fn
|
str
|
Python/NumPy expression string used in compiled callables
(e.g. |
required |
latex_str
|
Optional[str]
|
LaTeX template string with |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in SRToolkit/utils/symbol_library.py
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_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. |
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. Must be a subset of the default symbol names. |
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
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 514 515 516 517 518 519 520 521 522 523 524 | |
to_dict
Serialize the library to a JSON-safe dictionary.
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. |
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. |