@thi.ng/math 5.14.0 → 5.15.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/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 211 standalone projects, maintained as part
10
+ > This is one of 212 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -77,7 +77,7 @@ For Node.js REPL:
77
77
  const math = await import("@thi.ng/math");
78
78
  ```
79
79
 
80
- Package sizes (brotli'd, pre-treeshake): ESM: 5.29 KB
80
+ Package sizes (brotli'd, pre-treeshake): ESM: 5.48 KB
81
81
 
82
82
  ## Dependencies
83
83
 
package/fraction.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ import type { FnN2 } from "@thi.ng/api";
2
+ /**
3
+ * Iteratively computes Greatest Common Divisor of given `a` and `b`.
4
+ *
5
+ * @remarks
6
+ * Reference:
7
+ *
8
+ * - https://en.wikipedia.org/wiki/Greatest_common_divisor
9
+ *
10
+ * @param a
11
+ * @param b
12
+ */
13
+ export declare const gcd: FnN2;
14
+ /**
15
+ * Computes Least Common Multiple for given `a` and `b`. Returns zero if either
16
+ * input is zero.
17
+ *
18
+ * @remarks
19
+ * Reference:
20
+ *
21
+ * - https://en.wikipedia.org/wiki/Least_common_multiple
22
+ *
23
+ * @param a
24
+ * @param b
25
+ */
26
+ export declare const lcm: FnN2;
27
+ /**
28
+ * Converts given `x` to a fraction with optional `maxDenom`inator, using
29
+ * continued fractions for best possible precision.
30
+ *
31
+ * @remarks
32
+ * Reference:
33
+ *
34
+ * - https://en.wikipedia.org/wiki/Continued_fraction
35
+ *
36
+ * @example
37
+ * ```ts tangle:../export/as-fraction.ts
38
+ * import { asFraction } from "@thi.ng/math";
39
+ *
40
+ * console.log(Math.PI, asFraction(Math.PI));
41
+ * // 3.141592653589793 [1146408, 364913]
42
+ *
43
+ * // keep denominator <= 1000
44
+ * console.log(Math.PI, asFraction(Math.PI, 1000));
45
+ * // 3.141592653589793 [ 355, 113 ]
46
+ * ```
47
+ *
48
+ * @param x
49
+ * @param maxDenom
50
+ */
51
+ export declare const asFraction: (x: number, maxDenom?: number) => number[];
52
+ /**
53
+ * Reverse op of {@link asFraction}. Converts a fraction tuple to a JS number.
54
+ *
55
+ * @param fraction
56
+ */
57
+ export declare const asFloat: ([a, b]: [number, number]) => number;
58
+ //# sourceMappingURL=fraction.d.ts.map
package/fraction.js ADDED
@@ -0,0 +1,46 @@
1
+ const gcd = (a, b) => {
2
+ a = Math.abs(a);
3
+ b = Math.abs(b);
4
+ while (b !== 0) {
5
+ const tmp = b;
6
+ b = a % b;
7
+ a = tmp;
8
+ }
9
+ return a;
10
+ };
11
+ const lcm = (a, b) => {
12
+ if (!Number.isFinite(a) || !Number.isFinite(b))
13
+ throw new Error("both inputs must be finite");
14
+ return a && b ? Math.abs(a * b) / gcd(a, b) : 0;
15
+ };
16
+ const asFraction = (x, maxDenom = 1e6) => {
17
+ if (!Number.isFinite(x)) throw new Error("input must be a finite");
18
+ if (Number.isInteger(x)) return [x, 1];
19
+ const sign = Math.sign(x);
20
+ x = Math.abs(x);
21
+ let n0 = 0;
22
+ let n1 = 1;
23
+ let d0 = 1;
24
+ let d1 = 0;
25
+ while (true) {
26
+ const i = Math.floor(x);
27
+ const d2 = i * d1 + d0;
28
+ if (d2 > maxDenom) break;
29
+ const n2 = i * n1 + n0;
30
+ n0 = n1;
31
+ n1 = n2;
32
+ d0 = d1;
33
+ d1 = d2;
34
+ const rem = x - i;
35
+ if (rem < Number.EPSILON) break;
36
+ x = 1 / rem;
37
+ }
38
+ return [sign * n1, d1];
39
+ };
40
+ const asFloat = ([a, b]) => a / b;
41
+ export {
42
+ asFloat,
43
+ asFraction,
44
+ gcd,
45
+ lcm
46
+ };
package/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./eqdelta.js";
6
6
  export * from "./extrema.js";
7
7
  export * from "./easing.js";
8
8
  export * from "./fit.js";
9
+ export * from "./fraction.js";
9
10
  export * from "./int.js";
10
11
  export * from "./interval.js";
11
12
  export * from "./libc.js";
package/index.js CHANGED
@@ -6,6 +6,7 @@ export * from "./eqdelta.js";
6
6
  export * from "./extrema.js";
7
7
  export * from "./easing.js";
8
8
  export * from "./fit.js";
9
+ export * from "./fraction.js";
9
10
  export * from "./int.js";
10
11
  export * from "./interval.js";
11
12
  export * from "./libc.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/math",
3
- "version": "5.14.0",
3
+ "version": "5.15.0",
4
4
  "description": "Assorted common math functions & utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -42,7 +42,7 @@
42
42
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
43
43
  },
44
44
  "dependencies": {
45
- "@thi.ng/api": "^8.12.10"
45
+ "@thi.ng/api": "^8.12.11"
46
46
  },
47
47
  "devDependencies": {
48
48
  "esbuild": "^0.27.0",
@@ -104,6 +104,9 @@
104
104
  "./fit": {
105
105
  "default": "./fit.js"
106
106
  },
107
+ "./fraction": {
108
+ "default": "./fraction.js"
109
+ },
107
110
  "./int": {
108
111
  "default": "./int.js"
109
112
  },
@@ -147,5 +150,5 @@
147
150
  "thi.ng": {
148
151
  "year": 2013
149
152
  },
150
- "gitHead": "824bf9047b5a10f777c5c5b4aeecf0c750a22c75\n"
153
+ "gitHead": "e7a21b9d2a188fa04d4c893d8531c40fbc0f4c06\n"
151
154
  }
package/regression.d.ts DELETED
@@ -1,43 +0,0 @@
1
- /**
2
- * Computes the coefficients of a polynomial regression for the given `samples`
3
- * (each a [x,y] tuple). The `degree` param defines the degree of the polynomial
4
- * and the number of returned coefficients (+1).
5
- *
6
- * @remarks
7
- * The resulting coeffs can be then used with {@link polynomial} to evaluate the
8
- * curve (i.e. used to make predictions).
9
- *
10
- * @param samples
11
- * @param degree
12
- */
13
- export declare const polynomialRegression: (samples: number[][], degree: number) => number[];
14
- /**
15
- * Computes polynomial for `x` and given `coeffs` (in order of increasing
16
- * exponents).
17
- *
18
- * @remarks
19
- * See {@link polynomialRegression} for computing coefficients.
20
- *
21
- * The number of given coefficients defines the degree (+1) of the polynomial,
22
- * i.e. a cubic function will require 4 coeffs, with the y-intercept being the
23
- * first coeff.
24
- *
25
- * @param x
26
- * @param coeffs
27
- */
28
- export declare const polynomial: (x: number, coeffs: number[]) => number;
29
- /**
30
- * Takes an augmented matrix (in column-major order) and uses Gaussian
31
- * elimination to compute solution coefficients for its system of linear
32
- * equations `Ax = b`.
33
- *
34
- * @remarks
35
- * References:
36
- * - https://en.wikipedia.org/wiki/Gaussian_elimination
37
- * - https://www.geeksforgeeks.org/dsa/gaussian-elimination/
38
- *
39
- * @param mat
40
- * @param degree
41
- */
42
- export declare const gaussianElimination: (mat: number[][], degree: number) => number[];
43
- //# sourceMappingURL=regression.d.ts.map
package/regression.js DELETED
@@ -1,51 +0,0 @@
1
- const polynomialRegression = (samples, degree) => {
2
- const mat = [];
3
- const aug = [];
4
- for (let i = 0; i <= degree; i++) {
5
- aug.push(samples.reduce((acc, x) => acc + x[0] ** i * x[1], 0));
6
- const col = [];
7
- for (let j = 0; j <= degree; j++) {
8
- col.push(samples.reduce((acc, x) => acc + x[0] ** (i + j), 0));
9
- }
10
- mat.push(col);
11
- }
12
- mat.push(aug);
13
- return gaussianElimination(mat, degree + 1);
14
- };
15
- const polynomial = (x, coeffs) => coeffs.reduce((sum, c, i) => sum + c * x ** i, 0);
16
- const gaussianElimination = (mat, degree) => {
17
- const n = mat.length - 1;
18
- const coeffs = [degree];
19
- for (let i = 0; i < n; i++) {
20
- let max = i;
21
- const col = mat[i];
22
- for (let j = i + 1; j < n; j++) {
23
- if (Math.abs(col[j]) > Math.abs(col[max])) max = j;
24
- }
25
- for (let k = i; k <= n; k++) {
26
- const col2 = mat[k];
27
- const tmp = col2[i];
28
- col2[i] = col2[max];
29
- col2[max] = tmp;
30
- }
31
- for (let j = i + 1; j < n; j++) {
32
- for (let k = n; k >= i; k--) {
33
- mat[k][j] -= mat[k][i] * mat[i][j] / mat[i][i];
34
- }
35
- }
36
- }
37
- for (let j = n; j-- > 0; ) {
38
- const d = mat[j][j];
39
- let sum = 0;
40
- for (let k = j + 1; k < n; k++) {
41
- sum += mat[k][j] * coeffs[k];
42
- }
43
- coeffs[j] = (mat[n][j] - sum) / d;
44
- }
45
- return coeffs;
46
- };
47
- export {
48
- gaussianElimination,
49
- polynomial,
50
- polynomialRegression
51
- };