@danielsimonjr/mathts-core 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2513,6 +2513,47 @@ declare function neumaierSum(xs: ArrayLike<number>): number;
2513
2513
  * so the accumulator only ever holds ratios in [0, 1].
2514
2514
  */
2515
2515
  declare function norm2(xs: ArrayLike<number>): number;
2516
+ /**
2517
+ * Dot product `Σ aᵢ·bᵢ` with **pairwise (cascade) summation** of the products — the accuracy of
2518
+ * {@link pairwiseSum} applied to a dot without materialising the product array.
2519
+ *
2520
+ * A naive `s += a[i]*b[i]` loop carries the same **O(n)·ε** error as a naive sum; measured against
2521
+ * an exact reference on an ill-conditioned dot (large mean × small factor, n = 10⁶) it is ~18×
2522
+ * worse than `np.dot`, which sums pairwise. This closes that gap — and, like {@link pairwiseSum},
2523
+ * costs the same number of flops as the naive loop.
2524
+ *
2525
+ * The two ranges are assumed the same length; callers guard that. Reads only `[start, end)`.
2526
+ */
2527
+ declare function pairwiseDot(a: ArrayLike<number>, b: ArrayLike<number>, start?: number, end?: number): number;
2528
+ /**
2529
+ * Euclidean distance `‖a − b‖₂` that does **not** overflow or underflow — {@link norm2}'s BLAS
2530
+ * `dnrm2` scaling applied to the elementwise difference.
2531
+ *
2532
+ * `distance` is just a 2-norm, so the obvious `sqrt(Σ(aᵢ−bᵢ)²)` inherits norm's pathology: it
2533
+ * squares before it adds, overflowing to `Infinity` and — the dangerous case — **flushing to a
2534
+ * silent 0** for small differences well inside the representable range. NumPy's `linalg.norm` has
2535
+ * the same bug; scaling on the largest *difference* seen avoids both. The scale tracks the residual
2536
+ * `a − b`, not the inputs, so it stays accurate even when `a` and `b` are individually huge.
2537
+ *
2538
+ * The two ranges are assumed the same length; callers guard that.
2539
+ */
2540
+ declare function scaledDistance(a: ArrayLike<number>, b: ArrayLike<number>): number;
2541
+ /**
2542
+ * Cumulative sum with **Neumaier compensation** — each prefix total is written to `out[i]` carrying
2543
+ * the low-order bits a naive running sum throws away.
2544
+ *
2545
+ * A cumulative sum is an inherently sequential prefix scan, so pairwise summation does not apply:
2546
+ * you need every intermediate total, not just the final one. `np.cumsum` therefore accumulates
2547
+ * naively and its tail drifts by **O(n)·ε** (relErr ~1.3e-11 over 10⁶ terms). Carrying a running
2548
+ * compensation costs a few extra flops per element, no extra memory, and makes every prefix exact
2549
+ * for practical purposes — a strict improvement over NumPy where accumulated drift matters (e.g.
2550
+ * integrating a signal).
2551
+ *
2552
+ * Writes `out[i]` for `i` in `[0, xs.length)`; `out` may be a `number[]` or a `Float64Array`.
2553
+ */
2554
+ declare function neumaierCumsum(xs: ArrayLike<number>, out: {
2555
+ [index: number]: number;
2556
+ }): void;
2516
2557
 
2517
2558
  /**
2518
2559
  * @danielsimonjr/mathts-core - Core types and utilities for MathTS
@@ -2521,4 +2562,4 @@ declare function norm2(xs: ArrayLike<number>): number;
2521
2562
 
2522
2563
  declare const VERSION = "0.1.0";
2523
2564
 
2524
- export { ALL_UNITS, BASE_UNITS, BEST_PREFIXES, BIGNUMBER_E, BIGNUMBER_LN10, BIGNUMBER_LN2, BIGNUMBER_NEG_ONE, BIGNUMBER_ONE, BIGNUMBER_PI, BIGNUMBER_TEN, BIGNUMBER_ZERO, type BackendType, BigNumber, type BigNumberConfig, COMPLEX_NEG_ONE, COMPLEX_ONE, COMPLEX_ZERO, Complex, type ConversionDef, DEFAULT_CONFIG, DERIVED_UNITS, DIMENSIONLESS, DUAL_UNARY_RULES, DimensionMismatchError, type Dimensions, Dual, type DualUnaryRule, type DualUnaryRuleName, E, FRACTION_HALF, FRACTION_NEG_ONE, FRACTION_ONE, FRACTION_QUARTER, FRACTION_THIRD, FRACTION_ZERO, type FactoryDependencies, type FactoryFunction, type FactoryImport, Fraction, FunctionRegistry, I, type IBigNumber, type IComplex, type IFraction, type IMatrix, LN10, LN2, LOG10E, LOG2E, MATHTS_CONVERSIONS, MATHTS_TYPES, type MathTSConfig, type MathTSValue, type MatrixBackend, type MatrixDimensions, type NumericScalar, type NumericType, PHI, PI, Range, type RangeForEachCallback, type RangeFormatOptions, type RangeJSON, type RangeMapCallback, type RoundingMode, SI_PREFIXES, SQRT1_2, SQRT2, type Scalar, TAU, type TypeDef, TypeRegistry, UNIT_ALIASES, type UnitDef, UnitParseError, VERSION, abs, addScalar, createFactory, createMathTSTyped, createRangeClass, createTypedFunction, dim, divideScalar, equal, fix, getPrefix, getUnitDef, initTypedWasm, isArray, isBigInt, isBigNumber, isBoolean, isComplex, isDenseMatrix, isDual, isFraction, isFunction, isMatrix, isNull, isNumber, isNumeric, isObject, isSparseMatrix, isString, isTypedWasmAvailable, isUndefined, isUnit, math, mathTyped, multiplyScalar, neumaierSum, norm2, number, pairwiseSum, pow, registerNativeTypes, registry, round, subtractScalar };
2565
+ export { ALL_UNITS, BASE_UNITS, BEST_PREFIXES, BIGNUMBER_E, BIGNUMBER_LN10, BIGNUMBER_LN2, BIGNUMBER_NEG_ONE, BIGNUMBER_ONE, BIGNUMBER_PI, BIGNUMBER_TEN, BIGNUMBER_ZERO, type BackendType, BigNumber, type BigNumberConfig, COMPLEX_NEG_ONE, COMPLEX_ONE, COMPLEX_ZERO, Complex, type ConversionDef, DEFAULT_CONFIG, DERIVED_UNITS, DIMENSIONLESS, DUAL_UNARY_RULES, DimensionMismatchError, type Dimensions, Dual, type DualUnaryRule, type DualUnaryRuleName, E, FRACTION_HALF, FRACTION_NEG_ONE, FRACTION_ONE, FRACTION_QUARTER, FRACTION_THIRD, FRACTION_ZERO, type FactoryDependencies, type FactoryFunction, type FactoryImport, Fraction, FunctionRegistry, I, type IBigNumber, type IComplex, type IFraction, type IMatrix, LN10, LN2, LOG10E, LOG2E, MATHTS_CONVERSIONS, MATHTS_TYPES, type MathTSConfig, type MathTSValue, type MatrixBackend, type MatrixDimensions, type NumericScalar, type NumericType, PHI, PI, Range, type RangeForEachCallback, type RangeFormatOptions, type RangeJSON, type RangeMapCallback, type RoundingMode, SI_PREFIXES, SQRT1_2, SQRT2, type Scalar, TAU, type TypeDef, TypeRegistry, UNIT_ALIASES, type UnitDef, UnitParseError, VERSION, abs, addScalar, createFactory, createMathTSTyped, createRangeClass, createTypedFunction, dim, divideScalar, equal, fix, getPrefix, getUnitDef, initTypedWasm, isArray, isBigInt, isBigNumber, isBoolean, isComplex, isDenseMatrix, isDual, isFraction, isFunction, isMatrix, isNull, isNumber, isNumeric, isObject, isSparseMatrix, isString, isTypedWasmAvailable, isUndefined, isUnit, math, mathTyped, multiplyScalar, neumaierCumsum, neumaierSum, norm2, number, pairwiseDot, pairwiseSum, pow, registerNativeTypes, registry, round, scaledDistance, subtractScalar };
package/dist/index.js CHANGED
@@ -1248,6 +1248,66 @@ function norm2(xs) {
1248
1248
  }
1249
1249
  return scale === 0 ? 0 : scale * Math.sqrt(ssq);
1250
1250
  }
1251
+ function pairwiseDot(a, b, start = 0, end = a.length) {
1252
+ const n = end - start;
1253
+ if (n <= PAIRWISE_BLOCK) {
1254
+ let s0 = 0;
1255
+ let s1 = 0;
1256
+ let s2 = 0;
1257
+ let s3 = 0;
1258
+ let s4 = 0;
1259
+ let s5 = 0;
1260
+ let s6 = 0;
1261
+ let s7 = 0;
1262
+ let i = start;
1263
+ const limit = start + (n - n % 8);
1264
+ for (; i < limit; i += 8) {
1265
+ s0 += a[i] * b[i];
1266
+ s1 += a[i + 1] * b[i + 1];
1267
+ s2 += a[i + 2] * b[i + 2];
1268
+ s3 += a[i + 3] * b[i + 3];
1269
+ s4 += a[i + 4] * b[i + 4];
1270
+ s5 += a[i + 5] * b[i + 5];
1271
+ s6 += a[i + 6] * b[i + 6];
1272
+ s7 += a[i + 7] * b[i + 7];
1273
+ }
1274
+ let tail = 0;
1275
+ for (; i < end; i++) tail += a[i] * b[i];
1276
+ return s0 + s1 + (s2 + s3) + (s4 + s5 + (s6 + s7)) + tail;
1277
+ }
1278
+ const half = (n >> 1) - (n >> 1) % 8;
1279
+ return pairwiseDot(a, b, start, start + half) + pairwiseDot(a, b, start + half, end);
1280
+ }
1281
+ function scaledDistance(a, b) {
1282
+ let scale = 0;
1283
+ let ssq = 1;
1284
+ for (let i = 0; i < a.length; i++) {
1285
+ const d = a[i] - b[i];
1286
+ if (d === 0) continue;
1287
+ if (Number.isNaN(d)) return NaN;
1288
+ const ad = Math.abs(d);
1289
+ if (scale < ad) {
1290
+ const r = scale / ad;
1291
+ ssq = 1 + ssq * r * r;
1292
+ scale = ad;
1293
+ } else {
1294
+ const r = ad / scale;
1295
+ ssq += r * r;
1296
+ }
1297
+ }
1298
+ return scale === 0 ? 0 : scale * Math.sqrt(ssq);
1299
+ }
1300
+ function neumaierCumsum(xs, out) {
1301
+ let sum = 0;
1302
+ let c = 0;
1303
+ for (let i = 0; i < xs.length; i++) {
1304
+ const x = xs[i];
1305
+ const t = sum + x;
1306
+ c += Math.abs(sum) >= Math.abs(x) ? sum - t + x : x - t + sum;
1307
+ sum = t;
1308
+ out[i] = sum + c;
1309
+ }
1310
+ }
1251
1311
 
1252
1312
  // src/index.ts
1253
1313
  var VERSION = "0.1.0";
@@ -1339,14 +1399,17 @@ export {
1339
1399
  math,
1340
1400
  mathTyped,
1341
1401
  multiplyScalar,
1402
+ neumaierCumsum,
1342
1403
  neumaierSum,
1343
1404
  norm2,
1344
1405
  number,
1406
+ pairwiseDot,
1345
1407
  pairwiseSum,
1346
1408
  pow,
1347
1409
  registerNativeTypes,
1348
1410
  registry,
1349
1411
  round,
1412
+ scaledDistance,
1350
1413
  subtractScalar,
1351
1414
  typed
1352
1415
  };
package/package.json CHANGED
@@ -1,59 +1,59 @@
1
- {
2
- "name": "@danielsimonjr/mathts-core",
3
- "version": "0.7.0",
4
- "description": "Core types, utilities, and configuration for MathTS",
5
- "author": "Daniel Simon Jr.",
6
- "license": "MIT",
7
- "type": "module",
8
- "main": "./dist/index.js",
9
- "module": "./dist/index.js",
10
- "types": "./dist/index.d.ts",
11
- "exports": {
12
- ".": {
13
- "import": "./dist/index.js",
14
- "types": "./dist/index.d.ts"
15
- },
16
- "./internal": {
17
- "import": "./dist/internal.js",
18
- "types": "./dist/internal.d.ts"
19
- }
20
- },
21
- "files": [
22
- "dist",
23
- "README.md"
24
- ],
25
- "scripts": {
26
- "build": "tsup src/index.ts src/internal.ts --format esm --dts --clean",
27
- "dev": "tsup src/index.ts src/internal.ts --format esm --dts --watch",
28
- "test": "vitest run",
29
- "test:watch": "vitest",
30
- "test:coverage": "vitest run --coverage",
31
- "typecheck": "tsc --noEmit",
32
- "lint": "eslint src --ext .ts",
33
- "lint:fix": "eslint src --ext .ts --fix",
34
- "clean": "rm -rf dist",
35
- "build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
36
- },
37
- "dependencies": {
38
- "typed-function": "github:danielsimonjr/typed-function"
39
- },
40
- "devDependencies": {
41
- "@types/node": "^25.5.2",
42
- "tsup": "^8.0.0",
43
- "typescript": "^5.3.0",
44
- "vitest": "^4.1.5"
45
- },
46
- "publishConfig": {
47
- "access": "public"
48
- },
49
- "repository": {
50
- "type": "git",
51
- "url": "https://github.com/danielsimonjr/mathts",
52
- "directory": "core"
53
- },
54
- "keywords": [
55
- "math",
56
- "typescript",
57
- "core"
58
- ]
59
- }
1
+ {
2
+ "name": "@danielsimonjr/mathts-core",
3
+ "version": "0.8.0",
4
+ "description": "Core types, utilities, and configuration for MathTS",
5
+ "author": "Daniel Simon Jr.",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "types": "./dist/index.d.ts"
15
+ },
16
+ "./internal": {
17
+ "import": "./dist/internal.js",
18
+ "types": "./dist/internal.d.ts"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsup src/index.ts src/internal.ts --format esm --dts --clean",
27
+ "dev": "tsup src/index.ts src/internal.ts --format esm --dts --watch",
28
+ "test": "vitest run",
29
+ "test:watch": "vitest",
30
+ "test:coverage": "vitest run --coverage",
31
+ "typecheck": "tsc --noEmit",
32
+ "lint": "eslint src --ext .ts",
33
+ "lint:fix": "eslint src --ext .ts --fix",
34
+ "clean": "rm -rf dist",
35
+ "build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
36
+ },
37
+ "dependencies": {
38
+ "typed-function": "github:danielsimonjr/typed-function"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^25.5.2",
42
+ "tsup": "^8.0.0",
43
+ "typescript": "^5.3.0",
44
+ "vitest": "^4.1.5"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://github.com/danielsimonjr/mathts",
52
+ "directory": "core"
53
+ },
54
+ "keywords": [
55
+ "math",
56
+ "typescript",
57
+ "core"
58
+ ]
59
+ }