UNPKG

1.03 kBJavaScriptView Raw
1'use strict'
2
3const clone = require('../../utils/object').clone
4const _flatten = require('../../utils/array').flatten
5
6function factory (type, config, load, typed) {
7 const matrix = load(require('../../type/matrix/function/matrix'))
8
9 /**
10 * Flatten a multi dimensional matrix into a single dimensional matrix.
11 *
12 * Syntax:
13 *
14 * math.flatten(x)
15 *
16 * Examples:
17 *
18 * math.flatten([[1,2], [3,4]]) // returns [1, 2, 3, 4]
19 *
20 * See also:
21 *
22 * concat, resize, size, squeeze
23 *
24 * @param {Matrix | Array} x Matrix to be flattened
25 * @return {Matrix | Array} Returns the flattened matrix
26 */
27 const flatten = typed('flatten', {
28 'Array': function (x) {
29 return _flatten(clone(x))
30 },
31
32 'Matrix': function (x) {
33 const flat = _flatten(clone(x.toArray()))
34 // TODO: return the same matrix type as x
35 return matrix(flat)
36 }
37 })
38
39 flatten.toTex = undefined // use default template
40
41 return flatten
42}
43
44exports.name = 'flatten'
45exports.factory = factory