Sponsored links: Algebra eBooks

The Maxima on-line user's manual

Algebra Calculator

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[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;

```          (%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-rank-solve

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

B:[1-c,-2*c,1-3*c,a,0...

rank(A);

Calculate

flatten-solve

eq1:m*(2-m/3)-2/3 = x;

flatten(solve(eq1, m));

Calculate

flatten-part-solve

e1:ib = s*Cpi*(vb-ve);

e2:s*Cpi*(vb-ve) + gm...

part(flatten(solve([e...

Calculate

flatten

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

Calculate

flatten

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

Calculate

a:2;

b:3;

n:200;

Calculate

flatten-matrix-solve

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

a:[1,0,1];

b:[-1,1,0];

Calculate

flatten-map

map(rhs, flatten(map(...

Calculate

a:2;

b:3;

n:200;

Calculate

flatten-matrix-solve

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

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

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

Calculate