UNPKG

3.54 kBJavaScriptView Raw
1'use strict'
2
3const isInteger = require('../../utils/number').isInteger
4const resize = require('../../utils/array').resize
5
6function factory (type, config, load, typed) {
7 const matrix = load(require('../../type/matrix/function/matrix'))
8
9 /**
10 * Create a matrix filled with zeros. The created matrix can have one or
11 * multiple dimensions.
12 *
13 * Syntax:
14 *
15 * math.zeros(m)
16 * math.zeros(m, format)
17 * math.zeros(m, n)
18 * math.zeros(m, n, format)
19 * math.zeros([m, n])
20 * math.zeros([m, n], format)
21 *
22 * Examples:
23 *
24 * math.zeros(3) // returns [0, 0, 0]
25 * math.zeros(3, 2) // returns [[0, 0], [0, 0], [0, 0]]
26 * math.zeros(3, 'dense') // returns [0, 0, 0]
27 *
28 * const A = [[1, 2, 3], [4, 5, 6]]
29 * math.zeros(math.size(A)) // returns [[0, 0, 0], [0, 0, 0]]
30 *
31 * See also:
32 *
33 * ones, identity, size, range
34 *
35 * @param {...number | Array} size The size of each dimension of the matrix
36 * @param {string} [format] The Matrix storage format
37 *
38 * @return {Array | Matrix} A matrix filled with zeros
39 */
40 const zeros = typed('zeros', {
41 '': function () {
42 return (config.matrix === 'Array')
43 ? _zeros([])
44 : _zeros([], 'default')
45 },
46
47 // math.zeros(m, n, p, ..., format)
48 // TODO: more accurate signature '...number | BigNumber, string' as soon as typed-function supports this
49 '...number | BigNumber | string': function (size) {
50 const last = size[size.length - 1]
51 if (typeof last === 'string') {
52 const format = size.pop()
53 return _zeros(size, format)
54 } else if (config.matrix === 'Array') {
55 return _zeros(size)
56 } else {
57 return _zeros(size, 'default')
58 }
59 },
60
61 'Array': _zeros,
62
63 'Matrix': function (size) {
64 const format = size.storage()
65 return _zeros(size.valueOf(), format)
66 },
67
68 'Array | Matrix, string': function (size, format) {
69 return _zeros(size.valueOf(), format)
70 }
71 })
72
73 zeros.toTex = undefined // use default template
74
75 return zeros
76
77 /**
78 * Create an Array or Matrix with zeros
79 * @param {Array} size
80 * @param {string} [format='default']
81 * @return {Array | Matrix}
82 * @private
83 */
84 function _zeros (size, format) {
85 const hasBigNumbers = _normalize(size)
86 const defaultValue = hasBigNumbers ? new type.BigNumber(0) : 0
87 _validate(size)
88
89 if (format) {
90 // return a matrix
91 const m = matrix(format)
92 if (size.length > 0) {
93 return m.resize(size, defaultValue)
94 }
95 return m
96 } else {
97 // return an Array
98 const arr = []
99 if (size.length > 0) {
100 return resize(arr, size, defaultValue)
101 }
102 return arr
103 }
104 }
105
106 // replace BigNumbers with numbers, returns true if size contained BigNumbers
107 function _normalize (size) {
108 let hasBigNumbers = false
109 size.forEach(function (value, index, arr) {
110 if (type.isBigNumber(value)) {
111 hasBigNumbers = true
112 arr[index] = value.toNumber()
113 }
114 })
115 return hasBigNumbers
116 }
117
118 // validate arguments
119 function _validate (size) {
120 size.forEach(function (value) {
121 if (typeof value !== 'number' || !isInteger(value) || value < 0) {
122 throw new Error('Parameters in function zeros must be positive integers')
123 }
124 })
125 }
126}
127
128// TODO: zeros contains almost the same code as ones. Reuse this?
129
130exports.name = 'zeros'
131exports.factory = factory