@danielsimonjr/mathts-matrix 0.1.2 → 0.1.4
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 +21 -25
- package/dist/chunk-4VMDO6W2.js +31 -0
- package/dist/{chunk-I6QDZ7SC.js → chunk-VCPLE6ED.js} +7 -31
- package/dist/{eig-YRLUBCGK.js → eig-A5GJGSJJ.js} +2 -1
- package/dist/index.d.ts +1167 -371
- package/dist/index.js +4108 -2029
- package/dist/integrity-X3CLNVGW.js +78 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -11,36 +11,32 @@ npm install @danielsimonjr/mathts-matrix
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import {
|
|
14
|
+
import { DenseMatrix } from '@danielsimonjr/mathts-matrix';
|
|
15
15
|
|
|
16
16
|
// Create a matrix from a 2D array
|
|
17
|
-
const A =
|
|
17
|
+
const A = DenseMatrix.fromArray([
|
|
18
18
|
[1, 2, 3],
|
|
19
19
|
[4, 5, 6],
|
|
20
|
-
[7, 8, 9]
|
|
20
|
+
[7, 8, 9],
|
|
21
21
|
]);
|
|
22
22
|
|
|
23
|
-
// Basic operations
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
autoBackend: true, // Or let it auto-select
|
|
32
|
-
wasmThreshold: 1000, // Switch to WASM above 1000 elements
|
|
33
|
-
gpuThreshold: 100000, // Switch to GPU above 100K elements
|
|
34
|
-
});
|
|
23
|
+
// Basic operations
|
|
24
|
+
const sum = A.add(A);
|
|
25
|
+
const product = A.multiply(A);
|
|
26
|
+
const transposed = A.transpose();
|
|
27
|
+
|
|
28
|
+
// Decompositions (operate on number[][])
|
|
29
|
+
import { svd, eig } from '@danielsimonjr/mathts-matrix';
|
|
30
|
+
const { U, S, V } = svd(A.toArray());
|
|
35
31
|
```
|
|
36
32
|
|
|
37
33
|
## Backends
|
|
38
34
|
|
|
39
|
-
| Backend
|
|
40
|
-
|
|
41
|
-
| **JS**
|
|
42
|
-
| **WASM** | >1K elements
|
|
43
|
-
| **GPU**
|
|
35
|
+
| Backend | Trigger | Performance |
|
|
36
|
+
| -------- | -------------- | ------------ |
|
|
37
|
+
| **JS** | Default | 1x baseline |
|
|
38
|
+
| **WASM** | >1K elements | ~10x faster |
|
|
39
|
+
| **GPU** | >100K elements | ~100x faster |
|
|
44
40
|
|
|
45
41
|
## Matrix Types
|
|
46
42
|
|
|
@@ -55,11 +51,11 @@ This package is under active development. Currently implemented:
|
|
|
55
51
|
- [x] Backend infrastructure
|
|
56
52
|
- [x] Dense matrix structure
|
|
57
53
|
- [x] Sparse matrix (CSR) structure
|
|
58
|
-
- [
|
|
59
|
-
- [
|
|
60
|
-
- [
|
|
61
|
-
- [
|
|
62
|
-
- [
|
|
54
|
+
- [x] Matrix operations (add, multiply, transpose, etc.)
|
|
55
|
+
- [x] WASM backend (Rust primary, AssemblyScript legacy)
|
|
56
|
+
- [x] GPU backend (WebGPU)
|
|
57
|
+
- [x] SVD decomposition
|
|
58
|
+
- [x] Eigenvalue solvers
|
|
63
59
|
|
|
64
60
|
## License
|
|
65
61
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __esm = (fn, res, err) => function __init() {
|
|
6
|
+
if (err) throw err[0];
|
|
7
|
+
try {
|
|
8
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
9
|
+
} catch (e) {
|
|
10
|
+
throw err = [e], e;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var __export = (target, all) => {
|
|
14
|
+
for (var name in all)
|
|
15
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
__esm,
|
|
29
|
+
__export,
|
|
30
|
+
__toCommonJS
|
|
31
|
+
};
|
|
@@ -1,24 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __esm = (fn, res) => function __init() {
|
|
6
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
7
|
-
};
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
-
|
|
22
1
|
// src/operations/eig.ts
|
|
23
2
|
var DEFAULT_MAX_ITERATIONS = 1e3;
|
|
24
3
|
var DEFAULT_TOLERANCE = 1e-12;
|
|
@@ -103,7 +82,7 @@ function applyHouseholderRight(A, v, beta, startRow, startCol) {
|
|
|
103
82
|
function hessenberg(A) {
|
|
104
83
|
const n = A.length;
|
|
105
84
|
const H = cloneMatrix(A);
|
|
106
|
-
|
|
85
|
+
const Q = eye(n);
|
|
107
86
|
for (let k = 0; k < n - 2; k++) {
|
|
108
87
|
const x = [];
|
|
109
88
|
for (let i = k + 1; i < n; i++) {
|
|
@@ -300,7 +279,7 @@ function inverseIteration(A, eigenvalue, tolerance, maxIterations) {
|
|
|
300
279
|
}
|
|
301
280
|
}
|
|
302
281
|
}
|
|
303
|
-
|
|
282
|
+
const v = new Array(n).fill(1);
|
|
304
283
|
let prevNorm = 0;
|
|
305
284
|
for (let iter = 0; iter < maxIterations; iter++) {
|
|
306
285
|
const y = new Array(n).fill(0);
|
|
@@ -352,10 +331,7 @@ function eig(matrix, options = {}) {
|
|
|
352
331
|
if (n2 !== Math.floor(n2)) {
|
|
353
332
|
throw new Error("Float64Array length must be a perfect square");
|
|
354
333
|
}
|
|
355
|
-
A = Array.from(
|
|
356
|
-
{ length: n2 },
|
|
357
|
-
(_, i) => Array.from({ length: n2 }, (_2, j) => matrix[i * n2 + j])
|
|
358
|
-
);
|
|
334
|
+
A = Array.from({ length: n2 }, (_, i) => Array.from({ length: n2 }, (_2, j) => matrix[i * n2 + j]));
|
|
359
335
|
} else {
|
|
360
336
|
A = matrix;
|
|
361
337
|
}
|
|
@@ -387,7 +363,10 @@ function eig(matrix, options = {}) {
|
|
|
387
363
|
const sqrtDisc = Math.sqrt(disc);
|
|
388
364
|
const e1 = (trace + sqrtDisc) / 2;
|
|
389
365
|
const e2 = (trace - sqrtDisc) / 2;
|
|
390
|
-
values2 = [
|
|
366
|
+
values2 = [
|
|
367
|
+
{ re: e1, im: 0 },
|
|
368
|
+
{ re: e2, im: 0 }
|
|
369
|
+
];
|
|
391
370
|
if (computeVectors) {
|
|
392
371
|
vectors2 = [];
|
|
393
372
|
for (const e of [e1, e2]) {
|
|
@@ -488,9 +467,6 @@ function powerIteration(matrix, options = {}) {
|
|
|
488
467
|
}
|
|
489
468
|
|
|
490
469
|
export {
|
|
491
|
-
__esm,
|
|
492
|
-
__export,
|
|
493
|
-
__toCommonJS,
|
|
494
470
|
eig,
|
|
495
471
|
eigvals,
|
|
496
472
|
powerIteration
|