@thi.ng/math 5.13.5 → 5.14.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 210 standalone projects, maintained as part
10
+ > This is one of 211 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.04 KB
80
+ Package sizes (brotli'd, pre-treeshake): ESM: 5.29 KB
81
81
 
82
82
  ## Dependencies
83
83
 
package/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export * from "./libc.js";
12
12
  export * from "./min-error.js";
13
13
  export * from "./mix.js";
14
14
  export * from "./permutations.js";
15
+ export * from "./polynomial.js";
15
16
  export * from "./prec.js";
16
17
  export * from "./prime.js";
17
18
  export * from "./ratio.js";
package/index.js CHANGED
@@ -12,6 +12,7 @@ export * from "./libc.js";
12
12
  export * from "./min-error.js";
13
13
  export * from "./mix.js";
14
14
  export * from "./permutations.js";
15
+ export * from "./polynomial.js";
15
16
  export * from "./prec.js";
16
17
  export * from "./prime.js";
17
18
  export * from "./ratio.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/math",
3
- "version": "5.13.5",
3
+ "version": "5.14.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.8"
45
+ "@thi.ng/api": "^8.12.10"
46
46
  },
47
47
  "devDependencies": {
48
48
  "esbuild": "^0.27.0",
@@ -122,6 +122,9 @@
122
122
  "./permutations": {
123
123
  "default": "./permutations.js"
124
124
  },
125
+ "./polynomial": {
126
+ "default": "./polynomial.js"
127
+ },
125
128
  "./prec": {
126
129
  "default": "./prec.js"
127
130
  },
@@ -144,5 +147,5 @@
144
147
  "thi.ng": {
145
148
  "year": 2013
146
149
  },
147
- "gitHead": "be6e7657b1e5c54d7d648d1b52888a7297e95a17\n"
150
+ "gitHead": "824bf9047b5a10f777c5c5b4aeecf0c750a22c75\n"
148
151
  }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Computes polynomial for `x` and given `coeffs` (in order of increasing
3
+ * exponents).
4
+ *
5
+ * @remarks
6
+ * See {@link polynomialRegression} for computing coefficients.
7
+ *
8
+ * The number of given coefficients defines the degree (+1) of the polynomial,
9
+ * i.e. a cubic function will require 4 coeffs, with the y-intercept being the
10
+ * first coeff.
11
+ *
12
+ * @example
13
+ * ```ts tangle:../export/polynomial.ts
14
+ * import { polynomial } from "@thi.ng/math";
15
+ *
16
+ * const coeffs = [-5, -4, 3, 2];
17
+ *
18
+ * for(let x = -2; x <= 2; x += 0.5) {
19
+ * console.log(`f(${x}) = ${polynomial(x, coeffs)}`);
20
+ * }
21
+ * // f(-2) = -1
22
+ * // f(-1.5) = 1
23
+ * // f(-1) = 0
24
+ * // f(-0.5) = -2.5
25
+ * // f(0) = -5
26
+ * // f(0.5) = -6
27
+ * // f(1) = -4
28
+ * // f(1.5) = 2.5
29
+ * // f(2) = 15
30
+ * ```
31
+ *
32
+ * @param x
33
+ * @param coeffs
34
+ */
35
+ export declare const polynomial: (x: number, coeffs: number[]) => number;
36
+ /**
37
+ * Computes the coefficients of a polynomial regression for the given `samples`
38
+ * (each a [x,y] tuple). The `degree` param defines the degree of the polynomial
39
+ * and the number of returned coefficients (+1).
40
+ *
41
+ * @remarks
42
+ * The resulting coeffs can be then used with {@link polynomial} to evaluate the
43
+ * curve (i.e. used to make predictions).
44
+ *
45
+ * @param samples
46
+ * @param degree
47
+ */
48
+ export declare const polynomialRegression: (samples: number[][], degree: number) => number[];
49
+ //# sourceMappingURL=polynomial.d.ts.map
package/polynomial.js ADDED
@@ -0,0 +1,20 @@
1
+ import { gaussianElimination } from "./solve.js";
2
+ const polynomial = (x, coeffs) => coeffs.reduce((sum, c, i) => sum + c * x ** i, 0);
3
+ const polynomialRegression = (samples, degree) => {
4
+ const mat = [];
5
+ const aug = [];
6
+ for (let i = 0; i <= degree; i++) {
7
+ const col = [];
8
+ for (let j = 0; j <= degree; j++) {
9
+ col.push(samples.reduce((acc, x) => acc + x[0] ** (i + j), 0));
10
+ }
11
+ mat.push(col);
12
+ aug.push(samples.reduce((acc, x) => acc + x[0] ** i * x[1], 0));
13
+ }
14
+ mat.push(aug);
15
+ return gaussianElimination(mat, degree + 1);
16
+ };
17
+ export {
18
+ polynomial,
19
+ polynomialRegression
20
+ };
@@ -0,0 +1,43 @@
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 ADDED
@@ -0,0 +1,51 @@
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
+ };
package/solve.d.ts CHANGED
@@ -75,4 +75,34 @@ export declare const solveCubic: (a: number, b: number, c: number, d: number, ep
75
75
  * @param d - input coefficients & output solutions [0,N-1]
76
76
  */
77
77
  export declare const solveTridiagonal: (a: NumericArray, b: NumericArray, c: NumericArray, d: NumericArray) => NumericArray;
78
+ /**
79
+ * Takes an augmented matrix (in column-major order) and uses Gaussian
80
+ * elimination to compute solution coefficients for its system of linear
81
+ * equations `Ax = b`.
82
+ *
83
+ * @remarks
84
+ * References:
85
+ * - https://en.wikipedia.org/wiki/Gaussian_elimination
86
+ * - https://www.geeksforgeeks.org/dsa/gaussian-elimination/
87
+ *
88
+ * @example
89
+ * ```ts tangle:../export/gaussian-elimination.ts
90
+ * import { gaussianElimination } from "@thi.ng/math";
91
+ *
92
+ * // in column-major order
93
+ * const matrix = [
94
+ * [3, 2, 5],
95
+ * [2, 3, -3],
96
+ * [-4, 3, 1],
97
+ * [3, 15, 14]
98
+ * ];
99
+ *
100
+ * console.log(gaussianElimination(matrix, 1));
101
+ * // [3, 1, 2] (rounded)
102
+ * ```
103
+ *
104
+ * @param mat
105
+ * @param degree
106
+ */
107
+ export declare const gaussianElimination: (mat: number[][], degree: number) => number[];
78
108
  //# sourceMappingURL=solve.d.ts.map
package/solve.js CHANGED
@@ -51,8 +51,40 @@ const solveTridiagonal = (a, b, c, d) => {
51
51
  }
52
52
  return d;
53
53
  };
54
+ const gaussianElimination = (mat, degree) => {
55
+ const n = mat.length - 1;
56
+ const coeffs = [degree];
57
+ for (let i = 0; i < n; i++) {
58
+ let max = i;
59
+ const col = mat[i];
60
+ for (let j = i + 1; j < n; j++) {
61
+ if (Math.abs(col[j]) > Math.abs(col[max])) max = j;
62
+ }
63
+ for (let k = i; k <= n; k++) {
64
+ const col2 = mat[k];
65
+ const tmp = col2[i];
66
+ col2[i] = col2[max];
67
+ col2[max] = tmp;
68
+ }
69
+ for (let j = i + 1; j < n; j++) {
70
+ for (let k = n; k >= i; k--) {
71
+ mat[k][j] -= mat[k][i] * mat[i][j] / mat[i][i];
72
+ }
73
+ }
74
+ }
75
+ for (let j = n; j-- > 0; ) {
76
+ const d = mat[j][j];
77
+ let sum = 0;
78
+ for (let k = j + 1; k < n; k++) {
79
+ sum += mat[k][j] * coeffs[k];
80
+ }
81
+ coeffs[j] = (mat[n][j] - sum) / d;
82
+ }
83
+ return coeffs;
84
+ };
54
85
  export {
55
86
  derivative,
87
+ gaussianElimination,
56
88
  solveCubic,
57
89
  solveLinear,
58
90
  solveQuadratic,