@thi.ng/math 5.13.6 → 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 +2 -2
- package/fraction.d.ts +58 -0
- package/fraction.js +46 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +9 -3
- package/polynomial.d.ts +49 -0
- package/polynomial.js +20 -0
- package/solve.d.ts +30 -0
- package/solve.js +32 -0
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
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.
|
|
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,12 +6,14 @@ 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";
|
|
12
13
|
export * from "./min-error.js";
|
|
13
14
|
export * from "./mix.js";
|
|
14
15
|
export * from "./permutations.js";
|
|
16
|
+
export * from "./polynomial.js";
|
|
15
17
|
export * from "./prec.js";
|
|
16
18
|
export * from "./prime.js";
|
|
17
19
|
export * from "./ratio.js";
|
package/index.js
CHANGED
|
@@ -6,12 +6,14 @@ 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";
|
|
12
13
|
export * from "./min-error.js";
|
|
13
14
|
export * from "./mix.js";
|
|
14
15
|
export * from "./permutations.js";
|
|
16
|
+
export * from "./polynomial.js";
|
|
15
17
|
export * from "./prec.js";
|
|
16
18
|
export * from "./prime.js";
|
|
17
19
|
export * from "./ratio.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/math",
|
|
3
|
-
"version": "5.
|
|
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.
|
|
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
|
},
|
|
@@ -122,6 +125,9 @@
|
|
|
122
125
|
"./permutations": {
|
|
123
126
|
"default": "./permutations.js"
|
|
124
127
|
},
|
|
128
|
+
"./polynomial": {
|
|
129
|
+
"default": "./polynomial.js"
|
|
130
|
+
},
|
|
125
131
|
"./prec": {
|
|
126
132
|
"default": "./prec.js"
|
|
127
133
|
},
|
|
@@ -144,5 +150,5 @@
|
|
|
144
150
|
"thi.ng": {
|
|
145
151
|
"year": 2013
|
|
146
152
|
},
|
|
147
|
-
"gitHead": "
|
|
153
|
+
"gitHead": "e7a21b9d2a188fa04d4c893d8531c40fbc0f4c06\n"
|
|
148
154
|
}
|
package/polynomial.d.ts
ADDED
|
@@ -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
|
+
};
|
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,
|