forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixIndex.js
More file actions
315 lines (281 loc) · 9.06 KB
/
MatrixIndex.js
File metadata and controls
315 lines (281 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { isArray, isMatrix, isRange } from '../../utils/is.js'
import { clone } from '../../utils/object.js'
import { isInteger } from '../../utils/number.js'
import { factory } from '../../utils/factory.js'
const name = 'Index'
const dependencies = ['ImmutableDenseMatrix', 'getMatrixDataType']
export const createIndexClass = /* #__PURE__ */ factory(name, dependencies, ({ ImmutableDenseMatrix, getMatrixDataType }) => {
/**
* Create an index. An Index can store ranges and sets for multiple dimensions.
* Matrix.get, Matrix.set, and math.subset accept an Index as input.
*
* Usage:
* const index = new Index(range1, range2, matrix1, array1, ...)
*
* Where each parameter can be any of:
* A number
* A string (containing a name of an object property)
* An instance of Range
* An Array with the Set values
* An Array with Booleans
* A Matrix with the Set values
* A Matrix with Booleans
*
* The parameters start, end, and step must be integer numbers.
*
* @class Index
* @Constructor Index
* @param {...*} ranges
*/
function Index (ranges) {
if (!(this instanceof Index)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
this._dimensions = []
this._sourceSize = []
this._isScalar = true
for (let i = 0, ii = arguments.length; i < ii; i++) {
const arg = arguments[i]
const argIsArray = isArray(arg)
const argIsMatrix = isMatrix(arg)
const argType = typeof arg
let sourceSize = null
if (isRange(arg)) {
this._dimensions.push(arg)
this._isScalar = false
} else if (argIsArray || argIsMatrix) {
// create matrix
let m
if (getMatrixDataType(arg) === 'boolean') {
if (argIsArray) m = _createImmutableMatrix(_booleansArrayToNumbersForIndex(arg).valueOf())
if (argIsMatrix) m = _createImmutableMatrix(_booleansArrayToNumbersForIndex(arg._data).valueOf())
sourceSize = arg.valueOf().length
} else {
m = _createImmutableMatrix(arg.valueOf())
}
this._dimensions.push(m)
// size
const size = m.size()
// scalar
if (size.length !== 1 || size[0] !== 1 || sourceSize !== null) {
this._isScalar = false
}
} else if (argType === 'number') {
this._dimensions.push(_createImmutableMatrix([arg]))
} else if (argType === 'bigint') {
this._dimensions.push(_createImmutableMatrix([Number(arg)]))
} else if (argType === 'string') {
// object property (arguments.count should be 1)
this._dimensions.push(arg)
} else {
throw new TypeError('Dimension must be an Array, Matrix, number, bigint, string, or Range')
}
this._sourceSize.push(sourceSize)
// TODO: implement support for wildcard '*'
}
}
/**
* Attach type information
*/
Index.prototype.type = 'Index'
Index.prototype.isIndex = true
function _createImmutableMatrix (arg) {
// loop array elements
for (let i = 0, l = arg.length; i < l; i++) {
if (typeof arg[i] !== 'number' || !isInteger(arg[i])) {
throw new TypeError('Index parameters must be positive integer numbers')
}
}
// create matrix
return new ImmutableDenseMatrix(arg)
}
/**
* Create a clone of the index
* @memberof Index
* @return {Index} clone
*/
Index.prototype.clone = function () {
const index = new Index()
index._dimensions = clone(this._dimensions)
index._isScalar = this._isScalar
index._sourceSize = this._sourceSize
return index
}
/**
* Create an index from an array with ranges/numbers
* @memberof Index
* @param {Array.<Array | number>} ranges
* @return {Index} index
* @private
*/
Index.create = function (ranges) {
const index = new Index()
Index.apply(index, ranges)
return index
}
/**
* Retrieve the size of the index, the number of elements for each dimension.
* @memberof Index
* @returns {number[]} size
*/
Index.prototype.size = function () {
const size = []
for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
const d = this._dimensions[i]
size[i] = (typeof d === 'string') ? 1 : d.size()[0]
}
return size
}
/**
* Get the maximum value for each of the indexes ranges.
* @memberof Index
* @returns {number[]} max
*/
Index.prototype.max = function () {
const values = []
for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
const range = this._dimensions[i]
values[i] = (typeof range === 'string') ? range : range.max()
}
return values
}
/**
* Get the minimum value for each of the indexes ranges.
* @memberof Index
* @returns {number[]} min
*/
Index.prototype.min = function () {
const values = []
for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
const range = this._dimensions[i]
values[i] = (typeof range === 'string') ? range : range.min()
}
return values
}
/**
* Loop over each of the ranges of the index
* @memberof Index
* @param {Function} callback Called for each range with a Range as first
* argument, the dimension as second, and the
* index object as third.
*/
Index.prototype.forEach = function (callback) {
for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
callback(this._dimensions[i], i, this)
}
}
/**
* Retrieve the dimension for the given index
* @memberof Index
* @param {Number} dim Number of the dimension
* @returns {Range | null} range
*/
Index.prototype.dimension = function (dim) {
if (typeof dim !== 'number') {
return null
}
return this._dimensions[dim] || null
}
/**
* Test whether this index contains an object property
* @returns {boolean} Returns true if the index is an object property
*/
Index.prototype.isObjectProperty = function () {
return this._dimensions.length === 1 && typeof this._dimensions[0] === 'string'
}
/**
* Returns the object property name when the Index holds a single object property,
* else returns null
* @returns {string | null}
*/
Index.prototype.getObjectProperty = function () {
return this.isObjectProperty() ? this._dimensions[0] : null
}
/**
* Test whether this index contains only a single value.
*
* This is the case when the index is created with only scalar values as ranges,
* not for ranges resolving into a single value.
* @memberof Index
* @return {boolean} isScalar
*/
Index.prototype.isScalar = function () {
return this._isScalar
}
/**
* Expand the Index into an array.
* For example new Index([0,3], [2,7]) returns [[0,1,2], [2,3,4,5,6]]
* @memberof Index
* @returns {Array} array
*/
Index.prototype.toArray = function () {
const array = []
for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
const dimension = this._dimensions[i]
array.push((typeof dimension === 'string') ? dimension : dimension.toArray())
}
return array
}
/**
* Get the primitive value of the Index, a two dimensional array.
* Equivalent to Index.toArray().
* @memberof Index
* @returns {Array} array
*/
Index.prototype.valueOf = Index.prototype.toArray
/**
* Get the string representation of the index, for example '[2:6]' or '[0:2:10, 4:7, [1,2,3]]'
* @memberof Index
* @returns {String} str
*/
Index.prototype.toString = function () {
const strings = []
for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
const dimension = this._dimensions[i]
if (typeof dimension === 'string') {
strings.push(JSON.stringify(dimension))
} else {
strings.push(dimension.toString())
}
}
return '[' + strings.join(', ') + ']'
}
/**
* Get a JSON representation of the Index
* @memberof Index
* @returns {Object} Returns a JSON object structured as:
* `{"mathjs": "Index", "ranges": [{"mathjs": "Range", start: 0, end: 10, step:1}, ...]}`
*/
Index.prototype.toJSON = function () {
return {
mathjs: 'Index',
dimensions: this._dimensions
}
}
/**
* Instantiate an Index from a JSON object
* @memberof Index
* @param {Object} json A JSON object structured as:
* `{"mathjs": "Index", "dimensions": [{"mathjs": "Range", start: 0, end: 10, step:1}, ...]}`
* @return {Index}
*/
Index.fromJSON = function (json) {
return Index.create(json.dimensions)
}
return Index
}, { isClass: true })
/**
* Receives an array of booleans and returns an array of Numbers for Index
* @param {Array} booleanArrayIndex An array of booleans
* @return {Array} A set of numbers ready for index
*/
function _booleansArrayToNumbersForIndex (booleanArrayIndex) {
// gets an array of booleans and returns an array of numbers
const indexOfNumbers = []
booleanArrayIndex.forEach((bool, idx) => {
if (bool) {
indexOfNumbers.push(idx)
}
})
return indexOfNumbers
}