atomica.function_parser.parse_function

atomica.function_parser.parse_function(fcn_str: str) tuple[source]

Parses a string into a positional-call optimized Python function

This function takes in the string representation of a function e.g. 'x+y' and returns a Python function object together with the deduplicated names of the quantities the function depends on. The returned function is intended to be called positionally in that order:

>>> fcn, deps = atomica.parse_function("x+y")
>>> deps
("x", "y")
>>> fcn(2, 3)
5

Note that for security, only a subset of Python functions are allowed to be called. These are mainly mathematical operations such as max or exp. A full listing can be found in function_parser.py.

A common usage pattern is to assemble a positional argument vector using the tuple of dependencies returned by parse_function. For example:

>>> args = [2, 2]
>>> fcn(*args)
4

The generated function uses ordinary Python positional-or-keyword parameters, so keyword calls still work, but note that positional argument are faster (and this is what model.py uses).

Parameters:

fcn_str (str) – A string containing a single Python expression

Return type:

tuple

Returns:

A tuple containing a function, and a deduplicated tuple of dependencies required by the function