Skip to content

string_to_function

Formula recognition from strings.

It recognizes and returns the formula and variables from strings via the sympy package.

Parameters:

Name Type Description Default
formula

The function formula as a string.

required
variable

The list of the variables involved in the formula.

required

Returns:

Type Description
FunctionClass

The recognized function of the input formula.

Source code in tinybig/util/util.py
def string_to_function(formula, variable):
    """
    Formula recognition from strings.

    It recognizes and returns the formula and variables from strings via the sympy package.

    Parameters
    ----------
    formula: str
        The function formula as a string.
    variable: list
        The list of the variables involved in the formula.

    Returns
    -------
    sympy.FunctionClass
        The recognized function of the input formula.
    """
    # Define the symbol
    var = sp.symbols(variable)

    # Parse the formula string into a sympy expression
    expression = sp.sympify(formula)

    # Convert the sympy expression to a lambda function
    func = sp.lambdify(var, expression, 'numpy')

    return func