UNPKG

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