UNPKG

1.35 kBJavaScriptView Raw
1'use strict'
2
3const array = require('../../utils/array')
4
5function factory (type, config, load, typed) {
6 const matrix = load(require('../../type/matrix/function/matrix'))
7
8 /**
9 * Calculate the size of a matrix or scalar.
10 *
11 * Syntax:
12 *
13 * math.size(x)
14 *
15 * Examples:
16 *
17 * math.size(2.3) // returns []
18 * math.size('hello world') // returns [11]
19 *
20 * const A = [[1, 2, 3], [4, 5, 6]]
21 * math.size(A) // returns [2, 3]
22 * math.size(math.range(1,6)) // returns [5]
23 *
24 * See also:
25 *
26 * resize, squeeze, subset
27 *
28 * @param {boolean | number | Complex | Unit | string | Array | Matrix} x A matrix
29 * @return {Array | Matrix} A vector with size of `x`.
30 */
31 const size = typed('size', {
32 'Matrix': function (x) {
33 // TODO: return the same matrix type as the input
34 return matrix(x.size())
35 },
36
37 'Array': array.size,
38
39 'string': function (x) {
40 return (config.matrix === 'Array') ? [x.length] : matrix([x.length])
41 },
42
43 'number | Complex | BigNumber | Unit | boolean | null': function (x) {
44 // scalar
45 return (config.matrix === 'Array') ? [] : matrix([])
46 }
47 })
48
49 size.toTex = undefined // use default template
50
51 return size
52}
53
54exports.name = 'size'
55exports.factory = factory