@danielsimonjr/mathts-core 0.18.1 → 0.20.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.
Files changed (43) hide show
  1. package/README.md +30 -5
  2. package/dist/collection.d.ts +35 -23
  3. package/dist/collection.d.ts.map +1 -1
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1348 -1534
  7. package/dist/internal.d.ts +3 -2
  8. package/dist/internal.d.ts.map +1 -1
  9. package/dist/internal.js +1021 -1514
  10. package/dist/numeric/rng.d.ts +88 -0
  11. package/dist/numeric/rng.d.ts.map +1 -0
  12. package/dist/numeric/strides.d.ts +18 -0
  13. package/dist/numeric/strides.d.ts.map +1 -0
  14. package/dist/object.d.ts +20 -2
  15. package/dist/object.d.ts.map +1 -1
  16. package/dist/shared.d.ts +19 -0
  17. package/dist/shared.d.ts.map +1 -1
  18. package/dist/switch.d.ts +2 -4
  19. package/dist/switch.d.ts.map +1 -1
  20. package/dist/types/bignumber.d.ts +7 -0
  21. package/dist/types/bignumber.d.ts.map +1 -1
  22. package/dist/types/complex.d.ts +20 -18
  23. package/dist/types/complex.d.ts.map +1 -1
  24. package/dist/types/fraction.d.ts +5 -0
  25. package/dist/types/fraction.d.ts.map +1 -1
  26. package/dist/types/si-dimension-vector.d.ts +62 -7
  27. package/dist/types/si-dimension-vector.d.ts.map +1 -1
  28. package/dist/types/unit/Unit.d.ts.map +1 -1
  29. package/dist/types/unit/index.d.ts +1 -1
  30. package/dist/types/unit/index.d.ts.map +1 -1
  31. package/dist/types/unit/unit-table.d.ts +8 -2
  32. package/dist/types/unit/unit-table.d.ts.map +1 -1
  33. package/dist/types/unit/unit-types.d.ts +10 -0
  34. package/dist/types/unit/unit-types.d.ts.map +1 -1
  35. package/dist/types/unit.d.ts +1 -1
  36. package/dist/types/unit.d.ts.map +1 -1
  37. package/dist/unit-Dul_E1rh.js +5946 -0
  38. package/dist/wasm-as-header.d.ts +17 -0
  39. package/dist/wasm-as-header.d.ts.map +1 -0
  40. package/dist/wasm-loader.d.ts +33 -1
  41. package/dist/wasm-loader.d.ts.map +1 -1
  42. package/package.json +6 -5
  43. package/dist/chunk-36FOYJAP.js +0 -6091
package/dist/index.js CHANGED
@@ -1,1598 +1,1412 @@
1
- import {
2
- BIGNUMBER_E,
3
- BIGNUMBER_LN10,
4
- BIGNUMBER_LN2,
5
- BIGNUMBER_NEG_ONE,
6
- BIGNUMBER_ONE,
7
- BIGNUMBER_PI,
8
- BIGNUMBER_TEN,
9
- BIGNUMBER_ZERO,
10
- BigNumber,
11
- COMPLEX_NEG_ONE,
12
- COMPLEX_ONE,
13
- COMPLEX_ZERO,
14
- Complex,
15
- DimensionMismatchError,
16
- FRACTION_HALF,
17
- FRACTION_NEG_ONE,
18
- FRACTION_ONE,
19
- FRACTION_QUARTER,
20
- FRACTION_THIRD,
21
- FRACTION_ZERO,
22
- Fraction,
23
- I,
24
- LN2_EXACT_SCALE,
25
- NAMED_IRRATIONALS,
26
- NAMED_SCALES,
27
- PI_EXACT_SCALE,
28
- UNIT_EXACT_SCALE,
29
- UNIT_PREFIX_SETS,
30
- UNIT_ROWS,
31
- UNIT_ROW_ALIASES,
32
- Unit,
33
- UnitParseError,
34
- abs,
35
- addExactScales,
36
- addScalar,
37
- binaryExactScale,
38
- clearExactScaleNumberCache,
39
- decimalExactScale,
40
- divideExactScales,
41
- divideScalar,
42
- equal,
43
- exactScaleNumberCacheSize,
44
- exactScaleOf,
45
- exactScaleToNumber,
46
- exactScalesEqual,
47
- factory,
48
- fix,
49
- format,
50
- formatExactScale,
51
- getUnitPrefix,
52
- getUnitRow,
53
- irrationalPower,
54
- isBigInt,
55
- isBigNumber,
56
- isBigNumber2,
57
- isComplex2 as isComplex,
58
- isFraction2 as isFraction,
59
- isNumeric,
60
- isRationalScale,
61
- isUnit,
62
- multiplyExactScales,
63
- multiplyScalar,
64
- namedExactScale,
65
- namedIrrationalScale,
66
- nearlyEqual,
67
- number,
68
- pow,
69
- powerExactScale,
70
- ratioExactScale,
71
- readScaleExpression,
72
- readUnitScale,
73
- round,
74
- sign,
75
- subtractScalar,
76
- unitPrefixExactScale,
77
- unitRowExactOffset,
78
- unitRowExactScale,
79
- unitRowValue
80
- } from "./chunk-36FOYJAP.js";
81
-
82
- // src/types/matrix/Range.ts
83
- var name = "Range";
84
- var dependencies = [];
85
- var createRangeClass = /* @__PURE__ */ factory(
86
- name,
87
- dependencies,
88
- () => {
89
- class Range2 {
90
- /**
91
- * Type identifier
92
- */
93
- type = "Range";
94
- /**
95
- * Range type flag
96
- */
97
- isRange = true;
98
- /**
99
- * Start value of the range (inclusive)
100
- */
101
- start;
102
- /**
103
- * End value of the range (exclusive)
104
- */
105
- end;
106
- /**
107
- * Step size for the range
108
- */
109
- step;
110
- /**
111
- * Cached primitive array representation.
112
- *
113
- * @internal Not part of the public API.
114
- *
115
- * These four were ES-private (`#cache`) until TypeScript 7. This class is
116
- * returned from a `factory()` call, so declaration emit describes it as an
117
- * ANONYMOUS class type -- and TS4094 forbids private or protected members
118
- * there, because an anonymous type has no name to refer them to. The
119
- * underscore prefix keeps the "do not touch" signal without the emit
120
- * failure. Restore `#` if the class is ever hoisted out of the factory.
121
- */
122
- _cache = null;
123
- _cacheStart;
124
- _cacheEnd;
125
- _cacheStep;
126
- /**
127
- * Create a range from start to end with a step.
128
- *
129
- * @param start - Included lower bound. Default is 0.
130
- * @param end - Excluded upper bound. Default is 0.
131
- * @param step - Optional step size. Default is 1.
132
- * @throws TypeError when start, end or step is not a number, bigint or BigNumber.
133
- * @throws Error when step is zero.
134
- */
135
- constructor(start, end, step) {
136
- if (!(this instanceof Range2)) {
137
- throw new SyntaxError("Constructor must be called with the new operator");
138
- }
139
- const hasStart = start !== null && start !== void 0;
140
- const hasEnd = end !== null && end !== void 0;
141
- const hasStep = step !== null && step !== void 0;
142
- let startValue = 0;
143
- let endValue = 0;
144
- let stepValue = 1;
145
- if (hasStart) {
146
- if (isBigNumber(start)) {
147
- startValue = start.toNumber();
148
- } else if (typeof start !== "number" && !isBigInt(start)) {
149
- throw new TypeError("Parameter start must be a number or bigint");
150
- } else {
151
- startValue = start;
152
- }
153
- }
154
- if (hasEnd) {
155
- if (isBigNumber(end)) {
156
- endValue = end.toNumber();
157
- } else if (typeof end !== "number" && !isBigInt(end)) {
158
- throw new TypeError("Parameter end must be a number or bigint");
159
- } else {
160
- endValue = end;
161
- }
162
- }
163
- if (hasStep) {
164
- if (isBigNumber(step)) {
165
- stepValue = step.toNumber();
166
- } else if (typeof step !== "number" && !isBigInt(step)) {
167
- throw new TypeError("Parameter step must be a number or bigint");
168
- } else {
169
- stepValue = step;
170
- }
171
- }
172
- this.start = hasStart ? parseFloat(startValue.toString()) : 0;
173
- this.end = hasEnd ? parseFloat(endValue.toString()) : 0;
174
- this.step = hasStep ? parseFloat(stepValue.toString()) : 1;
175
- if (hasStep && nearlyEqual(this.step, 0)) {
176
- throw new Error("Step must not be zero");
177
- }
178
- }
179
- /**
180
- * Parse a string into a range,
181
- * The string contains the start, optional step, and end, separated by a colon.
182
- * If the string does not contain a valid range, null is returned.
183
- * For example str='0:2:11'.
184
- * @memberof Range
185
- * @param str
186
- * @return range
187
- */
188
- static parse(str) {
189
- if (typeof str !== "string") {
190
- return null;
191
- }
192
- const args = str.split(":");
193
- const nums = args.map(function(arg) {
194
- return parseFloat(arg);
195
- });
196
- const invalid = nums.some(function(num) {
197
- return isNaN(num);
198
- });
199
- if (invalid) {
200
- return null;
201
- }
202
- switch (nums.length) {
203
- case 2:
204
- return new Range2(nums[0], nums[1]);
205
- case 3:
206
- return new Range2(nums[0], nums[2], nums[1]);
207
- default:
208
- return null;
209
- }
210
- }
211
- /**
212
- * Create a clone of the range
213
- * @return clone
214
- */
215
- clone() {
216
- return new Range2(this.start, this.end, this.step);
217
- }
218
- /**
219
- * Retrieve the size of the range.
220
- * Returns an array containing one number, the number of elements in the range.
221
- * @memberof Range
222
- * @returns size
223
- */
224
- size() {
225
- let len = 0;
226
- const start = this.start;
227
- const step = this.step;
228
- const end = this.end;
229
- const diff = end - start;
230
- if (sign(step) === sign(diff)) {
231
- len = Math.ceil(diff / step);
232
- } else if (diff === 0) {
233
- len = 0;
234
- }
235
- if (isNaN(len)) {
236
- len = 0;
237
- }
238
- return [len];
239
- }
240
- /**
241
- * Calculate the minimum value in the range
242
- * @memberof Range
243
- * @return min
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-Dul_E1rh.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
- // src/types/dual.ts
429
- var Dual = class _Dual {
430
- /** Function value (the real part). */
431
- value;
432
- /** First derivative (the ε coefficient). */
433
- deriv;
434
- constructor(value, deriv = 0) {
435
- this.value = value;
436
- this.deriv = deriv;
437
- }
438
- /** A constant (derivative 0). */
439
- static constant(v) {
440
- return new _Dual(v, 0);
441
- }
442
- /** A seed variable (derivative 1) — the point of differentiation. */
443
- static variable(v) {
444
- return new _Dual(v, 1);
445
- }
446
- // --- arithmetic ----------------------------------------------------------
447
- add(o) {
448
- return new _Dual(this.value + o.value, this.deriv + o.deriv);
449
- }
450
- sub(o) {
451
- return new _Dual(this.value - o.value, this.deriv - o.deriv);
452
- }
453
- mul(o) {
454
- return new _Dual(this.value * o.value, this.deriv * o.value + this.value * o.deriv);
455
- }
456
- div(o) {
457
- const d = (this.deriv * o.value - this.value * o.deriv) / (o.value * o.value);
458
- return new _Dual(this.value / o.value, d);
459
- }
460
- neg() {
461
- return new _Dual(-this.value, -this.deriv);
462
- }
463
- /** Power with a constant real exponent: d(xᵏ) = k·xᵏ⁻¹·dx. */
464
- powConst(k) {
465
- return new _Dual(Math.pow(this.value, k), this.deriv * k * Math.pow(this.value, k - 1));
466
- }
467
- /** General power a^b: d = a^b·(b'·ln a + b·a'/a). */
468
- pow(o) {
469
- const v = Math.pow(this.value, o.value);
470
- const d = v * (o.deriv * Math.log(this.value) + o.value * this.deriv / this.value);
471
- return new _Dual(v, d);
472
- }
473
- // --- elementary functions ------------------------------------------------
474
- // Each applies the shared chain rule (value, deriv) ↦ (f(v), deriv·f′(v))
475
- // using the canonical rule from `DUAL_UNARY_RULES`, so the scalar and tensor
476
- // AD paths can never drift apart.
477
- unary(rule) {
478
- const y = rule.primal(this.value);
479
- return new _Dual(y, this.deriv * rule.deriv(this.value, y));
480
- }
481
- sin() {
482
- return this.unary(DUAL_UNARY_RULES.sin);
483
- }
484
- cos() {
485
- return this.unary(DUAL_UNARY_RULES.cos);
486
- }
487
- tan() {
488
- return this.unary(DUAL_UNARY_RULES.tan);
489
- }
490
- exp() {
491
- return this.unary(DUAL_UNARY_RULES.exp);
492
- }
493
- log() {
494
- return this.unary(DUAL_UNARY_RULES.log);
495
- }
496
- sqrt() {
497
- return this.unary(DUAL_UNARY_RULES.sqrt);
498
- }
499
- square() {
500
- return this.unary(DUAL_UNARY_RULES.square);
501
- }
502
- abs() {
503
- return this.unary(DUAL_UNARY_RULES.abs);
504
- }
505
- sinh() {
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
- return x instanceof Dual;
326
+ return x instanceof Dual;
520
327
  }
521
-
522
- // src/constants.ts
523
- var PI = Math.PI;
524
- var E = Math.E;
525
- var TAU = 2 * Math.PI;
526
- var PHI = (1 + Math.sqrt(5)) / 2;
527
- var SQRT2 = Math.SQRT2;
528
- var SQRT1_2 = Math.SQRT1_2;
529
- var LN2 = Math.LN2;
530
- var LN10 = Math.LN10;
531
- var LOG2E = Math.LOG2E;
532
- var LOG10E = Math.LOG10E;
533
-
534
- // src/types/unit-definitions.ts
535
- var DIMENSIONLESS = Object.freeze({
536
- length: 0,
537
- mass: 0,
538
- time: 0,
539
- current: 0,
540
- temperature: 0,
541
- amount: 0,
542
- luminosity: 0
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
- return {
546
- length: partial.length ?? 0,
547
- mass: partial.mass ?? 0,
548
- time: partial.time ?? 0,
549
- current: partial.current ?? 0,
550
- temperature: partial.temperature ?? 0,
551
- amount: partial.amount ?? 0,
552
- luminosity: partial.luminosity ?? 0
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
- var BASE_UNITS = {
556
- m: { multiplier: 1, dimensions: dim({ length: 1 }), prefixable: true },
557
- // The base unit of mass is *kilogram*, but the prefix-able symbol is "g".
558
- // Internally we treat 1 kg as the canonical value, so "g" -> 0.001 kg.
559
- kg: { multiplier: 1, dimensions: dim({ mass: 1 }) },
560
- s: { multiplier: 1, dimensions: dim({ time: 1 }), prefixable: true },
561
- A: { multiplier: 1, dimensions: dim({ current: 1 }), prefixable: true },
562
- K: { multiplier: 1, dimensions: dim({ temperature: 1 }), prefixable: true },
563
- mol: { multiplier: 1, dimensions: dim({ amount: 1 }), prefixable: true },
564
- cd: { multiplier: 1, dimensions: dim({ luminosity: 1 }), prefixable: true }
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(name2, dimensions, prefixable) {
567
- const row = getUnitRow(name2);
568
- if (row === void 0) throw new Error(`unit-definitions: '${name2}' is not in the unit table`);
569
- const scale = readUnitScale(row.scale);
570
- const def = { multiplier: exactScaleToNumber(scale), dimensions };
571
- if (row.offset !== void 0) {
572
- def.offset = exactScaleToNumber(multiplyExactScales(scale, readUnitScale(row.offset)));
573
- }
574
- if (prefixable) def.prefixable = true;
575
- return def;
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
- var DERIVED_UNITS = {
578
- // --- Length ---------------------------------------------------------------
579
- // Note: 'm' is a base unit (above), already prefixable.
580
- ft: tableUnit("ft", dim({ length: 1 })),
581
- foot: tableUnit("foot", dim({ length: 1 })),
582
- in: tableUnit("in", dim({ length: 1 })),
583
- inch: tableUnit("inch", dim({ length: 1 })),
584
- yd: tableUnit("yd", dim({ length: 1 })),
585
- yard: tableUnit("yard", dim({ length: 1 })),
586
- mi: tableUnit("mi", dim({ length: 1 })),
587
- mile: tableUnit("mile", dim({ length: 1 })),
588
- // --- Mass -----------------------------------------------------------------
589
- // The "g" entry is gram (0.001 kg). It is prefixable, so kg/mg/µg work via
590
- // prefix application on this entry rather than the base "kg".
591
- g: tableUnit("g", dim({ mass: 1 }), true),
592
- lb: tableUnit("lb", dim({ mass: 1 })),
593
- lbm: tableUnit("lbm", dim({ mass: 1 })),
594
- oz: tableUnit("oz", dim({ mass: 1 })),
595
- ton: tableUnit("ton", dim({ mass: 1 })),
596
- // US short ton
597
- tonne: tableUnit("tonne", dim({ mass: 1 })),
598
- // metric ton
599
- // --- Time -----------------------------------------------------------------
600
- min: tableUnit("min", dim({ time: 1 })),
601
- h: tableUnit("h", dim({ time: 1 })),
602
- hr: tableUnit("hr", dim({ time: 1 })),
603
- day: tableUnit("day", dim({ time: 1 })),
604
- week: tableUnit("week", dim({ time: 1 })),
605
- year: tableUnit("year", dim({ time: 1 })),
606
- // Julian year
607
- // --- Temperature ----------------------------------------------------------
608
- // K is the base.
609
- // °C → K is offset by 273.15 (no multiplicative factor change).
610
- // °F → K is more complex (offset & scale).
611
- degC: tableUnit("degC", dim({ temperature: 1 })),
612
- degF: tableUnit("degF", dim({ temperature: 1 })),
613
- degR: tableUnit("degR", dim({ temperature: 1 })),
614
- // Rankine
615
- // --- Plane angle (dimensionless, but often treated as units) --------------
616
- rad: tableUnit("rad", dim({}), true),
617
- deg: tableUnit("deg", dim({})),
618
- grad: tableUnit("grad", dim({})),
619
- // --- Force ----------------------------------------------------------------
620
- // N = kg·m·s^-2 = 1 kg·m·s^-2; in our base-units representation that's
621
- // value=1, dim {length: 1, mass: 1, time: -2}.
622
- N: tableUnit("N", dim({ length: 1, mass: 1, time: -2 }), true),
623
- dyn: tableUnit("dyn", dim({ length: 1, mass: 1, time: -2 })),
624
- lbf: tableUnit("lbf", dim({ length: 1, mass: 1, time: -2 })),
625
- // --- Energy ---------------------------------------------------------------
626
- J: tableUnit("J", dim({ length: 2, mass: 1, time: -2 }), true),
627
- erg: tableUnit("erg", dim({ length: 2, mass: 1, time: -2 })),
628
- cal: tableUnit("cal", dim({ length: 2, mass: 1, time: -2 })),
629
- eV: tableUnit("eV", dim({ length: 2, mass: 1, time: -2 }), true),
630
- BTU: tableUnit("BTU", dim({ length: 2, mass: 1, time: -2 })),
631
- // --- Power ----------------------------------------------------------------
632
- W: tableUnit("W", dim({ length: 2, mass: 1, time: -3 }), true),
633
- hp: tableUnit("hp", dim({ length: 2, mass: 1, time: -3 })),
634
- // --- Pressure -------------------------------------------------------------
635
- Pa: tableUnit("Pa", dim({ length: -1, mass: 1, time: -2 }), true),
636
- bar: tableUnit("bar", dim({ length: -1, mass: 1, time: -2 }), true),
637
- atm: tableUnit("atm", dim({ length: -1, mass: 1, time: -2 })),
638
- psi: tableUnit("psi", dim({ length: -1, mass: 1, time: -2 })),
639
- torr: tableUnit("torr", dim({ length: -1, mass: 1, time: -2 })),
640
- mmHg: tableUnit("mmHg", dim({ length: -1, mass: 1, time: -2 })),
641
- // --- Electric -------------------------------------------------------------
642
- C: tableUnit("C", dim({ time: 1, current: 1 }), true),
643
- V: tableUnit("V", dim({ length: 2, mass: 1, time: -3, current: -1 }), true),
644
- ohm: tableUnit("ohm", dim({ length: 2, mass: 1, time: -3, current: -2 }), true),
645
- \u03A9: tableUnit("ohm", dim({ length: 2, mass: 1, time: -3, current: -2 }), true),
646
- F: tableUnit("F", dim({ length: -2, mass: -1, time: 4, current: 2 }), true),
647
- H: tableUnit("H", dim({ length: 2, mass: 1, time: -2, current: -2 }), true),
648
- // --- Frequency ------------------------------------------------------------
649
- Hz: tableUnit("Hz", dim({ time: -1 }), true),
650
- // --- Volume ---------------------------------------------------------------
651
- L: tableUnit("L", dim({ length: 3 }), true),
652
- l: tableUnit("l", dim({ length: 3 }), true),
653
- gal: tableUnit("gal", dim({ length: 3 })),
654
- // US gallon
655
- // --- Area -----------------------------------------------------------------
656
- ha: tableUnit("hectare", dim({ length: 2 })),
657
- // hectare
658
- acre: tableUnit("acre", dim({ length: 2 }))
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
- var ALL_UNITS = { ...BASE_UNITS, ...DERIVED_UNITS };
661
- var UNIT_ALIASES = {
662
- "\xB0C": "degC",
663
- "\xB0F": "degF",
664
- "\xB0R": "degR",
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(name2) {
669
- return ALL_UNITS[name2];
568
+ function getUnitDef(name) {
569
+ return ALL_UNITS[name];
670
570
  }
671
-
672
- // src/types/si-dimension-vector.ts
673
- var UNIT_DIMENSION_LENGTH = 10;
674
- var SI_DIMENSION_LENGTH = 7;
675
- var EXTRA_INDEXES = [7, 8, 9];
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
- if (typeof value !== "number" || !Number.isFinite(value)) {
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`);
582
+ }
583
+ function numberToFractionExponent(value, label = "exponent") {
584
+ assertFinite(label, value);
585
+ if (Number.isInteger(value)) return new Fraction(BigInt(value), 1n);
586
+ return Fraction.fromNumber(value);
587
+ }
588
+ function siExponentToFraction(value, label = "exponent") {
589
+ if (isFraction(value)) return value;
590
+ return numberToFractionExponent(value, label);
680
591
  }
681
592
  function assertUnitVector(vector) {
682
- if (vector.length !== UNIT_DIMENSION_LENGTH) {
683
- throw new TypeError(`dimension vector must have length ${UNIT_DIMENSION_LENGTH}`);
684
- }
685
- for (let i = 0; i < vector.length; i++) {
686
- assertFinite(`dimension vector[${i}]`, vector[i]);
687
- }
593
+ if (vector.length !== 10) throw new TypeError(`dimension vector must have length 10`);
594
+ for (let i = 0; i < vector.length; i++) assertFinite(`dimension vector[${i}]`, vector[i]);
688
595
  }
689
596
  function rejectExtra(vector, options) {
690
- if (options?.ignoreExtra) return;
691
- for (const index of EXTRA_INDEXES) {
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
- }
597
+ if (options?.ignoreExtra) return;
598
+ 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
599
  }
699
600
  function toSiDimensions(vector, options) {
700
- assertUnitVector(vector);
701
- rejectExtra(vector, options);
702
- return {
703
- length: vector[1],
704
- mass: vector[0],
705
- time: vector[2],
706
- current: vector[3],
707
- temperature: vector[4],
708
- amount: vector[6],
709
- luminosity: vector[5]
710
- };
601
+ assertUnitVector(vector);
602
+ rejectExtra(vector, options);
603
+ return {
604
+ length: vector[1],
605
+ mass: vector[0],
606
+ time: vector[2],
607
+ current: vector[3],
608
+ temperature: vector[4],
609
+ amount: vector[6],
610
+ luminosity: vector[5]
611
+ };
711
612
  }
712
613
  function toSiDimensionVector(vector, options) {
713
- const d = toSiDimensions(vector, options);
714
- return [d.length, d.mass, d.time, d.current, d.temperature, d.amount, d.luminosity];
614
+ const d = toSiDimensions(vector, options);
615
+ return [
616
+ numberToFractionExponent(d.length),
617
+ numberToFractionExponent(d.mass),
618
+ numberToFractionExponent(d.time),
619
+ numberToFractionExponent(d.current),
620
+ numberToFractionExponent(d.temperature),
621
+ numberToFractionExponent(d.amount),
622
+ numberToFractionExponent(d.luminosity)
623
+ ];
715
624
  }
716
625
  function fromSiDimensions(dimensions) {
717
- const fields = [
718
- ["mass", dimensions.mass],
719
- ["length", dimensions.length],
720
- ["time", dimensions.time],
721
- ["current", dimensions.current],
722
- ["temperature", dimensions.temperature],
723
- ["luminosity", dimensions.luminosity],
724
- ["amount", dimensions.amount]
725
- ];
726
- for (const [name2, value] of fields) {
727
- assertFinite(`dimensions.${name2}`, value);
728
- }
729
- return [
730
- dimensions.mass,
731
- dimensions.length,
732
- dimensions.time,
733
- dimensions.current,
734
- dimensions.temperature,
735
- dimensions.luminosity,
736
- dimensions.amount,
737
- 0,
738
- 0,
739
- 0
740
- ];
626
+ const fields = [
627
+ ["mass", dimensions.mass],
628
+ ["length", dimensions.length],
629
+ ["time", dimensions.time],
630
+ ["current", dimensions.current],
631
+ ["temperature", dimensions.temperature],
632
+ ["luminosity", dimensions.luminosity],
633
+ ["amount", dimensions.amount]
634
+ ];
635
+ for (const [name, value] of fields) assertFinite(`dimensions.${name}`, value);
636
+ return [
637
+ dimensions.mass,
638
+ dimensions.length,
639
+ dimensions.time,
640
+ dimensions.current,
641
+ dimensions.temperature,
642
+ dimensions.luminosity,
643
+ dimensions.amount,
644
+ 0,
645
+ 0,
646
+ 0
647
+ ];
741
648
  }
742
649
  function fromSiDimensionVector(vector) {
743
- if (vector.length !== SI_DIMENSION_LENGTH) {
744
- throw new TypeError(
745
- `SI dimension vector must have length ${SI_DIMENSION_LENGTH} [L, M, T, I, \u0398, N, J]`
746
- );
747
- }
748
- return fromSiDimensions({
749
- length: vector[0],
750
- mass: vector[1],
751
- time: vector[2],
752
- current: vector[3],
753
- temperature: vector[4],
754
- amount: vector[5],
755
- luminosity: vector[6]
756
- });
650
+ if (vector.length !== 7) throw new TypeError(`SI dimension vector must have length 7 [L, M, T, I, Θ, N, J]`);
651
+ const nums = vector.map((component, i) => {
652
+ const label = `SI dimension vector[${i}]`;
653
+ const n = siExponentToFraction(component, label).toNumber();
654
+ assertFinite(label, n);
655
+ return n;
656
+ });
657
+ return fromSiDimensions({
658
+ length: nums[0],
659
+ mass: nums[1],
660
+ time: nums[2],
661
+ current: nums[3],
662
+ temperature: nums[4],
663
+ amount: nums[5],
664
+ luminosity: nums[6]
665
+ });
757
666
  }
758
-
759
- // src/types/unit-prefixes.ts
760
- var SI_PREFIXES = (() => {
761
- const prefixes = {};
762
- const short = UNIT_PREFIX_SETS.SHORT;
763
- for (const name2 of Object.keys(short)) {
764
- if (name2 === "") continue;
765
- prefixes[name2] = exactScaleToNumber(unitPrefixExactScale(short[name2]));
766
- }
767
- prefixes["\xB5"] = prefixes.u;
768
- return prefixes;
667
+ //#endregion
668
+ //#region src/types/unit-prefixes.ts
669
+ const SI_PREFIXES = (() => {
670
+ const prefixes = {};
671
+ const short = UNIT_PREFIX_SETS.SHORT;
672
+ for (const name of Object.keys(short)) {
673
+ if (name === "") continue;
674
+ prefixes[name] = exactScaleToNumber(unitPrefixExactScale(short[name]));
675
+ }
676
+ prefixes["µ"] = prefixes.u;
677
+ return prefixes;
769
678
  })();
770
- var BEST_PREFIXES = [
771
- "Y",
772
- "Z",
773
- "E",
774
- "P",
775
- "T",
776
- "G",
777
- "M",
778
- "k",
779
- "",
780
- "m",
781
- "u",
782
- "n",
783
- "p",
784
- "f",
785
- "a",
786
- "z",
787
- "y"
679
+ const BEST_PREFIXES = [
680
+ "Y",
681
+ "Z",
682
+ "E",
683
+ "P",
684
+ "T",
685
+ "G",
686
+ "M",
687
+ "k",
688
+ "",
689
+ "m",
690
+ "u",
691
+ "n",
692
+ "p",
693
+ "f",
694
+ "a",
695
+ "z",
696
+ "y"
788
697
  ];
789
- function getPrefix(name2) {
790
- return SI_PREFIXES[name2];
698
+ function getPrefix(name) {
699
+ return SI_PREFIXES[name];
791
700
  }
792
-
793
- // src/typed/mathts-typed.ts
794
- import typed, { create } from "typed-function";
795
- var wasmInitialized = false;
796
- var wasmAvailable = false;
701
+ //#endregion
702
+ //#region src/typed/mathts-typed.ts
703
+ let wasmInitialized = false;
704
+ let wasmAvailable = false;
797
705
  async function initTypedWasm(options = {}) {
798
- if (wasmInitialized) return wasmAvailable;
799
- try {
800
- if (typeof typed.init === "function") {
801
- await typed.init({ preferWasm: options.preferWasm ?? true });
802
- wasmAvailable = typed.isWasmEnabled?.() ?? false;
803
- }
804
- } catch {
805
- wasmAvailable = false;
806
- }
807
- wasmInitialized = true;
808
- return wasmAvailable;
706
+ if (wasmInitialized) return wasmAvailable;
707
+ try {
708
+ if (typeof typed.init === "function") {
709
+ await typed.init({ preferWasm: options.preferWasm ?? true });
710
+ wasmAvailable = typed.isWasmEnabled?.() ?? false;
711
+ }
712
+ } catch {
713
+ wasmAvailable = false;
714
+ }
715
+ wasmInitialized = true;
716
+ return wasmAvailable;
809
717
  }
810
718
  function isTypedWasmAvailable() {
811
- return typed.isWasmEnabled?.() ?? wasmAvailable;
719
+ return typed.isWasmEnabled?.() ?? wasmAvailable;
812
720
  }
813
- var isNumber = (x) => typeof x === "number";
814
- var isBoolean = (x) => typeof x === "boolean";
815
- var isString = (x) => typeof x === "string";
816
- var isBigInt2 = (x) => typeof x === "bigint";
817
- var isArray = (x) => Array.isArray(x);
818
- var isFunction = (x) => typeof x === "function";
819
- var isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
820
- var isNull = (x) => x === null;
821
- var isUndefined = (x) => x === void 0;
822
- var isFloat64Array = (x) => x instanceof Float64Array;
823
- var isFloat32Array = (x) => x instanceof Float32Array;
824
- var isInt32Array = (x) => x instanceof Int32Array;
825
- var isUint32Array = (x) => x instanceof Uint32Array;
826
- var isUint8Array = (x) => x instanceof Uint8Array;
827
- var isComplex2 = (x) => isComplex(x);
828
- var isFraction2 = (x) => isFraction(x);
829
- var isBigNumber3 = (x) => isBigNumber2(x);
830
- var isMatrix = (x) => isObject(x) && "rows" in x && "cols" in x && "get" in x && typeof x.type === "string";
831
- var isDenseMatrix = (x) => isMatrix(x) && x.type === "DenseMatrix";
832
- var isSparseMatrix = (x) => isMatrix(x) && x.type === "SparseMatrix";
833
- var isUnit2 = (x) => {
834
- if (!isObject(x)) return false;
835
- const o = x;
836
- if (typeof o.type !== "string") return false;
837
- if (o.type === "Unit" && "value" in o && "dimensions" in o) return true;
838
- if ("value" in o && "unit" in o) return true;
839
- return false;
721
+ const isNumber = (x) => typeof x === "number";
722
+ const isBoolean = (x) => typeof x === "boolean";
723
+ const isString = (x) => typeof x === "string";
724
+ const isBigInt = (x) => typeof x === "bigint";
725
+ const isArray = (x) => Array.isArray(x);
726
+ const isFunction = (x) => typeof x === "function";
727
+ const isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
728
+ const isNull = (x) => x === null;
729
+ const isUndefined = (x) => x === void 0;
730
+ const isFloat64Array = (x) => x instanceof Float64Array;
731
+ const isFloat32Array = (x) => x instanceof Float32Array;
732
+ const isInt32Array = (x) => x instanceof Int32Array;
733
+ const isUint32Array = (x) => x instanceof Uint32Array;
734
+ const isUint8Array = (x) => x instanceof Uint8Array;
735
+ const isComplex$1 = (x) => isComplex(x);
736
+ const isFraction$1 = (x) => isFraction(x);
737
+ const isBigNumber$1 = (x) => isBigNumber(x);
738
+ const isMatrix = (x) => isObject(x) && "rows" in x && "cols" in x && "get" in x && typeof x.type === "string";
739
+ const isDenseMatrix = (x) => isMatrix(x) && x.type === "DenseMatrix";
740
+ const isSparseMatrix = (x) => isMatrix(x) && x.type === "SparseMatrix";
741
+ const isUnit = (x) => {
742
+ if (!isObject(x)) return false;
743
+ const o = x;
744
+ if (typeof o.type !== "string") return false;
745
+ if (o.type === "Unit" && "value" in o && "dimensions" in o) return true;
746
+ if ("value" in o && "unit" in o) return true;
747
+ return false;
840
748
  };
841
- var MATHTS_TYPES = [
842
- // bigint is not built into typed-function
843
- { name: "bigint", test: isBigInt2 },
844
- // MathTS numeric types (ordered by specificity for conversion priority)
845
- // WASM masks use typed.masks.OBJECT | typed.masks.custom(N) pattern
846
- { name: "Complex", test: isComplex2 },
847
- { name: "Fraction", test: isFraction2 },
848
- { name: "BigNumber", test: isBigNumber3 },
849
- // TypedArrays (for parallel-first WASM operations)
850
- { name: "Float64Array", test: isFloat64Array },
851
- { name: "Float32Array", test: isFloat32Array },
852
- { name: "Int32Array", test: isInt32Array },
853
- { name: "Uint32Array", test: isUint32Array },
854
- { name: "Uint8Array", test: isUint8Array },
855
- // Matrix types
856
- { name: "DenseMatrix", test: isDenseMatrix },
857
- { name: "SparseMatrix", test: isSparseMatrix },
858
- { name: "Matrix", test: isMatrix },
859
- // Other types
860
- { name: "Unit", test: isUnit2 },
861
- { name: "Dual", test: isDual }
749
+ const MATHTS_TYPES = [
750
+ {
751
+ name: "bigint",
752
+ test: isBigInt
753
+ },
754
+ {
755
+ name: "Complex",
756
+ test: isComplex$1
757
+ },
758
+ {
759
+ name: "Fraction",
760
+ test: isFraction$1
761
+ },
762
+ {
763
+ name: "BigNumber",
764
+ test: isBigNumber$1
765
+ },
766
+ {
767
+ name: "Float64Array",
768
+ test: isFloat64Array
769
+ },
770
+ {
771
+ name: "Float32Array",
772
+ test: isFloat32Array
773
+ },
774
+ {
775
+ name: "Int32Array",
776
+ test: isInt32Array
777
+ },
778
+ {
779
+ name: "Uint32Array",
780
+ test: isUint32Array
781
+ },
782
+ {
783
+ name: "Uint8Array",
784
+ test: isUint8Array
785
+ },
786
+ {
787
+ name: "DenseMatrix",
788
+ test: isDenseMatrix
789
+ },
790
+ {
791
+ name: "SparseMatrix",
792
+ test: isSparseMatrix
793
+ },
794
+ {
795
+ name: "Matrix",
796
+ test: isMatrix
797
+ },
798
+ {
799
+ name: "Unit",
800
+ test: isUnit
801
+ },
802
+ {
803
+ name: "Dual",
804
+ test: isDual
805
+ }
862
806
  ];
863
- var MATHTS_CONVERSIONS = [
864
- // -------------------------------------------------------------------------
865
- // To Complex conversions
866
- // -------------------------------------------------------------------------
867
- {
868
- from: "number",
869
- to: "Complex",
870
- convert: (n) => Complex.fromNumber(n)
871
- },
872
- {
873
- from: "Fraction",
874
- to: "Complex",
875
- convert: (f) => Complex.fromNumber(f.toNumber())
876
- },
877
- {
878
- from: "BigNumber",
879
- to: "Complex",
880
- convert: (bn) => Complex.fromNumber(bn.valueOf())
881
- },
882
- {
883
- from: "string",
884
- to: "Complex",
885
- convert: (s) => Complex.parse(s)
886
- },
887
- // -------------------------------------------------------------------------
888
- // To Fraction conversions
889
- // -------------------------------------------------------------------------
890
- {
891
- from: "number",
892
- to: "Fraction",
893
- convert: (n) => Fraction.fromNumber(n)
894
- },
895
- {
896
- from: "bigint",
897
- to: "Fraction",
898
- convert: (n) => new Fraction(n, 1n)
899
- },
900
- {
901
- from: "string",
902
- to: "Fraction",
903
- convert: (s) => Fraction.parse(s)
904
- },
905
- // -------------------------------------------------------------------------
906
- // To BigNumber conversions
907
- // -------------------------------------------------------------------------
908
- {
909
- from: "number",
910
- to: "BigNumber",
911
- convert: (n) => BigNumber.fromNumber(n)
912
- },
913
- {
914
- from: "bigint",
915
- to: "BigNumber",
916
- convert: (n) => BigNumber.fromBigInt(n)
917
- },
918
- {
919
- from: "Fraction",
920
- to: "BigNumber",
921
- convert: (f) => BigNumber.fromNumber(f.toNumber())
922
- },
923
- {
924
- from: "string",
925
- to: "BigNumber",
926
- convert: (s) => BigNumber.parse(s)
927
- },
928
- // -------------------------------------------------------------------------
929
- // To number conversions
930
- // -------------------------------------------------------------------------
931
- {
932
- from: "boolean",
933
- to: "number",
934
- convert: (b) => b ? 1 : 0
935
- },
936
- {
937
- from: "string",
938
- to: "number",
939
- convert: (s) => {
940
- const n = parseFloat(s);
941
- if (isNaN(n)) throw new Error(`Cannot convert "${s}" to number`);
942
- return n;
943
- }
944
- },
945
- {
946
- from: "bigint",
947
- to: "number",
948
- convert: (n) => Number(n)
949
- },
950
- {
951
- from: "Fraction",
952
- to: "number",
953
- convert: (f) => f.toNumber()
954
- },
955
- {
956
- from: "BigNumber",
957
- to: "number",
958
- convert: (bn) => bn.valueOf()
959
- },
960
- // -------------------------------------------------------------------------
961
- // To bigint conversions
962
- // -------------------------------------------------------------------------
963
- {
964
- from: "number",
965
- to: "bigint",
966
- convert: (n) => BigInt(Math.trunc(n))
967
- },
968
- {
969
- from: "Fraction",
970
- to: "bigint",
971
- convert: (f) => f.trunc().numerator
972
- },
973
- {
974
- from: "BigNumber",
975
- to: "bigint",
976
- convert: (bn) => bn.toBigInt()
977
- },
978
- {
979
- from: "string",
980
- to: "bigint",
981
- convert: (s) => BigInt(s)
982
- },
983
- // -------------------------------------------------------------------------
984
- // Matrix conversions (placeholder until Matrix is implemented)
985
- // -------------------------------------------------------------------------
986
- {
987
- from: "Array",
988
- to: "Matrix",
989
- convert: (a) => {
990
- const arr = a;
991
- return {
992
- rows: arr.length,
993
- cols: arr[0]?.length ?? 0,
994
- data: arr.flat(),
995
- type: "DenseMatrix",
996
- get: (r, c) => arr[r]?.[c]
997
- };
998
- }
999
- },
1000
- {
1001
- from: "Matrix",
1002
- to: "Array",
1003
- convert: (m) => {
1004
- const matrix = m;
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
- }
807
+ const MATHTS_CONVERSIONS = [
808
+ {
809
+ from: "number",
810
+ to: "Complex",
811
+ convert: (n) => Complex.fromNumber(n)
812
+ },
813
+ {
814
+ from: "Fraction",
815
+ to: "Complex",
816
+ convert: (f) => Complex.fromNumber(f.toNumber())
817
+ },
818
+ {
819
+ from: "BigNumber",
820
+ to: "Complex",
821
+ convert: (bn) => Complex.fromNumber(bn.valueOf())
822
+ },
823
+ {
824
+ from: "string",
825
+ to: "Complex",
826
+ convert: (s) => Complex.parse(s)
827
+ },
828
+ {
829
+ from: "number",
830
+ to: "Fraction",
831
+ convert: (n) => Fraction.fromNumber(n)
832
+ },
833
+ {
834
+ from: "bigint",
835
+ to: "Fraction",
836
+ convert: (n) => new Fraction(n, 1n)
837
+ },
838
+ {
839
+ from: "string",
840
+ to: "Fraction",
841
+ convert: (s) => Fraction.parse(s)
842
+ },
843
+ {
844
+ from: "number",
845
+ to: "BigNumber",
846
+ convert: (n) => BigNumber.fromNumber(n)
847
+ },
848
+ {
849
+ from: "bigint",
850
+ to: "BigNumber",
851
+ convert: (n) => BigNumber.fromBigInt(n)
852
+ },
853
+ {
854
+ from: "Fraction",
855
+ to: "BigNumber",
856
+ convert: (f) => BigNumber.fromNumber(f.toNumber())
857
+ },
858
+ {
859
+ from: "string",
860
+ to: "BigNumber",
861
+ convert: (s) => BigNumber.parse(s)
862
+ },
863
+ {
864
+ from: "boolean",
865
+ to: "number",
866
+ convert: (b) => b ? 1 : 0
867
+ },
868
+ {
869
+ from: "string",
870
+ to: "number",
871
+ convert: (s) => {
872
+ const n = parseFloat(s);
873
+ if (isNaN(n)) throw new Error(`Cannot convert "${s}" to number`);
874
+ return n;
875
+ }
876
+ },
877
+ {
878
+ from: "bigint",
879
+ to: "number",
880
+ convert: (n) => Number(n)
881
+ },
882
+ {
883
+ from: "Fraction",
884
+ to: "number",
885
+ convert: (f) => f.toNumber()
886
+ },
887
+ {
888
+ from: "BigNumber",
889
+ to: "number",
890
+ convert: (bn) => bn.valueOf()
891
+ },
892
+ {
893
+ from: "number",
894
+ to: "bigint",
895
+ convert: (n) => BigInt(Math.trunc(n))
896
+ },
897
+ {
898
+ from: "Fraction",
899
+ to: "bigint",
900
+ convert: (f) => f.trunc().numerator
901
+ },
902
+ {
903
+ from: "BigNumber",
904
+ to: "bigint",
905
+ convert: (bn) => bn.toBigInt()
906
+ },
907
+ {
908
+ from: "string",
909
+ to: "bigint",
910
+ convert: (s) => BigInt(s)
911
+ },
912
+ {
913
+ from: "Array",
914
+ to: "Matrix",
915
+ convert: (a) => {
916
+ const arr = a;
917
+ return {
918
+ rows: arr.length,
919
+ cols: arr[0]?.length ?? 0,
920
+ data: arr.flat(),
921
+ type: "DenseMatrix",
922
+ get: (r, c) => arr[r]?.[c]
923
+ };
924
+ }
925
+ },
926
+ {
927
+ from: "Matrix",
928
+ to: "Array",
929
+ convert: (m) => {
930
+ const matrix = m;
931
+ const result = [];
932
+ for (let i = 0; i < matrix.rows; i++) {
933
+ result[i] = [];
934
+ for (let j = 0; j < matrix.cols; j++) result[i][j] = matrix.get(i, j);
935
+ }
936
+ return result;
937
+ }
938
+ },
939
+ {
940
+ from: "DenseMatrix",
941
+ to: "SparseMatrix",
942
+ convert: (m) => {
943
+ return {
944
+ ...m,
945
+ type: "SparseMatrix"
946
+ };
947
+ }
948
+ }
1022
949
  ];
1023
950
  function createMathTSTyped() {
1024
- const instance = create();
1025
- if (typeof instance.addType === "function") {
1026
- for (const typeDef of MATHTS_TYPES) {
1027
- instance.addType(typeDef);
1028
- }
1029
- } else {
1030
- instance.addTypes(MATHTS_TYPES, "any");
1031
- }
1032
- instance.addConversions(MATHTS_CONVERSIONS);
1033
- return instance;
951
+ const instance = create();
952
+ if (typeof instance.addType === "function") for (const typeDef of MATHTS_TYPES) instance.addType(typeDef);
953
+ else instance.addTypes(MATHTS_TYPES, "any");
954
+ instance.addConversions(MATHTS_CONVERSIONS);
955
+ return instance;
1034
956
  }
1035
- var mathTyped = createMathTSTyped();
957
+ const mathTyped = createMathTSTyped();
1036
958
  var TypeRegistry = class {
1037
- types = /* @__PURE__ */ new Map();
1038
- conversions = /* @__PURE__ */ new Map();
1039
- instance = null;
1040
- /**
1041
- * Register a new type
1042
- */
1043
- registerType(name2, test) {
1044
- this.types.set(name2, { name: name2, test });
1045
- this.instance = null;
1046
- return this;
1047
- }
1048
- /**
1049
- * Register a type conversion
1050
- */
1051
- registerConversion(from, to, convert) {
1052
- const key = `${from}->${to}`;
1053
- this.conversions.set(key, {
1054
- from,
1055
- to,
1056
- convert
1057
- });
1058
- this.instance = null;
1059
- return this;
1060
- }
1061
- /**
1062
- * Check if a type is registered
1063
- */
1064
- hasType(name2) {
1065
- return this.types.has(name2);
1066
- }
1067
- /**
1068
- * Check if a conversion is registered
1069
- */
1070
- hasConversion(from, to) {
1071
- return this.conversions.has(`${from}->${to}`);
1072
- }
1073
- /**
1074
- * Get all registered type names
1075
- */
1076
- getTypeNames() {
1077
- return Array.from(this.types.keys());
1078
- }
1079
- /**
1080
- * Build a typed-function instance from the registry
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
- }
959
+ types = /* @__PURE__ */ new Map();
960
+ conversions = /* @__PURE__ */ new Map();
961
+ instance = null;
962
+ registerType(name, test) {
963
+ this.types.set(name, {
964
+ name,
965
+ test
966
+ });
967
+ this.instance = null;
968
+ return this;
969
+ }
970
+ registerConversion(from, to, convert) {
971
+ const key = `${from}->${to}`;
972
+ this.conversions.set(key, {
973
+ from,
974
+ to,
975
+ convert
976
+ });
977
+ this.instance = null;
978
+ return this;
979
+ }
980
+ hasType(name) {
981
+ return this.types.has(name);
982
+ }
983
+ hasConversion(from, to) {
984
+ return this.conversions.has(`${from}->${to}`);
985
+ }
986
+ getTypeNames() {
987
+ return Array.from(this.types.keys());
988
+ }
989
+ build() {
990
+ if (!this.instance) {
991
+ const inst = create();
992
+ inst.addTypes(Array.from(this.types.values()), "any");
993
+ inst.addConversions(Array.from(this.conversions.values()));
994
+ this.instance = inst;
995
+ }
996
+ return this.instance;
997
+ }
998
+ clear() {
999
+ this.types.clear();
1000
+ this.conversions.clear();
1001
+ this.instance = null;
1002
+ }
1099
1003
  };
1100
- function createTypedFunction(name2, signatures, typedInstance = mathTyped) {
1101
- return typedInstance(name2, signatures);
1004
+ function createTypedFunction(name, signatures, typedInstance = mathTyped) {
1005
+ return typedInstance(name, signatures);
1102
1006
  }
1103
-
1104
- // src/typed/type-bridge.ts
1105
- var registered = false;
1007
+ //#endregion
1008
+ //#region src/typed/type-bridge.ts
1009
+ let registered = false;
1106
1010
  function registerNativeTypes() {
1107
- if (registered) return;
1108
- registered = true;
1109
- if (!("isComplex" in Complex.prototype)) {
1110
- Object.defineProperty(Complex.prototype, "isComplex", {
1111
- value: true,
1112
- writable: false,
1113
- enumerable: false,
1114
- configurable: false
1115
- });
1116
- }
1117
- if (!("type" in Complex.prototype)) {
1118
- Object.defineProperty(Complex.prototype, "type", {
1119
- value: "Complex",
1120
- writable: false,
1121
- enumerable: false,
1122
- configurable: false
1123
- });
1124
- }
1125
- if (!("isFraction" in Fraction.prototype)) {
1126
- Object.defineProperty(Fraction.prototype, "isFraction", {
1127
- value: true,
1128
- writable: false,
1129
- enumerable: false,
1130
- configurable: false
1131
- });
1132
- }
1133
- if (!("type" in Fraction.prototype)) {
1134
- Object.defineProperty(Fraction.prototype, "type", {
1135
- value: "Fraction",
1136
- writable: false,
1137
- enumerable: false,
1138
- configurable: false
1139
- });
1140
- }
1141
- if (!("isBigNumber" in BigNumber.prototype)) {
1142
- Object.defineProperty(BigNumber.prototype, "isBigNumber", {
1143
- value: true,
1144
- writable: false,
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
- }
1011
+ if (registered) return;
1012
+ registered = true;
1013
+ if (!("isComplex" in Complex.prototype)) Object.defineProperty(Complex.prototype, "isComplex", {
1014
+ value: true,
1015
+ writable: false,
1016
+ enumerable: false,
1017
+ configurable: false
1018
+ });
1019
+ if (!("type" in Complex.prototype)) Object.defineProperty(Complex.prototype, "type", {
1020
+ value: "Complex",
1021
+ writable: false,
1022
+ enumerable: false,
1023
+ configurable: false
1024
+ });
1025
+ if (!("isFraction" in Fraction.prototype)) Object.defineProperty(Fraction.prototype, "isFraction", {
1026
+ value: true,
1027
+ writable: false,
1028
+ enumerable: false,
1029
+ configurable: false
1030
+ });
1031
+ if (!("type" in Fraction.prototype)) Object.defineProperty(Fraction.prototype, "type", {
1032
+ value: "Fraction",
1033
+ writable: false,
1034
+ enumerable: false,
1035
+ configurable: false
1036
+ });
1037
+ if (!("isBigNumber" in BigNumber.prototype)) Object.defineProperty(BigNumber.prototype, "isBigNumber", {
1038
+ value: true,
1039
+ writable: false,
1040
+ enumerable: false,
1041
+ configurable: false
1042
+ });
1043
+ if (!("type" in BigNumber.prototype)) Object.defineProperty(BigNumber.prototype, "type", {
1044
+ value: "BigNumber",
1045
+ writable: false,
1046
+ enumerable: false,
1047
+ configurable: false
1048
+ });
1157
1049
  }
1158
-
1159
- // src/factory/factory.ts
1160
- var DEFAULT_CONFIG = {
1161
- precision: 64,
1162
- matrix: "Matrix",
1163
- number: "number",
1164
- randomSeed: null,
1165
- epsilon: 1e-12,
1166
- preferredBackend: "auto",
1167
- wasmThreshold: 1e3,
1168
- gpuThreshold: 1e5,
1169
- parallelEnabled: true,
1170
- parallelThreshold: 5e4
1050
+ //#endregion
1051
+ //#region src/factory/factory.ts
1052
+ const DEFAULT_CONFIG = {
1053
+ precision: 64,
1054
+ matrix: "Matrix",
1055
+ number: "number",
1056
+ randomSeed: null,
1057
+ epsilon: 1e-12,
1058
+ preferredBackend: "auto",
1059
+ wasmThreshold: 1e3,
1060
+ gpuThreshold: 1e5,
1061
+ parallelEnabled: true,
1062
+ parallelThreshold: 5e4
1171
1063
  };
1172
1064
  var FunctionRegistry = class {
1173
- factories = /* @__PURE__ */ new Map();
1174
- instances = /* @__PURE__ */ new Map();
1175
- dependencies;
1176
- creating = /* @__PURE__ */ new Set();
1177
- constructor(config = {}, typed2 = mathTyped) {
1178
- this.dependencies = {
1179
- config: { ...DEFAULT_CONFIG, ...config },
1180
- typed: typed2
1181
- };
1182
- }
1183
- /**
1184
- * Register a factory function
1185
- */
1186
- register(factory2) {
1187
- this.factories.set(factory2.name, factory2);
1188
- }
1189
- /**
1190
- * Register multiple factory functions
1191
- */
1192
- registerAll(factories) {
1193
- for (const factory2 of factories) {
1194
- this.register(factory2);
1195
- }
1196
- }
1197
- /**
1198
- * Get or create a function by name
1199
- */
1200
- get(name2) {
1201
- const existing = this.instances.get(name2);
1202
- if (existing) return existing;
1203
- if (this.creating.has(name2)) {
1204
- throw new Error(`Circular dependency detected: ${name2}`);
1205
- }
1206
- const factory2 = this.factories.get(name2);
1207
- if (!factory2) {
1208
- throw new Error(`Unknown function: ${name2}`);
1209
- }
1210
- this.creating.add(name2);
1211
- try {
1212
- const deps = { ...this.dependencies };
1213
- for (const depName of factory2.dependencies) {
1214
- if (depName === "config" || depName === "typed") continue;
1215
- deps[depName] = this.get(depName);
1216
- }
1217
- const fn = factory2.factory(deps);
1218
- this.instances.set(name2, fn);
1219
- this.dependencies[name2] = fn;
1220
- return fn;
1221
- } finally {
1222
- this.creating.delete(name2);
1223
- }
1224
- }
1225
- /**
1226
- * Check if a function is registered
1227
- */
1228
- has(name2) {
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
- }
1065
+ factories = /* @__PURE__ */ new Map();
1066
+ instances = /* @__PURE__ */ new Map();
1067
+ dependencies;
1068
+ creating = /* @__PURE__ */ new Set();
1069
+ constructor(config = {}, typed = mathTyped) {
1070
+ this.dependencies = {
1071
+ config: {
1072
+ ...DEFAULT_CONFIG,
1073
+ ...config
1074
+ },
1075
+ typed
1076
+ };
1077
+ }
1078
+ register(factory) {
1079
+ this.factories.set(factory.name, factory);
1080
+ }
1081
+ registerAll(factories) {
1082
+ for (const factory of factories) this.register(factory);
1083
+ }
1084
+ get(name) {
1085
+ const existing = this.instances.get(name);
1086
+ if (existing) return existing;
1087
+ if (this.creating.has(name)) throw new Error(`Circular dependency detected: ${name}`);
1088
+ const factory = this.factories.get(name);
1089
+ if (!factory) throw new Error(`Unknown function: ${name}`);
1090
+ this.creating.add(name);
1091
+ try {
1092
+ const deps = { ...this.dependencies };
1093
+ for (const depName of factory.dependencies) {
1094
+ if (depName === "config" || depName === "typed") continue;
1095
+ deps[depName] = this.get(depName);
1096
+ }
1097
+ const fn = factory.factory(deps);
1098
+ this.instances.set(name, fn);
1099
+ this.dependencies[name] = fn;
1100
+ return fn;
1101
+ } finally {
1102
+ this.creating.delete(name);
1103
+ }
1104
+ }
1105
+ has(name) {
1106
+ return this.factories.has(name);
1107
+ }
1108
+ names() {
1109
+ return Array.from(this.factories.keys());
1110
+ }
1111
+ updateConfig(config) {
1112
+ this.dependencies.config = {
1113
+ ...this.dependencies.config,
1114
+ ...config
1115
+ };
1116
+ this.instances.clear();
1117
+ }
1118
+ getConfig() {
1119
+ return { ...this.dependencies.config };
1120
+ }
1250
1121
  };
1251
- function createFactory(name2, dependencies2, factory2) {
1252
- return { name: name2, dependencies: dependencies2, factory: factory2 };
1122
+ function createFactory(name, dependencies, factory) {
1123
+ return {
1124
+ name,
1125
+ dependencies,
1126
+ factory
1127
+ };
1253
1128
  }
1254
- var registry = new FunctionRegistry();
1255
- var math = {
1256
- /**
1257
- * Get a registered function
1258
- */
1259
- get: (name2) => registry.get(name2),
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)
1129
+ const registry = new FunctionRegistry();
1130
+ const math = {
1131
+ get: (name) => registry.get(name),
1132
+ register: (factory) => registry.register(factory),
1133
+ config: () => registry.getConfig(),
1134
+ configure: (config) => registry.updateConfig(config)
1272
1135
  };
1273
-
1274
- // src/numeric/stable.ts
1275
- var PAIRWISE_BLOCK = 128;
1136
+ //#endregion
1137
+ //#region src/numeric/stable.ts
1138
+ const PAIRWISE_BLOCK = 128;
1276
1139
  function pairwiseSum(xs, start = 0, end = xs.length) {
1277
- const n = end - start;
1278
- if (n <= PAIRWISE_BLOCK) {
1279
- let s0 = 0;
1280
- let s1 = 0;
1281
- let s2 = 0;
1282
- let s3 = 0;
1283
- let s4 = 0;
1284
- let s5 = 0;
1285
- let s6 = 0;
1286
- let s7 = 0;
1287
- let i = start;
1288
- const limit = start + (n - n % 8);
1289
- for (; i < limit; i += 8) {
1290
- s0 += xs[i];
1291
- s1 += xs[i + 1];
1292
- s2 += xs[i + 2];
1293
- s3 += xs[i + 3];
1294
- s4 += xs[i + 4];
1295
- s5 += xs[i + 5];
1296
- s6 += xs[i + 6];
1297
- s7 += xs[i + 7];
1298
- }
1299
- let tail = 0;
1300
- for (; i < end; i++) tail += xs[i];
1301
- return s0 + s1 + (s2 + s3) + (s4 + s5 + (s6 + s7)) + tail;
1302
- }
1303
- const half = (n >> 1) - (n >> 1) % 8;
1304
- return pairwiseSum(xs, start, start + half) + pairwiseSum(xs, start + half, end);
1140
+ const n = end - start;
1141
+ if (n <= PAIRWISE_BLOCK) {
1142
+ let s0 = 0;
1143
+ let s1 = 0;
1144
+ let s2 = 0;
1145
+ let s3 = 0;
1146
+ let s4 = 0;
1147
+ let s5 = 0;
1148
+ let s6 = 0;
1149
+ let s7 = 0;
1150
+ let i = start;
1151
+ const limit = start + (n - n % 8);
1152
+ for (; i < limit; i += 8) {
1153
+ s0 += xs[i];
1154
+ s1 += xs[i + 1];
1155
+ s2 += xs[i + 2];
1156
+ s3 += xs[i + 3];
1157
+ s4 += xs[i + 4];
1158
+ s5 += xs[i + 5];
1159
+ s6 += xs[i + 6];
1160
+ s7 += xs[i + 7];
1161
+ }
1162
+ let tail = 0;
1163
+ for (; i < end; i++) tail += xs[i];
1164
+ return s0 + s1 + (s2 + s3) + (s4 + s5 + (s6 + s7)) + tail;
1165
+ }
1166
+ const half = (n >> 1) - (n >> 1) % 8;
1167
+ return pairwiseSum(xs, start, start + half) + pairwiseSum(xs, start + half, end);
1305
1168
  }
1306
1169
  function neumaierSum(xs) {
1307
- let sum = 0;
1308
- let c = 0;
1309
- for (let i = 0; i < xs.length; i++) {
1310
- const x = xs[i];
1311
- const t = sum + x;
1312
- c += Math.abs(sum) >= Math.abs(x) ? sum - t + x : x - t + sum;
1313
- sum = t;
1314
- }
1315
- return sum + c;
1170
+ let sum = 0;
1171
+ let c = 0;
1172
+ for (let i = 0; i < xs.length; i++) {
1173
+ const x = xs[i];
1174
+ const t = sum + x;
1175
+ c += Math.abs(sum) >= Math.abs(x) ? sum - t + x : x - t + sum;
1176
+ sum = t;
1177
+ }
1178
+ return sum + c;
1316
1179
  }
1317
1180
  function norm2(xs) {
1318
- let scale = 0;
1319
- let ssq = 1;
1320
- for (let i = 0; i < xs.length; i++) {
1321
- const x = xs[i];
1322
- if (x === 0) continue;
1323
- if (Number.isNaN(x)) return NaN;
1324
- const ax = Math.abs(x);
1325
- if (scale < ax) {
1326
- const r = scale / ax;
1327
- ssq = 1 + ssq * r * r;
1328
- scale = ax;
1329
- } else {
1330
- const r = ax / scale;
1331
- ssq += r * r;
1332
- }
1333
- }
1334
- return scale === 0 ? 0 : scale * Math.sqrt(ssq);
1181
+ let scale = 0;
1182
+ let ssq = 1;
1183
+ for (let i = 0; i < xs.length; i++) {
1184
+ const x = xs[i];
1185
+ if (x === 0) continue;
1186
+ if (Number.isNaN(x)) return NaN;
1187
+ const ax = Math.abs(x);
1188
+ if (scale < ax) {
1189
+ const r = scale / ax;
1190
+ ssq = 1 + ssq * r * r;
1191
+ scale = ax;
1192
+ } else {
1193
+ const r = ax / scale;
1194
+ ssq += r * r;
1195
+ }
1196
+ }
1197
+ return scale === 0 ? 0 : scale * Math.sqrt(ssq);
1335
1198
  }
1336
1199
  function pairwiseDot(a, b, start = 0, end = a.length) {
1337
- const n = end - start;
1338
- if (n <= PAIRWISE_BLOCK) {
1339
- let s0 = 0;
1340
- let s1 = 0;
1341
- let s2 = 0;
1342
- let s3 = 0;
1343
- let s4 = 0;
1344
- let s5 = 0;
1345
- let s6 = 0;
1346
- let s7 = 0;
1347
- let i = start;
1348
- const limit = start + (n - n % 8);
1349
- for (; i < limit; i += 8) {
1350
- s0 += a[i] * b[i];
1351
- s1 += a[i + 1] * b[i + 1];
1352
- s2 += a[i + 2] * b[i + 2];
1353
- s3 += a[i + 3] * b[i + 3];
1354
- s4 += a[i + 4] * b[i + 4];
1355
- s5 += a[i + 5] * b[i + 5];
1356
- s6 += a[i + 6] * b[i + 6];
1357
- s7 += a[i + 7] * b[i + 7];
1358
- }
1359
- let tail = 0;
1360
- for (; i < end; i++) tail += a[i] * b[i];
1361
- return s0 + s1 + (s2 + s3) + (s4 + s5 + (s6 + s7)) + tail;
1362
- }
1363
- const half = (n >> 1) - (n >> 1) % 8;
1364
- return pairwiseDot(a, b, start, start + half) + pairwiseDot(a, b, start + half, end);
1200
+ const n = end - start;
1201
+ if (n <= PAIRWISE_BLOCK) {
1202
+ let s0 = 0;
1203
+ let s1 = 0;
1204
+ let s2 = 0;
1205
+ let s3 = 0;
1206
+ let s4 = 0;
1207
+ let s5 = 0;
1208
+ let s6 = 0;
1209
+ let s7 = 0;
1210
+ let i = start;
1211
+ const limit = start + (n - n % 8);
1212
+ for (; i < limit; i += 8) {
1213
+ s0 += a[i] * b[i];
1214
+ s1 += a[i + 1] * b[i + 1];
1215
+ s2 += a[i + 2] * b[i + 2];
1216
+ s3 += a[i + 3] * b[i + 3];
1217
+ s4 += a[i + 4] * b[i + 4];
1218
+ s5 += a[i + 5] * b[i + 5];
1219
+ s6 += a[i + 6] * b[i + 6];
1220
+ s7 += a[i + 7] * b[i + 7];
1221
+ }
1222
+ let tail = 0;
1223
+ for (; i < end; i++) tail += a[i] * b[i];
1224
+ return s0 + s1 + (s2 + s3) + (s4 + s5 + (s6 + s7)) + tail;
1225
+ }
1226
+ const half = (n >> 1) - (n >> 1) % 8;
1227
+ return pairwiseDot(a, b, start, start + half) + pairwiseDot(a, b, start + half, end);
1365
1228
  }
1366
1229
  function scaledDistance(a, b) {
1367
- let scale = 0;
1368
- let ssq = 1;
1369
- for (let i = 0; i < a.length; i++) {
1370
- const d = a[i] - b[i];
1371
- if (d === 0) continue;
1372
- if (Number.isNaN(d)) return NaN;
1373
- const ad = Math.abs(d);
1374
- if (scale < ad) {
1375
- const r = scale / ad;
1376
- ssq = 1 + ssq * r * r;
1377
- scale = ad;
1378
- } else {
1379
- const r = ad / scale;
1380
- ssq += r * r;
1381
- }
1382
- }
1383
- return scale === 0 ? 0 : scale * Math.sqrt(ssq);
1230
+ let scale = 0;
1231
+ let ssq = 1;
1232
+ for (let i = 0; i < a.length; i++) {
1233
+ const d = a[i] - b[i];
1234
+ if (d === 0) continue;
1235
+ if (Number.isNaN(d)) return NaN;
1236
+ const ad = Math.abs(d);
1237
+ if (scale < ad) {
1238
+ const r = scale / ad;
1239
+ ssq = 1 + ssq * r * r;
1240
+ scale = ad;
1241
+ } else {
1242
+ const r = ad / scale;
1243
+ ssq += r * r;
1244
+ }
1245
+ }
1246
+ return scale === 0 ? 0 : scale * Math.sqrt(ssq);
1384
1247
  }
1385
1248
  function sumSquaredDeviations(xs) {
1386
- const n = xs.length;
1387
- if (n < 2) return 0;
1388
- const mean = pairwiseSum(xs) / n;
1389
- const { sumD, sumDD } = pairwiseDeviationSums(xs, mean, 0, n);
1390
- const corrected = sumDD - sumD * sumD / n;
1391
- return corrected < 0 ? 0 : corrected;
1249
+ const n = xs.length;
1250
+ if (n < 2) return 0;
1251
+ const { sumD, sumDD } = pairwiseDeviationSums(xs, pairwiseSum(xs) / n, 0, n);
1252
+ const corrected = sumDD - sumD * sumD / n;
1253
+ return corrected < 0 ? 0 : corrected;
1392
1254
  }
1393
1255
  function pairwiseDeviationSums(xs, mean, start, end) {
1394
- const n = end - start;
1395
- if (n <= PAIRWISE_BLOCK) {
1396
- let d0 = 0, d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0, d7 = 0;
1397
- let q0 = 0, q1 = 0, q2 = 0, q3 = 0, q4 = 0, q5 = 0, q6 = 0, q7 = 0;
1398
- let i = start;
1399
- const limit = start + (n - n % 8);
1400
- for (; i < limit; i += 8) {
1401
- const a0 = xs[i] - mean;
1402
- const a1 = xs[i + 1] - mean;
1403
- const a2 = xs[i + 2] - mean;
1404
- const a3 = xs[i + 3] - mean;
1405
- const a4 = xs[i + 4] - mean;
1406
- const a5 = xs[i + 5] - mean;
1407
- const a6 = xs[i + 6] - mean;
1408
- const a7 = xs[i + 7] - mean;
1409
- d0 += a0;
1410
- q0 += a0 * a0;
1411
- d1 += a1;
1412
- q1 += a1 * a1;
1413
- d2 += a2;
1414
- q2 += a2 * a2;
1415
- d3 += a3;
1416
- q3 += a3 * a3;
1417
- d4 += a4;
1418
- q4 += a4 * a4;
1419
- d5 += a5;
1420
- q5 += a5 * a5;
1421
- d6 += a6;
1422
- q6 += a6 * a6;
1423
- d7 += a7;
1424
- q7 += a7 * a7;
1425
- }
1426
- let tailD = 0;
1427
- let tailDD = 0;
1428
- for (; i < end; i++) {
1429
- const a = xs[i] - mean;
1430
- tailD += a;
1431
- tailDD += a * a;
1432
- }
1433
- return {
1434
- sumD: d0 + d1 + (d2 + d3) + (d4 + d5 + (d6 + d7)) + tailD,
1435
- sumDD: q0 + q1 + (q2 + q3) + (q4 + q5 + (q6 + q7)) + tailDD
1436
- };
1437
- }
1438
- const half = (n >> 1) - (n >> 1) % 8;
1439
- const left = pairwiseDeviationSums(xs, mean, start, start + half);
1440
- const right = pairwiseDeviationSums(xs, mean, start + half, end);
1441
- return { sumD: left.sumD + right.sumD, sumDD: left.sumDD + right.sumDD };
1256
+ const n = end - start;
1257
+ if (n <= PAIRWISE_BLOCK) {
1258
+ let d0 = 0, d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0, d7 = 0;
1259
+ let q0 = 0, q1 = 0, q2 = 0, q3 = 0, q4 = 0, q5 = 0, q6 = 0, q7 = 0;
1260
+ let i = start;
1261
+ const limit = start + (n - n % 8);
1262
+ for (; i < limit; i += 8) {
1263
+ const a0 = xs[i] - mean;
1264
+ const a1 = xs[i + 1] - mean;
1265
+ const a2 = xs[i + 2] - mean;
1266
+ const a3 = xs[i + 3] - mean;
1267
+ const a4 = xs[i + 4] - mean;
1268
+ const a5 = xs[i + 5] - mean;
1269
+ const a6 = xs[i + 6] - mean;
1270
+ const a7 = xs[i + 7] - mean;
1271
+ d0 += a0;
1272
+ q0 += a0 * a0;
1273
+ d1 += a1;
1274
+ q1 += a1 * a1;
1275
+ d2 += a2;
1276
+ q2 += a2 * a2;
1277
+ d3 += a3;
1278
+ q3 += a3 * a3;
1279
+ d4 += a4;
1280
+ q4 += a4 * a4;
1281
+ d5 += a5;
1282
+ q5 += a5 * a5;
1283
+ d6 += a6;
1284
+ q6 += a6 * a6;
1285
+ d7 += a7;
1286
+ q7 += a7 * a7;
1287
+ }
1288
+ let tailD = 0;
1289
+ let tailDD = 0;
1290
+ for (; i < end; i++) {
1291
+ const a = xs[i] - mean;
1292
+ tailD += a;
1293
+ tailDD += a * a;
1294
+ }
1295
+ return {
1296
+ sumD: d0 + d1 + (d2 + d3) + (d4 + d5 + (d6 + d7)) + tailD,
1297
+ sumDD: q0 + q1 + (q2 + q3) + (q4 + q5 + (q6 + q7)) + tailDD
1298
+ };
1299
+ }
1300
+ const half = (n >> 1) - (n >> 1) % 8;
1301
+ const left = pairwiseDeviationSums(xs, mean, start, start + half);
1302
+ const right = pairwiseDeviationSums(xs, mean, start + half, end);
1303
+ return {
1304
+ sumD: left.sumD + right.sumD,
1305
+ sumDD: left.sumDD + right.sumDD
1306
+ };
1442
1307
  }
1443
1308
  function neumaierCumsum(xs, out) {
1444
- let sum = 0;
1445
- let c = 0;
1446
- for (let i = 0; i < xs.length; i++) {
1447
- const x = xs[i];
1448
- const t = sum + x;
1449
- c += Math.abs(sum) >= Math.abs(x) ? sum - t + x : x - t + sum;
1450
- sum = t;
1451
- out[i] = sum + c;
1452
- }
1309
+ let sum = 0;
1310
+ let c = 0;
1311
+ for (let i = 0; i < xs.length; i++) {
1312
+ const x = xs[i];
1313
+ const t = sum + x;
1314
+ c += Math.abs(sum) >= Math.abs(x) ? sum - t + x : x - t + sum;
1315
+ sum = t;
1316
+ out[i] = sum + c;
1317
+ }
1453
1318
  }
1454
-
1455
- // src/index.ts
1456
- var VERSION = "0.18.1";
1457
- export {
1458
- ALL_UNITS,
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
- };
1319
+ //#endregion
1320
+ //#region src/numeric/rng.ts
1321
+ function mulberry32(seed) {
1322
+ let s = seed | 0;
1323
+ return () => {
1324
+ s = s + 1831565813 | 0;
1325
+ let z = s;
1326
+ z = Math.imul(z ^ z >>> 15, z | 1);
1327
+ z ^= z + Math.imul(z ^ z >>> 7, z | 61);
1328
+ z = (z ^ z >>> 14) >>> 0;
1329
+ return z / 4294967296;
1330
+ };
1331
+ }
1332
+ function boxMuller(u1, u2) {
1333
+ const r = Math.sqrt(-2 * Math.log(u1 === 0 ? Number.EPSILON : u1));
1334
+ const theta = 2 * Math.PI * u2;
1335
+ return [r * Math.cos(theta), r * Math.sin(theta)];
1336
+ }
1337
+ const FNV_OFFSET = 2166136261;
1338
+ const FNV_PRIME = 16777619;
1339
+ function fnv1a32(bytes) {
1340
+ let h = FNV_OFFSET;
1341
+ for (let i = 0; i < bytes.length; i++) {
1342
+ h ^= bytes[i];
1343
+ h = Math.imul(h, FNV_PRIME) >>> 0;
1344
+ }
1345
+ return h >>> 0;
1346
+ }
1347
+ const textEncoder = new TextEncoder();
1348
+ function coerceSeedInt(seed) {
1349
+ if (typeof seed === "number") return seed | 0;
1350
+ if (typeof seed === "string") return fnv1a32(textEncoder.encode(seed)) | 0;
1351
+ throw new TypeError("seed must be a number or a string");
1352
+ }
1353
+ function mixFork(parentSeed, label) {
1354
+ const payload = [
1355
+ parentSeed >>> 0 & 255,
1356
+ parentSeed >>> 8 & 255,
1357
+ parentSeed >>> 16 & 255,
1358
+ parentSeed >>> 24 & 255
1359
+ ];
1360
+ if (typeof label === "number") {
1361
+ const n = label | 0;
1362
+ payload.push(0, n >>> 0 & 255, n >>> 8 & 255, n >>> 16 & 255, n >>> 24 & 255);
1363
+ } else if (typeof label === "string") {
1364
+ payload.push(1);
1365
+ for (const b of textEncoder.encode(label)) payload.push(b);
1366
+ } else throw new TypeError("fork label must be a string or a number");
1367
+ return fnv1a32(new Uint8Array(payload)) | 0;
1368
+ }
1369
+ function makeSeededRng(seedInt) {
1370
+ const uniform = mulberry32(seedInt);
1371
+ let spare = null;
1372
+ return Object.freeze({
1373
+ seed: seedInt,
1374
+ next: () => uniform(),
1375
+ nextInt(maxExclusive) {
1376
+ if (!Number.isInteger(maxExclusive) || maxExclusive < 1 || maxExclusive > 4294967296) throw new RangeError("maxExclusive must be an integer in [1, 4294967296]");
1377
+ return Math.floor(uniform() * maxExclusive);
1378
+ },
1379
+ normal() {
1380
+ if (spare !== null) {
1381
+ const z = spare;
1382
+ spare = null;
1383
+ return z;
1384
+ }
1385
+ const pair = boxMuller(uniform(), uniform());
1386
+ spare = pair[1];
1387
+ return pair[0];
1388
+ },
1389
+ fork(label) {
1390
+ return makeSeededRng(mixFork(seedInt, label));
1391
+ }
1392
+ });
1393
+ }
1394
+ function createSeededRng(seed) {
1395
+ return makeSeededRng(coerceSeedInt(seed));
1396
+ }
1397
+ //#endregion
1398
+ //#region src/numeric/strides.ts
1399
+ function rowMajorStrides(shape) {
1400
+ const strides = new Array(shape.length);
1401
+ let acc = 1;
1402
+ for (let k = shape.length - 1; k >= 0; k--) {
1403
+ strides[k] = acc;
1404
+ acc *= shape[k];
1405
+ }
1406
+ return strides;
1407
+ }
1408
+ //#endregion
1409
+ //#region src/index.ts
1410
+ const VERSION = "0.20.0";
1411
+ //#endregion
1412
+ 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, boxMuller, clearExactScaleNumberCache, create, createFactory, createMathTSTyped, createRangeClass, createSeededRng, 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, mulberry32, multiplyExactScales, multiplyScalar, namedExactScale, namedIrrationalScale, neumaierCumsum, neumaierSum, norm2, number, pairwiseDot, pairwiseSum, pow, powerExactScale, ratioExactScale, readScaleExpression, readUnitScale, registerNativeTypes, registry, round, rowMajorStrides, scaledDistance, subtractScalar, sumSquaredDeviations, toSiDimensionVector, toSiDimensions, typed, unitPrefixExactScale, unitRowExactOffset, unitRowExactScale, unitRowValue };