UNPKG

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