UNPKG

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