@danielsimonjr/mathts-core 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-2WW76EEF.js → chunk-7BUNGMQB.js} +144 -54
- package/dist/{index-zKEDY9aj.d.ts → index-B4LIOfHN.d.ts} +85 -1
- package/dist/index.d.ts +10 -3
- package/dist/index.js +1 -1
- package/dist/internal.d.ts +767 -9
- package/dist/internal.js +1346 -1
- package/package.json +1 -1
package/dist/internal.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
|
+
DEFAULT_CONFIG,
|
|
3
|
+
MathjsError,
|
|
2
4
|
Unit,
|
|
3
5
|
acosh,
|
|
4
6
|
asinh,
|
|
7
|
+
assertDependencies,
|
|
5
8
|
atanh,
|
|
6
9
|
canDefineProperty,
|
|
7
10
|
cbrt,
|
|
8
11
|
clone,
|
|
9
12
|
copysign,
|
|
10
13
|
cosh,
|
|
14
|
+
create,
|
|
11
15
|
createUnitClass,
|
|
12
16
|
deepExtend,
|
|
13
17
|
deepFlatten,
|
|
@@ -16,6 +20,7 @@ import {
|
|
|
16
20
|
endsWith,
|
|
17
21
|
expm1,
|
|
18
22
|
extend,
|
|
23
|
+
factory,
|
|
19
24
|
format,
|
|
20
25
|
get,
|
|
21
26
|
hasOwnProperty,
|
|
@@ -27,6 +32,7 @@ import {
|
|
|
27
32
|
isComplex2 as isComplex,
|
|
28
33
|
isDate,
|
|
29
34
|
isDenseMatrix,
|
|
35
|
+
isFactory,
|
|
30
36
|
isFraction,
|
|
31
37
|
isFunction,
|
|
32
38
|
isIndex,
|
|
@@ -37,6 +43,7 @@ import {
|
|
|
37
43
|
isNull,
|
|
38
44
|
isNumber,
|
|
39
45
|
isObject,
|
|
46
|
+
isOptionalDependency,
|
|
40
47
|
isPowZeroAtInfinity,
|
|
41
48
|
isRange,
|
|
42
49
|
isRegExp,
|
|
@@ -59,7 +66,9 @@ import {
|
|
|
59
66
|
set,
|
|
60
67
|
sign,
|
|
61
68
|
sinh,
|
|
69
|
+
sortFactories,
|
|
62
70
|
splitNumber,
|
|
71
|
+
stripOptionalNotation,
|
|
63
72
|
tanh,
|
|
64
73
|
toEngineering,
|
|
65
74
|
toExponential,
|
|
@@ -69,28 +78,1339 @@ import {
|
|
|
69
78
|
typeOf,
|
|
70
79
|
unitDependencies,
|
|
71
80
|
warnOnce
|
|
72
|
-
} from "./chunk-
|
|
81
|
+
} from "./chunk-7BUNGMQB.js";
|
|
82
|
+
|
|
83
|
+
// src/error/DimensionError.ts
|
|
84
|
+
var DimensionError = class _DimensionError extends RangeError {
|
|
85
|
+
actual;
|
|
86
|
+
expected;
|
|
87
|
+
relation;
|
|
88
|
+
isDimensionError = true;
|
|
89
|
+
/**
|
|
90
|
+
* @param actual - The actual size or custom error message
|
|
91
|
+
* @param expected - The expected size (optional if actual is a custom message)
|
|
92
|
+
* @param relation - Optional relation between actual and expected size: '!=', '<', etc.
|
|
93
|
+
*/
|
|
94
|
+
constructor(actual, expected, relation) {
|
|
95
|
+
let message;
|
|
96
|
+
if (typeof actual === "string" && expected === void 0) {
|
|
97
|
+
message = actual;
|
|
98
|
+
} else {
|
|
99
|
+
message = "Dimension mismatch (" + (Array.isArray(actual) ? "[" + actual.join(", ") + "]" : actual) + " " + (relation || "!=") + " " + (Array.isArray(expected) ? "[" + expected.join(", ") + "]" : expected) + ")";
|
|
100
|
+
}
|
|
101
|
+
super(message);
|
|
102
|
+
this.name = "DimensionError";
|
|
103
|
+
if (typeof actual === "string" && expected === void 0) {
|
|
104
|
+
this.actual = void 0;
|
|
105
|
+
this.expected = void 0;
|
|
106
|
+
this.relation = void 0;
|
|
107
|
+
} else {
|
|
108
|
+
this.actual = actual;
|
|
109
|
+
this.expected = expected;
|
|
110
|
+
this.relation = relation;
|
|
111
|
+
}
|
|
112
|
+
const ErrorWithCapture = Error;
|
|
113
|
+
if (ErrorWithCapture.captureStackTrace) {
|
|
114
|
+
ErrorWithCapture.captureStackTrace(this, _DimensionError);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// src/error/IndexError.ts
|
|
120
|
+
var IndexError = class _IndexError extends RangeError {
|
|
121
|
+
index;
|
|
122
|
+
min;
|
|
123
|
+
max;
|
|
124
|
+
isIndexError = true;
|
|
125
|
+
/**
|
|
126
|
+
* Create an IndexError
|
|
127
|
+
*
|
|
128
|
+
* Can be called in two ways:
|
|
129
|
+
* - IndexError(index, max) - assumes min=0
|
|
130
|
+
* - IndexError(index, min, max)
|
|
131
|
+
*
|
|
132
|
+
* @param index The actual index
|
|
133
|
+
* @param min Minimum index (included), or max if only 2 args provided
|
|
134
|
+
* @param max Maximum index (excluded)
|
|
135
|
+
*/
|
|
136
|
+
constructor(index, min, max) {
|
|
137
|
+
let actualMin;
|
|
138
|
+
let actualMax;
|
|
139
|
+
if (max === void 0) {
|
|
140
|
+
actualMin = 0;
|
|
141
|
+
actualMax = min;
|
|
142
|
+
} else {
|
|
143
|
+
actualMin = min;
|
|
144
|
+
actualMax = max;
|
|
145
|
+
}
|
|
146
|
+
let message;
|
|
147
|
+
if (actualMin !== void 0 && index < actualMin) {
|
|
148
|
+
message = "Index out of range (" + index + " < " + actualMin + ")";
|
|
149
|
+
} else if (actualMax !== void 0 && index >= actualMax) {
|
|
150
|
+
message = "Index out of range (" + index + " > " + (actualMax - 1) + ")";
|
|
151
|
+
} else {
|
|
152
|
+
message = "Index out of range (" + index + ")";
|
|
153
|
+
}
|
|
154
|
+
super(message);
|
|
155
|
+
this.index = index;
|
|
156
|
+
this.min = actualMin;
|
|
157
|
+
this.max = actualMax;
|
|
158
|
+
this.name = "IndexError";
|
|
159
|
+
const ErrorWithCapture = Error;
|
|
160
|
+
if (ErrorWithCapture.captureStackTrace) {
|
|
161
|
+
ErrorWithCapture.captureStackTrace(this, _IndexError);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
function createIndexError(index, min, max) {
|
|
166
|
+
return new IndexError(index, min, max);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/bignumber-formatter.ts
|
|
170
|
+
function formatBigNumberToBase(n, base, size) {
|
|
171
|
+
const BigNumberCtor = n.constructor;
|
|
172
|
+
const big2 = new BigNumberCtor(2);
|
|
173
|
+
let suffix = "";
|
|
174
|
+
if (size) {
|
|
175
|
+
if (size < 1) {
|
|
176
|
+
throw new Error("size must be in greater than 0");
|
|
177
|
+
}
|
|
178
|
+
if (!isInteger(size)) {
|
|
179
|
+
throw new Error("size must be an integer");
|
|
180
|
+
}
|
|
181
|
+
if (n.greaterThan(big2.pow(size - 1).sub(1)) || n.lessThan(big2.pow(size - 1).mul(-1))) {
|
|
182
|
+
throw new Error(`Value must be in range [-2^${size - 1}, 2^${size - 1}-1]`);
|
|
183
|
+
}
|
|
184
|
+
if (!n.isInteger()) {
|
|
185
|
+
throw new Error("Value must be an integer");
|
|
186
|
+
}
|
|
187
|
+
if (n.lessThan(0)) {
|
|
188
|
+
n = n.add(big2.pow(size));
|
|
189
|
+
}
|
|
190
|
+
suffix = `i${size}`;
|
|
191
|
+
}
|
|
192
|
+
switch (base) {
|
|
193
|
+
case 2:
|
|
194
|
+
return `${n.toBinary()}${suffix}`;
|
|
195
|
+
case 8:
|
|
196
|
+
return `${n.toOctal()}${suffix}`;
|
|
197
|
+
case 16:
|
|
198
|
+
return `${n.toHexadecimal()}${suffix}`;
|
|
199
|
+
default:
|
|
200
|
+
throw new Error(`Base ${base} not supported `);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function format2(value, options) {
|
|
204
|
+
const v = value;
|
|
205
|
+
if (typeof options === "function") {
|
|
206
|
+
return options(value);
|
|
207
|
+
}
|
|
208
|
+
if (!v.isFinite()) {
|
|
209
|
+
return v.isNaN() ? "NaN" : v.gt(0) ? "Infinity" : "-Infinity";
|
|
210
|
+
}
|
|
211
|
+
const { notation, precision, wordSize } = normalizeFormatOptions(
|
|
212
|
+
options
|
|
213
|
+
);
|
|
214
|
+
switch (notation) {
|
|
215
|
+
case "fixed":
|
|
216
|
+
return toFixed2(v, precision);
|
|
217
|
+
case "exponential":
|
|
218
|
+
return toExponential2(v, precision);
|
|
219
|
+
case "engineering":
|
|
220
|
+
return toEngineering2(v, precision);
|
|
221
|
+
case "bin":
|
|
222
|
+
return formatBigNumberToBase(v, 2, wordSize);
|
|
223
|
+
case "oct":
|
|
224
|
+
return formatBigNumberToBase(v, 8, wordSize);
|
|
225
|
+
case "hex":
|
|
226
|
+
return formatBigNumberToBase(v, 16, wordSize);
|
|
227
|
+
case "auto": {
|
|
228
|
+
const optionsObj = options;
|
|
229
|
+
const lowerExp = _toNumberOrDefault(optionsObj?.lowerExp, -3);
|
|
230
|
+
const upperExp = _toNumberOrDefault(optionsObj?.upperExp, 5);
|
|
231
|
+
if (v.isZero()) return "0";
|
|
232
|
+
let str;
|
|
233
|
+
const rounded = v.toSignificantDigits(precision);
|
|
234
|
+
const exp = rounded.e;
|
|
235
|
+
if (exp >= lowerExp && exp < upperExp) {
|
|
236
|
+
str = rounded.toFixed();
|
|
237
|
+
} else {
|
|
238
|
+
str = toExponential2(v, precision);
|
|
239
|
+
}
|
|
240
|
+
return str.replace(
|
|
241
|
+
/((\.\d*?)(0+))($|e)/,
|
|
242
|
+
function(_match, _g1, digits2, _g3, e) {
|
|
243
|
+
return digits2 !== "." ? digits2 + e : e;
|
|
244
|
+
}
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
default:
|
|
248
|
+
throw new Error(
|
|
249
|
+
'Unknown notation "' + notation + '". Choose "auto", "exponential", "fixed", "bin", "oct", or "hex.'
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function toEngineering2(value, precision) {
|
|
254
|
+
const e = value.e;
|
|
255
|
+
const newExp = e % 3 === 0 ? e : e < 0 ? e - 3 - e % 3 : e - e % 3;
|
|
256
|
+
const valueWithoutExp = value.mul(Math.pow(10, -newExp));
|
|
257
|
+
let valueStr = valueWithoutExp.toPrecision(precision);
|
|
258
|
+
if (valueStr.includes("e")) {
|
|
259
|
+
const BigNumberCtor = value.constructor;
|
|
260
|
+
valueStr = new BigNumberCtor(valueStr).toFixed();
|
|
261
|
+
}
|
|
262
|
+
return valueStr + "e" + (e >= 0 ? "+" : "") + newExp.toString();
|
|
263
|
+
}
|
|
264
|
+
function toExponential2(value, precision) {
|
|
265
|
+
if (precision !== void 0) {
|
|
266
|
+
return value.toExponential(precision - 1);
|
|
267
|
+
} else {
|
|
268
|
+
return value.toExponential();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function toFixed2(value, precision) {
|
|
272
|
+
return value.toFixed(precision);
|
|
273
|
+
}
|
|
274
|
+
function _toNumberOrDefault(value, defaultValue) {
|
|
275
|
+
if (isNumber(value)) {
|
|
276
|
+
return value;
|
|
277
|
+
} else if (isBigNumber(value)) {
|
|
278
|
+
return value.toNumber();
|
|
279
|
+
} else {
|
|
280
|
+
return defaultValue;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// src/string.ts
|
|
285
|
+
function format3(value, options) {
|
|
286
|
+
const result = _format(value, options);
|
|
287
|
+
if (options && typeof options === "object" && "truncate" in options) {
|
|
288
|
+
const truncate = options.truncate;
|
|
289
|
+
if (typeof truncate === "number" && result.length > truncate) {
|
|
290
|
+
return result.substring(0, truncate - 3) + "...";
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
function _format(value, options) {
|
|
296
|
+
if (typeof value === "number") {
|
|
297
|
+
return format(value, options);
|
|
298
|
+
}
|
|
299
|
+
if (isBigNumber(value)) {
|
|
300
|
+
return format2(value, options);
|
|
301
|
+
}
|
|
302
|
+
if (looksLikeFraction(value)) {
|
|
303
|
+
if (!options || options.fraction !== "decimal") {
|
|
304
|
+
const signedNumerator = typeof value.n === "bigint" ? BigInt(value.s) * value.n : value.s * value.n;
|
|
305
|
+
return `${signedNumerator}/${value.d}`;
|
|
306
|
+
} else {
|
|
307
|
+
return value.toString();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (Array.isArray(value)) {
|
|
311
|
+
return formatArray(value, options);
|
|
312
|
+
}
|
|
313
|
+
if (isString(value)) {
|
|
314
|
+
return stringify(value);
|
|
315
|
+
}
|
|
316
|
+
if (typeof value === "function") {
|
|
317
|
+
const syntax = value.syntax;
|
|
318
|
+
return syntax ? String(syntax) : "function";
|
|
319
|
+
}
|
|
320
|
+
if (value && typeof value === "object") {
|
|
321
|
+
const obj = value;
|
|
322
|
+
if (typeof obj.format === "function") {
|
|
323
|
+
return obj.format(options);
|
|
324
|
+
} else if (obj.toString(options) !== {}.toString()) {
|
|
325
|
+
return obj.toString(options);
|
|
326
|
+
} else {
|
|
327
|
+
const entries = Object.keys(obj).map((key) => {
|
|
328
|
+
return stringify(key) + ": " + format3(obj[key], options);
|
|
329
|
+
});
|
|
330
|
+
return "{" + entries.join(", ") + "}";
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return String(value);
|
|
334
|
+
}
|
|
335
|
+
function stringify(value) {
|
|
336
|
+
const text = String(value);
|
|
337
|
+
let escaped = "";
|
|
338
|
+
let i = 0;
|
|
339
|
+
while (i < text.length) {
|
|
340
|
+
const c = text.charAt(i);
|
|
341
|
+
escaped += c in controlCharacters ? controlCharacters[c] : c;
|
|
342
|
+
i++;
|
|
343
|
+
}
|
|
344
|
+
return '"' + escaped + '"';
|
|
345
|
+
}
|
|
346
|
+
var controlCharacters = {
|
|
347
|
+
'"': '\\"',
|
|
348
|
+
"\\": "\\\\",
|
|
349
|
+
"\b": "\\b",
|
|
350
|
+
"\f": "\\f",
|
|
351
|
+
"\n": "\\n",
|
|
352
|
+
"\r": "\\r",
|
|
353
|
+
" ": "\\t"
|
|
354
|
+
};
|
|
355
|
+
function escape(value) {
|
|
356
|
+
let text = String(value);
|
|
357
|
+
text = text.replace(/&/g, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
|
|
358
|
+
return text;
|
|
359
|
+
}
|
|
360
|
+
function formatArray(array, options) {
|
|
361
|
+
if (Array.isArray(array)) {
|
|
362
|
+
let str = "[";
|
|
363
|
+
const len = array.length;
|
|
364
|
+
for (let i = 0; i < len; i++) {
|
|
365
|
+
if (i !== 0) {
|
|
366
|
+
str += ", ";
|
|
367
|
+
}
|
|
368
|
+
str += formatArray(array[i], options);
|
|
369
|
+
}
|
|
370
|
+
str += "]";
|
|
371
|
+
return str;
|
|
372
|
+
} else {
|
|
373
|
+
return format3(array, options);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
function looksLikeFraction(value) {
|
|
377
|
+
const frac = value;
|
|
378
|
+
return !!frac && typeof frac === "object" && typeof frac.s === "bigint" && typeof frac.n === "bigint" && typeof frac.d === "bigint" || false;
|
|
379
|
+
}
|
|
380
|
+
function compareText(x, y) {
|
|
381
|
+
if (!isString(x)) {
|
|
382
|
+
throw new TypeError(
|
|
383
|
+
"Unexpected type of argument in function compareText (expected: string or Array or Matrix, actual: " + typeOf(x) + ", index: 0)"
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
if (!isString(y)) {
|
|
387
|
+
throw new TypeError(
|
|
388
|
+
"Unexpected type of argument in function compareText (expected: string or Array or Matrix, actual: " + typeOf(y) + ", index: 1)"
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
return x === y ? 0 : x > y ? 1 : -1;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// src/array.ts
|
|
395
|
+
function arraySize(x) {
|
|
396
|
+
const s = [];
|
|
397
|
+
let current = x;
|
|
398
|
+
while (Array.isArray(current)) {
|
|
399
|
+
s.push(current.length);
|
|
400
|
+
current = current[0];
|
|
401
|
+
}
|
|
402
|
+
return s;
|
|
403
|
+
}
|
|
404
|
+
function _validate(array, size, dim) {
|
|
405
|
+
let i;
|
|
406
|
+
const len = array.length;
|
|
407
|
+
if (len !== size[dim]) {
|
|
408
|
+
throw new DimensionError(len, size[dim]);
|
|
409
|
+
}
|
|
410
|
+
if (dim < size.length - 1) {
|
|
411
|
+
const dimNext = dim + 1;
|
|
412
|
+
for (i = 0; i < len; i++) {
|
|
413
|
+
const child = array[i];
|
|
414
|
+
if (!Array.isArray(child)) {
|
|
415
|
+
throw new DimensionError(size.length - 1, size.length, "<");
|
|
416
|
+
}
|
|
417
|
+
_validate(array[i], size, dimNext);
|
|
418
|
+
}
|
|
419
|
+
} else {
|
|
420
|
+
for (i = 0; i < len; i++) {
|
|
421
|
+
if (Array.isArray(array[i])) {
|
|
422
|
+
throw new DimensionError(size.length + 1, size.length, ">");
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function validate(array, size) {
|
|
428
|
+
const isScalar = size.length === 0;
|
|
429
|
+
if (isScalar) {
|
|
430
|
+
if (Array.isArray(array)) {
|
|
431
|
+
throw new DimensionError(array.length, 0);
|
|
432
|
+
}
|
|
433
|
+
} else {
|
|
434
|
+
_validate(array, size, 0);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function validateIndexSourceSize(value, index) {
|
|
438
|
+
const valueSize = value.isMatrix ? value._size : arraySize(value);
|
|
439
|
+
const sourceSize = index._sourceSize;
|
|
440
|
+
sourceSize.forEach((sourceDim, i) => {
|
|
441
|
+
if (sourceDim !== null && sourceDim !== valueSize[i]) {
|
|
442
|
+
throw new DimensionError(sourceDim, valueSize[i]);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
function validateIndex(index, length) {
|
|
447
|
+
if (index !== void 0) {
|
|
448
|
+
if (!isNumber(index) || !isInteger(index)) {
|
|
449
|
+
throw new TypeError("Index must be an integer (value: " + index + ")");
|
|
450
|
+
}
|
|
451
|
+
if (index < 0 || typeof length === "number" && index >= length) {
|
|
452
|
+
throw new IndexError(index, length);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function isEmptyIndex(index) {
|
|
457
|
+
for (let i = 0; i < index._dimensions.length; ++i) {
|
|
458
|
+
const dimension = index._dimensions[i];
|
|
459
|
+
if (isString(dimension)) {
|
|
460
|
+
if (dimension.length === 0) {
|
|
461
|
+
return true;
|
|
462
|
+
}
|
|
463
|
+
} else {
|
|
464
|
+
const dim = dimension;
|
|
465
|
+
if (dim._data && isArray(dim._data)) {
|
|
466
|
+
if (dim._size[0] === 0) {
|
|
467
|
+
return true;
|
|
468
|
+
}
|
|
469
|
+
} else if (dim.isRange) {
|
|
470
|
+
if (dim.start === dim.end) {
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
function resize(array, size, defaultValue) {
|
|
479
|
+
if (!Array.isArray(size)) {
|
|
480
|
+
throw new TypeError("Array expected");
|
|
481
|
+
}
|
|
482
|
+
if (size.length === 0) {
|
|
483
|
+
throw new Error("Resizing to scalar is not supported");
|
|
484
|
+
}
|
|
485
|
+
size.forEach(function(value) {
|
|
486
|
+
if (!isNumber(value) || !isInteger(value) || value < 0) {
|
|
487
|
+
throw new TypeError(
|
|
488
|
+
"Invalid size, must contain positive integers (size: " + format3(size, {}) + ")"
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
let arr = array;
|
|
493
|
+
if (isNumber(array) || isBigNumber(array)) {
|
|
494
|
+
arr = [array];
|
|
495
|
+
}
|
|
496
|
+
const _defaultValue = defaultValue !== void 0 ? defaultValue : 0;
|
|
497
|
+
_resize(arr, size, 0, _defaultValue);
|
|
498
|
+
return arr;
|
|
499
|
+
}
|
|
500
|
+
function _resize(array, size, dim, defaultValue) {
|
|
501
|
+
let i;
|
|
502
|
+
let elem;
|
|
503
|
+
const oldLen = array.length;
|
|
504
|
+
const newLen = size[dim];
|
|
505
|
+
const minLen = Math.min(oldLen, newLen);
|
|
506
|
+
array.length = newLen;
|
|
507
|
+
if (dim < size.length - 1) {
|
|
508
|
+
const dimNext = dim + 1;
|
|
509
|
+
for (i = 0; i < minLen; i++) {
|
|
510
|
+
let current = array[i];
|
|
511
|
+
if (!Array.isArray(current)) {
|
|
512
|
+
current = [current];
|
|
513
|
+
array[i] = current;
|
|
514
|
+
}
|
|
515
|
+
_resize(current, size, dimNext, defaultValue);
|
|
516
|
+
}
|
|
517
|
+
for (i = minLen; i < newLen; i++) {
|
|
518
|
+
elem = [];
|
|
519
|
+
array[i] = elem;
|
|
520
|
+
_resize(elem, size, dimNext, defaultValue);
|
|
521
|
+
}
|
|
522
|
+
} else {
|
|
523
|
+
for (i = 0; i < minLen; i++) {
|
|
524
|
+
let value = array[i];
|
|
525
|
+
while (Array.isArray(value)) {
|
|
526
|
+
value = value[0];
|
|
527
|
+
}
|
|
528
|
+
array[i] = value;
|
|
529
|
+
}
|
|
530
|
+
for (i = minLen; i < newLen; i++) {
|
|
531
|
+
array[i] = defaultValue;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
function reshape(array, sizes) {
|
|
536
|
+
const flatArray = flatten(array, true);
|
|
537
|
+
const currentLength = flatArray.length;
|
|
538
|
+
if (!Array.isArray(array) || !Array.isArray(sizes)) {
|
|
539
|
+
throw new TypeError("Array expected");
|
|
540
|
+
}
|
|
541
|
+
if (sizes.length === 0) {
|
|
542
|
+
throw new DimensionError(0, currentLength, "!=");
|
|
543
|
+
}
|
|
544
|
+
const processedSizes = processSizesWildcard(sizes, currentLength);
|
|
545
|
+
const newLength = product(processedSizes);
|
|
546
|
+
if (currentLength !== newLength) {
|
|
547
|
+
throw new DimensionError(newLength, currentLength, "!=");
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
return _reshape(flatArray, processedSizes);
|
|
551
|
+
} catch (e) {
|
|
552
|
+
if (e instanceof DimensionError) {
|
|
553
|
+
throw new DimensionError(newLength, currentLength, "!=");
|
|
554
|
+
}
|
|
555
|
+
throw e;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function processSizesWildcard(sizes, currentLength) {
|
|
559
|
+
const newLength = product(sizes);
|
|
560
|
+
const processedSizes = sizes.slice();
|
|
561
|
+
const WILDCARD = -1;
|
|
562
|
+
const wildCardIndex = sizes.indexOf(WILDCARD);
|
|
563
|
+
const isMoreThanOneWildcard = sizes.indexOf(WILDCARD, wildCardIndex + 1) >= 0;
|
|
564
|
+
if (isMoreThanOneWildcard) {
|
|
565
|
+
throw new Error("More than one wildcard in sizes");
|
|
566
|
+
}
|
|
567
|
+
const hasWildcard = wildCardIndex >= 0;
|
|
568
|
+
const canReplaceWildcard = currentLength % newLength === 0;
|
|
569
|
+
if (hasWildcard) {
|
|
570
|
+
if (canReplaceWildcard) {
|
|
571
|
+
processedSizes[wildCardIndex] = -currentLength / newLength;
|
|
572
|
+
} else {
|
|
573
|
+
throw new Error(
|
|
574
|
+
"Could not replace wildcard, since " + currentLength + " is no multiple of " + -newLength
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return processedSizes;
|
|
579
|
+
}
|
|
580
|
+
function product(array) {
|
|
581
|
+
return array.reduce((prev, curr) => prev * curr, 1);
|
|
582
|
+
}
|
|
583
|
+
function _reshape(array, sizes) {
|
|
584
|
+
let tmpArray = array;
|
|
585
|
+
let tmpArray2;
|
|
586
|
+
for (let sizeIndex = sizes.length - 1; sizeIndex > 0; sizeIndex--) {
|
|
587
|
+
const size = sizes[sizeIndex];
|
|
588
|
+
tmpArray2 = [];
|
|
589
|
+
const length = tmpArray.length / size;
|
|
590
|
+
for (let i = 0; i < length; i++) {
|
|
591
|
+
tmpArray2.push(tmpArray.slice(i * size, (i + 1) * size));
|
|
592
|
+
}
|
|
593
|
+
tmpArray = tmpArray2;
|
|
594
|
+
}
|
|
595
|
+
return tmpArray;
|
|
596
|
+
}
|
|
597
|
+
function squeeze(array, size) {
|
|
598
|
+
const s = size || arraySize(array);
|
|
599
|
+
let arr = array;
|
|
600
|
+
while (Array.isArray(arr) && arr.length === 1) {
|
|
601
|
+
arr = arr[0];
|
|
602
|
+
s.shift();
|
|
603
|
+
}
|
|
604
|
+
let dims = s.length;
|
|
605
|
+
while (s[dims - 1] === 1) {
|
|
606
|
+
dims--;
|
|
607
|
+
}
|
|
608
|
+
if (dims < s.length) {
|
|
609
|
+
arr = _squeeze(arr, dims, 0);
|
|
610
|
+
s.length = dims;
|
|
611
|
+
}
|
|
612
|
+
return arr;
|
|
613
|
+
}
|
|
614
|
+
function _squeeze(array, dims, dim) {
|
|
615
|
+
if (dim < dims) {
|
|
616
|
+
const next = dim + 1;
|
|
617
|
+
const arr = array;
|
|
618
|
+
for (let i = 0, ii = arr.length; i < ii; i++) {
|
|
619
|
+
arr[i] = _squeeze(arr[i], dims, next);
|
|
620
|
+
}
|
|
621
|
+
return arr;
|
|
622
|
+
} else {
|
|
623
|
+
let result = array;
|
|
624
|
+
while (Array.isArray(result)) {
|
|
625
|
+
result = result[0];
|
|
626
|
+
}
|
|
627
|
+
return result;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
function unsqueeze(array, dims, outer, size) {
|
|
631
|
+
const s = size || arraySize(array);
|
|
632
|
+
let arr = array;
|
|
633
|
+
if (outer) {
|
|
634
|
+
for (let i = 0; i < outer; i++) {
|
|
635
|
+
arr = [arr];
|
|
636
|
+
s.unshift(1);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
arr = _unsqueeze(arr, dims, 0);
|
|
640
|
+
while (s.length < dims) {
|
|
641
|
+
s.push(1);
|
|
642
|
+
}
|
|
643
|
+
return arr;
|
|
644
|
+
}
|
|
645
|
+
function _unsqueeze(array, dims, dim) {
|
|
646
|
+
if (Array.isArray(array)) {
|
|
647
|
+
const next = dim + 1;
|
|
648
|
+
const arr = array;
|
|
649
|
+
for (let i = 0, ii = arr.length; i < ii; i++) {
|
|
650
|
+
arr[i] = _unsqueeze(arr[i], dims, next);
|
|
651
|
+
}
|
|
652
|
+
return arr;
|
|
653
|
+
} else {
|
|
654
|
+
let result = array;
|
|
655
|
+
for (let d = dim; d < dims; d++) {
|
|
656
|
+
result = [result];
|
|
657
|
+
}
|
|
658
|
+
return result;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
function flatten(array, isRectangular = false) {
|
|
662
|
+
if (!Array.isArray(array)) {
|
|
663
|
+
return array;
|
|
664
|
+
}
|
|
665
|
+
if (typeof isRectangular !== "boolean") {
|
|
666
|
+
throw new TypeError("Boolean expected for second argument of flatten");
|
|
667
|
+
}
|
|
668
|
+
const flat = [];
|
|
669
|
+
if (isRectangular) {
|
|
670
|
+
_flattenRectangular(array);
|
|
671
|
+
} else {
|
|
672
|
+
_flatten(array);
|
|
673
|
+
}
|
|
674
|
+
return flat;
|
|
675
|
+
function _flatten(arr) {
|
|
676
|
+
for (let i = 0; i < arr.length; i++) {
|
|
677
|
+
const item = arr[i];
|
|
678
|
+
if (Array.isArray(item)) {
|
|
679
|
+
_flatten(item);
|
|
680
|
+
} else {
|
|
681
|
+
flat.push(item);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
function _flattenRectangular(arr) {
|
|
686
|
+
if (Array.isArray(arr[0])) {
|
|
687
|
+
for (let i = 0; i < arr.length; i++) {
|
|
688
|
+
_flattenRectangular(arr[i]);
|
|
689
|
+
}
|
|
690
|
+
} else {
|
|
691
|
+
for (let i = 0; i < arr.length; i++) {
|
|
692
|
+
flat.push(arr[i]);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
function map(array, callback) {
|
|
698
|
+
return Array.prototype.map.call(array, callback);
|
|
699
|
+
}
|
|
700
|
+
function forEach(array, callback) {
|
|
701
|
+
Array.prototype.forEach.call(array, callback);
|
|
702
|
+
}
|
|
703
|
+
function filter(array, callback) {
|
|
704
|
+
if (arraySize(array).length !== 1) {
|
|
705
|
+
throw new Error("Only one dimensional matrices supported");
|
|
706
|
+
}
|
|
707
|
+
return Array.prototype.filter.call(array, callback);
|
|
708
|
+
}
|
|
709
|
+
function filterRegExp(array, regexp) {
|
|
710
|
+
if (arraySize(array).length !== 1) {
|
|
711
|
+
throw new Error("Only one dimensional matrices supported");
|
|
712
|
+
}
|
|
713
|
+
return Array.prototype.filter.call(array, (entry) => regexp.test(entry));
|
|
714
|
+
}
|
|
715
|
+
function join(array, separator) {
|
|
716
|
+
return Array.prototype.join.call(array, separator);
|
|
717
|
+
}
|
|
718
|
+
function identify(a) {
|
|
719
|
+
if (!Array.isArray(a)) {
|
|
720
|
+
throw new TypeError("Array input expected");
|
|
721
|
+
}
|
|
722
|
+
if (a.length === 0) {
|
|
723
|
+
return [];
|
|
724
|
+
}
|
|
725
|
+
const b = [];
|
|
726
|
+
let count = 0;
|
|
727
|
+
b[0] = { value: a[0], identifier: 0 };
|
|
728
|
+
for (let i = 1; i < a.length; i++) {
|
|
729
|
+
if (a[i] === a[i - 1]) {
|
|
730
|
+
count++;
|
|
731
|
+
} else {
|
|
732
|
+
count = 0;
|
|
733
|
+
}
|
|
734
|
+
b.push({ value: a[i], identifier: count });
|
|
735
|
+
}
|
|
736
|
+
return b;
|
|
737
|
+
}
|
|
738
|
+
function generalize(a) {
|
|
739
|
+
if (!Array.isArray(a)) {
|
|
740
|
+
throw new TypeError("Array input expected");
|
|
741
|
+
}
|
|
742
|
+
if (a.length === 0) {
|
|
743
|
+
return [];
|
|
744
|
+
}
|
|
745
|
+
const b = [];
|
|
746
|
+
for (let i = 0; i < a.length; i++) {
|
|
747
|
+
b.push(a[i].value);
|
|
748
|
+
}
|
|
749
|
+
return b;
|
|
750
|
+
}
|
|
751
|
+
function getArrayDataType(array, typeOf2) {
|
|
752
|
+
let type;
|
|
753
|
+
let length = 0;
|
|
754
|
+
for (let i = 0; i < array.length; i++) {
|
|
755
|
+
const item = array[i];
|
|
756
|
+
const isArray2 = Array.isArray(item);
|
|
757
|
+
if (i === 0 && isArray2) {
|
|
758
|
+
length = item.length;
|
|
759
|
+
}
|
|
760
|
+
if (isArray2 && item.length !== length) {
|
|
761
|
+
return void 0;
|
|
762
|
+
}
|
|
763
|
+
const itemType = isArray2 ? getArrayDataType(item, typeOf2) : typeOf2(item);
|
|
764
|
+
if (type === void 0) {
|
|
765
|
+
type = itemType;
|
|
766
|
+
} else if (type !== itemType) {
|
|
767
|
+
return "mixed";
|
|
768
|
+
} else {
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
return type;
|
|
772
|
+
}
|
|
773
|
+
function last(array) {
|
|
774
|
+
return array[array.length - 1];
|
|
775
|
+
}
|
|
776
|
+
function initial(array) {
|
|
777
|
+
return array.slice(0, array.length - 1);
|
|
778
|
+
}
|
|
779
|
+
function concatRecursive(a, b, concatDim, dim) {
|
|
780
|
+
if (dim < concatDim) {
|
|
781
|
+
if (a.length !== b.length) {
|
|
782
|
+
throw new DimensionError(a.length, b.length);
|
|
783
|
+
}
|
|
784
|
+
const c = [];
|
|
785
|
+
for (let i = 0; i < a.length; i++) {
|
|
786
|
+
c[i] = concatRecursive(
|
|
787
|
+
a[i],
|
|
788
|
+
b[i],
|
|
789
|
+
concatDim,
|
|
790
|
+
dim + 1
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
return c;
|
|
794
|
+
} else {
|
|
795
|
+
return a.concat(b);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function concat(...args) {
|
|
799
|
+
const arrays = Array.prototype.slice.call(args, 0, -1);
|
|
800
|
+
const concatDim = Array.prototype.slice.call(args, -1)[0];
|
|
801
|
+
if (arrays.length === 1) {
|
|
802
|
+
return arrays[0];
|
|
803
|
+
}
|
|
804
|
+
if (arrays.length > 1) {
|
|
805
|
+
return arrays.slice(1).reduce(function(A, B) {
|
|
806
|
+
return concatRecursive(A, B, concatDim, 0);
|
|
807
|
+
}, arrays[0]);
|
|
808
|
+
} else {
|
|
809
|
+
throw new Error("Wrong number of arguments in function concat");
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
function broadcastSizes(...sizes) {
|
|
813
|
+
const dimensions = sizes.map((s) => s.length);
|
|
814
|
+
const N = Math.max(...dimensions);
|
|
815
|
+
const sizeMax = new Array(N).fill(null);
|
|
816
|
+
for (let i = 0; i < sizes.length; i++) {
|
|
817
|
+
const size = sizes[i];
|
|
818
|
+
const dim = dimensions[i];
|
|
819
|
+
for (let j = 0; j < dim; j++) {
|
|
820
|
+
const n = N - dim + j;
|
|
821
|
+
if (size[j] > sizeMax[n]) {
|
|
822
|
+
sizeMax[n] = size[j];
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
for (let i = 0; i < sizes.length; i++) {
|
|
827
|
+
checkBroadcastingRules(sizes[i], sizeMax);
|
|
828
|
+
}
|
|
829
|
+
return sizeMax;
|
|
830
|
+
}
|
|
831
|
+
function checkBroadcastingRules(size, toSize) {
|
|
832
|
+
const N = toSize.length;
|
|
833
|
+
const dim = size.length;
|
|
834
|
+
for (let j = 0; j < dim; j++) {
|
|
835
|
+
const n = N - dim + j;
|
|
836
|
+
if (size[j] < toSize[n] && size[j] > 1 || size[j] > toSize[n]) {
|
|
837
|
+
throw new Error(
|
|
838
|
+
`shape mismatch: mismatch is found in arg with shape (${size}) not possible to broadcast dimension ${dim} with size ${size[j]} to size ${toSize[n]}`
|
|
839
|
+
);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
function broadcastTo(array, toSize) {
|
|
844
|
+
let Asize = arraySize(array);
|
|
845
|
+
if (deepStrictEqual(Asize, toSize)) {
|
|
846
|
+
return array;
|
|
847
|
+
}
|
|
848
|
+
checkBroadcastingRules(Asize, toSize);
|
|
849
|
+
const broadcastedSize = broadcastSizes(Asize, toSize);
|
|
850
|
+
const N = broadcastedSize.length;
|
|
851
|
+
const paddedSize = [...Array(N - Asize.length).fill(1), ...Asize];
|
|
852
|
+
let A = clone2(array);
|
|
853
|
+
if (Asize.length < N) {
|
|
854
|
+
A = reshape(A, paddedSize);
|
|
855
|
+
Asize = arraySize(A);
|
|
856
|
+
}
|
|
857
|
+
for (let dim = 0; dim < N; dim++) {
|
|
858
|
+
if (Asize[dim] < broadcastedSize[dim]) {
|
|
859
|
+
A = stretch(A, broadcastedSize[dim], dim);
|
|
860
|
+
Asize = arraySize(A);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
return A;
|
|
864
|
+
}
|
|
865
|
+
function broadcastArrays(...arrays) {
|
|
866
|
+
if (arrays.length === 0) {
|
|
867
|
+
throw new Error("Insufficient number of arguments in function broadcastArrays");
|
|
868
|
+
}
|
|
869
|
+
if (arrays.length === 1) {
|
|
870
|
+
return arrays[0];
|
|
871
|
+
}
|
|
872
|
+
const sizes = arrays.map(function(array) {
|
|
873
|
+
return arraySize(array);
|
|
874
|
+
});
|
|
875
|
+
const broadcastedSize = broadcastSizes(...sizes);
|
|
876
|
+
const broadcastedArrays = [];
|
|
877
|
+
arrays.forEach(function(array) {
|
|
878
|
+
broadcastedArrays.push(broadcastTo(array, broadcastedSize));
|
|
879
|
+
});
|
|
880
|
+
return broadcastedArrays;
|
|
881
|
+
}
|
|
882
|
+
function stretch(arrayToStretch, sizeToStretch, dimToStretch) {
|
|
883
|
+
return concat(...Array(sizeToStretch).fill(arrayToStretch), dimToStretch);
|
|
884
|
+
}
|
|
885
|
+
function get2(array, index) {
|
|
886
|
+
if (!Array.isArray(array)) {
|
|
887
|
+
throw new Error("Array expected");
|
|
888
|
+
}
|
|
889
|
+
const size = arraySize(array);
|
|
890
|
+
if (index.length !== size.length) {
|
|
891
|
+
throw new DimensionError(index.length, size.length);
|
|
892
|
+
}
|
|
893
|
+
for (let x = 0; x < index.length; x++) {
|
|
894
|
+
validateIndex(index[x], size[x]);
|
|
895
|
+
}
|
|
896
|
+
return index.reduce((acc, curr) => acc[curr], array);
|
|
897
|
+
}
|
|
898
|
+
function deepMap(array, callback, skipIndex = false) {
|
|
899
|
+
if (Array.isArray(array) && array.length === 0) {
|
|
900
|
+
return [];
|
|
901
|
+
}
|
|
902
|
+
if (skipIndex) {
|
|
903
|
+
return recursiveMap(array);
|
|
904
|
+
}
|
|
905
|
+
const index = [];
|
|
906
|
+
return recursiveMapWithIndex(array, 0);
|
|
907
|
+
function recursiveMapWithIndex(value, depth) {
|
|
908
|
+
if (Array.isArray(value)) {
|
|
909
|
+
const N = value.length;
|
|
910
|
+
const result = Array(N);
|
|
911
|
+
for (let i = 0; i < N; i++) {
|
|
912
|
+
index[depth] = i;
|
|
913
|
+
result[i] = recursiveMapWithIndex(value[i], depth + 1);
|
|
914
|
+
}
|
|
915
|
+
return result;
|
|
916
|
+
} else {
|
|
917
|
+
return callback(
|
|
918
|
+
value,
|
|
919
|
+
index.slice(0, depth),
|
|
920
|
+
array
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
function recursiveMap(value) {
|
|
925
|
+
if (Array.isArray(value)) {
|
|
926
|
+
const N = value.length;
|
|
927
|
+
const result = Array(N);
|
|
928
|
+
for (let i = 0; i < N; i++) {
|
|
929
|
+
result[i] = recursiveMap(value[i]);
|
|
930
|
+
}
|
|
931
|
+
return result;
|
|
932
|
+
} else {
|
|
933
|
+
return callback(value);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
function deepForEach(array, callback, skipIndex = false) {
|
|
938
|
+
if (Array.isArray(array) && array.length === 0) {
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
if (skipIndex) {
|
|
942
|
+
recursiveForEach(array);
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
const index = [];
|
|
946
|
+
recursiveForEachWithIndex(array, 0);
|
|
947
|
+
function recursiveForEachWithIndex(value, depth) {
|
|
948
|
+
if (Array.isArray(value)) {
|
|
949
|
+
const N = value.length;
|
|
950
|
+
for (let i = 0; i < N; i++) {
|
|
951
|
+
index[depth] = i;
|
|
952
|
+
recursiveForEachWithIndex(value[i], depth + 1);
|
|
953
|
+
}
|
|
954
|
+
} else {
|
|
955
|
+
callback(
|
|
956
|
+
value,
|
|
957
|
+
index.slice(0, depth),
|
|
958
|
+
array
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
function recursiveForEach(value) {
|
|
963
|
+
if (Array.isArray(value)) {
|
|
964
|
+
const N = value.length;
|
|
965
|
+
for (let i = 0; i < N; i++) {
|
|
966
|
+
recursiveForEach(value[i]);
|
|
967
|
+
}
|
|
968
|
+
} else {
|
|
969
|
+
callback(value);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
function clone2(array) {
|
|
974
|
+
return Object.assign([], array);
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// src/switch.ts
|
|
978
|
+
function _switch(mat) {
|
|
979
|
+
const I = mat.length;
|
|
980
|
+
const J = mat[0].length;
|
|
981
|
+
let i, j;
|
|
982
|
+
const ret = [];
|
|
983
|
+
for (j = 0; j < J; j++) {
|
|
984
|
+
const tmp = [];
|
|
985
|
+
for (i = 0; i < I; i++) {
|
|
986
|
+
tmp.push(mat[i][j]);
|
|
987
|
+
}
|
|
988
|
+
ret.push(tmp);
|
|
989
|
+
}
|
|
990
|
+
return ret;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
// src/collection.ts
|
|
994
|
+
function containsCollections(array) {
|
|
995
|
+
for (let i = 0; i < array.length; i++) {
|
|
996
|
+
if (isCollection(array[i])) {
|
|
997
|
+
return true;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
return false;
|
|
1001
|
+
}
|
|
1002
|
+
function deepForEach2(array, callback) {
|
|
1003
|
+
if (isMatrix(array)) {
|
|
1004
|
+
array.forEach((x) => callback(x), false, true);
|
|
1005
|
+
} else {
|
|
1006
|
+
deepForEach(array, callback, true);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
function deepMap2(array, callback, skipZeros) {
|
|
1010
|
+
if (!skipZeros) {
|
|
1011
|
+
if (isMatrix(array)) {
|
|
1012
|
+
return array.map((x) => callback(x), false, true);
|
|
1013
|
+
} else {
|
|
1014
|
+
return deepMap(array, callback, true);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
const skipZerosCallback = (x) => x === 0 ? x : callback(x);
|
|
1018
|
+
if (isMatrix(array)) {
|
|
1019
|
+
return array.map((x) => skipZerosCallback(x), false, true);
|
|
1020
|
+
} else {
|
|
1021
|
+
return deepMap(array, skipZerosCallback, true);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
function reduce(mat, dim, callback) {
|
|
1025
|
+
const size = Array.isArray(mat) ? arraySize(mat) : mat.size();
|
|
1026
|
+
if (dim < 0 || dim >= size.length) {
|
|
1027
|
+
throw new IndexError(dim, 0, size.length);
|
|
1028
|
+
}
|
|
1029
|
+
if (isMatrix(mat)) {
|
|
1030
|
+
const reduced = _reduce(mat.valueOf(), dim, callback);
|
|
1031
|
+
return mat.create(
|
|
1032
|
+
reduced,
|
|
1033
|
+
mat.datatype()
|
|
1034
|
+
);
|
|
1035
|
+
} else {
|
|
1036
|
+
return _reduce(mat, dim, callback);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
function _reduce(mat, dim, callback) {
|
|
1040
|
+
let i;
|
|
1041
|
+
let ret;
|
|
1042
|
+
let val;
|
|
1043
|
+
let tran;
|
|
1044
|
+
if (dim <= 0) {
|
|
1045
|
+
if (!Array.isArray(mat[0])) {
|
|
1046
|
+
val = mat[0];
|
|
1047
|
+
for (i = 1; i < mat.length; i++) {
|
|
1048
|
+
val = callback(val, mat[i]);
|
|
1049
|
+
}
|
|
1050
|
+
return val;
|
|
1051
|
+
} else {
|
|
1052
|
+
tran = _switch(mat);
|
|
1053
|
+
ret = [];
|
|
1054
|
+
for (i = 0; i < tran.length; i++) {
|
|
1055
|
+
ret[i] = _reduce(tran[i], dim - 1, callback);
|
|
1056
|
+
}
|
|
1057
|
+
return ret;
|
|
1058
|
+
}
|
|
1059
|
+
} else {
|
|
1060
|
+
ret = [];
|
|
1061
|
+
for (i = 0; i < mat.length; i++) {
|
|
1062
|
+
ret[i] = _reduce(mat[i], dim - 1, callback);
|
|
1063
|
+
}
|
|
1064
|
+
return ret;
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
function scatter(a, j, w, x, u, mark, cindex, f, inverse, update, value) {
|
|
1068
|
+
const avalues = a._values;
|
|
1069
|
+
const aindex = a._index;
|
|
1070
|
+
const aptr = a._ptr;
|
|
1071
|
+
let k;
|
|
1072
|
+
let k0;
|
|
1073
|
+
let k1;
|
|
1074
|
+
let i;
|
|
1075
|
+
if (x) {
|
|
1076
|
+
for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) {
|
|
1077
|
+
i = aindex[k];
|
|
1078
|
+
if (w[i] !== mark) {
|
|
1079
|
+
w[i] = mark;
|
|
1080
|
+
cindex.push(i);
|
|
1081
|
+
if (update && f) {
|
|
1082
|
+
x[i] = inverse ? f(avalues[k], value) : f(value, avalues[k]);
|
|
1083
|
+
u[i] = mark;
|
|
1084
|
+
} else {
|
|
1085
|
+
x[i] = avalues[k];
|
|
1086
|
+
}
|
|
1087
|
+
} else {
|
|
1088
|
+
if (f) {
|
|
1089
|
+
x[i] = inverse ? f(avalues[k], x[i]) : f(x[i], avalues[k]);
|
|
1090
|
+
}
|
|
1091
|
+
u[i] = mark;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
} else {
|
|
1095
|
+
for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) {
|
|
1096
|
+
i = aindex[k];
|
|
1097
|
+
if (w[i] !== mark) {
|
|
1098
|
+
w[i] = mark;
|
|
1099
|
+
cindex.push(i);
|
|
1100
|
+
} else {
|
|
1101
|
+
u[i] = mark;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
// src/customs.ts
|
|
1108
|
+
function getSafeProperty(object, prop) {
|
|
1109
|
+
const obj = object;
|
|
1110
|
+
if (isSafeProperty(object, prop)) {
|
|
1111
|
+
return obj[prop];
|
|
1112
|
+
}
|
|
1113
|
+
if (typeof obj[prop] === "function" && isSafeMethod(object, prop)) {
|
|
1114
|
+
throw new Error('Cannot access method "' + prop + '" as a property');
|
|
1115
|
+
}
|
|
1116
|
+
throw new Error('No access to property "' + prop + '"');
|
|
1117
|
+
}
|
|
1118
|
+
function setSafeProperty(object, prop, value) {
|
|
1119
|
+
if (isSafeProperty(object, prop)) {
|
|
1120
|
+
object[prop] = value;
|
|
1121
|
+
return value;
|
|
1122
|
+
}
|
|
1123
|
+
throw new Error('No access to property "' + prop + '"');
|
|
1124
|
+
}
|
|
1125
|
+
function isSafeProperty(object, prop) {
|
|
1126
|
+
if (!isPlainObject(object) && !Array.isArray(object)) {
|
|
1127
|
+
return false;
|
|
1128
|
+
}
|
|
1129
|
+
if (hasOwnProperty(safeNativeProperties, prop)) {
|
|
1130
|
+
return true;
|
|
1131
|
+
}
|
|
1132
|
+
if (prop in Object.prototype) {
|
|
1133
|
+
return false;
|
|
1134
|
+
}
|
|
1135
|
+
if (prop in Function.prototype) {
|
|
1136
|
+
return false;
|
|
1137
|
+
}
|
|
1138
|
+
return true;
|
|
1139
|
+
}
|
|
1140
|
+
function isSafeMethod(object, method) {
|
|
1141
|
+
if (object === null || object === void 0 || typeof object[method] !== "function") {
|
|
1142
|
+
return false;
|
|
1143
|
+
}
|
|
1144
|
+
if (hasOwnProperty(object, method) && Object.getPrototypeOf && method in Object.getPrototypeOf(object)) {
|
|
1145
|
+
return false;
|
|
1146
|
+
}
|
|
1147
|
+
if (hasOwnProperty(safeNativeMethods, method)) {
|
|
1148
|
+
return true;
|
|
1149
|
+
}
|
|
1150
|
+
if (method in Object.prototype) {
|
|
1151
|
+
return false;
|
|
1152
|
+
}
|
|
1153
|
+
if (method in Function.prototype) {
|
|
1154
|
+
return false;
|
|
1155
|
+
}
|
|
1156
|
+
return true;
|
|
1157
|
+
}
|
|
1158
|
+
function isPlainObject(object) {
|
|
1159
|
+
return typeof object === "object" && !!object && object.constructor === Object;
|
|
1160
|
+
}
|
|
1161
|
+
var safeNativeProperties = {
|
|
1162
|
+
length: true,
|
|
1163
|
+
name: true
|
|
1164
|
+
};
|
|
1165
|
+
var safeNativeMethods = {
|
|
1166
|
+
toString: true,
|
|
1167
|
+
valueOf: true,
|
|
1168
|
+
toLocaleString: true
|
|
1169
|
+
};
|
|
1170
|
+
|
|
1171
|
+
// src/map.ts
|
|
1172
|
+
var ObjectWrappingMap = class {
|
|
1173
|
+
wrappedObject;
|
|
1174
|
+
[Symbol.toStringTag] = "ObjectWrappingMap";
|
|
1175
|
+
constructor(object) {
|
|
1176
|
+
this.wrappedObject = object;
|
|
1177
|
+
}
|
|
1178
|
+
/**
|
|
1179
|
+
* Declared as a real method rather than assigned in the constructor through a
|
|
1180
|
+
* cast. The old form satisfied the runtime but never appeared in the emitted
|
|
1181
|
+
* type, so `implements Map<K, V>` was a lie that only surfaced downstream:
|
|
1182
|
+
* consumers compiling with `skipLibCheck: false` got TS2420 ("incorrectly
|
|
1183
|
+
* implements interface 'Map'... '[Symbol.iterator]' is missing").
|
|
1184
|
+
*/
|
|
1185
|
+
[Symbol.iterator]() {
|
|
1186
|
+
return this.entries();
|
|
1187
|
+
}
|
|
1188
|
+
// @ts-expect-error: Implementation is compatible but TS can't infer it
|
|
1189
|
+
keys() {
|
|
1190
|
+
return Object.keys(this.wrappedObject).filter((key) => this.has(key)).values();
|
|
1191
|
+
}
|
|
1192
|
+
get(key) {
|
|
1193
|
+
return getSafeProperty(this.wrappedObject, key);
|
|
1194
|
+
}
|
|
1195
|
+
set(key, value) {
|
|
1196
|
+
setSafeProperty(this.wrappedObject, key, value);
|
|
1197
|
+
return this;
|
|
1198
|
+
}
|
|
1199
|
+
has(key) {
|
|
1200
|
+
return isSafeProperty(this.wrappedObject, key) && key in this.wrappedObject;
|
|
1201
|
+
}
|
|
1202
|
+
// @ts-expect-error: Implementation is compatible but TS can't infer it
|
|
1203
|
+
entries() {
|
|
1204
|
+
return mapIterator(this.keys(), (key) => [key, this.get(key)]);
|
|
1205
|
+
}
|
|
1206
|
+
// @ts-expect-error: Implementation is compatible but TS can't infer it
|
|
1207
|
+
*values() {
|
|
1208
|
+
for (const key of this.keys()) {
|
|
1209
|
+
yield this.get(key);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
forEach(callback) {
|
|
1213
|
+
for (const key of this.keys()) {
|
|
1214
|
+
callback(this.get(key), key, this);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
delete(key) {
|
|
1218
|
+
if (isSafeProperty(this.wrappedObject, key)) {
|
|
1219
|
+
delete this.wrappedObject[key];
|
|
1220
|
+
return true;
|
|
1221
|
+
}
|
|
1222
|
+
return false;
|
|
1223
|
+
}
|
|
1224
|
+
clear() {
|
|
1225
|
+
for (const key of this.keys()) {
|
|
1226
|
+
this.delete(key);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
get size() {
|
|
1230
|
+
return Object.keys(this.wrappedObject).length;
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
var PartitionedMap = class {
|
|
1234
|
+
a;
|
|
1235
|
+
b;
|
|
1236
|
+
bKeys;
|
|
1237
|
+
[Symbol.toStringTag] = "PartitionedMap";
|
|
1238
|
+
/**
|
|
1239
|
+
* @param a - Primary map
|
|
1240
|
+
* @param b - Secondary map
|
|
1241
|
+
* @param bKeys - Set of keys that should be read/written to map b
|
|
1242
|
+
*/
|
|
1243
|
+
constructor(a, b, bKeys) {
|
|
1244
|
+
this.a = a;
|
|
1245
|
+
this.b = b;
|
|
1246
|
+
this.bKeys = bKeys;
|
|
1247
|
+
}
|
|
1248
|
+
/** See the note on `ObjectWrappingMap[Symbol.iterator]` — same fix. */
|
|
1249
|
+
[Symbol.iterator]() {
|
|
1250
|
+
return this.entries();
|
|
1251
|
+
}
|
|
1252
|
+
get(key) {
|
|
1253
|
+
return this.bKeys.has(key) ? this.b.get(key) : this.a.get(key);
|
|
1254
|
+
}
|
|
1255
|
+
set(key, value) {
|
|
1256
|
+
if (this.bKeys.has(key)) {
|
|
1257
|
+
this.b.set(key, value);
|
|
1258
|
+
} else {
|
|
1259
|
+
this.a.set(key, value);
|
|
1260
|
+
}
|
|
1261
|
+
return this;
|
|
1262
|
+
}
|
|
1263
|
+
has(key) {
|
|
1264
|
+
return this.b.has(key) || this.a.has(key);
|
|
1265
|
+
}
|
|
1266
|
+
// @ts-expect-error: Implementation is compatible but TS can't infer it
|
|
1267
|
+
keys() {
|
|
1268
|
+
return (/* @__PURE__ */ new Set([...this.a.keys(), ...this.b.keys()]))[Symbol.iterator]();
|
|
1269
|
+
}
|
|
1270
|
+
// @ts-expect-error: Implementation is compatible but TS can't infer it
|
|
1271
|
+
*values() {
|
|
1272
|
+
for (const key of this.keys()) {
|
|
1273
|
+
yield this.get(key);
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
// @ts-expect-error: Implementation is compatible but TS can't infer it
|
|
1277
|
+
entries() {
|
|
1278
|
+
return mapIterator(this.keys(), (key) => [key, this.get(key)]);
|
|
1279
|
+
}
|
|
1280
|
+
forEach(callback) {
|
|
1281
|
+
for (const key of this.keys()) {
|
|
1282
|
+
callback(this.get(key), key, this);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
delete(key) {
|
|
1286
|
+
return this.bKeys.has(key) ? this.b.delete(key) : this.a.delete(key);
|
|
1287
|
+
}
|
|
1288
|
+
clear() {
|
|
1289
|
+
this.a.clear();
|
|
1290
|
+
this.b.clear();
|
|
1291
|
+
}
|
|
1292
|
+
get size() {
|
|
1293
|
+
return [...this.keys()].length;
|
|
1294
|
+
}
|
|
1295
|
+
};
|
|
1296
|
+
function mapIterator(it, callback) {
|
|
1297
|
+
return {
|
|
1298
|
+
next: () => {
|
|
1299
|
+
const n = it.next();
|
|
1300
|
+
return n.done ? n : {
|
|
1301
|
+
value: callback(n.value),
|
|
1302
|
+
done: false
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
}
|
|
1307
|
+
function createEmptyMap() {
|
|
1308
|
+
return /* @__PURE__ */ new Map();
|
|
1309
|
+
}
|
|
1310
|
+
function createMap(mapOrObject) {
|
|
1311
|
+
if (!mapOrObject) {
|
|
1312
|
+
return createEmptyMap();
|
|
1313
|
+
}
|
|
1314
|
+
if (isMap(mapOrObject)) {
|
|
1315
|
+
return mapOrObject;
|
|
1316
|
+
}
|
|
1317
|
+
if (isObject(mapOrObject)) {
|
|
1318
|
+
return new ObjectWrappingMap(mapOrObject);
|
|
1319
|
+
}
|
|
1320
|
+
throw new Error("createMap can create maps from objects or Maps");
|
|
1321
|
+
}
|
|
1322
|
+
function toObject(map2) {
|
|
1323
|
+
if (map2 instanceof ObjectWrappingMap) {
|
|
1324
|
+
return map2.wrappedObject;
|
|
1325
|
+
}
|
|
1326
|
+
const object = {};
|
|
1327
|
+
for (const key of map2.keys()) {
|
|
1328
|
+
const value = map2.get(key);
|
|
1329
|
+
setSafeProperty(object, key, value);
|
|
1330
|
+
}
|
|
1331
|
+
return object;
|
|
1332
|
+
}
|
|
1333
|
+
function assign(map2, ...objects) {
|
|
1334
|
+
for (const args of objects) {
|
|
1335
|
+
if (!args) {
|
|
1336
|
+
continue;
|
|
1337
|
+
}
|
|
1338
|
+
if (isMap(args)) {
|
|
1339
|
+
for (const key of args.keys()) {
|
|
1340
|
+
map2.set(key, args.get(key));
|
|
1341
|
+
}
|
|
1342
|
+
} else if (isObject(args)) {
|
|
1343
|
+
for (const key of Object.keys(args)) {
|
|
1344
|
+
map2.set(key, args[key]);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
return map2;
|
|
1349
|
+
}
|
|
1350
|
+
function isObjectWrappingMap(object) {
|
|
1351
|
+
if (!isMap(object)) return false;
|
|
1352
|
+
const wrapper = object;
|
|
1353
|
+
return isObject(wrapper.wrappedObject);
|
|
1354
|
+
}
|
|
73
1355
|
export {
|
|
74
1356
|
Unit as CoreUnit,
|
|
1357
|
+
DEFAULT_CONFIG,
|
|
1358
|
+
DimensionError,
|
|
1359
|
+
IndexError,
|
|
1360
|
+
MathjsError,
|
|
1361
|
+
ObjectWrappingMap,
|
|
1362
|
+
PartitionedMap,
|
|
75
1363
|
acosh,
|
|
1364
|
+
arraySize,
|
|
76
1365
|
asinh,
|
|
1366
|
+
assertDependencies,
|
|
1367
|
+
assign as assignMap,
|
|
77
1368
|
atanh,
|
|
1369
|
+
broadcastArrays,
|
|
1370
|
+
broadcastSizes,
|
|
1371
|
+
broadcastTo,
|
|
78
1372
|
canDefineProperty,
|
|
79
1373
|
cbrt,
|
|
1374
|
+
checkBroadcastingRules,
|
|
80
1375
|
clone,
|
|
1376
|
+
clone2 as cloneArray,
|
|
1377
|
+
compareText,
|
|
1378
|
+
concat,
|
|
1379
|
+
containsCollections,
|
|
81
1380
|
copysign,
|
|
82
1381
|
cosh,
|
|
1382
|
+
create,
|
|
1383
|
+
createEmptyMap,
|
|
1384
|
+
createIndexError,
|
|
1385
|
+
createMap,
|
|
83
1386
|
createUnitClass,
|
|
84
1387
|
deepExtend,
|
|
85
1388
|
deepFlatten,
|
|
1389
|
+
deepForEach2 as deepForEach,
|
|
1390
|
+
deepForEach as deepForEachArray,
|
|
1391
|
+
deepMap2 as deepMap,
|
|
1392
|
+
deepMap as deepMapArray,
|
|
86
1393
|
deepStrictEqual,
|
|
87
1394
|
digits,
|
|
88
1395
|
endsWith,
|
|
1396
|
+
escape,
|
|
89
1397
|
expm1,
|
|
90
1398
|
extend,
|
|
1399
|
+
factory,
|
|
1400
|
+
filter,
|
|
1401
|
+
filterRegExp,
|
|
1402
|
+
flatten,
|
|
1403
|
+
forEach,
|
|
91
1404
|
format,
|
|
1405
|
+
format2 as formatBigNumber,
|
|
1406
|
+
format3 as formatGeneric,
|
|
1407
|
+
generalize,
|
|
92
1408
|
get,
|
|
1409
|
+
getArrayDataType,
|
|
1410
|
+
get2 as getArrayElement,
|
|
93
1411
|
hasOwnProperty,
|
|
1412
|
+
identify,
|
|
1413
|
+
initial,
|
|
94
1414
|
isArray,
|
|
95
1415
|
isBigInt,
|
|
96
1416
|
isBigNumber,
|
|
@@ -99,6 +1419,8 @@ export {
|
|
|
99
1419
|
isComplex,
|
|
100
1420
|
isDate,
|
|
101
1421
|
isDenseMatrix,
|
|
1422
|
+
isEmptyIndex,
|
|
1423
|
+
isFactory,
|
|
102
1424
|
isFraction,
|
|
103
1425
|
isFunction,
|
|
104
1426
|
isIndex,
|
|
@@ -109,6 +1431,8 @@ export {
|
|
|
109
1431
|
isNull,
|
|
110
1432
|
isNumber,
|
|
111
1433
|
isObject,
|
|
1434
|
+
isObjectWrappingMap,
|
|
1435
|
+
isOptionalDependency,
|
|
112
1436
|
isPowZeroAtInfinity,
|
|
113
1437
|
isRange,
|
|
114
1438
|
isRegExp,
|
|
@@ -116,29 +1440,50 @@ export {
|
|
|
116
1440
|
isString,
|
|
117
1441
|
isUndefined,
|
|
118
1442
|
isUnit,
|
|
1443
|
+
join,
|
|
1444
|
+
last,
|
|
119
1445
|
lazy,
|
|
120
1446
|
log10,
|
|
121
1447
|
log1p,
|
|
122
1448
|
log2,
|
|
1449
|
+
map,
|
|
123
1450
|
mapObject,
|
|
124
1451
|
memoize,
|
|
125
1452
|
nearlyEqual,
|
|
126
1453
|
normalizeFormatOptions,
|
|
127
1454
|
pick,
|
|
128
1455
|
pickShallow,
|
|
1456
|
+
processSizesWildcard,
|
|
1457
|
+
reduce,
|
|
1458
|
+
reshape,
|
|
1459
|
+
resize,
|
|
129
1460
|
roundDigits,
|
|
130
1461
|
safeNumberType,
|
|
1462
|
+
scatter,
|
|
131
1463
|
set,
|
|
132
1464
|
sign,
|
|
133
1465
|
sinh,
|
|
1466
|
+
sortFactories,
|
|
134
1467
|
splitNumber,
|
|
1468
|
+
squeeze,
|
|
1469
|
+
stretch,
|
|
1470
|
+
stringify,
|
|
1471
|
+
stripOptionalNotation,
|
|
135
1472
|
tanh,
|
|
136
1473
|
toEngineering,
|
|
1474
|
+
toEngineering2 as toEngineeringBigNumber,
|
|
137
1475
|
toExponential,
|
|
1476
|
+
toExponential2 as toExponentialBigNumber,
|
|
138
1477
|
toFixed,
|
|
1478
|
+
toFixed2 as toFixedBigNumber,
|
|
1479
|
+
toObject,
|
|
139
1480
|
toPrecision,
|
|
140
1481
|
traverse,
|
|
141
1482
|
typeOf,
|
|
142
1483
|
unitDependencies,
|
|
1484
|
+
unsqueeze,
|
|
1485
|
+
validate,
|
|
1486
|
+
validateIndex,
|
|
1487
|
+
validateIndexSourceSize,
|
|
143
1488
|
warnOnce
|
|
144
1489
|
};
|