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