UNPKG

1.69 kBJavaScriptView Raw
1'use strict'
2
3const object = require('../../utils/object')
4const array = require('../../utils/array')
5
6function factory (type, config, load, typed) {
7 const matrix = load(require('../../type/matrix/function/matrix'))
8
9 /**
10 * Squeeze a matrix, remove inner and outer singleton dimensions from a matrix.
11 *
12 * Syntax:
13 *
14 * math.squeeze(x)
15 *
16 * Examples:
17 *
18 * math.squeeze([3]) // returns 3
19 * math.squeeze([[3]]) // returns 3
20 *
21 * const A = math.zeros(3, 1) // returns [[0], [0], [0]] (size 3x1)
22 * math.squeeze(A) // returns [0, 0, 0] (size 3)
23 *
24 * const B = math.zeros(1, 3) // returns [[0, 0, 0]] (size 1x3)
25 * math.squeeze(B) // returns [0, 0, 0] (size 3)
26 *
27 * // only inner and outer dimensions are removed
28 * const C = math.zeros(2, 1, 3) // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3)
29 * math.squeeze(C) // returns [[[0, 0, 0]], [[0, 0, 0]]] (size 2x1x3)
30 *
31 * See also:
32 *
33 * subset
34 *
35 * @param {Matrix | Array} x Matrix to be squeezed
36 * @return {Matrix | Array} Squeezed matrix
37 */
38 const squeeze = typed('squeeze', {
39 'Array': function (x) {
40 return array.squeeze(object.clone(x))
41 },
42
43 'Matrix': function (x) {
44 const res = array.squeeze(x.toArray())
45 // FIXME: return the same type of matrix as the input
46 return Array.isArray(res) ? matrix(res) : res
47 },
48
49 'any': function (x) {
50 // scalar
51 return object.clone(x)
52 }
53 })
54
55 squeeze.toTex = undefined // use default template
56
57 return squeeze
58}
59
60exports.name = 'squeeze'
61exports.factory = factory