UNPKG

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