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