UNPKG

1.03 kBJavaScriptView Raw
1import { clone } from '../../utils/object';
2import { flatten as flattenArray } from '../../utils/array';
3import { factory } from '../../utils/factory';
4var name = 'flatten';
5var dependencies = ['typed', 'matrix'];
6export var createFlatten = /* #__PURE__ */factory(name, dependencies, function (_ref) {
7 var typed = _ref.typed,
8 matrix = _ref.matrix;
9
10 /**
11 * Flatten a multi dimensional matrix into a single dimensional matrix.
12 *
13 * Syntax:
14 *
15 * math.flatten(x)
16 *
17 * Examples:
18 *
19 * math.flatten([[1,2], [3,4]]) // returns [1, 2, 3, 4]
20 *
21 * See also:
22 *
23 * concat, resize, size, squeeze
24 *
25 * @param {Matrix | Array} x Matrix to be flattened
26 * @return {Matrix | Array} Returns the flattened matrix
27 */
28 return typed(name, {
29 Array: function Array(x) {
30 return flattenArray(clone(x));
31 },
32 Matrix: function Matrix(x) {
33 var flat = flattenArray(clone(x.toArray())); // TODO: return the same matrix type as x
34
35 return matrix(flat);
36 }
37 });
38});
\No newline at end of file