Maxima Function
flatten (expr)
Collects arguments of subexpressions which have the same operator as expr and constructs an expression from these collected arguments.
Subexpressions in which the operator is different from the main operator of expr
are copied without modification,
even if they, in turn, contain some subexpressions in which the operator is the same as for expr.
It may be possible for flatten to construct expressions in which the number
of arguments differs from the declared arguments for an operator;
this may provoke an error message from the simplifier or evaluator.
flatten does not try to detect such situations.
Expressions with special representations, for example, canonical rational expressions (CRE),
cannot be flattened; in such cases, flatten returns its argument unchanged.
Examples:
Applied to a list, flatten gathers all list elements that are lists.
(%i1) flatten ([a, b, [c, [d, e], f], [[g, h]], i, j]); (%o1) [a, b, c, d, e, f, g, h, i, j]
Applied to a set, flatten gathers all members of set elements that are sets.
(%i1) flatten ({a, {b}, {{c}}}); (%o1) {a, b, c} (%i2) flatten ({a, {[a], {a}}}); (%o2) {a, [a]}
flatten is similar to the effect of declaring the main operator n-ary.
However, flatten has no effect on subexpressions which have an operator
different from the main operator, while an n-ary declaration affects those.
(%i1) expr: flatten (f (g (f (f (x))))); (%o1) f(g(f(f(x)))) (%i2) declare (f, nary); (%o2) done (%i3) ev (expr); (%o3) f(g(f(x)))
flatten treats subscripted functions the same as any other operator.
(%i1) flatten (f[5] (f[5] (x, y), z)); (%o1) f (x, y, z) 5
It may be possible for flatten to construct expressions in which the number
of arguments differs from the declared arguments for an operator;