@danielsimonjr/mathts-matrix 0.7.1 → 0.7.3
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/backends/Backend.d.ts +146 -0
- package/dist/backends/Backend.d.ts.map +1 -0
- package/dist/backends/BackendManager.d.ts +189 -0
- package/dist/backends/BackendManager.d.ts.map +1 -0
- package/dist/backends/GPUBackend.d.ts +128 -0
- package/dist/backends/GPUBackend.d.ts.map +1 -0
- package/dist/backends/GPUMatrixBackend.d.ts +123 -0
- package/dist/backends/GPUMatrixBackend.d.ts.map +1 -0
- package/dist/backends/JSBackend.d.ts +93 -0
- package/dist/backends/JSBackend.d.ts.map +1 -0
- package/dist/backends/ParallelBackend.d.ts +131 -0
- package/dist/backends/ParallelBackend.d.ts.map +1 -0
- package/dist/backends/WASMBackend.d.ts +156 -0
- package/dist/backends/WASMBackend.d.ts.map +1 -0
- package/dist/backends/WasmLoader.d.ts +326 -0
- package/dist/backends/WasmLoader.d.ts.map +1 -0
- package/dist/backends/gpu/BatchExecutor.d.ts +190 -0
- package/dist/backends/gpu/BatchExecutor.d.ts.map +1 -0
- package/dist/backends/gpu/Sync.d.ts +167 -0
- package/dist/backends/gpu/Sync.d.ts.map +1 -0
- package/dist/backends/gpu/builtin-shaders.d.ts +178 -0
- package/dist/backends/gpu/builtin-shaders.d.ts.map +1 -0
- package/dist/backends/gpu/index.d.ts +13 -0
- package/dist/backends/gpu/index.d.ts.map +1 -0
- package/dist/backends/index.d.ts +18 -0
- package/dist/backends/index.d.ts.map +1 -0
- package/dist/backends/register-backends.d.ts +18 -0
- package/dist/backends/register-backends.d.ts.map +1 -0
- package/dist/backends/wasm/detect.d.ts +58 -0
- package/dist/backends/wasm/detect.d.ts.map +1 -0
- package/dist/backends/wasm/fft-wasm.d.ts +120 -0
- package/dist/backends/wasm/fft-wasm.d.ts.map +1 -0
- package/dist/backends/wasm/index.d.ts +9 -0
- package/dist/backends/wasm/index.d.ts.map +1 -0
- package/dist/backends/wasm/integrity.d.ts +18 -0
- package/dist/backends/wasm/integrity.d.ts.map +1 -0
- package/dist/backends/wasm/resolve.d.ts +12 -0
- package/dist/backends/wasm/resolve.d.ts.map +1 -0
- package/dist/{chunk-4VMDO6W2.js → chunk-PNKVD2UK.js} +2 -7
- package/dist/config.d.ts +145 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/index.d.ts +14 -3037
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -9
- package/dist/{integrity-LIWCCDIJ.js → integrity-IBNQNHXP.js} +1 -1
- package/dist/operations/cholesky.d.ts +24 -0
- package/dist/operations/cholesky.d.ts.map +1 -0
- package/dist/operations/common.d.ts +61 -0
- package/dist/operations/common.d.ts.map +1 -0
- package/dist/operations/condest.d.ts +27 -0
- package/dist/operations/condest.d.ts.map +1 -0
- package/dist/operations/eig-wasm.d.ts +35 -0
- package/dist/operations/eig-wasm.d.ts.map +1 -0
- package/dist/operations/eig.d.ts +75 -0
- package/dist/operations/eig.d.ts.map +1 -0
- package/dist/operations/expm.d.ts +46 -0
- package/dist/operations/expm.d.ts.map +1 -0
- package/dist/operations/index.d.ts +21 -0
- package/dist/operations/index.d.ts.map +1 -0
- package/dist/operations/logm.d.ts +59 -0
- package/dist/operations/logm.d.ts.map +1 -0
- package/dist/operations/lu.d.ts +51 -0
- package/dist/operations/lu.d.ts.map +1 -0
- package/dist/operations/pinv.d.ts +36 -0
- package/dist/operations/pinv.d.ts.map +1 -0
- package/dist/operations/qr-family.d.ts +52 -0
- package/dist/operations/qr-family.d.ts.map +1 -0
- package/dist/operations/qr-pivoted.d.ts +39 -0
- package/dist/operations/qr-pivoted.d.ts.map +1 -0
- package/dist/operations/qr.d.ts +35 -0
- package/dist/operations/qr.d.ts.map +1 -0
- package/dist/operations/schur.d.ts +58 -0
- package/dist/operations/schur.d.ts.map +1 -0
- package/dist/operations/sqrtm.d.ts +76 -0
- package/dist/operations/sqrtm.d.ts.map +1 -0
- package/dist/operations/svd-wasm.d.ts +23 -0
- package/dist/operations/svd-wasm.d.ts.map +1 -0
- package/dist/operations/svd.d.ts +70 -0
- package/dist/operations/svd.d.ts.map +1 -0
- package/dist/parallel-matrix.d.ts +223 -0
- package/dist/parallel-matrix.d.ts.map +1 -0
- package/dist/{resolve-DDCEWHWX.js → resolve-2SX6WJ6R.js} +1 -1
- package/dist/typed-operations.d.ts +162 -0
- package/dist/typed-operations.d.ts.map +1 -0
- package/dist/types/DenseMatrix.d.ts +213 -0
- package/dist/types/DenseMatrix.d.ts.map +1 -0
- package/dist/types/Matrix.d.ts +188 -0
- package/dist/types/Matrix.d.ts.map +1 -0
- package/dist/types/SparseMatrix.d.ts +214 -0
- package/dist/types/SparseMatrix.d.ts.map +1 -0
- package/dist/types/dense/arithmetic.d.ts +8 -0
- package/dist/types/dense/arithmetic.d.ts.map +1 -0
- package/dist/types/dense/reduction.d.ts +8 -0
- package/dist/types/dense/reduction.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/wasm/mathts-as.wasm +0 -0
- package/dist/wasm/wasm-manifest.json +1 -1
- package/package.json +70 -70
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure TypeScript Matrix Backend
|
|
3
|
+
*
|
|
4
|
+
* Reference implementation of matrix operations using plain JavaScript.
|
|
5
|
+
* Serves as baseline for correctness and fallback when WASM/GPU unavailable.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import { DenseMatrix } from '../types/DenseMatrix.js';
|
|
10
|
+
import type { MatrixBackend, BackendType } from './Backend.js';
|
|
11
|
+
/**
|
|
12
|
+
* Pure JavaScript matrix backend
|
|
13
|
+
*
|
|
14
|
+
* All operations are implemented in TypeScript without external dependencies.
|
|
15
|
+
* Performance is optimized where possible but prioritizes correctness.
|
|
16
|
+
*/
|
|
17
|
+
export declare class JSBackend implements MatrixBackend {
|
|
18
|
+
readonly type: BackendType;
|
|
19
|
+
/**
|
|
20
|
+
* JS backend is always available
|
|
21
|
+
*/
|
|
22
|
+
isAvailable(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* No initialization needed for JS backend
|
|
25
|
+
*/
|
|
26
|
+
initialize(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Matrix addition
|
|
29
|
+
*/
|
|
30
|
+
add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
31
|
+
/**
|
|
32
|
+
* Matrix subtraction
|
|
33
|
+
*/
|
|
34
|
+
subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
35
|
+
/**
|
|
36
|
+
* Element-wise multiplication
|
|
37
|
+
*/
|
|
38
|
+
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
39
|
+
/**
|
|
40
|
+
* Element-wise division
|
|
41
|
+
*/
|
|
42
|
+
divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
43
|
+
/**
|
|
44
|
+
* Scalar multiplication
|
|
45
|
+
*/
|
|
46
|
+
scale(a: DenseMatrix, scalar: number): DenseMatrix;
|
|
47
|
+
/**
|
|
48
|
+
* Element-wise absolute value
|
|
49
|
+
*/
|
|
50
|
+
abs(a: DenseMatrix): DenseMatrix;
|
|
51
|
+
/**
|
|
52
|
+
* Element-wise negation
|
|
53
|
+
*/
|
|
54
|
+
negate(a: DenseMatrix): DenseMatrix;
|
|
55
|
+
/**
|
|
56
|
+
* Matrix multiplication using the naive O(n³) algorithm
|
|
57
|
+
*
|
|
58
|
+
* For larger matrices, consider using blocked/tiled multiplication
|
|
59
|
+
* or Strassen's algorithm (implemented in WASM backend).
|
|
60
|
+
*/
|
|
61
|
+
multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
62
|
+
/**
|
|
63
|
+
* Matrix transpose
|
|
64
|
+
*/
|
|
65
|
+
transpose(a: DenseMatrix): DenseMatrix;
|
|
66
|
+
/**
|
|
67
|
+
* Sum of all elements
|
|
68
|
+
*/
|
|
69
|
+
sum(a: DenseMatrix): number;
|
|
70
|
+
/**
|
|
71
|
+
* Sum along an axis
|
|
72
|
+
* axis=0: sum columns (result is 1×n row vector)
|
|
73
|
+
* axis=1: sum rows (result is m×1 column vector)
|
|
74
|
+
*/
|
|
75
|
+
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
76
|
+
/**
|
|
77
|
+
* Frobenius norm: sqrt(sum of squared elements)
|
|
78
|
+
*/
|
|
79
|
+
norm(a: DenseMatrix): number;
|
|
80
|
+
/**
|
|
81
|
+
* Dot product of two vectors
|
|
82
|
+
*
|
|
83
|
+
* Works with row vectors (1×n) or column vectors (m×1).
|
|
84
|
+
*/
|
|
85
|
+
dot(a: DenseMatrix, b: DenseMatrix): number;
|
|
86
|
+
private checkDimensionsMatch;
|
|
87
|
+
private checkMultiplyDimensions;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Default JS backend instance
|
|
91
|
+
*/
|
|
92
|
+
export declare const jsBackend: JSBackend;
|
|
93
|
+
//# sourceMappingURL=JSBackend.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JSBackend.d.ts","sourceRoot":"","sources":["../../src/backends/JSBackend.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE/D;;;;;GAKG;AACH,qBAAa,SAAU,YAAW,aAAa;IAC7C,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAQ;IAElC;;OAEG;IACH,WAAW,IAAI,OAAO,CAErB;IAED;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAEhC;IAMD;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAA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ker pool parallelization for operations
|
|
5
|
+
* on large matrices. Automatically falls back to sequential execution
|
|
6
|
+
* for smaller matrices.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import { ComputePool, type ComputePoolConfig } from '@danielsimonjr/mathts-parallel';
|
|
11
|
+
import { DenseMatrix } from '../types/DenseMatrix.js';
|
|
12
|
+
import type { BackendType } from './Backend.js';
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for ParallelBackend
|
|
15
|
+
*/
|
|
16
|
+
export interface ParallelBackendConfig {
|
|
17
|
+
/** Custom ComputePool instance (optional) */
|
|
18
|
+
pool?: ComputePool;
|
|
19
|
+
/** Pool configuration (if creating new pool) */
|
|
20
|
+
poolConfig?: Partial<ComputePoolConfig>;
|
|
21
|
+
/** Threshold for parallel execution (elements) */
|
|
22
|
+
parallelThreshold?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parallel matrix backend using worker pool
|
|
26
|
+
*
|
|
27
|
+
* Provides parallel implementations of matrix operations for large matrices.
|
|
28
|
+
* Uses automatic chunking and result aggregation for efficient parallel execution.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const backend = new ParallelBackend();
|
|
33
|
+
* await backend.initialize();
|
|
34
|
+
*
|
|
35
|
+
* const result = await backend.multiply(largeMatrixA, largeMatrixB);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class ParallelBackend {
|
|
39
|
+
readonly type: BackendType;
|
|
40
|
+
private pool;
|
|
41
|
+
private initialized;
|
|
42
|
+
private parallelThreshold;
|
|
43
|
+
constructor(config?: ParallelBackendConfig);
|
|
44
|
+
/**
|
|
45
|
+
* Check if parallel backend is available
|
|
46
|
+
*/
|
|
47
|
+
isAvailable(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Initialize the worker pool
|
|
50
|
+
*/
|
|
51
|
+
initialize(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Check if initialized
|
|
54
|
+
*/
|
|
55
|
+
isReady(): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Determine if operation should be parallelized
|
|
58
|
+
*/
|
|
59
|
+
private shouldParallelize;
|
|
60
|
+
/**
|
|
61
|
+
* Parallel matrix addition
|
|
62
|
+
*/
|
|
63
|
+
add(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
64
|
+
/**
|
|
65
|
+
* Parallel matrix subtraction
|
|
66
|
+
*/
|
|
67
|
+
subtract(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
68
|
+
/**
|
|
69
|
+
* Parallel element-wise multiplication
|
|
70
|
+
*/
|
|
71
|
+
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
72
|
+
/**
|
|
73
|
+
* Parallel element-wise division
|
|
74
|
+
*/
|
|
75
|
+
divideElementwise(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
76
|
+
/**
|
|
77
|
+
* Parallel scalar multiplication
|
|
78
|
+
*/
|
|
79
|
+
scale(a: DenseMatrix, scalar: number): Promise<DenseMatrix>;
|
|
80
|
+
/**
|
|
81
|
+
* Element-wise absolute value (sequential, as it's simple)
|
|
82
|
+
*/
|
|
83
|
+
abs(a: DenseMatrix): DenseMatrix;
|
|
84
|
+
/**
|
|
85
|
+
* Element-wise negation
|
|
86
|
+
*/
|
|
87
|
+
negate(a: DenseMatrix): DenseMatrix;
|
|
88
|
+
/**
|
|
89
|
+
* Parallel matrix multiplication
|
|
90
|
+
*/
|
|
91
|
+
multiply(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
92
|
+
/**
|
|
93
|
+
* Parallel matrix transpose
|
|
94
|
+
*/
|
|
95
|
+
transpose(a: DenseMatrix): Promise<DenseMatrix>;
|
|
96
|
+
/**
|
|
97
|
+
* Parallel sum of all elements
|
|
98
|
+
*/
|
|
99
|
+
sum(a: DenseMatrix): Promise<number>;
|
|
100
|
+
/**
|
|
101
|
+
* Sum along an axis (sequential)
|
|
102
|
+
*/
|
|
103
|
+
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
104
|
+
/**
|
|
105
|
+
* Frobenius norm (sequential)
|
|
106
|
+
*/
|
|
107
|
+
norm(a: DenseMatrix): number;
|
|
108
|
+
/**
|
|
109
|
+
* Parallel dot product
|
|
110
|
+
*/
|
|
111
|
+
dot(a: DenseMatrix, b: DenseMatrix): Promise<number>;
|
|
112
|
+
/**
|
|
113
|
+
* Terminate the worker pool
|
|
114
|
+
*/
|
|
115
|
+
terminate(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Get pool statistics
|
|
118
|
+
*/
|
|
119
|
+
getStats(): import("workerpool").PoolStats;
|
|
120
|
+
private checkDimensionsMatch;
|
|
121
|
+
private checkMultiplyDimensions;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Default parallel backend instance using global compute pool
|
|
125
|
+
*/
|
|
126
|
+
export declare const parallelBackend: ParallelBackend;
|
|
127
|
+
/**
|
|
128
|
+
* Create a new parallel backend with custom configuration
|
|
129
|
+
*/
|
|
130
|
+
export declare function createParallelBackend(config?: ParallelBackendConfig): ParallelBackend;
|
|
131
|
+
//# sourceMappingURL=ParallelBackend.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ParallelBackend.d.ts","sourceRoot":"","sources":["../../src/backends/ParallelBackend.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAe,WAAW,EAAE,KAAK,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAClG,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,6CAA6C;IAC7C,IAAI,CAAC,EAAE,WAAW,CA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AEG;IACG,QAAQ,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAenE;IAED;;OAEG;IACG,SAAS,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAWpD;IAMD;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAUzC;IAED;;OAEG;IACH,OAAO,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,GAAG,WAAW,CAoBhD;IAED;;OAEG;IACH,IAAI,CAAC,CAAC,EAAE,WAAW,GAAG,MAAM,CAE3B;IAED;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAwBzD;IAMD;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC,CAK/B;IAED;;OAEG;IACH,QAAQ,mCAEP;IAMD,OAAO,CAAC,oBAAoB;IAM5B,OAAO,CAAC,uBAAuB;CAKhC;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,iBAA6C,CAAC;AAE1E;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,CAAC,EAAE,qBAAqB,GAAG,eAAe,CAErF"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Matrix Backend (AssemblyScript)
|
|
3
|
+
*
|
|
4
|
+
* Implements MatrixBackend using the AssemblyScript-compiled WebAssembly
|
|
5
|
+
* module (`dist/wasm/mathts-as.wasm`, ~65 KB). Falls back to JSBackend when
|
|
6
|
+
* the WASM module is unavailable or for small matrices.
|
|
7
|
+
*
|
|
8
|
+
* ABI:
|
|
9
|
+
* The AS module exports a managed runtime (`__new` / `__pin` / `__unpin` /
|
|
10
|
+
* `__collect`) and its matrix/array functions accept managed `Float64Array`
|
|
11
|
+
* *header* pointers — NOT raw flat-memory pointers. A typed-array header is
|
|
12
|
+
* a 12-byte block at some offset that stores (buffer, buffer, byteLength).
|
|
13
|
+
*
|
|
14
|
+
* To call e.g. `matrix_add(a, b, result)` from JS, we have to:
|
|
15
|
+
* 1. `__new(byteLength, 1)` — allocate the data buffer (id=1 = ArrayBuffer)
|
|
16
|
+
* 2. `__pin(buffer)` — pin so it survives GC while building header
|
|
17
|
+
* 3. `__new(12, 5)` — allocate the Float64Array header (id=5)
|
|
18
|
+
* 4. Write [buffer, buffer, byteLength] into the header
|
|
19
|
+
* 5. Copy caller data into the buffer
|
|
20
|
+
* 6. `__unpin(buffer)` — the header now owns the buffer reference
|
|
21
|
+
*
|
|
22
|
+
* The legacy artifact had no managed runtime and used a completely
|
|
23
|
+
* different ABI (camelCase exports, flat-memory raw pointers). It was
|
|
24
|
+
* retired in Phase 7b; the AssemblyScript binary is now the only WASM
|
|
25
|
+
* backend.
|
|
26
|
+
*
|
|
27
|
+
* Naming map (legacy backend ↔ AssemblyScript) — kept for historical reference:
|
|
28
|
+
*
|
|
29
|
+
* Op Legacy (camelCase, flat ptrs) AS (snake_case, header refs)
|
|
30
|
+
* ------------------ --------------------------------- -------------------------------
|
|
31
|
+
* add add(aPtr,bPtr,n,resPtr) matrix_add(a,b,result)
|
|
32
|
+
* subtract subtract(aPtr,bPtr,n,resPtr) matrix_sub(a,b,result)
|
|
33
|
+
* multiplyElementwise simdMulF64(aPtr,bPtr,resPtr,n) matrix_mul_elementwise(a,b,result)
|
|
34
|
+
* divideElementwise (none) matrix_div_elementwise(a,b,result)
|
|
35
|
+
* scale simdScaleF64(aPtr,s,resPtr,n) matrix_scale(a,scalar,result)
|
|
36
|
+
* abs simdAbsF64(aPtr,resPtr,n) array_abs(a,result)
|
|
37
|
+
* negate simdScaleF64(aPtr,-1,resPtr,n) matrix_neg(a,result)
|
|
38
|
+
* multiply multiplyDense(a,r,c,b,r,c,res) matrix_multiply(a,aRows,aCols,b,bCols,result)
|
|
39
|
+
* transpose transpose(a,r,c,res) matrix_transpose(a,rows,cols,result)
|
|
40
|
+
* sum simdSumF64(aPtr,n) array_sum(a)
|
|
41
|
+
* norm simdNormF64(aPtr,n) array_norm(a)
|
|
42
|
+
* dot dotProduct(aPtr,bPtr,n) array_dot(a,b)
|
|
43
|
+
*
|
|
44
|
+
* AssemblyScript also exports LU / QR / Cholesky / inverse / determinant
|
|
45
|
+
* (see assembly/src/algebra/decomposition.ts); the methods below dispatch
|
|
46
|
+
* to them when present and fall back to the in-process JS implementation
|
|
47
|
+
* otherwise.
|
|
48
|
+
*
|
|
49
|
+
* @packageDocumentation
|
|
50
|
+
*/
|
|
51
|
+
import type { MatrixBackend, BackendType } from './Backend.js';
|
|
52
|
+
import { DenseMatrix } from '../types/DenseMatrix.js';
|
|
53
|
+
import { type WasmFeatures } from './wasm/detect.js';
|
|
54
|
+
/**
|
|
55
|
+
* WASM Backend configuration
|
|
56
|
+
*/
|
|
57
|
+
export interface WASMBackendConfig {
|
|
58
|
+
/** Minimum elements to use WASM (default: 100) */
|
|
59
|
+
minElements?: number;
|
|
60
|
+
/** Path to WASM file (defaults to the packaged AS artifact, `dist/wasm/mathts-as.wasm`) */
|
|
61
|
+
wasmPath?: string;
|
|
62
|
+
/** Enable SIMD code paths if the AS module exposes them (currently a no-op
|
|
63
|
+
* — AS export surface does not include `simd*` ops; kept for API stability). */
|
|
64
|
+
useSIMD?: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* WASM Backend for matrix operations (AssemblyScript path).
|
|
68
|
+
*
|
|
69
|
+
* This is the sole WASM backend after Phase 7b retired the legacy path; it
|
|
70
|
+
* reports `type: 'wasm'` and is registered by `register-backends.ts`.
|
|
71
|
+
*/
|
|
72
|
+
export declare class WASMBackend implements MatrixBackend {
|
|
73
|
+
readonly type: BackendType;
|
|
74
|
+
private config;
|
|
75
|
+
private wasmModule;
|
|
76
|
+
private features;
|
|
77
|
+
private initPromise;
|
|
78
|
+
/** Pooled allocations — see `AsAllocCache` for rationale. */
|
|
79
|
+
private allocCache;
|
|
80
|
+
constructor(config?: WASMBackendConfig);
|
|
81
|
+
isAvailable(): boolean;
|
|
82
|
+
initialize(): Promise<void>;
|
|
83
|
+
private doInitialize;
|
|
84
|
+
/**
|
|
85
|
+
* Resolve the path to the AssemblyScript artifact (`dist/wasm/mathts-as.wasm`).
|
|
86
|
+
*/
|
|
87
|
+
private resolveAsWasmPath;
|
|
88
|
+
/**
|
|
89
|
+
* Compile + instantiate the AssemblyScript WASM artifact. Each
|
|
90
|
+
* WASMBackend instance owns its own instance — this is intentional so
|
|
91
|
+
* tests and multiple backend instances can coexist in the same process.
|
|
92
|
+
*/
|
|
93
|
+
private loadAsModule;
|
|
94
|
+
/**
|
|
95
|
+
* WASM is only worth its copy/alloc overhead for compute-dense ops. The 2026-07-01
|
|
96
|
+
* backend audit (`tools/benchmarks/backend-audit`) measured element-wise / transpose
|
|
97
|
+
* ops 4–6× SLOWER on WASM than JS (memory-bound; the SIMD matmul kernel is the only
|
|
98
|
+
* clear win, 9–12×). So WASM is reserved for `opKind: 'matmul'`; everything else stays
|
|
99
|
+
* on JS via each method's existing `jsBackend` fallback.
|
|
100
|
+
*/
|
|
101
|
+
private shouldUseWasm;
|
|
102
|
+
getFeatures(): WasmFeatures | null;
|
|
103
|
+
add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
104
|
+
subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
105
|
+
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
106
|
+
divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
107
|
+
scale(a: DenseMatrix, scalar: number): DenseMatrix;
|
|
108
|
+
abs(a: DenseMatrix): DenseMatrix;
|
|
109
|
+
negate(a: DenseMatrix): DenseMatrix;
|
|
110
|
+
multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
111
|
+
transpose(a: DenseMatrix): DenseMatrix;
|
|
112
|
+
sum(a: DenseMatrix): number;
|
|
113
|
+
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
114
|
+
norm(a: DenseMatrix): number;
|
|
115
|
+
dot(a: DenseMatrix, b: DenseMatrix): number;
|
|
116
|
+
luDecomposition(a: DenseMatrix): Promise<{
|
|
117
|
+
lu: DenseMatrix;
|
|
118
|
+
perm: Int32Array;
|
|
119
|
+
singular: boolean;
|
|
120
|
+
}>;
|
|
121
|
+
private luDecompositionJS;
|
|
122
|
+
qrDecomposition(a: DenseMatrix): Promise<{
|
|
123
|
+
q: DenseMatrix;
|
|
124
|
+
r: DenseMatrix;
|
|
125
|
+
}>;
|
|
126
|
+
private qrDecompositionJS;
|
|
127
|
+
inverse(a: DenseMatrix): Promise<{
|
|
128
|
+
inverse: DenseMatrix;
|
|
129
|
+
singular: boolean;
|
|
130
|
+
}>;
|
|
131
|
+
private inverseJS;
|
|
132
|
+
determinantWasm(a: DenseMatrix): Promise<number>;
|
|
133
|
+
private determinantJS;
|
|
134
|
+
choleskyDecomposition(a: DenseMatrix): Promise<{
|
|
135
|
+
l: DenseMatrix;
|
|
136
|
+
positiveDefinite: boolean;
|
|
137
|
+
}>;
|
|
138
|
+
private choleskyDecompositionJS;
|
|
139
|
+
/**
|
|
140
|
+
* Update configuration
|
|
141
|
+
*/
|
|
142
|
+
updateConfig(config: Partial<WASMBackendConfig>): void;
|
|
143
|
+
/**
|
|
144
|
+
* Get current configuration
|
|
145
|
+
*/
|
|
146
|
+
getConfig(): Required<WASMBackendConfig>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Global WASM backend instance (AssemblyScript path)
|
|
150
|
+
*/
|
|
151
|
+
export declare const wasmBackend: WASMBackend;
|
|
152
|
+
/**
|
|
153
|
+
* Create a WASM backend (AssemblyScript path) with custom configuration
|
|
154
|
+
*/
|
|
155
|
+
export declare function createWASMBackend(config?: WASMBackendConfig): WASMBackend;
|
|
156
|
+
//# sourceMappingUR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