@danielsimonjr/mathts-core 0.18.0 → 0.19.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/index.js +1244 -1534
- package/dist/internal.js +863 -1515
- package/dist/types/fraction.d.ts.map +1 -1
- package/dist/types/unit/Unit.d.ts.map +1 -1
- package/dist/types/unit/exact-scale.d.ts +3 -3
- package/dist/types/unit/unit-table.d.ts +8 -2
- package/dist/types/unit/unit-table.d.ts.map +1 -1
- package/dist/types/unit/unit-types.d.ts +10 -0
- package/dist/types/unit/unit-types.d.ts.map +1 -1
- package/dist/unit-l_r8z5Fv.js +5917 -0
- package/package.json +5 -5
- package/dist/chunk-UEBMU6EV.js +0 -6086
package/dist/index.js
CHANGED
|
@@ -1,1598 +1,1308 @@
|
|
|
1
|
-
import {
|
|
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
|
-
min() {
|
|
246
|
-
const size = this.size()[0];
|
|
247
|
-
if (size > 0) {
|
|
248
|
-
if (this.step > 0) {
|
|
249
|
-
return this.start;
|
|
250
|
-
} else {
|
|
251
|
-
return this.start + (size - 1) * this.step;
|
|
252
|
-
}
|
|
253
|
-
} else {
|
|
254
|
-
return void 0;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Calculate the maximum value in the range
|
|
259
|
-
* @memberof Range
|
|
260
|
-
* @return max
|
|
261
|
-
*/
|
|
262
|
-
max() {
|
|
263
|
-
const size = this.size()[0];
|
|
264
|
-
if (size > 0) {
|
|
265
|
-
if (this.step > 0) {
|
|
266
|
-
return this.start + (size - 1) * this.step;
|
|
267
|
-
} else {
|
|
268
|
-
return this.start;
|
|
269
|
-
}
|
|
270
|
-
} else {
|
|
271
|
-
return void 0;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Execute a callback function for each value in the range.
|
|
276
|
-
* @memberof Range
|
|
277
|
-
* @param callback - The callback method is invoked with three
|
|
278
|
-
* parameters: the value of the element, the index
|
|
279
|
-
* of the element, and the Range being traversed.
|
|
280
|
-
*/
|
|
281
|
-
forEach(callback) {
|
|
282
|
-
let x = this.start;
|
|
283
|
-
const step = this.step;
|
|
284
|
-
const end = this.end;
|
|
285
|
-
let i = 0;
|
|
286
|
-
if (step > 0) {
|
|
287
|
-
while (x < end) {
|
|
288
|
-
callback(x, [i], this);
|
|
289
|
-
x += step;
|
|
290
|
-
i++;
|
|
291
|
-
}
|
|
292
|
-
} else if (step < 0) {
|
|
293
|
-
while (x > end) {
|
|
294
|
-
callback(x, [i], this);
|
|
295
|
-
x += step;
|
|
296
|
-
i++;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Execute a callback function for each value in the Range, and return the
|
|
302
|
-
* results as an array
|
|
303
|
-
* @memberof Range
|
|
304
|
-
* @param callback - The callback method is invoked with three
|
|
305
|
-
* parameters: the value of the element, the index
|
|
306
|
-
* of the element, and the Matrix being traversed.
|
|
307
|
-
* @returns array
|
|
308
|
-
*/
|
|
309
|
-
map(callback) {
|
|
310
|
-
const array = [];
|
|
311
|
-
this.forEach((value, index) => {
|
|
312
|
-
array[index[0]] = callback(value, index, this);
|
|
313
|
-
});
|
|
314
|
-
return array;
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Create an Array with a copy of the Ranges data
|
|
318
|
-
* @memberof Range
|
|
319
|
-
* @returns array
|
|
320
|
-
*/
|
|
321
|
-
toArray() {
|
|
322
|
-
const array = [];
|
|
323
|
-
this.forEach(function(value, index) {
|
|
324
|
-
array[index[0]] = value;
|
|
325
|
-
});
|
|
326
|
-
return array;
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Get the primitive value of the Range, a one dimensional array
|
|
330
|
-
* @memberof Range
|
|
331
|
-
* @returns array
|
|
332
|
-
*/
|
|
333
|
-
valueOf() {
|
|
334
|
-
if (this._cache !== null && this.start === this._cacheStart && this.end === this._cacheEnd && this.step === this._cacheStep) {
|
|
335
|
-
return this._cache;
|
|
336
|
-
}
|
|
337
|
-
this._cache = this.toArray();
|
|
338
|
-
this._cacheStart = this.start;
|
|
339
|
-
this._cacheEnd = this.end;
|
|
340
|
-
this._cacheStep = this.step;
|
|
341
|
-
return this._cache;
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Get a string representation of the range, with optional formatting options.
|
|
345
|
-
* Output is formatted as 'start:step:end', for example '2:6' or '0:0.2:11'
|
|
346
|
-
* @memberof Range
|
|
347
|
-
* @param options - Optional. Formatting options. See
|
|
348
|
-
* lib/utils/number:format for a
|
|
349
|
-
* description of the available
|
|
350
|
-
* options.
|
|
351
|
-
* @returns str
|
|
352
|
-
*/
|
|
353
|
-
format(options) {
|
|
354
|
-
let str = format(this.start, options);
|
|
355
|
-
if (this.step !== 1) {
|
|
356
|
-
str += ":" + format(this.step, options);
|
|
357
|
-
}
|
|
358
|
-
str += ":" + format(this.end, options);
|
|
359
|
-
return str;
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Get a string representation of the range.
|
|
363
|
-
* @memberof Range
|
|
364
|
-
* @returns
|
|
365
|
-
*/
|
|
366
|
-
toString() {
|
|
367
|
-
return this.format();
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Get a JSON representation of the range
|
|
371
|
-
* @memberof Range
|
|
372
|
-
* @returns Returns a JSON object structured as:
|
|
373
|
-
* `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}`
|
|
374
|
-
*/
|
|
375
|
-
toJSON() {
|
|
376
|
-
return {
|
|
377
|
-
mathjs: "Range",
|
|
378
|
-
start: this.start,
|
|
379
|
-
end: this.end,
|
|
380
|
-
step: this.step
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Instantiate a Range from a JSON object
|
|
385
|
-
* @memberof Range
|
|
386
|
-
* @param json - A JSON object structured as:
|
|
387
|
-
* `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}`
|
|
388
|
-
* @return
|
|
389
|
-
*/
|
|
390
|
-
static fromJSON(json) {
|
|
391
|
-
return new Range2(json.start, json.end, json.step);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
return Range2;
|
|
395
|
-
},
|
|
396
|
-
{ isClass: true }
|
|
397
|
-
);
|
|
398
|
-
var Range = createRangeClass({});
|
|
399
|
-
|
|
400
|
-
// src/types/dual-rules.ts
|
|
401
|
-
var DUAL_UNARY_RULES = {
|
|
402
|
-
exp: { primal: Math.exp, deriv: (_x, y) => y },
|
|
403
|
-
expm1: { primal: Math.expm1, deriv: (_x, y) => y + 1 },
|
|
404
|
-
log: { primal: Math.log, deriv: (x) => 1 / x },
|
|
405
|
-
log2: { primal: Math.log2, deriv: (x) => 1 / (x * Math.LN2) },
|
|
406
|
-
log10: { primal: Math.log10, deriv: (x) => 1 / (x * Math.LN10) },
|
|
407
|
-
log1p: { primal: Math.log1p, deriv: (x) => 1 / (1 + x) },
|
|
408
|
-
sin: { primal: Math.sin, deriv: (x) => Math.cos(x) },
|
|
409
|
-
cos: { primal: Math.cos, deriv: (x) => -Math.sin(x) },
|
|
410
|
-
tan: { primal: Math.tan, deriv: (x) => 1 / (Math.cos(x) * Math.cos(x)) },
|
|
411
|
-
asin: { primal: Math.asin, deriv: (x) => 1 / Math.sqrt(1 - x * x) },
|
|
412
|
-
acos: { primal: Math.acos, deriv: (x) => -1 / Math.sqrt(1 - x * x) },
|
|
413
|
-
atan: { primal: Math.atan, deriv: (x) => 1 / (1 + x * x) },
|
|
414
|
-
sinh: { primal: Math.sinh, deriv: (x) => Math.cosh(x) },
|
|
415
|
-
cosh: { primal: Math.cosh, deriv: (x) => Math.sinh(x) },
|
|
416
|
-
tanh: { primal: Math.tanh, deriv: (_x, y) => 1 - y * y },
|
|
417
|
-
asinh: { primal: Math.asinh, deriv: (x) => 1 / Math.sqrt(x * x + 1) },
|
|
418
|
-
acosh: { primal: Math.acosh, deriv: (x) => 1 / Math.sqrt(x * x - 1) },
|
|
419
|
-
atanh: { primal: Math.atanh, deriv: (x) => 1 / (1 - x * x) },
|
|
420
|
-
sqrt: { primal: Math.sqrt, deriv: (_x, y) => 1 / (2 * y) },
|
|
421
|
-
cbrt: { primal: Math.cbrt, deriv: (_x, y) => 1 / (3 * y * y) },
|
|
422
|
-
square: { primal: (x) => x * x, deriv: (x) => 2 * x },
|
|
423
|
-
reciprocal: { primal: (x) => 1 / x, deriv: (x) => -1 / (x * x) },
|
|
424
|
-
abs: { primal: Math.abs, deriv: (x) => Math.sign(x) },
|
|
425
|
-
sign: { primal: Math.sign, deriv: () => 0 }
|
|
1
|
+
import { $ as BIGNUMBER_LN2, A as irrationalPower, B as addScalar, Bn as I, C as decimalExactScale, D as exactScaleToNumber, E as exactScaleOf, F as ratioExactScale, G as multiplyScalar, H as equal, I as readScaleExpression, In as COMPLEX_NEG_ONE, J as round, K as number, L as DimensionMismatchError, Ln as COMPLEX_ONE, M as multiplyExactScales, N as namedIrrationalScale, O as exactScalesEqual, P as powerExactScale, Pn as isUnit$1, Q as BIGNUMBER_LN10, R as UnitParseError, Rn as COMPLEX_ZERO, S as clearExactScaleNumberCache, T as exactScaleNumberCacheSize, U as fix, V as divideScalar, Vn as isComplex, W as isNumeric, Y as subtractScalar, Yt as format, Z as BIGNUMBER_E, _ as NAMED_IRRATIONALS, _t as factory, a as UNIT_PREFIX_SETS, an as sign, at as BigNumber, b as addExactScales, c as getUnitPrefix, ct as FRACTION_NEG_ONE, d as readUnitScale, dt as FRACTION_THIRD, et as BIGNUMBER_NEG_ONE, f as unitPrefixExactScale, ft as FRACTION_ZERO, g as LN2_EXACT_SCALE, h as unitRowValue, hn as isBigNumber$2, i as NAMED_SCALES, it as BIGNUMBER_ZERO, j as isRationalScale, k as formatExactScale, l as getUnitRow, lt as FRACTION_ONE, m as unitRowExactScale, mn as isBigInt$1, mt as isFraction, nt as BIGNUMBER_PI, o as UNIT_ROWS, ot as isBigNumber, p as unitRowExactOffset, pt as Fraction, q as pow, rt as BIGNUMBER_TEN, s as UNIT_ROW_ALIASES, st as FRACTION_HALF, t as Unit, tn as nearlyEqual, tt as BIGNUMBER_ONE, u as namedExactScale, ut as FRACTION_QUARTER, v as PI_EXACT_SCALE, w as divideExactScales, x as binaryExactScale, y as UNIT_EXACT_SCALE, z as abs, zn as Complex } from "./unit-l_r8z5Fv.js";
|
|
2
|
+
import typed, { create } from "typed-function";
|
|
3
|
+
const createRangeClass = /* #__PURE__ */ factory("Range", [], () => {
|
|
4
|
+
class Range {
|
|
5
|
+
type = "Range";
|
|
6
|
+
isRange = true;
|
|
7
|
+
start;
|
|
8
|
+
end;
|
|
9
|
+
step;
|
|
10
|
+
_cache = null;
|
|
11
|
+
_cacheStart;
|
|
12
|
+
_cacheEnd;
|
|
13
|
+
_cacheStep;
|
|
14
|
+
constructor(start, end, step) {
|
|
15
|
+
if (!(this instanceof Range)) throw new SyntaxError("Constructor must be called with the new operator");
|
|
16
|
+
const hasStart = start !== null && start !== void 0;
|
|
17
|
+
const hasEnd = end !== null && end !== void 0;
|
|
18
|
+
const hasStep = step !== null && step !== void 0;
|
|
19
|
+
let startValue = 0;
|
|
20
|
+
let endValue = 0;
|
|
21
|
+
let stepValue = 1;
|
|
22
|
+
if (hasStart) {
|
|
23
|
+
if (isBigNumber$2(start)) startValue = start.toNumber();
|
|
24
|
+
else if (typeof start !== "number" && !isBigInt$1(start)) throw new TypeError("Parameter start must be a number or bigint");
|
|
25
|
+
else startValue = start;
|
|
26
|
+
}
|
|
27
|
+
if (hasEnd) {
|
|
28
|
+
if (isBigNumber$2(end)) endValue = end.toNumber();
|
|
29
|
+
else if (typeof end !== "number" && !isBigInt$1(end)) throw new TypeError("Parameter end must be a number or bigint");
|
|
30
|
+
else endValue = end;
|
|
31
|
+
}
|
|
32
|
+
if (hasStep) {
|
|
33
|
+
if (isBigNumber$2(step)) stepValue = step.toNumber();
|
|
34
|
+
else if (typeof step !== "number" && !isBigInt$1(step)) throw new TypeError("Parameter step must be a number or bigint");
|
|
35
|
+
else stepValue = step;
|
|
36
|
+
}
|
|
37
|
+
this.start = hasStart ? parseFloat(startValue.toString()) : 0;
|
|
38
|
+
this.end = hasEnd ? parseFloat(endValue.toString()) : 0;
|
|
39
|
+
this.step = hasStep ? parseFloat(stepValue.toString()) : 1;
|
|
40
|
+
if (hasStep && nearlyEqual(this.step, 0)) throw new Error("Step must not be zero");
|
|
41
|
+
}
|
|
42
|
+
static parse(str) {
|
|
43
|
+
if (typeof str !== "string") return null;
|
|
44
|
+
const nums = str.split(":").map(function(arg) {
|
|
45
|
+
return parseFloat(arg);
|
|
46
|
+
});
|
|
47
|
+
if (nums.some(function(num) {
|
|
48
|
+
return isNaN(num);
|
|
49
|
+
})) return null;
|
|
50
|
+
switch (nums.length) {
|
|
51
|
+
case 2: return new Range(nums[0], nums[1]);
|
|
52
|
+
case 3: return new Range(nums[0], nums[2], nums[1]);
|
|
53
|
+
default: return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
clone() {
|
|
57
|
+
return new Range(this.start, this.end, this.step);
|
|
58
|
+
}
|
|
59
|
+
size() {
|
|
60
|
+
let len = 0;
|
|
61
|
+
const start = this.start;
|
|
62
|
+
const step = this.step;
|
|
63
|
+
const diff = this.end - start;
|
|
64
|
+
if (sign(step) === sign(diff)) len = Math.ceil(diff / step);
|
|
65
|
+
else if (diff === 0) len = 0;
|
|
66
|
+
if (isNaN(len)) len = 0;
|
|
67
|
+
return [len];
|
|
68
|
+
}
|
|
69
|
+
min() {
|
|
70
|
+
const size = this.size()[0];
|
|
71
|
+
if (size > 0) {
|
|
72
|
+
if (this.step > 0) return this.start;
|
|
73
|
+
else return this.start + (size - 1) * this.step;
|
|
74
|
+
} else return;
|
|
75
|
+
}
|
|
76
|
+
max() {
|
|
77
|
+
const size = this.size()[0];
|
|
78
|
+
if (size > 0) {
|
|
79
|
+
if (this.step > 0) return this.start + (size - 1) * this.step;
|
|
80
|
+
else return this.start;
|
|
81
|
+
} else return;
|
|
82
|
+
}
|
|
83
|
+
forEach(callback) {
|
|
84
|
+
let x = this.start;
|
|
85
|
+
const step = this.step;
|
|
86
|
+
const end = this.end;
|
|
87
|
+
let i = 0;
|
|
88
|
+
if (step > 0) while (x < end) {
|
|
89
|
+
callback(x, [i], this);
|
|
90
|
+
x += step;
|
|
91
|
+
i++;
|
|
92
|
+
}
|
|
93
|
+
else if (step < 0) while (x > end) {
|
|
94
|
+
callback(x, [i], this);
|
|
95
|
+
x += step;
|
|
96
|
+
i++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
map(callback) {
|
|
100
|
+
const array = [];
|
|
101
|
+
this.forEach((value, index) => {
|
|
102
|
+
array[index[0]] = callback(value, index, this);
|
|
103
|
+
});
|
|
104
|
+
return array;
|
|
105
|
+
}
|
|
106
|
+
toArray() {
|
|
107
|
+
const array = [];
|
|
108
|
+
this.forEach(function(value, index) {
|
|
109
|
+
array[index[0]] = value;
|
|
110
|
+
});
|
|
111
|
+
return array;
|
|
112
|
+
}
|
|
113
|
+
valueOf() {
|
|
114
|
+
if (this._cache !== null && this.start === this._cacheStart && this.end === this._cacheEnd && this.step === this._cacheStep) return this._cache;
|
|
115
|
+
this._cache = this.toArray();
|
|
116
|
+
this._cacheStart = this.start;
|
|
117
|
+
this._cacheEnd = this.end;
|
|
118
|
+
this._cacheStep = this.step;
|
|
119
|
+
return this._cache;
|
|
120
|
+
}
|
|
121
|
+
format(options) {
|
|
122
|
+
let str = format(this.start, options);
|
|
123
|
+
if (this.step !== 1) str += ":" + format(this.step, options);
|
|
124
|
+
str += ":" + format(this.end, options);
|
|
125
|
+
return str;
|
|
126
|
+
}
|
|
127
|
+
toString() {
|
|
128
|
+
return this.format();
|
|
129
|
+
}
|
|
130
|
+
toJSON() {
|
|
131
|
+
return {
|
|
132
|
+
mathjs: "Range",
|
|
133
|
+
start: this.start,
|
|
134
|
+
end: this.end,
|
|
135
|
+
step: this.step
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
static fromJSON(json) {
|
|
139
|
+
return new Range(json.start, json.end, json.step);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return Range;
|
|
143
|
+
}, { isClass: true });
|
|
144
|
+
const Range = createRangeClass({});
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/types/dual-rules.ts
|
|
147
|
+
const DUAL_UNARY_RULES = {
|
|
148
|
+
exp: {
|
|
149
|
+
primal: Math.exp,
|
|
150
|
+
deriv: (_x, y) => y
|
|
151
|
+
},
|
|
152
|
+
expm1: {
|
|
153
|
+
primal: Math.expm1,
|
|
154
|
+
deriv: (_x, y) => y + 1
|
|
155
|
+
},
|
|
156
|
+
log: {
|
|
157
|
+
primal: Math.log,
|
|
158
|
+
deriv: (x) => 1 / x
|
|
159
|
+
},
|
|
160
|
+
log2: {
|
|
161
|
+
primal: Math.log2,
|
|
162
|
+
deriv: (x) => 1 / (x * Math.LN2)
|
|
163
|
+
},
|
|
164
|
+
log10: {
|
|
165
|
+
primal: Math.log10,
|
|
166
|
+
deriv: (x) => 1 / (x * Math.LN10)
|
|
167
|
+
},
|
|
168
|
+
log1p: {
|
|
169
|
+
primal: Math.log1p,
|
|
170
|
+
deriv: (x) => 1 / (1 + x)
|
|
171
|
+
},
|
|
172
|
+
sin: {
|
|
173
|
+
primal: Math.sin,
|
|
174
|
+
deriv: (x) => Math.cos(x)
|
|
175
|
+
},
|
|
176
|
+
cos: {
|
|
177
|
+
primal: Math.cos,
|
|
178
|
+
deriv: (x) => -Math.sin(x)
|
|
179
|
+
},
|
|
180
|
+
tan: {
|
|
181
|
+
primal: Math.tan,
|
|
182
|
+
deriv: (x) => 1 / (Math.cos(x) * Math.cos(x))
|
|
183
|
+
},
|
|
184
|
+
asin: {
|
|
185
|
+
primal: Math.asin,
|
|
186
|
+
deriv: (x) => 1 / Math.sqrt(1 - x * x)
|
|
187
|
+
},
|
|
188
|
+
acos: {
|
|
189
|
+
primal: Math.acos,
|
|
190
|
+
deriv: (x) => -1 / Math.sqrt(1 - x * x)
|
|
191
|
+
},
|
|
192
|
+
atan: {
|
|
193
|
+
primal: Math.atan,
|
|
194
|
+
deriv: (x) => 1 / (1 + x * x)
|
|
195
|
+
},
|
|
196
|
+
sinh: {
|
|
197
|
+
primal: Math.sinh,
|
|
198
|
+
deriv: (x) => Math.cosh(x)
|
|
199
|
+
},
|
|
200
|
+
cosh: {
|
|
201
|
+
primal: Math.cosh,
|
|
202
|
+
deriv: (x) => Math.sinh(x)
|
|
203
|
+
},
|
|
204
|
+
tanh: {
|
|
205
|
+
primal: Math.tanh,
|
|
206
|
+
deriv: (_x, y) => 1 - y * y
|
|
207
|
+
},
|
|
208
|
+
asinh: {
|
|
209
|
+
primal: Math.asinh,
|
|
210
|
+
deriv: (x) => 1 / Math.sqrt(x * x + 1)
|
|
211
|
+
},
|
|
212
|
+
acosh: {
|
|
213
|
+
primal: Math.acosh,
|
|
214
|
+
deriv: (x) => 1 / Math.sqrt(x * x - 1)
|
|
215
|
+
},
|
|
216
|
+
atanh: {
|
|
217
|
+
primal: Math.atanh,
|
|
218
|
+
deriv: (x) => 1 / (1 - x * x)
|
|
219
|
+
},
|
|
220
|
+
sqrt: {
|
|
221
|
+
primal: Math.sqrt,
|
|
222
|
+
deriv: (_x, y) => 1 / (2 * y)
|
|
223
|
+
},
|
|
224
|
+
cbrt: {
|
|
225
|
+
primal: Math.cbrt,
|
|
226
|
+
deriv: (_x, y) => 1 / (3 * y * y)
|
|
227
|
+
},
|
|
228
|
+
square: {
|
|
229
|
+
primal: (x) => x * x,
|
|
230
|
+
deriv: (x) => 2 * x
|
|
231
|
+
},
|
|
232
|
+
reciprocal: {
|
|
233
|
+
primal: (x) => 1 / x,
|
|
234
|
+
deriv: (x) => -1 / (x * x)
|
|
235
|
+
},
|
|
236
|
+
abs: {
|
|
237
|
+
primal: Math.abs,
|
|
238
|
+
deriv: (x) => Math.sign(x)
|
|
239
|
+
},
|
|
240
|
+
sign: {
|
|
241
|
+
primal: Math.sign,
|
|
242
|
+
deriv: () => 0
|
|
243
|
+
}
|
|
426
244
|
};
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
var Dual = class
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
return this.unary(DUAL_UNARY_RULES.sinh);
|
|
507
|
-
}
|
|
508
|
-
cosh() {
|
|
509
|
-
return this.unary(DUAL_UNARY_RULES.cosh);
|
|
510
|
-
}
|
|
511
|
-
tanh() {
|
|
512
|
-
return this.unary(DUAL_UNARY_RULES.tanh);
|
|
513
|
-
}
|
|
514
|
-
toString() {
|
|
515
|
-
return `Dual(${this.value}, ${this.deriv})`;
|
|
516
|
-
}
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/types/dual.ts
|
|
247
|
+
var Dual = class Dual {
|
|
248
|
+
value;
|
|
249
|
+
deriv;
|
|
250
|
+
constructor(value, deriv = 0) {
|
|
251
|
+
this.value = value;
|
|
252
|
+
this.deriv = deriv;
|
|
253
|
+
}
|
|
254
|
+
static constant(v) {
|
|
255
|
+
return new Dual(v, 0);
|
|
256
|
+
}
|
|
257
|
+
static variable(v) {
|
|
258
|
+
return new Dual(v, 1);
|
|
259
|
+
}
|
|
260
|
+
add(o) {
|
|
261
|
+
return new Dual(this.value + o.value, this.deriv + o.deriv);
|
|
262
|
+
}
|
|
263
|
+
sub(o) {
|
|
264
|
+
return new Dual(this.value - o.value, this.deriv - o.deriv);
|
|
265
|
+
}
|
|
266
|
+
mul(o) {
|
|
267
|
+
return new Dual(this.value * o.value, this.deriv * o.value + this.value * o.deriv);
|
|
268
|
+
}
|
|
269
|
+
div(o) {
|
|
270
|
+
const d = (this.deriv * o.value - this.value * o.deriv) / (o.value * o.value);
|
|
271
|
+
return new Dual(this.value / o.value, d);
|
|
272
|
+
}
|
|
273
|
+
neg() {
|
|
274
|
+
return new Dual(-this.value, -this.deriv);
|
|
275
|
+
}
|
|
276
|
+
powConst(k) {
|
|
277
|
+
return new Dual(Math.pow(this.value, k), this.deriv * k * Math.pow(this.value, k - 1));
|
|
278
|
+
}
|
|
279
|
+
pow(o) {
|
|
280
|
+
const v = Math.pow(this.value, o.value);
|
|
281
|
+
const d = v * (o.deriv * Math.log(this.value) + o.value * this.deriv / this.value);
|
|
282
|
+
return new Dual(v, d);
|
|
283
|
+
}
|
|
284
|
+
unary(rule) {
|
|
285
|
+
const y = rule.primal(this.value);
|
|
286
|
+
return new Dual(y, this.deriv * rule.deriv(this.value, y));
|
|
287
|
+
}
|
|
288
|
+
sin() {
|
|
289
|
+
return this.unary(DUAL_UNARY_RULES.sin);
|
|
290
|
+
}
|
|
291
|
+
cos() {
|
|
292
|
+
return this.unary(DUAL_UNARY_RULES.cos);
|
|
293
|
+
}
|
|
294
|
+
tan() {
|
|
295
|
+
return this.unary(DUAL_UNARY_RULES.tan);
|
|
296
|
+
}
|
|
297
|
+
exp() {
|
|
298
|
+
return this.unary(DUAL_UNARY_RULES.exp);
|
|
299
|
+
}
|
|
300
|
+
log() {
|
|
301
|
+
return this.unary(DUAL_UNARY_RULES.log);
|
|
302
|
+
}
|
|
303
|
+
sqrt() {
|
|
304
|
+
return this.unary(DUAL_UNARY_RULES.sqrt);
|
|
305
|
+
}
|
|
306
|
+
square() {
|
|
307
|
+
return this.unary(DUAL_UNARY_RULES.square);
|
|
308
|
+
}
|
|
309
|
+
abs() {
|
|
310
|
+
return this.unary(DUAL_UNARY_RULES.abs);
|
|
311
|
+
}
|
|
312
|
+
sinh() {
|
|
313
|
+
return this.unary(DUAL_UNARY_RULES.sinh);
|
|
314
|
+
}
|
|
315
|
+
cosh() {
|
|
316
|
+
return this.unary(DUAL_UNARY_RULES.cosh);
|
|
317
|
+
}
|
|
318
|
+
tanh() {
|
|
319
|
+
return this.unary(DUAL_UNARY_RULES.tanh);
|
|
320
|
+
}
|
|
321
|
+
toString() {
|
|
322
|
+
return `Dual(${this.value}, ${this.deriv})`;
|
|
323
|
+
}
|
|
517
324
|
};
|
|
518
325
|
function isDual(x) {
|
|
519
|
-
|
|
326
|
+
return x instanceof Dual;
|
|
520
327
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/constants.ts
|
|
330
|
+
const PI = Math.PI;
|
|
331
|
+
const E = Math.E;
|
|
332
|
+
const TAU = 2 * Math.PI;
|
|
333
|
+
const PHI = (1 + Math.sqrt(5)) / 2;
|
|
334
|
+
const SQRT2 = Math.SQRT2;
|
|
335
|
+
const SQRT1_2 = Math.SQRT1_2;
|
|
336
|
+
const LN2 = Math.LN2;
|
|
337
|
+
const LN10 = Math.LN10;
|
|
338
|
+
const LOG2E = Math.LOG2E;
|
|
339
|
+
const LOG10E = Math.LOG10E;
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region src/types/unit-definitions.ts
|
|
342
|
+
const DIMENSIONLESS = Object.freeze({
|
|
343
|
+
length: 0,
|
|
344
|
+
mass: 0,
|
|
345
|
+
time: 0,
|
|
346
|
+
current: 0,
|
|
347
|
+
temperature: 0,
|
|
348
|
+
amount: 0,
|
|
349
|
+
luminosity: 0
|
|
543
350
|
});
|
|
544
351
|
function dim(partial) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
352
|
+
return {
|
|
353
|
+
length: partial.length ?? 0,
|
|
354
|
+
mass: partial.mass ?? 0,
|
|
355
|
+
time: partial.time ?? 0,
|
|
356
|
+
current: partial.current ?? 0,
|
|
357
|
+
temperature: partial.temperature ?? 0,
|
|
358
|
+
amount: partial.amount ?? 0,
|
|
359
|
+
luminosity: partial.luminosity ?? 0
|
|
360
|
+
};
|
|
554
361
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
362
|
+
const BASE_UNITS = {
|
|
363
|
+
m: {
|
|
364
|
+
multiplier: 1,
|
|
365
|
+
dimensions: dim({ length: 1 }),
|
|
366
|
+
prefixable: true
|
|
367
|
+
},
|
|
368
|
+
kg: {
|
|
369
|
+
multiplier: 1,
|
|
370
|
+
dimensions: dim({ mass: 1 })
|
|
371
|
+
},
|
|
372
|
+
s: {
|
|
373
|
+
multiplier: 1,
|
|
374
|
+
dimensions: dim({ time: 1 }),
|
|
375
|
+
prefixable: true
|
|
376
|
+
},
|
|
377
|
+
A: {
|
|
378
|
+
multiplier: 1,
|
|
379
|
+
dimensions: dim({ current: 1 }),
|
|
380
|
+
prefixable: true
|
|
381
|
+
},
|
|
382
|
+
K: {
|
|
383
|
+
multiplier: 1,
|
|
384
|
+
dimensions: dim({ temperature: 1 }),
|
|
385
|
+
prefixable: true
|
|
386
|
+
},
|
|
387
|
+
mol: {
|
|
388
|
+
multiplier: 1,
|
|
389
|
+
dimensions: dim({ amount: 1 }),
|
|
390
|
+
prefixable: true
|
|
391
|
+
},
|
|
392
|
+
cd: {
|
|
393
|
+
multiplier: 1,
|
|
394
|
+
dimensions: dim({ luminosity: 1 }),
|
|
395
|
+
prefixable: true
|
|
396
|
+
}
|
|
565
397
|
};
|
|
566
|
-
function tableUnit(
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
398
|
+
function tableUnit(name, dimensions, prefixable) {
|
|
399
|
+
const row = getUnitRow(name);
|
|
400
|
+
if (row === void 0) throw new Error(`unit-definitions: '${name}' is not in the unit table`);
|
|
401
|
+
const scale = readUnitScale(row.scale);
|
|
402
|
+
const def = {
|
|
403
|
+
multiplier: exactScaleToNumber(scale),
|
|
404
|
+
dimensions
|
|
405
|
+
};
|
|
406
|
+
if (row.offset !== void 0) def.offset = exactScaleToNumber(multiplyExactScales(scale, readUnitScale(row.offset)));
|
|
407
|
+
if (prefixable) def.prefixable = true;
|
|
408
|
+
return def;
|
|
576
409
|
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
410
|
+
const DERIVED_UNITS = {
|
|
411
|
+
ft: tableUnit("ft", dim({ length: 1 })),
|
|
412
|
+
foot: tableUnit("foot", dim({ length: 1 })),
|
|
413
|
+
in: tableUnit("in", dim({ length: 1 })),
|
|
414
|
+
inch: tableUnit("inch", dim({ length: 1 })),
|
|
415
|
+
yd: tableUnit("yd", dim({ length: 1 })),
|
|
416
|
+
yard: tableUnit("yard", dim({ length: 1 })),
|
|
417
|
+
mi: tableUnit("mi", dim({ length: 1 })),
|
|
418
|
+
mile: tableUnit("mile", dim({ length: 1 })),
|
|
419
|
+
g: tableUnit("g", dim({ mass: 1 }), true),
|
|
420
|
+
lb: tableUnit("lb", dim({ mass: 1 })),
|
|
421
|
+
lbm: tableUnit("lbm", dim({ mass: 1 })),
|
|
422
|
+
oz: tableUnit("oz", dim({ mass: 1 })),
|
|
423
|
+
ton: tableUnit("ton", dim({ mass: 1 })),
|
|
424
|
+
tonne: tableUnit("tonne", dim({ mass: 1 })),
|
|
425
|
+
min: tableUnit("min", dim({ time: 1 })),
|
|
426
|
+
h: tableUnit("h", dim({ time: 1 })),
|
|
427
|
+
hr: tableUnit("hr", dim({ time: 1 })),
|
|
428
|
+
day: tableUnit("day", dim({ time: 1 })),
|
|
429
|
+
week: tableUnit("week", dim({ time: 1 })),
|
|
430
|
+
year: tableUnit("year", dim({ time: 1 })),
|
|
431
|
+
degC: tableUnit("degC", dim({ temperature: 1 })),
|
|
432
|
+
degF: tableUnit("degF", dim({ temperature: 1 })),
|
|
433
|
+
degR: tableUnit("degR", dim({ temperature: 1 })),
|
|
434
|
+
rad: tableUnit("rad", dim({}), true),
|
|
435
|
+
deg: tableUnit("deg", dim({})),
|
|
436
|
+
grad: tableUnit("grad", dim({})),
|
|
437
|
+
N: tableUnit("N", dim({
|
|
438
|
+
length: 1,
|
|
439
|
+
mass: 1,
|
|
440
|
+
time: -2
|
|
441
|
+
}), true),
|
|
442
|
+
dyn: tableUnit("dyn", dim({
|
|
443
|
+
length: 1,
|
|
444
|
+
mass: 1,
|
|
445
|
+
time: -2
|
|
446
|
+
})),
|
|
447
|
+
lbf: tableUnit("lbf", dim({
|
|
448
|
+
length: 1,
|
|
449
|
+
mass: 1,
|
|
450
|
+
time: -2
|
|
451
|
+
})),
|
|
452
|
+
J: tableUnit("J", dim({
|
|
453
|
+
length: 2,
|
|
454
|
+
mass: 1,
|
|
455
|
+
time: -2
|
|
456
|
+
}), true),
|
|
457
|
+
erg: tableUnit("erg", dim({
|
|
458
|
+
length: 2,
|
|
459
|
+
mass: 1,
|
|
460
|
+
time: -2
|
|
461
|
+
})),
|
|
462
|
+
cal: tableUnit("cal", dim({
|
|
463
|
+
length: 2,
|
|
464
|
+
mass: 1,
|
|
465
|
+
time: -2
|
|
466
|
+
})),
|
|
467
|
+
eV: tableUnit("eV", dim({
|
|
468
|
+
length: 2,
|
|
469
|
+
mass: 1,
|
|
470
|
+
time: -2
|
|
471
|
+
}), true),
|
|
472
|
+
BTU: tableUnit("BTU", dim({
|
|
473
|
+
length: 2,
|
|
474
|
+
mass: 1,
|
|
475
|
+
time: -2
|
|
476
|
+
})),
|
|
477
|
+
W: tableUnit("W", dim({
|
|
478
|
+
length: 2,
|
|
479
|
+
mass: 1,
|
|
480
|
+
time: -3
|
|
481
|
+
}), true),
|
|
482
|
+
hp: tableUnit("hp", dim({
|
|
483
|
+
length: 2,
|
|
484
|
+
mass: 1,
|
|
485
|
+
time: -3
|
|
486
|
+
})),
|
|
487
|
+
Pa: tableUnit("Pa", dim({
|
|
488
|
+
length: -1,
|
|
489
|
+
mass: 1,
|
|
490
|
+
time: -2
|
|
491
|
+
}), true),
|
|
492
|
+
bar: tableUnit("bar", dim({
|
|
493
|
+
length: -1,
|
|
494
|
+
mass: 1,
|
|
495
|
+
time: -2
|
|
496
|
+
}), true),
|
|
497
|
+
atm: tableUnit("atm", dim({
|
|
498
|
+
length: -1,
|
|
499
|
+
mass: 1,
|
|
500
|
+
time: -2
|
|
501
|
+
})),
|
|
502
|
+
psi: tableUnit("psi", dim({
|
|
503
|
+
length: -1,
|
|
504
|
+
mass: 1,
|
|
505
|
+
time: -2
|
|
506
|
+
})),
|
|
507
|
+
torr: tableUnit("torr", dim({
|
|
508
|
+
length: -1,
|
|
509
|
+
mass: 1,
|
|
510
|
+
time: -2
|
|
511
|
+
})),
|
|
512
|
+
mmHg: tableUnit("mmHg", dim({
|
|
513
|
+
length: -1,
|
|
514
|
+
mass: 1,
|
|
515
|
+
time: -2
|
|
516
|
+
})),
|
|
517
|
+
C: tableUnit("C", dim({
|
|
518
|
+
time: 1,
|
|
519
|
+
current: 1
|
|
520
|
+
}), true),
|
|
521
|
+
V: tableUnit("V", dim({
|
|
522
|
+
length: 2,
|
|
523
|
+
mass: 1,
|
|
524
|
+
time: -3,
|
|
525
|
+
current: -1
|
|
526
|
+
}), true),
|
|
527
|
+
ohm: tableUnit("ohm", dim({
|
|
528
|
+
length: 2,
|
|
529
|
+
mass: 1,
|
|
530
|
+
time: -3,
|
|
531
|
+
current: -2
|
|
532
|
+
}), true),
|
|
533
|
+
Ω: tableUnit("ohm", dim({
|
|
534
|
+
length: 2,
|
|
535
|
+
mass: 1,
|
|
536
|
+
time: -3,
|
|
537
|
+
current: -2
|
|
538
|
+
}), true),
|
|
539
|
+
F: tableUnit("F", dim({
|
|
540
|
+
length: -2,
|
|
541
|
+
mass: -1,
|
|
542
|
+
time: 4,
|
|
543
|
+
current: 2
|
|
544
|
+
}), true),
|
|
545
|
+
H: tableUnit("H", dim({
|
|
546
|
+
length: 2,
|
|
547
|
+
mass: 1,
|
|
548
|
+
time: -2,
|
|
549
|
+
current: -2
|
|
550
|
+
}), true),
|
|
551
|
+
Hz: tableUnit("Hz", dim({ time: -1 }), true),
|
|
552
|
+
L: tableUnit("L", dim({ length: 3 }), true),
|
|
553
|
+
l: tableUnit("l", dim({ length: 3 }), true),
|
|
554
|
+
gal: tableUnit("gal", dim({ length: 3 })),
|
|
555
|
+
ha: tableUnit("hectare", dim({ length: 2 })),
|
|
556
|
+
acre: tableUnit("acre", dim({ length: 2 }))
|
|
557
|
+
};
|
|
558
|
+
const ALL_UNITS = {
|
|
559
|
+
...BASE_UNITS,
|
|
560
|
+
...DERIVED_UNITS
|
|
659
561
|
};
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
"\xB0": "deg"
|
|
666
|
-
// The Unicode micro sign is handled in prefixes; °K is non-standard.
|
|
562
|
+
const UNIT_ALIASES = {
|
|
563
|
+
"°C": "degC",
|
|
564
|
+
"°F": "degF",
|
|
565
|
+
"°R": "degR",
|
|
566
|
+
"°": "deg"
|
|
667
567
|
};
|
|
668
|
-
function getUnitDef(
|
|
669
|
-
|
|
568
|
+
function getUnitDef(name) {
|
|
569
|
+
return ALL_UNITS[name];
|
|
670
570
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
571
|
+
//#endregion
|
|
572
|
+
//#region src/types/si-dimension-vector.ts
|
|
573
|
+
const UNIT_DIMENSION_LENGTH = 10;
|
|
574
|
+
const SI_DIMENSION_LENGTH = 7;
|
|
575
|
+
const EXTRA_INDEXES = [
|
|
576
|
+
7,
|
|
577
|
+
8,
|
|
578
|
+
9
|
|
579
|
+
];
|
|
676
580
|
function assertFinite(label, value) {
|
|
677
|
-
|
|
678
|
-
throw new TypeError(`${label} must be a finite number`);
|
|
679
|
-
}
|
|
581
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new TypeError(`${label} must be a finite number`);
|
|
680
582
|
}
|
|
681
583
|
function assertUnitVector(vector) {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
}
|
|
685
|
-
for (let i = 0; i < vector.length; i++) {
|
|
686
|
-
assertFinite(`dimension vector[${i}]`, vector[i]);
|
|
687
|
-
}
|
|
584
|
+
if (vector.length !== 10) throw new TypeError(`dimension vector must have length 10`);
|
|
585
|
+
for (let i = 0; i < vector.length; i++) assertFinite(`dimension vector[${i}]`, vector[i]);
|
|
688
586
|
}
|
|
689
587
|
function rejectExtra(vector, options) {
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
if (vector[index] !== 0) {
|
|
693
|
-
throw new RangeError(
|
|
694
|
-
`dimension vector has a nonzero angle, bit, or solid-angle exponent at index ${index}; pass { ignoreExtra: true } to drop it`
|
|
695
|
-
);
|
|
696
|
-
}
|
|
697
|
-
}
|
|
588
|
+
if (options?.ignoreExtra) return;
|
|
589
|
+
for (const index of EXTRA_INDEXES) if (vector[index] !== 0) throw new RangeError(`dimension vector has a nonzero angle, bit, or solid-angle exponent at index ${index}; pass { ignoreExtra: true } to drop it`);
|
|
698
590
|
}
|
|
699
591
|
function toSiDimensions(vector, options) {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
592
|
+
assertUnitVector(vector);
|
|
593
|
+
rejectExtra(vector, options);
|
|
594
|
+
return {
|
|
595
|
+
length: vector[1],
|
|
596
|
+
mass: vector[0],
|
|
597
|
+
time: vector[2],
|
|
598
|
+
current: vector[3],
|
|
599
|
+
temperature: vector[4],
|
|
600
|
+
amount: vector[6],
|
|
601
|
+
luminosity: vector[5]
|
|
602
|
+
};
|
|
711
603
|
}
|
|
712
604
|
function toSiDimensionVector(vector, options) {
|
|
713
|
-
|
|
714
|
-
|
|
605
|
+
const d = toSiDimensions(vector, options);
|
|
606
|
+
return [
|
|
607
|
+
d.length,
|
|
608
|
+
d.mass,
|
|
609
|
+
d.time,
|
|
610
|
+
d.current,
|
|
611
|
+
d.temperature,
|
|
612
|
+
d.amount,
|
|
613
|
+
d.luminosity
|
|
614
|
+
];
|
|
715
615
|
}
|
|
716
616
|
function fromSiDimensions(dimensions) {
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
0
|
|
740
|
-
];
|
|
617
|
+
const fields = [
|
|
618
|
+
["mass", dimensions.mass],
|
|
619
|
+
["length", dimensions.length],
|
|
620
|
+
["time", dimensions.time],
|
|
621
|
+
["current", dimensions.current],
|
|
622
|
+
["temperature", dimensions.temperature],
|
|
623
|
+
["luminosity", dimensions.luminosity],
|
|
624
|
+
["amount", dimensions.amount]
|
|
625
|
+
];
|
|
626
|
+
for (const [name, value] of fields) assertFinite(`dimensions.${name}`, value);
|
|
627
|
+
return [
|
|
628
|
+
dimensions.mass,
|
|
629
|
+
dimensions.length,
|
|
630
|
+
dimensions.time,
|
|
631
|
+
dimensions.current,
|
|
632
|
+
dimensions.temperature,
|
|
633
|
+
dimensions.luminosity,
|
|
634
|
+
dimensions.amount,
|
|
635
|
+
0,
|
|
636
|
+
0,
|
|
637
|
+
0
|
|
638
|
+
];
|
|
741
639
|
}
|
|
742
640
|
function fromSiDimensionVector(vector) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
temperature: vector[4],
|
|
754
|
-
amount: vector[5],
|
|
755
|
-
luminosity: vector[6]
|
|
756
|
-
});
|
|
641
|
+
if (vector.length !== 7) throw new TypeError(`SI dimension vector must have length 7 [L, M, T, I, Θ, N, J]`);
|
|
642
|
+
return fromSiDimensions({
|
|
643
|
+
length: vector[0],
|
|
644
|
+
mass: vector[1],
|
|
645
|
+
time: vector[2],
|
|
646
|
+
current: vector[3],
|
|
647
|
+
temperature: vector[4],
|
|
648
|
+
amount: vector[5],
|
|
649
|
+
luminosity: vector[6]
|
|
650
|
+
});
|
|
757
651
|
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
652
|
+
//#endregion
|
|
653
|
+
//#region src/types/unit-prefixes.ts
|
|
654
|
+
const SI_PREFIXES = (() => {
|
|
655
|
+
const prefixes = {};
|
|
656
|
+
const short = UNIT_PREFIX_SETS.SHORT;
|
|
657
|
+
for (const name of Object.keys(short)) {
|
|
658
|
+
if (name === "") continue;
|
|
659
|
+
prefixes[name] = exactScaleToNumber(unitPrefixExactScale(short[name]));
|
|
660
|
+
}
|
|
661
|
+
prefixes["µ"] = prefixes.u;
|
|
662
|
+
return prefixes;
|
|
769
663
|
})();
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
664
|
+
const BEST_PREFIXES = [
|
|
665
|
+
"Y",
|
|
666
|
+
"Z",
|
|
667
|
+
"E",
|
|
668
|
+
"P",
|
|
669
|
+
"T",
|
|
670
|
+
"G",
|
|
671
|
+
"M",
|
|
672
|
+
"k",
|
|
673
|
+
"",
|
|
674
|
+
"m",
|
|
675
|
+
"u",
|
|
676
|
+
"n",
|
|
677
|
+
"p",
|
|
678
|
+
"f",
|
|
679
|
+
"a",
|
|
680
|
+
"z",
|
|
681
|
+
"y"
|
|
788
682
|
];
|
|
789
|
-
function getPrefix(
|
|
790
|
-
|
|
683
|
+
function getPrefix(name) {
|
|
684
|
+
return SI_PREFIXES[name];
|
|
791
685
|
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
var wasmAvailable = false;
|
|
686
|
+
//#endregion
|
|
687
|
+
//#region src/typed/mathts-typed.ts
|
|
688
|
+
let wasmInitialized = false;
|
|
689
|
+
let wasmAvailable = false;
|
|
797
690
|
async function initTypedWasm(options = {}) {
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
691
|
+
if (wasmInitialized) return wasmAvailable;
|
|
692
|
+
try {
|
|
693
|
+
if (typeof typed.init === "function") {
|
|
694
|
+
await typed.init({ preferWasm: options.preferWasm ?? true });
|
|
695
|
+
wasmAvailable = typed.isWasmEnabled?.() ?? false;
|
|
696
|
+
}
|
|
697
|
+
} catch {
|
|
698
|
+
wasmAvailable = false;
|
|
699
|
+
}
|
|
700
|
+
wasmInitialized = true;
|
|
701
|
+
return wasmAvailable;
|
|
809
702
|
}
|
|
810
703
|
function isTypedWasmAvailable() {
|
|
811
|
-
|
|
704
|
+
return typed.isWasmEnabled?.() ?? wasmAvailable;
|
|
812
705
|
}
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
706
|
+
const isNumber = (x) => typeof x === "number";
|
|
707
|
+
const isBoolean = (x) => typeof x === "boolean";
|
|
708
|
+
const isString = (x) => typeof x === "string";
|
|
709
|
+
const isBigInt = (x) => typeof x === "bigint";
|
|
710
|
+
const isArray = (x) => Array.isArray(x);
|
|
711
|
+
const isFunction = (x) => typeof x === "function";
|
|
712
|
+
const isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
|
|
713
|
+
const isNull = (x) => x === null;
|
|
714
|
+
const isUndefined = (x) => x === void 0;
|
|
715
|
+
const isFloat64Array = (x) => x instanceof Float64Array;
|
|
716
|
+
const isFloat32Array = (x) => x instanceof Float32Array;
|
|
717
|
+
const isInt32Array = (x) => x instanceof Int32Array;
|
|
718
|
+
const isUint32Array = (x) => x instanceof Uint32Array;
|
|
719
|
+
const isUint8Array = (x) => x instanceof Uint8Array;
|
|
720
|
+
const isComplex$1 = (x) => isComplex(x);
|
|
721
|
+
const isFraction$1 = (x) => isFraction(x);
|
|
722
|
+
const isBigNumber$1 = (x) => isBigNumber(x);
|
|
723
|
+
const isMatrix = (x) => isObject(x) && "rows" in x && "cols" in x && "get" in x && typeof x.type === "string";
|
|
724
|
+
const isDenseMatrix = (x) => isMatrix(x) && x.type === "DenseMatrix";
|
|
725
|
+
const isSparseMatrix = (x) => isMatrix(x) && x.type === "SparseMatrix";
|
|
726
|
+
const isUnit = (x) => {
|
|
727
|
+
if (!isObject(x)) return false;
|
|
728
|
+
const o = x;
|
|
729
|
+
if (typeof o.type !== "string") return false;
|
|
730
|
+
if (o.type === "Unit" && "value" in o && "dimensions" in o) return true;
|
|
731
|
+
if ("value" in o && "unit" in o) return true;
|
|
732
|
+
return false;
|
|
840
733
|
};
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
734
|
+
const MATHTS_TYPES = [
|
|
735
|
+
{
|
|
736
|
+
name: "bigint",
|
|
737
|
+
test: isBigInt
|
|
738
|
+
},
|
|
739
|
+
{
|
|
740
|
+
name: "Complex",
|
|
741
|
+
test: isComplex$1
|
|
742
|
+
},
|
|
743
|
+
{
|
|
744
|
+
name: "Fraction",
|
|
745
|
+
test: isFraction$1
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
name: "BigNumber",
|
|
749
|
+
test: isBigNumber$1
|
|
750
|
+
},
|
|
751
|
+
{
|
|
752
|
+
name: "Float64Array",
|
|
753
|
+
test: isFloat64Array
|
|
754
|
+
},
|
|
755
|
+
{
|
|
756
|
+
name: "Float32Array",
|
|
757
|
+
test: isFloat32Array
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
name: "Int32Array",
|
|
761
|
+
test: isInt32Array
|
|
762
|
+
},
|
|
763
|
+
{
|
|
764
|
+
name: "Uint32Array",
|
|
765
|
+
test: isUint32Array
|
|
766
|
+
},
|
|
767
|
+
{
|
|
768
|
+
name: "Uint8Array",
|
|
769
|
+
test: isUint8Array
|
|
770
|
+
},
|
|
771
|
+
{
|
|
772
|
+
name: "DenseMatrix",
|
|
773
|
+
test: isDenseMatrix
|
|
774
|
+
},
|
|
775
|
+
{
|
|
776
|
+
name: "SparseMatrix",
|
|
777
|
+
test: isSparseMatrix
|
|
778
|
+
},
|
|
779
|
+
{
|
|
780
|
+
name: "Matrix",
|
|
781
|
+
test: isMatrix
|
|
782
|
+
},
|
|
783
|
+
{
|
|
784
|
+
name: "Unit",
|
|
785
|
+
test: isUnit
|
|
786
|
+
},
|
|
787
|
+
{
|
|
788
|
+
name: "Dual",
|
|
789
|
+
test: isDual
|
|
790
|
+
}
|
|
862
791
|
];
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
const result = [];
|
|
1006
|
-
for (let i = 0; i < matrix.rows; i++) {
|
|
1007
|
-
result[i] = [];
|
|
1008
|
-
for (let j = 0; j < matrix.cols; j++) {
|
|
1009
|
-
result[i][j] = matrix.get(i, j);
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
return result;
|
|
1013
|
-
}
|
|
1014
|
-
},
|
|
1015
|
-
{
|
|
1016
|
-
from: "DenseMatrix",
|
|
1017
|
-
to: "SparseMatrix",
|
|
1018
|
-
convert: (m) => {
|
|
1019
|
-
return { ...m, type: "SparseMatrix" };
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
792
|
+
const MATHTS_CONVERSIONS = [
|
|
793
|
+
{
|
|
794
|
+
from: "number",
|
|
795
|
+
to: "Complex",
|
|
796
|
+
convert: (n) => Complex.fromNumber(n)
|
|
797
|
+
},
|
|
798
|
+
{
|
|
799
|
+
from: "Fraction",
|
|
800
|
+
to: "Complex",
|
|
801
|
+
convert: (f) => Complex.fromNumber(f.toNumber())
|
|
802
|
+
},
|
|
803
|
+
{
|
|
804
|
+
from: "BigNumber",
|
|
805
|
+
to: "Complex",
|
|
806
|
+
convert: (bn) => Complex.fromNumber(bn.valueOf())
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
from: "string",
|
|
810
|
+
to: "Complex",
|
|
811
|
+
convert: (s) => Complex.parse(s)
|
|
812
|
+
},
|
|
813
|
+
{
|
|
814
|
+
from: "number",
|
|
815
|
+
to: "Fraction",
|
|
816
|
+
convert: (n) => Fraction.fromNumber(n)
|
|
817
|
+
},
|
|
818
|
+
{
|
|
819
|
+
from: "bigint",
|
|
820
|
+
to: "Fraction",
|
|
821
|
+
convert: (n) => new Fraction(n, 1n)
|
|
822
|
+
},
|
|
823
|
+
{
|
|
824
|
+
from: "string",
|
|
825
|
+
to: "Fraction",
|
|
826
|
+
convert: (s) => Fraction.parse(s)
|
|
827
|
+
},
|
|
828
|
+
{
|
|
829
|
+
from: "number",
|
|
830
|
+
to: "BigNumber",
|
|
831
|
+
convert: (n) => BigNumber.fromNumber(n)
|
|
832
|
+
},
|
|
833
|
+
{
|
|
834
|
+
from: "bigint",
|
|
835
|
+
to: "BigNumber",
|
|
836
|
+
convert: (n) => BigNumber.fromBigInt(n)
|
|
837
|
+
},
|
|
838
|
+
{
|
|
839
|
+
from: "Fraction",
|
|
840
|
+
to: "BigNumber",
|
|
841
|
+
convert: (f) => BigNumber.fromNumber(f.toNumber())
|
|
842
|
+
},
|
|
843
|
+
{
|
|
844
|
+
from: "string",
|
|
845
|
+
to: "BigNumber",
|
|
846
|
+
convert: (s) => BigNumber.parse(s)
|
|
847
|
+
},
|
|
848
|
+
{
|
|
849
|
+
from: "boolean",
|
|
850
|
+
to: "number",
|
|
851
|
+
convert: (b) => b ? 1 : 0
|
|
852
|
+
},
|
|
853
|
+
{
|
|
854
|
+
from: "string",
|
|
855
|
+
to: "number",
|
|
856
|
+
convert: (s) => {
|
|
857
|
+
const n = parseFloat(s);
|
|
858
|
+
if (isNaN(n)) throw new Error(`Cannot convert "${s}" to number`);
|
|
859
|
+
return n;
|
|
860
|
+
}
|
|
861
|
+
},
|
|
862
|
+
{
|
|
863
|
+
from: "bigint",
|
|
864
|
+
to: "number",
|
|
865
|
+
convert: (n) => Number(n)
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
from: "Fraction",
|
|
869
|
+
to: "number",
|
|
870
|
+
convert: (f) => f.toNumber()
|
|
871
|
+
},
|
|
872
|
+
{
|
|
873
|
+
from: "BigNumber",
|
|
874
|
+
to: "number",
|
|
875
|
+
convert: (bn) => bn.valueOf()
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
from: "number",
|
|
879
|
+
to: "bigint",
|
|
880
|
+
convert: (n) => BigInt(Math.trunc(n))
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
from: "Fraction",
|
|
884
|
+
to: "bigint",
|
|
885
|
+
convert: (f) => f.trunc().numerator
|
|
886
|
+
},
|
|
887
|
+
{
|
|
888
|
+
from: "BigNumber",
|
|
889
|
+
to: "bigint",
|
|
890
|
+
convert: (bn) => bn.toBigInt()
|
|
891
|
+
},
|
|
892
|
+
{
|
|
893
|
+
from: "string",
|
|
894
|
+
to: "bigint",
|
|
895
|
+
convert: (s) => BigInt(s)
|
|
896
|
+
},
|
|
897
|
+
{
|
|
898
|
+
from: "Array",
|
|
899
|
+
to: "Matrix",
|
|
900
|
+
convert: (a) => {
|
|
901
|
+
const arr = a;
|
|
902
|
+
return {
|
|
903
|
+
rows: arr.length,
|
|
904
|
+
cols: arr[0]?.length ?? 0,
|
|
905
|
+
data: arr.flat(),
|
|
906
|
+
type: "DenseMatrix",
|
|
907
|
+
get: (r, c) => arr[r]?.[c]
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
},
|
|
911
|
+
{
|
|
912
|
+
from: "Matrix",
|
|
913
|
+
to: "Array",
|
|
914
|
+
convert: (m) => {
|
|
915
|
+
const matrix = m;
|
|
916
|
+
const result = [];
|
|
917
|
+
for (let i = 0; i < matrix.rows; i++) {
|
|
918
|
+
result[i] = [];
|
|
919
|
+
for (let j = 0; j < matrix.cols; j++) result[i][j] = matrix.get(i, j);
|
|
920
|
+
}
|
|
921
|
+
return result;
|
|
922
|
+
}
|
|
923
|
+
},
|
|
924
|
+
{
|
|
925
|
+
from: "DenseMatrix",
|
|
926
|
+
to: "SparseMatrix",
|
|
927
|
+
convert: (m) => {
|
|
928
|
+
return {
|
|
929
|
+
...m,
|
|
930
|
+
type: "SparseMatrix"
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
}
|
|
1022
934
|
];
|
|
1023
935
|
function createMathTSTyped() {
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
} else {
|
|
1030
|
-
instance.addTypes(MATHTS_TYPES, "any");
|
|
1031
|
-
}
|
|
1032
|
-
instance.addConversions(MATHTS_CONVERSIONS);
|
|
1033
|
-
return instance;
|
|
936
|
+
const instance = create();
|
|
937
|
+
if (typeof instance.addType === "function") for (const typeDef of MATHTS_TYPES) instance.addType(typeDef);
|
|
938
|
+
else instance.addTypes(MATHTS_TYPES, "any");
|
|
939
|
+
instance.addConversions(MATHTS_CONVERSIONS);
|
|
940
|
+
return instance;
|
|
1034
941
|
}
|
|
1035
|
-
|
|
942
|
+
const mathTyped = createMathTSTyped();
|
|
1036
943
|
var TypeRegistry = class {
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
*/
|
|
1082
|
-
build() {
|
|
1083
|
-
if (!this.instance) {
|
|
1084
|
-
const inst = create();
|
|
1085
|
-
inst.addTypes(Array.from(this.types.values()), "any");
|
|
1086
|
-
inst.addConversions(Array.from(this.conversions.values()));
|
|
1087
|
-
this.instance = inst;
|
|
1088
|
-
}
|
|
1089
|
-
return this.instance;
|
|
1090
|
-
}
|
|
1091
|
-
/**
|
|
1092
|
-
* Clear all registered types and conversions
|
|
1093
|
-
*/
|
|
1094
|
-
clear() {
|
|
1095
|
-
this.types.clear();
|
|
1096
|
-
this.conversions.clear();
|
|
1097
|
-
this.instance = null;
|
|
1098
|
-
}
|
|
944
|
+
types = /* @__PURE__ */ new Map();
|
|
945
|
+
conversions = /* @__PURE__ */ new Map();
|
|
946
|
+
instance = null;
|
|
947
|
+
registerType(name, test) {
|
|
948
|
+
this.types.set(name, {
|
|
949
|
+
name,
|
|
950
|
+
test
|
|
951
|
+
});
|
|
952
|
+
this.instance = null;
|
|
953
|
+
return this;
|
|
954
|
+
}
|
|
955
|
+
registerConversion(from, to, convert) {
|
|
956
|
+
const key = `${from}->${to}`;
|
|
957
|
+
this.conversions.set(key, {
|
|
958
|
+
from,
|
|
959
|
+
to,
|
|
960
|
+
convert
|
|
961
|
+
});
|
|
962
|
+
this.instance = null;
|
|
963
|
+
return this;
|
|
964
|
+
}
|
|
965
|
+
hasType(name) {
|
|
966
|
+
return this.types.has(name);
|
|
967
|
+
}
|
|
968
|
+
hasConversion(from, to) {
|
|
969
|
+
return this.conversions.has(`${from}->${to}`);
|
|
970
|
+
}
|
|
971
|
+
getTypeNames() {
|
|
972
|
+
return Array.from(this.types.keys());
|
|
973
|
+
}
|
|
974
|
+
build() {
|
|
975
|
+
if (!this.instance) {
|
|
976
|
+
const inst = create();
|
|
977
|
+
inst.addTypes(Array.from(this.types.values()), "any");
|
|
978
|
+
inst.addConversions(Array.from(this.conversions.values()));
|
|
979
|
+
this.instance = inst;
|
|
980
|
+
}
|
|
981
|
+
return this.instance;
|
|
982
|
+
}
|
|
983
|
+
clear() {
|
|
984
|
+
this.types.clear();
|
|
985
|
+
this.conversions.clear();
|
|
986
|
+
this.instance = null;
|
|
987
|
+
}
|
|
1099
988
|
};
|
|
1100
|
-
function createTypedFunction(
|
|
1101
|
-
|
|
989
|
+
function createTypedFunction(name, signatures, typedInstance = mathTyped) {
|
|
990
|
+
return typedInstance(name, signatures);
|
|
1102
991
|
}
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
992
|
+
//#endregion
|
|
993
|
+
//#region src/typed/type-bridge.ts
|
|
994
|
+
let registered = false;
|
|
1106
995
|
function registerNativeTypes() {
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
enumerable: false,
|
|
1146
|
-
configurable: false
|
|
1147
|
-
});
|
|
1148
|
-
}
|
|
1149
|
-
if (!("type" in BigNumber.prototype)) {
|
|
1150
|
-
Object.defineProperty(BigNumber.prototype, "type", {
|
|
1151
|
-
value: "BigNumber",
|
|
1152
|
-
writable: false,
|
|
1153
|
-
enumerable: false,
|
|
1154
|
-
configurable: false
|
|
1155
|
-
});
|
|
1156
|
-
}
|
|
996
|
+
if (registered) return;
|
|
997
|
+
registered = true;
|
|
998
|
+
if (!("isComplex" in Complex.prototype)) Object.defineProperty(Complex.prototype, "isComplex", {
|
|
999
|
+
value: true,
|
|
1000
|
+
writable: false,
|
|
1001
|
+
enumerable: false,
|
|
1002
|
+
configurable: false
|
|
1003
|
+
});
|
|
1004
|
+
if (!("type" in Complex.prototype)) Object.defineProperty(Complex.prototype, "type", {
|
|
1005
|
+
value: "Complex",
|
|
1006
|
+
writable: false,
|
|
1007
|
+
enumerable: false,
|
|
1008
|
+
configurable: false
|
|
1009
|
+
});
|
|
1010
|
+
if (!("isFraction" in Fraction.prototype)) Object.defineProperty(Fraction.prototype, "isFraction", {
|
|
1011
|
+
value: true,
|
|
1012
|
+
writable: false,
|
|
1013
|
+
enumerable: false,
|
|
1014
|
+
configurable: false
|
|
1015
|
+
});
|
|
1016
|
+
if (!("type" in Fraction.prototype)) Object.defineProperty(Fraction.prototype, "type", {
|
|
1017
|
+
value: "Fraction",
|
|
1018
|
+
writable: false,
|
|
1019
|
+
enumerable: false,
|
|
1020
|
+
configurable: false
|
|
1021
|
+
});
|
|
1022
|
+
if (!("isBigNumber" in BigNumber.prototype)) Object.defineProperty(BigNumber.prototype, "isBigNumber", {
|
|
1023
|
+
value: true,
|
|
1024
|
+
writable: false,
|
|
1025
|
+
enumerable: false,
|
|
1026
|
+
configurable: false
|
|
1027
|
+
});
|
|
1028
|
+
if (!("type" in BigNumber.prototype)) Object.defineProperty(BigNumber.prototype, "type", {
|
|
1029
|
+
value: "BigNumber",
|
|
1030
|
+
writable: false,
|
|
1031
|
+
enumerable: false,
|
|
1032
|
+
configurable: false
|
|
1033
|
+
});
|
|
1157
1034
|
}
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1035
|
+
//#endregion
|
|
1036
|
+
//#region src/factory/factory.ts
|
|
1037
|
+
const DEFAULT_CONFIG = {
|
|
1038
|
+
precision: 64,
|
|
1039
|
+
matrix: "Matrix",
|
|
1040
|
+
number: "number",
|
|
1041
|
+
randomSeed: null,
|
|
1042
|
+
epsilon: 1e-12,
|
|
1043
|
+
preferredBackend: "auto",
|
|
1044
|
+
wasmThreshold: 1e3,
|
|
1045
|
+
gpuThreshold: 1e5,
|
|
1046
|
+
parallelEnabled: true,
|
|
1047
|
+
parallelThreshold: 5e4
|
|
1171
1048
|
};
|
|
1172
1049
|
var FunctionRegistry = class {
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
return this.factories.has(name2);
|
|
1230
|
-
}
|
|
1231
|
-
/**
|
|
1232
|
-
* Get all registered function names
|
|
1233
|
-
*/
|
|
1234
|
-
names() {
|
|
1235
|
-
return Array.from(this.factories.keys());
|
|
1236
|
-
}
|
|
1237
|
-
/**
|
|
1238
|
-
* Update configuration
|
|
1239
|
-
*/
|
|
1240
|
-
updateConfig(config) {
|
|
1241
|
-
this.dependencies.config = { ...this.dependencies.config, ...config };
|
|
1242
|
-
this.instances.clear();
|
|
1243
|
-
}
|
|
1244
|
-
/**
|
|
1245
|
-
* Get current configuration
|
|
1246
|
-
*/
|
|
1247
|
-
getConfig() {
|
|
1248
|
-
return { ...this.dependencies.config };
|
|
1249
|
-
}
|
|
1050
|
+
factories = /* @__PURE__ */ new Map();
|
|
1051
|
+
instances = /* @__PURE__ */ new Map();
|
|
1052
|
+
dependencies;
|
|
1053
|
+
creating = /* @__PURE__ */ new Set();
|
|
1054
|
+
constructor(config = {}, typed = mathTyped) {
|
|
1055
|
+
this.dependencies = {
|
|
1056
|
+
config: {
|
|
1057
|
+
...DEFAULT_CONFIG,
|
|
1058
|
+
...config
|
|
1059
|
+
},
|
|
1060
|
+
typed
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
register(factory) {
|
|
1064
|
+
this.factories.set(factory.name, factory);
|
|
1065
|
+
}
|
|
1066
|
+
registerAll(factories) {
|
|
1067
|
+
for (const factory of factories) this.register(factory);
|
|
1068
|
+
}
|
|
1069
|
+
get(name) {
|
|
1070
|
+
const existing = this.instances.get(name);
|
|
1071
|
+
if (existing) return existing;
|
|
1072
|
+
if (this.creating.has(name)) throw new Error(`Circular dependency detected: ${name}`);
|
|
1073
|
+
const factory = this.factories.get(name);
|
|
1074
|
+
if (!factory) throw new Error(`Unknown function: ${name}`);
|
|
1075
|
+
this.creating.add(name);
|
|
1076
|
+
try {
|
|
1077
|
+
const deps = { ...this.dependencies };
|
|
1078
|
+
for (const depName of factory.dependencies) {
|
|
1079
|
+
if (depName === "config" || depName === "typed") continue;
|
|
1080
|
+
deps[depName] = this.get(depName);
|
|
1081
|
+
}
|
|
1082
|
+
const fn = factory.factory(deps);
|
|
1083
|
+
this.instances.set(name, fn);
|
|
1084
|
+
this.dependencies[name] = fn;
|
|
1085
|
+
return fn;
|
|
1086
|
+
} finally {
|
|
1087
|
+
this.creating.delete(name);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
has(name) {
|
|
1091
|
+
return this.factories.has(name);
|
|
1092
|
+
}
|
|
1093
|
+
names() {
|
|
1094
|
+
return Array.from(this.factories.keys());
|
|
1095
|
+
}
|
|
1096
|
+
updateConfig(config) {
|
|
1097
|
+
this.dependencies.config = {
|
|
1098
|
+
...this.dependencies.config,
|
|
1099
|
+
...config
|
|
1100
|
+
};
|
|
1101
|
+
this.instances.clear();
|
|
1102
|
+
}
|
|
1103
|
+
getConfig() {
|
|
1104
|
+
return { ...this.dependencies.config };
|
|
1105
|
+
}
|
|
1250
1106
|
};
|
|
1251
|
-
function createFactory(
|
|
1252
|
-
|
|
1107
|
+
function createFactory(name, dependencies, factory) {
|
|
1108
|
+
return {
|
|
1109
|
+
name,
|
|
1110
|
+
dependencies,
|
|
1111
|
+
factory
|
|
1112
|
+
};
|
|
1253
1113
|
}
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
/**
|
|
1261
|
-
* Register a factory function
|
|
1262
|
-
*/
|
|
1263
|
-
register: (factory2) => registry.register(factory2),
|
|
1264
|
-
/**
|
|
1265
|
-
* Get configuration
|
|
1266
|
-
*/
|
|
1267
|
-
config: () => registry.getConfig(),
|
|
1268
|
-
/**
|
|
1269
|
-
* Update configuration
|
|
1270
|
-
*/
|
|
1271
|
-
configure: (config) => registry.updateConfig(config)
|
|
1114
|
+
const registry = new FunctionRegistry();
|
|
1115
|
+
const math = {
|
|
1116
|
+
get: (name) => registry.get(name),
|
|
1117
|
+
register: (factory) => registry.register(factory),
|
|
1118
|
+
config: () => registry.getConfig(),
|
|
1119
|
+
configure: (config) => registry.updateConfig(config)
|
|
1272
1120
|
};
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1121
|
+
//#endregion
|
|
1122
|
+
//#region src/numeric/stable.ts
|
|
1123
|
+
const PAIRWISE_BLOCK = 128;
|
|
1276
1124
|
function pairwiseSum(xs, start = 0, end = xs.length) {
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1125
|
+
const n = end - start;
|
|
1126
|
+
if (n <= PAIRWISE_BLOCK) {
|
|
1127
|
+
let s0 = 0;
|
|
1128
|
+
let s1 = 0;
|
|
1129
|
+
let s2 = 0;
|
|
1130
|
+
let s3 = 0;
|
|
1131
|
+
let s4 = 0;
|
|
1132
|
+
let s5 = 0;
|
|
1133
|
+
let s6 = 0;
|
|
1134
|
+
let s7 = 0;
|
|
1135
|
+
let i = start;
|
|
1136
|
+
const limit = start + (n - n % 8);
|
|
1137
|
+
for (; i < limit; i += 8) {
|
|
1138
|
+
s0 += xs[i];
|
|
1139
|
+
s1 += xs[i + 1];
|
|
1140
|
+
s2 += xs[i + 2];
|
|
1141
|
+
s3 += xs[i + 3];
|
|
1142
|
+
s4 += xs[i + 4];
|
|
1143
|
+
s5 += xs[i + 5];
|
|
1144
|
+
s6 += xs[i + 6];
|
|
1145
|
+
s7 += xs[i + 7];
|
|
1146
|
+
}
|
|
1147
|
+
let tail = 0;
|
|
1148
|
+
for (; i < end; i++) tail += xs[i];
|
|
1149
|
+
return s0 + s1 + (s2 + s3) + (s4 + s5 + (s6 + s7)) + tail;
|
|
1150
|
+
}
|
|
1151
|
+
const half = (n >> 1) - (n >> 1) % 8;
|
|
1152
|
+
return pairwiseSum(xs, start, start + half) + pairwiseSum(xs, start + half, end);
|
|
1305
1153
|
}
|
|
1306
1154
|
function neumaierSum(xs) {
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1155
|
+
let sum = 0;
|
|
1156
|
+
let c = 0;
|
|
1157
|
+
for (let i = 0; i < xs.length; i++) {
|
|
1158
|
+
const x = xs[i];
|
|
1159
|
+
const t = sum + x;
|
|
1160
|
+
c += Math.abs(sum) >= Math.abs(x) ? sum - t + x : x - t + sum;
|
|
1161
|
+
sum = t;
|
|
1162
|
+
}
|
|
1163
|
+
return sum + c;
|
|
1316
1164
|
}
|
|
1317
1165
|
function norm2(xs) {
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1166
|
+
let scale = 0;
|
|
1167
|
+
let ssq = 1;
|
|
1168
|
+
for (let i = 0; i < xs.length; i++) {
|
|
1169
|
+
const x = xs[i];
|
|
1170
|
+
if (x === 0) continue;
|
|
1171
|
+
if (Number.isNaN(x)) return NaN;
|
|
1172
|
+
const ax = Math.abs(x);
|
|
1173
|
+
if (scale < ax) {
|
|
1174
|
+
const r = scale / ax;
|
|
1175
|
+
ssq = 1 + ssq * r * r;
|
|
1176
|
+
scale = ax;
|
|
1177
|
+
} else {
|
|
1178
|
+
const r = ax / scale;
|
|
1179
|
+
ssq += r * r;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
return scale === 0 ? 0 : scale * Math.sqrt(ssq);
|
|
1335
1183
|
}
|
|
1336
1184
|
function pairwiseDot(a, b, start = 0, end = a.length) {
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1185
|
+
const n = end - start;
|
|
1186
|
+
if (n <= PAIRWISE_BLOCK) {
|
|
1187
|
+
let s0 = 0;
|
|
1188
|
+
let s1 = 0;
|
|
1189
|
+
let s2 = 0;
|
|
1190
|
+
let s3 = 0;
|
|
1191
|
+
let s4 = 0;
|
|
1192
|
+
let s5 = 0;
|
|
1193
|
+
let s6 = 0;
|
|
1194
|
+
let s7 = 0;
|
|
1195
|
+
let i = start;
|
|
1196
|
+
const limit = start + (n - n % 8);
|
|
1197
|
+
for (; i < limit; i += 8) {
|
|
1198
|
+
s0 += a[i] * b[i];
|
|
1199
|
+
s1 += a[i + 1] * b[i + 1];
|
|
1200
|
+
s2 += a[i + 2] * b[i + 2];
|
|
1201
|
+
s3 += a[i + 3] * b[i + 3];
|
|
1202
|
+
s4 += a[i + 4] * b[i + 4];
|
|
1203
|
+
s5 += a[i + 5] * b[i + 5];
|
|
1204
|
+
s6 += a[i + 6] * b[i + 6];
|
|
1205
|
+
s7 += a[i + 7] * b[i + 7];
|
|
1206
|
+
}
|
|
1207
|
+
let tail = 0;
|
|
1208
|
+
for (; i < end; i++) tail += a[i] * b[i];
|
|
1209
|
+
return s0 + s1 + (s2 + s3) + (s4 + s5 + (s6 + s7)) + tail;
|
|
1210
|
+
}
|
|
1211
|
+
const half = (n >> 1) - (n >> 1) % 8;
|
|
1212
|
+
return pairwiseDot(a, b, start, start + half) + pairwiseDot(a, b, start + half, end);
|
|
1365
1213
|
}
|
|
1366
1214
|
function scaledDistance(a, b) {
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1215
|
+
let scale = 0;
|
|
1216
|
+
let ssq = 1;
|
|
1217
|
+
for (let i = 0; i < a.length; i++) {
|
|
1218
|
+
const d = a[i] - b[i];
|
|
1219
|
+
if (d === 0) continue;
|
|
1220
|
+
if (Number.isNaN(d)) return NaN;
|
|
1221
|
+
const ad = Math.abs(d);
|
|
1222
|
+
if (scale < ad) {
|
|
1223
|
+
const r = scale / ad;
|
|
1224
|
+
ssq = 1 + ssq * r * r;
|
|
1225
|
+
scale = ad;
|
|
1226
|
+
} else {
|
|
1227
|
+
const r = ad / scale;
|
|
1228
|
+
ssq += r * r;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
return scale === 0 ? 0 : scale * Math.sqrt(ssq);
|
|
1384
1232
|
}
|
|
1385
1233
|
function sumSquaredDeviations(xs) {
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
return corrected < 0 ? 0 : corrected;
|
|
1234
|
+
const n = xs.length;
|
|
1235
|
+
if (n < 2) return 0;
|
|
1236
|
+
const { sumD, sumDD } = pairwiseDeviationSums(xs, pairwiseSum(xs) / n, 0, n);
|
|
1237
|
+
const corrected = sumDD - sumD * sumD / n;
|
|
1238
|
+
return corrected < 0 ? 0 : corrected;
|
|
1392
1239
|
}
|
|
1393
1240
|
function pairwiseDeviationSums(xs, mean, start, end) {
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1241
|
+
const n = end - start;
|
|
1242
|
+
if (n <= PAIRWISE_BLOCK) {
|
|
1243
|
+
let d0 = 0, d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0, d7 = 0;
|
|
1244
|
+
let q0 = 0, q1 = 0, q2 = 0, q3 = 0, q4 = 0, q5 = 0, q6 = 0, q7 = 0;
|
|
1245
|
+
let i = start;
|
|
1246
|
+
const limit = start + (n - n % 8);
|
|
1247
|
+
for (; i < limit; i += 8) {
|
|
1248
|
+
const a0 = xs[i] - mean;
|
|
1249
|
+
const a1 = xs[i + 1] - mean;
|
|
1250
|
+
const a2 = xs[i + 2] - mean;
|
|
1251
|
+
const a3 = xs[i + 3] - mean;
|
|
1252
|
+
const a4 = xs[i + 4] - mean;
|
|
1253
|
+
const a5 = xs[i + 5] - mean;
|
|
1254
|
+
const a6 = xs[i + 6] - mean;
|
|
1255
|
+
const a7 = xs[i + 7] - mean;
|
|
1256
|
+
d0 += a0;
|
|
1257
|
+
q0 += a0 * a0;
|
|
1258
|
+
d1 += a1;
|
|
1259
|
+
q1 += a1 * a1;
|
|
1260
|
+
d2 += a2;
|
|
1261
|
+
q2 += a2 * a2;
|
|
1262
|
+
d3 += a3;
|
|
1263
|
+
q3 += a3 * a3;
|
|
1264
|
+
d4 += a4;
|
|
1265
|
+
q4 += a4 * a4;
|
|
1266
|
+
d5 += a5;
|
|
1267
|
+
q5 += a5 * a5;
|
|
1268
|
+
d6 += a6;
|
|
1269
|
+
q6 += a6 * a6;
|
|
1270
|
+
d7 += a7;
|
|
1271
|
+
q7 += a7 * a7;
|
|
1272
|
+
}
|
|
1273
|
+
let tailD = 0;
|
|
1274
|
+
let tailDD = 0;
|
|
1275
|
+
for (; i < end; i++) {
|
|
1276
|
+
const a = xs[i] - mean;
|
|
1277
|
+
tailD += a;
|
|
1278
|
+
tailDD += a * a;
|
|
1279
|
+
}
|
|
1280
|
+
return {
|
|
1281
|
+
sumD: d0 + d1 + (d2 + d3) + (d4 + d5 + (d6 + d7)) + tailD,
|
|
1282
|
+
sumDD: q0 + q1 + (q2 + q3) + (q4 + q5 + (q6 + q7)) + tailDD
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
const half = (n >> 1) - (n >> 1) % 8;
|
|
1286
|
+
const left = pairwiseDeviationSums(xs, mean, start, start + half);
|
|
1287
|
+
const right = pairwiseDeviationSums(xs, mean, start + half, end);
|
|
1288
|
+
return {
|
|
1289
|
+
sumD: left.sumD + right.sumD,
|
|
1290
|
+
sumDD: left.sumDD + right.sumDD
|
|
1291
|
+
};
|
|
1442
1292
|
}
|
|
1443
1293
|
function neumaierCumsum(xs, out) {
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1294
|
+
let sum = 0;
|
|
1295
|
+
let c = 0;
|
|
1296
|
+
for (let i = 0; i < xs.length; i++) {
|
|
1297
|
+
const x = xs[i];
|
|
1298
|
+
const t = sum + x;
|
|
1299
|
+
c += Math.abs(sum) >= Math.abs(x) ? sum - t + x : x - t + sum;
|
|
1300
|
+
sum = t;
|
|
1301
|
+
out[i] = sum + c;
|
|
1302
|
+
}
|
|
1453
1303
|
}
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
BASE_UNITS,
|
|
1460
|
-
BEST_PREFIXES,
|
|
1461
|
-
BIGNUMBER_E,
|
|
1462
|
-
BIGNUMBER_LN10,
|
|
1463
|
-
BIGNUMBER_LN2,
|
|
1464
|
-
BIGNUMBER_NEG_ONE,
|
|
1465
|
-
BIGNUMBER_ONE,
|
|
1466
|
-
BIGNUMBER_PI,
|
|
1467
|
-
BIGNUMBER_TEN,
|
|
1468
|
-
BIGNUMBER_ZERO,
|
|
1469
|
-
BigNumber,
|
|
1470
|
-
COMPLEX_NEG_ONE,
|
|
1471
|
-
COMPLEX_ONE,
|
|
1472
|
-
COMPLEX_ZERO,
|
|
1473
|
-
Complex,
|
|
1474
|
-
DEFAULT_CONFIG,
|
|
1475
|
-
DERIVED_UNITS,
|
|
1476
|
-
DIMENSIONLESS,
|
|
1477
|
-
DUAL_UNARY_RULES,
|
|
1478
|
-
DimensionMismatchError,
|
|
1479
|
-
Dual,
|
|
1480
|
-
E,
|
|
1481
|
-
FRACTION_HALF,
|
|
1482
|
-
FRACTION_NEG_ONE,
|
|
1483
|
-
FRACTION_ONE,
|
|
1484
|
-
FRACTION_QUARTER,
|
|
1485
|
-
FRACTION_THIRD,
|
|
1486
|
-
FRACTION_ZERO,
|
|
1487
|
-
Fraction,
|
|
1488
|
-
FunctionRegistry,
|
|
1489
|
-
I,
|
|
1490
|
-
LN10,
|
|
1491
|
-
LN2,
|
|
1492
|
-
LN2_EXACT_SCALE,
|
|
1493
|
-
LOG10E,
|
|
1494
|
-
LOG2E,
|
|
1495
|
-
MATHTS_CONVERSIONS,
|
|
1496
|
-
MATHTS_TYPES,
|
|
1497
|
-
NAMED_IRRATIONALS,
|
|
1498
|
-
NAMED_SCALES,
|
|
1499
|
-
PHI,
|
|
1500
|
-
PI,
|
|
1501
|
-
PI_EXACT_SCALE,
|
|
1502
|
-
Range,
|
|
1503
|
-
SI_DIMENSION_LENGTH,
|
|
1504
|
-
SI_PREFIXES,
|
|
1505
|
-
SQRT1_2,
|
|
1506
|
-
SQRT2,
|
|
1507
|
-
TAU,
|
|
1508
|
-
TypeRegistry,
|
|
1509
|
-
UNIT_ALIASES,
|
|
1510
|
-
UNIT_DIMENSION_LENGTH,
|
|
1511
|
-
UNIT_EXACT_SCALE,
|
|
1512
|
-
UNIT_PREFIX_SETS,
|
|
1513
|
-
UNIT_ROWS,
|
|
1514
|
-
UNIT_ROW_ALIASES,
|
|
1515
|
-
Unit,
|
|
1516
|
-
UnitParseError,
|
|
1517
|
-
VERSION,
|
|
1518
|
-
abs,
|
|
1519
|
-
addExactScales,
|
|
1520
|
-
addScalar,
|
|
1521
|
-
binaryExactScale,
|
|
1522
|
-
clearExactScaleNumberCache,
|
|
1523
|
-
create,
|
|
1524
|
-
createFactory,
|
|
1525
|
-
createMathTSTyped,
|
|
1526
|
-
createRangeClass,
|
|
1527
|
-
createTypedFunction,
|
|
1528
|
-
decimalExactScale,
|
|
1529
|
-
dim,
|
|
1530
|
-
divideExactScales,
|
|
1531
|
-
divideScalar,
|
|
1532
|
-
equal,
|
|
1533
|
-
exactScaleNumberCacheSize,
|
|
1534
|
-
exactScaleOf,
|
|
1535
|
-
exactScaleToNumber,
|
|
1536
|
-
exactScalesEqual,
|
|
1537
|
-
fix,
|
|
1538
|
-
formatExactScale,
|
|
1539
|
-
fromSiDimensionVector,
|
|
1540
|
-
fromSiDimensions,
|
|
1541
|
-
getPrefix,
|
|
1542
|
-
getUnitDef,
|
|
1543
|
-
getUnitPrefix,
|
|
1544
|
-
getUnitRow,
|
|
1545
|
-
initTypedWasm,
|
|
1546
|
-
irrationalPower,
|
|
1547
|
-
isArray,
|
|
1548
|
-
isBigInt2 as isBigInt,
|
|
1549
|
-
isBigNumber2 as isBigNumber,
|
|
1550
|
-
isBoolean,
|
|
1551
|
-
isComplex,
|
|
1552
|
-
isDenseMatrix,
|
|
1553
|
-
isDual,
|
|
1554
|
-
isFraction,
|
|
1555
|
-
isFunction,
|
|
1556
|
-
isMatrix,
|
|
1557
|
-
isNull,
|
|
1558
|
-
isNumber,
|
|
1559
|
-
isNumeric,
|
|
1560
|
-
isObject,
|
|
1561
|
-
isRationalScale,
|
|
1562
|
-
isSparseMatrix,
|
|
1563
|
-
isString,
|
|
1564
|
-
isTypedWasmAvailable,
|
|
1565
|
-
isUndefined,
|
|
1566
|
-
isUnit2 as isUnit,
|
|
1567
|
-
isUnit as isUnitValue,
|
|
1568
|
-
math,
|
|
1569
|
-
mathTyped,
|
|
1570
|
-
multiplyExactScales,
|
|
1571
|
-
multiplyScalar,
|
|
1572
|
-
namedExactScale,
|
|
1573
|
-
namedIrrationalScale,
|
|
1574
|
-
neumaierCumsum,
|
|
1575
|
-
neumaierSum,
|
|
1576
|
-
norm2,
|
|
1577
|
-
number,
|
|
1578
|
-
pairwiseDot,
|
|
1579
|
-
pairwiseSum,
|
|
1580
|
-
pow,
|
|
1581
|
-
powerExactScale,
|
|
1582
|
-
ratioExactScale,
|
|
1583
|
-
readScaleExpression,
|
|
1584
|
-
readUnitScale,
|
|
1585
|
-
registerNativeTypes,
|
|
1586
|
-
registry,
|
|
1587
|
-
round,
|
|
1588
|
-
scaledDistance,
|
|
1589
|
-
subtractScalar,
|
|
1590
|
-
sumSquaredDeviations,
|
|
1591
|
-
toSiDimensionVector,
|
|
1592
|
-
toSiDimensions,
|
|
1593
|
-
typed,
|
|
1594
|
-
unitPrefixExactScale,
|
|
1595
|
-
unitRowExactOffset,
|
|
1596
|
-
unitRowExactScale,
|
|
1597
|
-
unitRowValue
|
|
1598
|
-
};
|
|
1304
|
+
//#endregion
|
|
1305
|
+
//#region src/index.ts
|
|
1306
|
+
const VERSION = "0.19.0";
|
|
1307
|
+
//#endregion
|
|
1308
|
+
export { ALL_UNITS, BASE_UNITS, BEST_PREFIXES, BIGNUMBER_E, BIGNUMBER_LN10, BIGNUMBER_LN2, BIGNUMBER_NEG_ONE, BIGNUMBER_ONE, BIGNUMBER_PI, BIGNUMBER_TEN, BIGNUMBER_ZERO, BigNumber, COMPLEX_NEG_ONE, COMPLEX_ONE, COMPLEX_ZERO, Complex, DEFAULT_CONFIG, DERIVED_UNITS, DIMENSIONLESS, DUAL_UNARY_RULES, DimensionMismatchError, Dual, E, FRACTION_HALF, FRACTION_NEG_ONE, FRACTION_ONE, FRACTION_QUARTER, FRACTION_THIRD, FRACTION_ZERO, Fraction, FunctionRegistry, I, LN10, LN2, LN2_EXACT_SCALE, LOG10E, LOG2E, MATHTS_CONVERSIONS, MATHTS_TYPES, NAMED_IRRATIONALS, NAMED_SCALES, PHI, PI, PI_EXACT_SCALE, Range, SI_DIMENSION_LENGTH, SI_PREFIXES, SQRT1_2, SQRT2, TAU, TypeRegistry, UNIT_ALIASES, UNIT_DIMENSION_LENGTH, UNIT_EXACT_SCALE, UNIT_PREFIX_SETS, UNIT_ROWS, UNIT_ROW_ALIASES, Unit, UnitParseError, VERSION, abs, addExactScales, addScalar, binaryExactScale, clearExactScaleNumberCache, create, createFactory, createMathTSTyped, createRangeClass, createTypedFunction, decimalExactScale, dim, divideExactScales, divideScalar, equal, exactScaleNumberCacheSize, exactScaleOf, exactScaleToNumber, exactScalesEqual, fix, formatExactScale, fromSiDimensionVector, fromSiDimensions, getPrefix, getUnitDef, getUnitPrefix, getUnitRow, initTypedWasm, irrationalPower, isArray, isBigInt, isBigNumber, isBoolean, isComplex, isDenseMatrix, isDual, isFraction, isFunction, isMatrix, isNull, isNumber, isNumeric, isObject, isRationalScale, isSparseMatrix, isString, isTypedWasmAvailable, isUndefined, isUnit, isUnit$1 as isUnitValue, math, mathTyped, multiplyExactScales, multiplyScalar, namedExactScale, namedIrrationalScale, neumaierCumsum, neumaierSum, norm2, number, pairwiseDot, pairwiseSum, pow, powerExactScale, ratioExactScale, readScaleExpression, readUnitScale, registerNativeTypes, registry, round, scaledDistance, subtractScalar, sumSquaredDeviations, toSiDimensionVector, toSiDimensions, typed, unitPrefixExactScale, unitRowExactOffset, unitRowExactScale, unitRowValue };
|