equations

knowledge.simulation.equations

Handling Equations and Expressions

Functions

Name Description
build_dependency_graph Builds a directed graph of dependencies from the eq_dict using SymPy.
convert_ifelse_to_np_where Convert a sympy.pycode output string from a simple if-else format to a numpy where format.
convert_numpy_where_to_sympy Converts a numpy.where expression to a sympy Piecewise expression.
extract_parts_from_numpy_where Extracts the condition, if_true, and if_false parts from a numpy.where or where expression string.
generate_global_coupling_function Generate the global coupling function based on given pre and post expressions.
get_symbolic_coupling Get the symbolic coupling expressions for the given coupling function.
piecewise2julia Convert a sympy Piecewise expression to a Julia ifelse expression string.
piecewise2numpy Convert a sympy Piecewise expression to an equivalent nested numpy.where expression.
topological_sort Performs topological sorting on the dependency graph.

build_dependency_graph

knowledge.simulation.equations.build_dependency_graph(eq_dict)

Builds a directed graph of dependencies from the eq_dict using SymPy.

convert_ifelse_to_np_where

knowledge.simulation.equations.convert_ifelse_to_np_where(code_str)

Convert a sympy.pycode output string from a simple if-else format to a numpy where format.

code_str (str): A string representing a sympy.pycode output, e.g., “((-0.1*z**7) if (z < 0) else (0))“.

Returns: str: Converted string using np.where, e.g., “np.where(z < 0, -0.1*z**7, 0)“.

convert_numpy_where_to_sympy

knowledge.simulation.equations.convert_numpy_where_to_sympy(python_string)

Converts a numpy.where expression to a sympy Piecewise expression. Args: - condition_str: The condition string. - if_true_str: The expression if the condition is True. - if_false_str: The expression if the condition is False. Returns: - A sympy Piecewise expression.

extract_parts_from_numpy_where

knowledge.simulation.equations.extract_parts_from_numpy_where(python_string)

Extracts the condition, if_true, and if_false parts from a numpy.where or where expression string. Args: - python_string: A string in the format “numpy.where(condition, if_true, if_false)” or “where(condition, if_true, if_false)” Returns: - A tuple containing the condition, if_true, and if_false parts as strings.

generate_global_coupling_function

knowledge.simulation.equations.generate_global_coupling_function(
    pre_expr,
    post_expr,
    j_index_start=0,
)

Generate the global coupling function based on given pre and post expressions.

:param pre_expr: The ‘pre’ sympy expression involving x_i and x_j. :param post_expr: The ‘post’ sympy expression involving gx. :return: The global coupling function as a sympy expression.

get_symbolic_coupling

knowledge.simulation.equations.get_symbolic_coupling(coupling_function)

Get the symbolic coupling expressions for the given coupling function.

Parameters

Name Type Description Default
coupling_function str or CouplingFunction The coupling function to retrieve symbolic expressions for. required

Returns

Name Type Description
dict dict A dictionary containing the symbolic expressions for the pre and post functions. The keys are ‘pre’ and ‘post’, and the values are SymPy expressions.

Raises

Name Type Description
SomeException Description of the exception raised, if any.

piecewise2julia

knowledge.simulation.equations.piecewise2julia(piecewise_expr)

Convert a sympy Piecewise expression to a Julia ifelse expression string.

Parameters: piecewise_expr (sympy.Piecewise): A sympy Piecewise object.

Returns: str: A Julia-compatible string representing the Piecewise expression using nested ifelse.

piecewise2numpy

knowledge.simulation.equations.piecewise2numpy(
    piecewise_expr,
    fully_qualified_modules=False,
)

Convert a sympy Piecewise expression to an equivalent nested numpy.where expression.

Parameters

Name Type Description Default
piecewise_expr Piecewise A sympy Piecewise expression. required

Returns

Name Type Description
str str A string representing the equivalent numpy.where statement.

topological_sort

knowledge.simulation.equations.topological_sort(graph)

Performs topological sorting on the dependency graph.