Expression Compiler
SRToolkit.utils.expression_compiler
Functions for compiling symbolic expressions into executable Python callables.
The primary interface are two functions:
- compile_expr:
returns a callable
f(X, C) → np.ndarray. - compile_expr_rmse:
returns an RMSE callable
f(X, C, y) → float.
Both accept a backend parameter that selects the evaluation engine:
"stack"(default): postfix stack-machine evaluator backed by Cython; falls back to pure-Python automatically when the compiled extension is unavailable."codegen": generates Python/NumPy source code viaexec(). Compatible with any custom symbol library and requires no compiled extensions."stack_py": pure-Python stack-machine evaluator; avoids the Cython→Python boundary overhead when the library contains many custom symbols.
The backend parameter selects the engine; lower-level per-backend functions are
internal and prefixed with _.
Use "stack" (the default) for best performance in most cases. Use "stack_py"
when the library contains many custom symbols to avoid Cython→Python call overhead per
instruction. Use "codegen" when evaluating on large datasets, as it generates a
single vectorised NumPy function with less per-instruction dispatch overhead.
compile_expr
compile_expr(expr: Union[List[str], Node], symbol_library: Optional[SymbolLibrary] = None, backend: str = 'stack') -> Callable[[np.ndarray, Optional[np.ndarray]], np.ndarray]
Compile an expression into a callable f(X, C) → np.ndarray.
Examples:
>>> f = compile_expr(["X_0", "+", "1"])
>>> f(np.array([[1.0], [2.0], [3.0]]), None)
array([2., 3., 4.])
>>> f = compile_expr(["X_0", "+", "1"], backend="codegen")
>>> f(np.array([[1], [2], [3]]), np.array([]))
array([2, 3, 4])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Union[List[str], Node]
|
Expression as a token list in infix notation or a Node tree. |
required |
symbol_library
|
Optional[SymbolLibrary]
|
Symbol library used to look up token types. Defaults to SymbolLibrary.default_symbols. |
None
|
backend
|
str
|
Evaluation backend. One of:
|
'stack'
|
Returns:
| Type | Description |
|---|---|
Callable[[ndarray, Optional[ndarray]], ndarray]
|
A callable |
Callable[[ndarray, Optional[ndarray]], ndarray]
|
|
Callable[[ndarray, Optional[ndarray]], ndarray]
|
(pass |
Callable[[ndarray, Optional[ndarray]], ndarray]
|
Returns a 1-D output array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in SRToolkit/utils/expression_compiler.py
compile_expr_rmse
compile_expr_rmse(expr: Union[List[str], Node], symbol_library: Optional[SymbolLibrary] = None, backend: str = 'stack', X: Optional[ndarray] = None) -> Callable[[np.ndarray, np.ndarray, np.ndarray], float]
Compile an expression into an RMSE callable f(X, C, y) → float.
Examples:
>>> f = compile_expr_rmse(["X_0", "+", "1"])
>>> f(np.array([[1.0], [2.0], [3.0]]), np.array([]), np.array([2.0, 3.0, 4.0]))
0.0
>>> f = compile_expr_rmse(["X_0", "+", "1"], backend="codegen")
>>> print(float(f(np.array([[1], [2], [3]]), np.array([]), np.array([2, 3, 4]))))
0.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Union[List[str], Node]
|
Expression as a token list in infix notation or a Node tree. |
required |
symbol_library
|
Optional[SymbolLibrary]
|
Symbol library used to look up token types. Defaults to SymbolLibrary.default_symbols. |
None
|
backend
|
str
|
Evaluation backend. One of:
|
'stack'
|
X
|
Optional[ndarray]
|
Optional input data of shape |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[ndarray, ndarray, ndarray], float]
|
A callable |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |