# User-Define Functions and DAG Utilities

EAGO has included basic functionality to manipulate user-defined functions. These features are largely experimental and we're interested in providing additional for novel use cases.

## DAG Substitution and Flattening

`EAGO.Script.dag_flattening!`

— Function`dag_flattening!`

Flattens (usually) the dag by making all registered substitutions for every nonlinear term in the Optimizer.

`EAGO.Script.flatten_expression!`

— Function`flatten_expression!`

Flattens (usually) the dag by making all registered substitutions for the expression `expr::MOINL.Expression`

. Performs a depth-first search through the expression adding the terminal node to the stack, then checking to determine if it matches a registered substitution pattern. If it doesn't not then node is added to the new expression graph representation and it's children are added to the queue. If an expression (node) is identified as a pattern then it is substituted and any children expression nodes are then checked for patterns until the depth first search is exhausted.

`EAGO.Script.register_substitution!`

— Function`register_substitution!`

Specifies that the `src::Template_Graph`

should be subsituted out for the `trg::Template_Graph`

.

Conventions for substition, the expression to be checked always appears at key 1 in the Template_Graph and operations are ordered from low value to high value left to right so if 1 is a -, and 4 => 1, 3 => 1 then the expression is 4 - 3

`EAGO.Script.Template_Graph`

— Type`Template_Graph`

Holds a list of Template_Nodes, set of directed edges, lengths, an adjacency matrix and the number of children.

`EAGO.Script.Template_Node`

— Type`Template_Node`

A structure which holds a symbol indicating whether the node is an operator, a number, or an expression `type`

, a value which identifies the function or symbol `value`

, potentially a numeric value `num_value`

, and a check that can be run to verify the node is correct `check`

.

## User-Defined Function (UDF) Scrubber

Missing docstring for `scrub`

. Check Documenter's build log for details.

Missing docstring for `scrub!`

. Check Documenter's build log for details.