Sponsored links: Algebra eBooks ### The Maxima on-line user's manual

Algebra Calculator

#### Search: #### Flatten

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 (f (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;

```          (%i1) mod (5, mod (7, 4));
(%o1)                   mod(5, mod(7, 4))
(%i2) flatten (%);
(%o2)                     mod(5, 7, 4)
(%i3) %, nouns;
Wrong number of arguments to mod
-- an error.  Quitting.  To debug this try debugmode(true);```

```(%o1)                                true
(%i2) ```

### Related Examples

##### flatten-matrix-solve

A:matrix([a11,a12,a13...

a:[1,0,1];

b:[-1,1,0];

Calculate

##### flatten-matrix-solve

A:matrix([1,1,-1,1,2]...

B:[1,0,1,1,0];

L:flatten(A.[x1,x2,x3...

Calculate

##### flatten

flatten((z^2*E1*E2*E3...

Calculate

mean(map(rhs, flatten...

Calculate

##### flatten-map-solve

Polynom:a*x^3+b*x^2+c...

f(x):=''Polynom;

g1:f(-5)=0;

Calculate

##### flatten-makelist-plot2d a:2;

b:3;

n:200;

Calculate

##### flatten

flatten([2,2,3]);

Calculate

##### flatten-map-solve

Polynom:a*x^3+b*x^2+c...

f(x):=''Polynom;

g1:f(-5)=0;

Calculate

##### flatten

flatten([,2,3]);

Calculate

##### flatten

flatten([[2,3],2,3]);

Calculate 