Expression Compiler Module
SRToolkit.utils.expression_compiler
This module contains functions that convert an expression in infix notation to an executable python function.
expr_to_executable_function(expr, symbol_library=SymbolLibrary.default_symbols())
Converts an expression in infix notation to an executable function.
Examples:
>>> executable_fun = expr_to_executable_function(["A", "+", "1"])
>>> executable_fun(np.array([[1], [2], [3], [4]]), np.array([]))
array([2, 3, 4, 5])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr
|
The expression in infix notation. |
required | |
symbol_library
|
The symbol library to use. Defaults to SymbolLibrary.default_symbols(). |
default_symbols()
|
Returns:
Type | Description |
---|---|
callable
|
An executable function that takes in a 2D array of input values and a 1D array of constant values and returns the output of the expression. |
Source code in SRToolkit/utils/expression_compiler.py
expr_to_error_function(expr, symbol_library=SymbolLibrary.default_symbols())
Converts an expression in infix notation to an executable function that returns the root mean squared error between the output of the expression and the target values.
Examples:
>>> executable_fun = expr_to_error_function(["X_0", "+", "1"])
>>> executable_fun(np.array([[1], [2], [3], [4]]), np.array([]), np.array([2, 3, 4, 5]))
0.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr
|
The expression in infix notation. |
required | |
symbol_library
|
The symbol library to use. Defaults to SymbolLibrary.default_symbols(). |
default_symbols()
|
Returns:
Type | Description |
---|---|
callable
|
An executable function that takes in a 2D array of input values |
Source code in SRToolkit/utils/expression_compiler.py
tree_to_function_rec(tree, symbol_library, var_counter=0, const_counter=0)
Recursively converts a parse tree into a string of Python code that can be executed to evaluate the expression represented by the tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree
|
Node
|
The root of the parse tree to convert. |
required |
symbol_library
|
SymbolLibrary
|
The symbol library to use when converting the tree. This library defines the properties of the symbols in the tree. |
required |
var_counter
|
int
|
The number of variables encountered so far. This is used to create a unique variable name for each variable. |
0
|
const_counter
|
int
|
The number of constants encountered so far. This is used to select the correct constant value from the constant array. |
0
|
Returns:
Type | Description |
---|---|
List[str]
|
A list of strings, where each string contains a line of Python code to execute to evaluate the expression represented by the tree. |
str
|
The name of the variable that represents the output of the expression. |
int
|
The updated value of |
int
|
The updated value of |
Raises:
Type | Description |
---|---|
Exception
|
If the parse tree contains an invalid symbol. |
Notes
This function is a helper function for expr_to_executable_function
and similar and should not be called directly
unless you want to customize the way the expression is defined. For examples, see the code of expr_to_executable_function
and expr_to_error_function
in this module.