@danielsimonjr/mathts-compat 0.1.8 → 0.2.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
@@ -66,6 +66,9 @@ declare function atan(x: number): number;
66
66
  declare function atan2(y: number, x: number): number;
67
67
  declare const sum: typeof sum$1;
68
68
  declare const mean: typeof mean$1;
69
+ type Normalization = 'unbiased' | 'uncorrected' | 'biased';
70
+ declare function variance(data: unknown, normalization?: Normalization): number;
71
+ declare function std(data: unknown, normalization?: Normalization): number;
69
72
  declare const min: typeof min$1;
70
73
  declare const max: typeof max$1;
71
74
  declare const gcd: typeof gcd$1;
@@ -90,9 +93,10 @@ declare function im(c: Complex | number): number;
90
93
  */
91
94
  declare function arg(c: Complex): number;
92
95
  /**
93
- * Transpose matrix (mathjs-compatible)
96
+ * Transpose matrix (mathjs-compatible). Array in → array out (mathjs returns a
97
+ * plain nested array for array input, a Matrix for Matrix input).
94
98
  */
95
- declare function transpose(m: DenseMatrix | SparseMatrix | number[][]): DenseMatrix | SparseMatrix;
99
+ declare function transpose(m: DenseMatrix | SparseMatrix | number[][]): DenseMatrix | SparseMatrix | number[][];
96
100
  /**
97
101
  * Matrix determinant (mathjs-compatible)
98
102
  *
@@ -157,6 +161,8 @@ declare const shims: {
157
161
  atan2: typeof atan2;
158
162
  sum: typeof sum$1;
159
163
  mean: typeof mean$1;
164
+ std: typeof std;
165
+ variance: typeof variance;
160
166
  min: typeof min$1;
161
167
  max: typeof max$1;
162
168
  gcd: typeof gcd$1;
@@ -194,6 +200,21 @@ declare const shims: {
194
200
  NaN: number;
195
201
  };
196
202
 
203
+ /**
204
+ * GC12 — mathjs-style fluent `chain` API.
205
+ *
206
+ * chain(3).add(4).multiply(2).done() // 14
207
+ *
208
+ * Each math function `f` becomes a chain method that calls `f(value, ...args)`
209
+ * and returns a new chain wrapping the result. `.done()` / `.valueOf()` unwrap.
210
+ */
211
+ interface Chain {
212
+ done(): unknown;
213
+ valueOf(): unknown;
214
+ toString(): string;
215
+ [method: string]: (...args: unknown[]) => Chain | unknown;
216
+ }
217
+
197
218
  /**
198
219
  * @danielsimonjr/mathts-compat - mathjs Compatibility Layer
199
220
  *
@@ -239,6 +260,8 @@ interface MathInstance {
239
260
  * `unknown` and can be narrowed by the caller.
240
261
  */
241
262
  [name: string]: unknown;
263
+ /** Fluent chain: `math.chain(x).add(3).done()`. */
264
+ chain: (value: unknown) => Chain;
242
265
  config: (newConfig?: Partial<MathJSConfig>) => MathJSConfig;
243
266
  complex: typeof shims.complex;
244
267
  fraction: typeof shims.fraction;
@@ -263,6 +286,8 @@ interface MathInstance {
263
286
  atan2: typeof shims.atan2;
264
287
  sum: typeof shims.sum;
265
288
  mean: typeof shims.mean;
289
+ std: typeof shims.std;
290
+ variance: typeof shims.variance;
266
291
  min: typeof shims.min;
267
292
  max: typeof shims.max;
268
293
  gcd: typeof shims.gcd;
@@ -326,4 +351,4 @@ declare function create(factories?: Record<string, unknown>, config?: Partial<Ma
326
351
  */
327
352
  declare const all: Record<string, unknown>;
328
353
 
329
- export { Infinity_, LN10, LN2, LOG10E, LOG2E, type MathInstance, type MathJSConfig, NaN_, SQRT1_2, SQRT2, abs, acos, add, all, arg, asin, atan, atan2, bignumber, ceil, complex, conj, cos, create, det, divide, e, exp, floor, fraction, gcd, i, identity, im, isBigNumber_, isComplex_, isFraction_, isMatrix, isNumber_, lcm, log, matrix, max, mean, min, multiply, ones, phi, pi, pow, re, round, shims, sin, size, sparse, sqrt, subtract, sum, tan, tau, transpose, zeros };
354
+ export { Infinity_, LN10, LN2, LOG10E, LOG2E, type MathInstance, type MathJSConfig, NaN_, SQRT1_2, SQRT2, abs, acos, add, all, arg, asin, atan, atan2, bignumber, ceil, complex, conj, cos, create, det, divide, e, exp, floor, fraction, gcd, i, identity, im, isBigNumber_, isComplex_, isFraction_, isMatrix, isNumber_, lcm, log, matrix, max, mean, min, multiply, ones, phi, pi, pow, re, round, shims, sin, size, sparse, sqrt, std, subtract, sum, tan, tau, transpose, variance, zeros };
package/dist/index.js CHANGED
@@ -33,7 +33,13 @@ import {
33
33
  floor as _floor,
34
34
  ceil as _ceil
35
35
  } from "@danielsimonjr/mathts-functions";
36
- import { DenseMatrix, SparseMatrix } from "@danielsimonjr/mathts-matrix";
36
+ import {
37
+ DenseMatrix,
38
+ SparseMatrix,
39
+ add as _matAdd,
40
+ subtract as _matSubtract,
41
+ multiply as _matMultiply
42
+ } from "@danielsimonjr/mathts-matrix";
37
43
  function complex(re2, im2) {
38
44
  if (re2 === void 0) {
39
45
  return COMPLEX_ZERO;
@@ -92,9 +98,38 @@ function sparse(data) {
92
98
  }
93
99
  return SparseMatrix.fromDense(DenseMatrix.fromArray(data));
94
100
  }
95
- var add = _add;
96
- var subtract = _subtract;
97
- var multiply = _multiply;
101
+ var isMatrixLike = (x) => x instanceof DenseMatrix || Array.isArray(x) && x.length > 0 && Array.isArray(x[0]);
102
+ var toDenseMatrix = (x) => x instanceof DenseMatrix ? x : DenseMatrix.fromArray(x);
103
+ var unwrap = (r) => r instanceof DenseMatrix ? r.toArray() : r;
104
+ var applyArithmetic = (scalarFn, matrixFn, a, b) => {
105
+ if (isMatrixLike(a) || isMatrixLike(b)) {
106
+ const aa = isMatrixLike(a) ? toDenseMatrix(a) : a;
107
+ const bb = isMatrixLike(b) ? toDenseMatrix(b) : b;
108
+ return unwrap(matrixFn(aa, bb));
109
+ }
110
+ return scalarFn(a, b);
111
+ };
112
+ var makeArithmetic = (scalarFn, matrixFn) => (...args) => {
113
+ if (args.length === 0) {
114
+ throw new TypeError("Too few arguments (expected at least 1)");
115
+ }
116
+ if (args.length === 1) {
117
+ return args[0];
118
+ }
119
+ return args.reduce((acc, next) => applyArithmetic(scalarFn, matrixFn, acc, next));
120
+ };
121
+ var add = makeArithmetic(
122
+ _add,
123
+ _matAdd
124
+ );
125
+ var subtract = makeArithmetic(
126
+ _subtract,
127
+ _matSubtract
128
+ );
129
+ var multiply = makeArithmetic(
130
+ _multiply,
131
+ _matMultiply
132
+ );
98
133
  var divide = _divide;
99
134
  var pow = _pow;
100
135
  var sqrt = _sqrt;
@@ -118,6 +153,23 @@ function atan2(y, x) {
118
153
  }
119
154
  var sum = _sum;
120
155
  var mean = _mean;
156
+ function toNumericArray(data) {
157
+ if (data instanceof DenseMatrix) return data.toArray().flat(Infinity);
158
+ if (Array.isArray(data)) return data.flat(Infinity);
159
+ return [data];
160
+ }
161
+ function variance(data, normalization = "unbiased") {
162
+ const arr = toNumericArray(data);
163
+ const n = arr.length;
164
+ if (n === 0) return NaN;
165
+ const mu = arr.reduce((s, x) => s + x, 0) / n;
166
+ const ss = arr.reduce((s, x) => s + (x - mu) * (x - mu), 0);
167
+ const denom = normalization === "uncorrected" ? n : normalization === "biased" ? n + 1 : n > 1 ? n - 1 : n;
168
+ return ss / denom;
169
+ }
170
+ function std(data, normalization = "unbiased") {
171
+ return Math.sqrt(variance(data, normalization));
172
+ }
121
173
  var min = _min;
122
174
  var max = _max;
123
175
  var gcd = _gcd;
@@ -149,7 +201,7 @@ function arg(c) {
149
201
  }
150
202
  function transpose(m) {
151
203
  if (Array.isArray(m)) {
152
- return DenseMatrix.fromArray(m).transpose();
204
+ return DenseMatrix.fromArray(m).transpose().toArray();
153
205
  }
154
206
  return m.transpose();
155
207
  }
@@ -281,6 +333,8 @@ var shims = {
281
333
  // Statistics
282
334
  sum,
283
335
  mean,
336
+ std,
337
+ variance,
284
338
  min,
285
339
  max,
286
340
  // Number theory
@@ -324,6 +378,30 @@ var shims = {
324
378
  NaN: NaN_
325
379
  };
326
380
 
381
+ // src/chain.ts
382
+ function createChain(math) {
383
+ function chain(value) {
384
+ const base = {
385
+ done: () => value,
386
+ valueOf: () => value,
387
+ toString: () => String(value)
388
+ };
389
+ return new Proxy(base, {
390
+ get(target, prop) {
391
+ if (prop in target || typeof prop === "symbol") {
392
+ return target[prop];
393
+ }
394
+ const fn = math[prop];
395
+ if (typeof fn === "function") {
396
+ return (...args) => chain(fn(value, ...args));
397
+ }
398
+ return void 0;
399
+ }
400
+ });
401
+ }
402
+ return chain;
403
+ }
404
+
327
405
  // src/index.ts
328
406
  import * as mathFunctions from "@danielsimonjr/mathts-functions";
329
407
  import {
@@ -351,7 +429,7 @@ var defaultConfig = {
351
429
  };
352
430
  function create(factories = all, config = {}) {
353
431
  const currentConfig = { ...defaultConfig, ...config };
354
- return {
432
+ const instance = {
355
433
  // Full @danielsimonjr/mathts-functions namespace — det, integrate, eigs,
356
434
  // simplify, and the rest. The compat-specific shims below overlay it.
357
435
  ...factories,
@@ -389,6 +467,8 @@ function create(factories = all, config = {}) {
389
467
  // Statistics
390
468
  sum: shims.sum,
391
469
  mean: shims.mean,
470
+ std: shims.std,
471
+ variance: shims.variance,
392
472
  min: shims.min,
393
473
  max: shims.max,
394
474
  // Number theory
@@ -429,8 +509,15 @@ function create(factories = all, config = {}) {
429
509
  SQRT2: shims.SQRT2,
430
510
  SQRT1_2: shims.SQRT1_2,
431
511
  Infinity: shims.Infinity,
432
- NaN: shims.NaN
512
+ NaN: shims.NaN,
513
+ // Placeholder; rebound below once `instance` exists so chain methods can
514
+ // resolve against the full surfaced namespace.
515
+ chain: (() => {
516
+ throw new Error("chain not initialized");
517
+ })
433
518
  };
519
+ instance.chain = createChain(instance);
520
+ return instance;
434
521
  }
435
522
  var all = {
436
523
  ...mathFunctions
@@ -506,10 +593,12 @@ export {
506
593
  size,
507
594
  sparse,
508
595
  sqrt,
596
+ std,
509
597
  subtract,
510
598
  sum,
511
599
  tan,
512
600
  tau,
513
601
  transpose,
602
+ variance,
514
603
  zeros
515
604
  };
package/package.json CHANGED
@@ -1,56 +1,56 @@
1
- {
2
- "name": "@danielsimonjr/mathts-compat",
3
- "version": "0.1.8",
4
- "description": "mathjs compatibility layer for MathTS",
5
- "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
12
- }
13
- },
14
- "files": [
15
- "dist",
16
- "README.md"
17
- ],
18
- "scripts": {
19
- "build": "tsup src/index.ts --format esm --dts --clean",
20
- "dev": "tsup src/index.ts --format esm --dts --watch",
21
- "typecheck": "tsc --noEmit",
22
- "test": "vitest run",
23
- "test:coverage": "vitest run --coverage",
24
- "build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
25
- },
26
- "dependencies": {
27
- "@danielsimonjr/mathts-core": "^0.1.4",
28
- "@danielsimonjr/mathts-functions": "^0.2.7",
29
- "@danielsimonjr/mathts-matrix": "^0.1.6",
30
- "@danielsimonjr/mathts-parallel": "^0.2.2"
31
- },
32
- "devDependencies": {
33
- "@types/node": "^25.5.2",
34
- "tsup": "^8.0.0",
35
- "typescript": "^5.3.0",
36
- "vitest": "^4.1.5"
37
- },
38
- "keywords": [
39
- "math",
40
- "mathjs",
41
- "compat",
42
- "compatibility",
43
- "migration"
44
- ],
45
- "author": "Daniel Simon Jr.",
46
- "license": "MIT",
47
- "repository": {
48
- "type": "git",
49
- "url": "https://github.com/danielsimonjr/mathts",
50
- "directory": "compat"
51
- },
52
- "module": "./dist/index.js",
53
- "publishConfig": {
54
- "access": "public"
55
- }
56
- }
1
+ {
2
+ "name": "@danielsimonjr/mathts-compat",
3
+ "version": "0.2.0",
4
+ "description": "mathjs compatibility layer for MathTS",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup src/index.ts --format esm --dts --clean",
20
+ "dev": "tsup src/index.ts --format esm --dts --watch",
21
+ "typecheck": "tsc --noEmit",
22
+ "test": "vitest run",
23
+ "test:coverage": "vitest run --coverage",
24
+ "build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
25
+ },
26
+ "dependencies": {
27
+ "@danielsimonjr/mathts-core": "^0.1.5",
28
+ "@danielsimonjr/mathts-functions": "^0.3.0",
29
+ "@danielsimonjr/mathts-matrix": "^0.1.10",
30
+ "@danielsimonjr/mathts-parallel": "^0.3.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^25.5.2",
34
+ "tsup": "^8.0.0",
35
+ "typescript": "^5.3.0",
36
+ "vitest": "^4.1.5"
37
+ },
38
+ "keywords": [
39
+ "math",
40
+ "mathjs",
41
+ "compat",
42
+ "compatibility",
43
+ "migration"
44
+ ],
45
+ "author": "Daniel Simon Jr.",
46
+ "license": "MIT",
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "https://github.com/danielsimonjr/mathts",
50
+ "directory": "compat"
51
+ },
52
+ "module": "./dist/index.js",
53
+ "publishConfig": {
54
+ "access": "public"
55
+ }
56
+ }