# ufl_legacy.corealg package

## Submodules

## ufl_legacy.corealg.map_dag module

Basic algorithms for applying functions to subexpressions.

ufl_legacy.corealg.map_dag.map_expr_dag(function, expression, compress=True, vcache=None, rcache=None)

  Apply a function to each subexpression node in an expression DAG.

  If compress is `True` (default) the output object from the function is cached in a `dict` and reused such that the resulting expression DAG does not contain duplicate objects.

  If the same funtion is called multiple times in a transformation (as for example in apply_derivatives), then to reuse caches across the call, provide these two arguments:

  Parameters:

    - vcache – Optional dict for caching results of intermediate transformations

    - rcache – Optional dict for caching results for compression.

  Return the result of the final function call.

ufl_legacy.corealg.map_dag.map_expr_dags(function, expressions, compress=True, vcache=None, rcache=None)

  Apply a function to each subexpression node in an expression DAG.

  If compress is `True` (default) the output object from the function is cached in a `dict` and reused such that the resulting expression DAG does not contain duplicate objects.

  If the same funtion is called multiple times in a transformation (as for example in apply_derivatives), then to reuse caches across the call, provide these two arguments:

  Parameters:

    - vcache – Optional dict for caching results of intermediate transformations

    - rcache – Optional dict for caching results for compression.

  Return a list with the result of the final function call for each expression.

## ufl_legacy.corealg.multifunction module

Base class for multifunctions with UFL `Expr` type dispatch.

class ufl_legacy.corealg.multifunction.MultiFunction

  Bases: `object`

  Base class for collections of non-recursive expression node handlers.

  Subclass this (remember to call the `__init__` method of this class), and implement handler functions for each `Expr` type, using the lower case handler name of the type (`exprtype._ufl_handler_name_`).

  This class is optimized for efficient type based dispatch in the `__call__` operator via typecode based lookup of the handler function bound to the algorithm object. Of course Python’s function call overhead still applies.

  expr(o, *args)

    Trigger error for types with missing handlers.

  reuse_if_untouched(o, *ops)

    Reuse object if operands are the same objects.

    Use in your own subclass by setting e.g.

        expr = MultiFunction.reuse_if_untouched

    as a default rule.

  undefined(o, *args)

    Trigger error for types with missing handlers.

ufl_legacy.corealg.multifunction.get_num_args(function)

  Return the number of arguments accepted by function.

ufl_legacy.corealg.multifunction.memoized_handler(handler)

  Function decorator to memoize `MultiFunction` handlers.

## ufl_legacy.corealg.traversal module

Various expression traversal utilities.

The algorithms here are non-recursive, which is faster than recursion by a factor of 10 or so because of the function call overhead.

ufl_legacy.corealg.traversal.cutoff_post_traversal(expr, cutofftypes)

  Yield `o` for each node `o` in expr, child before parent, but skipping subtrees of the cutofftypes.

ufl_legacy.corealg.traversal.cutoff_unique_post_traversal(expr, cutofftypes, visited=None)

  Yield `o` for each node `o` in expr, child before parent.

  Never visit a node twice.

ufl_legacy.corealg.traversal.post_traversal(expr)

  Yield `o` for each node `o` in expr, child before parent.

ufl_legacy.corealg.traversal.pre_traversal(expr)

  Yield `o` for each tree node `o` in expr, parent before child.

ufl_legacy.corealg.traversal.traverse_terminals(expr)

ufl_legacy.corealg.traversal.traverse_unique_terminals(expr, visited=None)

ufl_legacy.corealg.traversal.unique_post_traversal(expr, visited=None)

  Yield `o` for each node `o` in expr, child before parent.

  Never visit a node twice.

ufl_legacy.corealg.traversal.unique_pre_traversal(expr, visited=None)

  Yield `o` for each tree node `o` in expr, parent before child.

  This version only visits each node once.

## Module contents