@danielsimonjr/mathts-matrix 0.7.0 → 0.7.2
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 +222 -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 +161 -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
package/dist/index.d.ts
CHANGED
|
@@ -1,3044 +1,21 @@
|
|
|
1
|
-
import { MatrixDimensions } from '@danielsimonjr/mathts-core';
|
|
2
|
-
export { MatrixDimensions } from '@danielsimonjr/mathts-core';
|
|
3
|
-
import * as workerpool from 'workerpool';
|
|
4
|
-
import { ComputePool, ComputePoolConfig } from '@danielsimonjr/mathts-parallel';
|
|
5
|
-
import { GPUContextOptions, GPUCapabilities, GPUContext, BufferPool, ShaderManager } from '@danielsimonjr/mathts-gpu';
|
|
6
|
-
export { BufferPool, GPUCapabilities, GPUContext, GPUContextOptions, ShaderManager, destroyGlobalGPU, detectGPUCapabilities, getGlobalGPUContext, getRecommendedWorkgroupSize, hasWebGPU } from '@danielsimonjr/mathts-gpu';
|
|
7
|
-
import * as typed_function from 'typed-function';
|
|
8
|
-
|
|
9
1
|
/**
|
|
10
|
-
*
|
|
2
|
+
* @danielsimonjr/mathts-matrix
|
|
11
3
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @packageDocumentation
|
|
16
|
-
*/
|
|
17
|
-
/**
|
|
18
|
-
* Matrix dimension type, consolidated from `@danielsimonjr/mathts-core`'s
|
|
19
|
-
* byte-identical copy (see docs/Architecture/duplicate-symbols.json).
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Matrix index for element access
|
|
24
|
-
*/
|
|
25
|
-
interface MatrixIndex {
|
|
26
|
-
row: number;
|
|
27
|
-
col: number;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Slice specification for submatrix extraction
|
|
31
|
-
*/
|
|
32
|
-
interface SliceSpec {
|
|
33
|
-
rowStart?: number;
|
|
34
|
-
rowEnd?: number;
|
|
35
|
-
colStart?: number;
|
|
36
|
-
colEnd?: number;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Matrix iterator result
|
|
40
|
-
*/
|
|
41
|
-
interface MatrixEntry<T = number> {
|
|
42
|
-
row: number;
|
|
43
|
-
col: number;
|
|
44
|
-
value: T;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Matrix storage format identifier
|
|
48
|
-
*/
|
|
49
|
-
type MatrixType = 'DenseMatrix' | 'SparseMatrix' | 'DiagonalMatrix' | 'BandMatrix';
|
|
50
|
-
/**
|
|
51
|
-
* Abstract base class for all matrix types
|
|
52
|
-
*
|
|
53
|
-
* @typeParam T - The element type (default: number)
|
|
54
|
-
*/
|
|
55
|
-
declare abstract class Matrix<T = number> {
|
|
56
|
-
/**
|
|
57
|
-
* The matrix storage format type
|
|
58
|
-
*/
|
|
59
|
-
abstract readonly type: MatrixType;
|
|
60
|
-
/**
|
|
61
|
-
* Number of rows
|
|
62
|
-
*/
|
|
63
|
-
abstract readonly rows: number;
|
|
64
|
-
/**
|
|
65
|
-
* Number of columns
|
|
66
|
-
*/
|
|
67
|
-
abstract readonly cols: number;
|
|
68
|
-
/**
|
|
69
|
-
* Get the dimensions of the matrix
|
|
70
|
-
*/
|
|
71
|
-
get size(): MatrixDimensions;
|
|
72
|
-
/**
|
|
73
|
-
* Check if the matrix is square
|
|
74
|
-
*/
|
|
75
|
-
get isSquare(): boolean;
|
|
76
|
-
/**
|
|
77
|
-
* Check if the matrix is a vector (single row or column)
|
|
78
|
-
*/
|
|
79
|
-
get isVector(): boolean;
|
|
80
|
-
/**
|
|
81
|
-
* Check if the matrix is a row vector
|
|
82
|
-
*/
|
|
83
|
-
get isRowVector(): boolean;
|
|
84
|
-
/**
|
|
85
|
-
* Check if the matrix is a column vector
|
|
86
|
-
*/
|
|
87
|
-
get isColumnVector(): boolean;
|
|
88
|
-
/**
|
|
89
|
-
* Total number of elements
|
|
90
|
-
*/
|
|
91
|
-
get length(): number;
|
|
92
|
-
/**
|
|
93
|
-
* Get element at position (row, col)
|
|
94
|
-
* @param row - Row index (0-based)
|
|
95
|
-
* @param col - Column index (0-based)
|
|
96
|
-
*/
|
|
97
|
-
abstract get(row: number, col: number): T;
|
|
98
|
-
/**
|
|
99
|
-
* Set element at position (row, col)
|
|
100
|
-
* @param row - Row index (0-based)
|
|
101
|
-
* @param col - Column index (0-based)
|
|
102
|
-
* @param value - Value to set
|
|
103
|
-
* @returns A new matrix with the updated value (immutable)
|
|
104
|
-
*/
|
|
105
|
-
abstract set(row: number, col: number, value: T): Matrix<T>;
|
|
106
|
-
/**
|
|
107
|
-
* Get a row as a new matrix (1×n)
|
|
108
|
-
* @param index - Row index
|
|
109
|
-
*/
|
|
110
|
-
abstract row(index: number): Matrix<T>;
|
|
111
|
-
/**
|
|
112
|
-
* Get a column as a new matrix (m×1)
|
|
113
|
-
* @param index - Column index
|
|
114
|
-
*/
|
|
115
|
-
abstract column(index: number): Matrix<T>;
|
|
116
|
-
/**
|
|
117
|
-
* Get a submatrix (view or copy depending on implementation)
|
|
118
|
-
* @param spec - Slice specification
|
|
119
|
-
*/
|
|
120
|
-
abstract slice(spec: SliceSpec): Matrix<T>;
|
|
121
|
-
/**
|
|
122
|
-
* Get the diagonal elements as a vector
|
|
123
|
-
* @param k - Diagonal offset (0 = main diagonal, positive = above, negative = below)
|
|
124
|
-
*/
|
|
125
|
-
abstract diagonal(k?: number): Matrix<T>;
|
|
126
|
-
/**
|
|
127
|
-
* Matrix addition
|
|
128
|
-
*/
|
|
129
|
-
abstract add(other: Matrix<T>): Matrix<T>;
|
|
130
|
-
/**
|
|
131
|
-
* Matrix subtraction
|
|
132
|
-
*/
|
|
133
|
-
abstract subtract(other: Matrix<T>): Matrix<T>;
|
|
134
|
-
/**
|
|
135
|
-
* Element-wise multiplication (Hadamard product)
|
|
136
|
-
*/
|
|
137
|
-
abstract multiplyElementwise(other: Matrix<T>): Matrix<T>;
|
|
138
|
-
/**
|
|
139
|
-
* Matrix multiplication
|
|
140
|
-
*/
|
|
141
|
-
abstract multiply(other: Matrix<T>): Matrix<T>;
|
|
142
|
-
/**
|
|
143
|
-
* Scalar multiplication
|
|
144
|
-
*/
|
|
145
|
-
abstract scale(scalar: T): Matrix<T>;
|
|
146
|
-
/**
|
|
147
|
-
* Matrix transpose
|
|
148
|
-
*/
|
|
149
|
-
abstract transpose(): Matrix<T>;
|
|
150
|
-
/**
|
|
151
|
-
* Convert to nested array representation
|
|
152
|
-
*/
|
|
153
|
-
abstract toArray(): T[][];
|
|
154
|
-
/**
|
|
155
|
-
* Convert to flat array (row-major order)
|
|
156
|
-
*/
|
|
157
|
-
abstract toFlatArray(): T[];
|
|
158
|
-
/**
|
|
159
|
-
* Create a deep copy of the matrix
|
|
160
|
-
*/
|
|
161
|
-
abstract clone(): Matrix<T>;
|
|
162
|
-
/**
|
|
163
|
-
* Iterate over all elements with their indices
|
|
164
|
-
*/
|
|
165
|
-
abstract entries(): IterableIterator<MatrixEntry<T>>;
|
|
166
|
-
/**
|
|
167
|
-
* Iterate over all values (row-major order)
|
|
168
|
-
*/
|
|
169
|
-
abstract values(): IterableIterator<T>;
|
|
170
|
-
/**
|
|
171
|
-
* Check if indices are within bounds
|
|
172
|
-
*/
|
|
173
|
-
protected checkBounds(row: number, col: number): void;
|
|
174
|
-
/**
|
|
175
|
-
* Check if dimensions match for element-wise operations
|
|
176
|
-
*/
|
|
177
|
-
protected checkDimensionsMatch(other: Matrix<T>): void;
|
|
178
|
-
/**
|
|
179
|
-
* Check if matrices can be multiplied
|
|
180
|
-
*/
|
|
181
|
-
protected checkMultiplyDimensions(other: Matrix<T>): void;
|
|
182
|
-
/**
|
|
183
|
-
* Format matrix for display
|
|
184
|
-
*/
|
|
185
|
-
toString(): string;
|
|
186
|
-
/**
|
|
187
|
-
* Check equality with another matrix
|
|
188
|
-
*/
|
|
189
|
-
equals(other: Matrix<T>, tolerance?: number): boolean;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Type guard to check if a value is a Matrix
|
|
193
|
-
*/
|
|
194
|
-
declare function isMatrix<T = number>(value: unknown): value is Matrix<T>;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Dense Matrix Implementation
|
|
198
|
-
*
|
|
199
|
-
* Row-major dense matrix backed by Float64Array for efficient storage
|
|
200
|
-
* and operations. Supports views (slices without copying) where possible.
|
|
201
|
-
*
|
|
202
|
-
* @packageDocumentation
|
|
203
|
-
*/
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Dense matrix implementation using Float64Array
|
|
207
|
-
*
|
|
208
|
-
* Data is stored in row-major order for cache-friendly row access.
|
|
209
|
-
* All operations return new matrices (immutable API).
|
|
210
|
-
*/
|
|
211
|
-
declare class DenseMatrix extends Matrix<number> {
|
|
212
|
-
readonly type: "DenseMatrix";
|
|
213
|
-
readonly rows: number;
|
|
214
|
-
readonly cols: number;
|
|
215
|
-
/**
|
|
216
|
-
* Internal data storage (row-major Float64Array)
|
|
217
|
-
*/
|
|
218
|
-
private readonly data;
|
|
219
|
-
/**
|
|
220
|
-
* Whether this matrix is a view into another matrix's data
|
|
221
|
-
*/
|
|
222
|
-
private readonly isView;
|
|
223
|
-
/**
|
|
224
|
-
* Offset into data array (for views)
|
|
225
|
-
*/
|
|
226
|
-
private readonly offset;
|
|
227
|
-
/**
|
|
228
|
-
* Stride between rows (for views)
|
|
229
|
-
*/
|
|
230
|
-
private readonly rowStride;
|
|
231
|
-
/**
|
|
232
|
-
* Create a new DenseMatrix
|
|
233
|
-
*
|
|
234
|
-
* @param rows - Number of rows
|
|
235
|
-
* @param cols - Number of columns
|
|
236
|
-
* @param data - Optional initial data (row-major order)
|
|
237
|
-
*/
|
|
238
|
-
constructor(rows: number, cols: number, data?: Float64Array | number[] | number[][], viewConfig?: {
|
|
239
|
-
isView: boolean;
|
|
240
|
-
offset: number;
|
|
241
|
-
rowStride: number;
|
|
242
|
-
});
|
|
243
|
-
/**
|
|
244
|
-
* Create a matrix from a nested array
|
|
245
|
-
*/
|
|
246
|
-
static fromArray(arr: number[][]): DenseMatrix;
|
|
247
|
-
/**
|
|
248
|
-
* Create a matrix from a flat array with specified dimensions
|
|
249
|
-
*/
|
|
250
|
-
static fromFlat(rows: number, cols: number, data: number[]): DenseMatrix;
|
|
251
|
-
/**
|
|
252
|
-
* Create a zero matrix
|
|
253
|
-
*/
|
|
254
|
-
static zeros(rows: number, cols: number): DenseMatrix;
|
|
255
|
-
/**
|
|
256
|
-
* Create a matrix filled with ones
|
|
257
|
-
*/
|
|
258
|
-
static ones(rows: number, cols: number): DenseMatrix;
|
|
259
|
-
/**
|
|
260
|
-
* Create an identity matrix
|
|
261
|
-
*/
|
|
262
|
-
static identity(n: number): DenseMatrix;
|
|
263
|
-
/**
|
|
264
|
-
* Create a diagonal matrix from values
|
|
265
|
-
*/
|
|
266
|
-
static diag(values: number[]): DenseMatrix;
|
|
267
|
-
/**
|
|
268
|
-
* Create a matrix filled with a constant value
|
|
269
|
-
*/
|
|
270
|
-
static fill(rows: number, cols: number, value: number): DenseMatrix;
|
|
271
|
-
/**
|
|
272
|
-
* Create a matrix with random values in [0, 1)
|
|
273
|
-
*/
|
|
274
|
-
static random(rows: number, cols: number): DenseMatrix;
|
|
275
|
-
/**
|
|
276
|
-
* Get element at (row, col)
|
|
277
|
-
* O(1) access time
|
|
278
|
-
*/
|
|
279
|
-
get(row: number, col: number): number;
|
|
280
|
-
/**
|
|
281
|
-
* Set element at (row, col) - returns new matrix
|
|
282
|
-
*/
|
|
283
|
-
set(row: number, col: number, value: number): DenseMatrix;
|
|
284
|
-
/**
|
|
285
|
-
* Get the raw Float64Array data (copy if view, reference otherwise)
|
|
286
|
-
*/
|
|
287
|
-
private toFlatFloat64Array;
|
|
288
|
-
/**
|
|
289
|
-
* Get a row as a 1×n matrix (view, no copy)
|
|
290
|
-
*/
|
|
291
|
-
row(index: number): DenseMatrix;
|
|
292
|
-
/**
|
|
293
|
-
* Get a column as an m×1 matrix (copy, since data is row-major)
|
|
294
|
-
*/
|
|
295
|
-
column(index: number): DenseMatrix;
|
|
296
|
-
/**
|
|
297
|
-
* Get a submatrix
|
|
298
|
-
*/
|
|
299
|
-
slice(spec: SliceSpec): DenseMatrix;
|
|
300
|
-
/**
|
|
301
|
-
* Get the diagonal elements
|
|
302
|
-
*/
|
|
303
|
-
diagonal(k?: number): DenseMatrix;
|
|
304
|
-
/**
|
|
305
|
-
* Matrix addition
|
|
306
|
-
*/
|
|
307
|
-
add(other: Matrix<number>): DenseMatrix;
|
|
308
|
-
/**
|
|
309
|
-
* Matrix subtraction
|
|
310
|
-
*/
|
|
311
|
-
subtract(other: Matrix<number>): DenseMatrix;
|
|
312
|
-
/**
|
|
313
|
-
* Element-wise multiplication (Hadamard product)
|
|
314
|
-
*/
|
|
315
|
-
multiplyElementwise(other: Matrix<number>): DenseMatrix;
|
|
316
|
-
/**
|
|
317
|
-
* Matrix multiplication
|
|
318
|
-
*/
|
|
319
|
-
multiply(other: Matrix<number>): DenseMatrix;
|
|
320
|
-
/**
|
|
321
|
-
* Scalar multiplication
|
|
322
|
-
*/
|
|
323
|
-
scale(scalar: number): DenseMatrix;
|
|
324
|
-
/**
|
|
325
|
-
* Matrix transpose
|
|
326
|
-
*/
|
|
327
|
-
transpose(): DenseMatrix;
|
|
328
|
-
/**
|
|
329
|
-
* Negate all elements
|
|
330
|
-
*/
|
|
331
|
-
negate(): DenseMatrix;
|
|
332
|
-
/**
|
|
333
|
-
* Sum of all elements
|
|
334
|
-
*/
|
|
335
|
-
sum(): number;
|
|
336
|
-
/**
|
|
337
|
-
* Mean of all elements
|
|
338
|
-
*/
|
|
339
|
-
mean(): number;
|
|
340
|
-
/**
|
|
341
|
-
* Minimum element
|
|
342
|
-
*/
|
|
343
|
-
min(): number;
|
|
344
|
-
/**
|
|
345
|
-
* Maximum element
|
|
346
|
-
*/
|
|
347
|
-
max(): number;
|
|
348
|
-
/**
|
|
349
|
-
* Frobenius norm (sqrt of sum of squared elements)
|
|
350
|
-
*/
|
|
351
|
-
norm(): number;
|
|
352
|
-
/**
|
|
353
|
-
* Trace (sum of diagonal elements)
|
|
354
|
-
*/
|
|
355
|
-
trace(): number;
|
|
356
|
-
/**
|
|
357
|
-
* Convert to nested array
|
|
358
|
-
*/
|
|
359
|
-
toArray(): number[][];
|
|
360
|
-
/**
|
|
361
|
-
* Convert to flat array (row-major)
|
|
362
|
-
*/
|
|
363
|
-
toFlatArray(): number[];
|
|
364
|
-
/**
|
|
365
|
-
* Get the underlying Float64Array (copy)
|
|
366
|
-
*/
|
|
367
|
-
toFloat64Array(): Float64Array;
|
|
368
|
-
/**
|
|
369
|
-
* Clone the matrix
|
|
370
|
-
*/
|
|
371
|
-
clone(): DenseMatrix;
|
|
372
|
-
/**
|
|
373
|
-
* Convert to sparse matrix (CSR format)
|
|
374
|
-
*
|
|
375
|
-
* This is a synchronous conversion that creates a SparseMatrix
|
|
376
|
-
* containing only the non-zero elements of this matrix.
|
|
377
|
-
*
|
|
378
|
-
* @param dropTolerance - Values below this threshold are treated as zero
|
|
379
|
-
* @returns SparseMatrix representation (typed as the `Matrix` base — the
|
|
380
|
-
* `SparseMatrix` subtype is loaded lazily to avoid an import cycle)
|
|
381
|
-
*/
|
|
382
|
-
toSparse(dropTolerance?: number): Matrix;
|
|
383
|
-
/**
|
|
384
|
-
* Iterate over entries with indices
|
|
385
|
-
*/
|
|
386
|
-
entries(): IterableIterator<MatrixEntry<number>>;
|
|
387
|
-
/**
|
|
388
|
-
* Iterate over values (row-major)
|
|
389
|
-
*/
|
|
390
|
-
values(): IterableIterator<number>;
|
|
391
|
-
/**
|
|
392
|
-
* Support for...of iteration (iterates over values)
|
|
393
|
-
*/
|
|
394
|
-
[Symbol.iterator](): IterableIterator<number>;
|
|
395
|
-
/**
|
|
396
|
-
* Apply a function to each element
|
|
397
|
-
*/
|
|
398
|
-
map(fn: (value: number, row: number, col: number) => number): DenseMatrix;
|
|
399
|
-
/**
|
|
400
|
-
* Apply a function to each element (no return, for side effects)
|
|
401
|
-
*/
|
|
402
|
-
forEach(fn: (value: number, row: number, col: number) => void): void;
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
* Type guard for DenseMatrix
|
|
406
|
-
*/
|
|
407
|
-
declare function isDenseMatrix(value: unknown): value is DenseMatrix;
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Sparse Matrix Implementation (CSR Format)
|
|
411
|
-
*
|
|
412
|
-
* Compressed Sparse Row (CSR) format sparse matrix for efficient storage
|
|
413
|
-
* and operations on matrices with many zero elements.
|
|
414
|
-
*
|
|
415
|
-
* CSR stores:
|
|
416
|
-
* - values: Non-zero values in row-major order
|
|
417
|
-
* - colIndices: Column index for each non-zero value
|
|
418
|
-
* - rowPointers: Index into values/colIndices where each row starts
|
|
419
|
-
*
|
|
420
|
-
* @packageDocumentation
|
|
421
|
-
*/
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* Sparse matrix using Compressed Sparse Row (CSR) format
|
|
425
|
-
*
|
|
426
|
-
* Efficient for:
|
|
427
|
-
* - Row slicing and iteration
|
|
428
|
-
* - Sparse matrix-vector multiplication
|
|
429
|
-
* - Matrices with many zeros (typically < 10% non-zero)
|
|
430
|
-
*/
|
|
431
|
-
declare class SparseMatrix extends Matrix<number> {
|
|
432
|
-
readonly type: "SparseMatrix";
|
|
433
|
-
readonly rows: number;
|
|
434
|
-
readonly cols: number;
|
|
435
|
-
/**
|
|
436
|
-
* Non-zero values in row-major order
|
|
437
|
-
*/
|
|
438
|
-
private readonly _data;
|
|
439
|
-
/**
|
|
440
|
-
* Column indices for each non-zero value
|
|
441
|
-
*/
|
|
442
|
-
private readonly _colIndices;
|
|
443
|
-
/**
|
|
444
|
-
* Row pointers: rowPointers[i] = index of first non-zero in row i
|
|
445
|
-
* rowPointers[rows] = total number of non-zeros (nnz)
|
|
446
|
-
*/
|
|
447
|
-
private readonly _rowPointers;
|
|
448
|
-
/**
|
|
449
|
-
* Create a sparse matrix from CSR components
|
|
450
|
-
*
|
|
451
|
-
* @param rows - Number of rows
|
|
452
|
-
* @param cols - Number of columns
|
|
453
|
-
* @param values - Non-zero values
|
|
454
|
-
* @param colIndices - Column index for each value
|
|
455
|
-
* @param rowPointers - Row start indices
|
|
456
|
-
*/
|
|
457
|
-
constructor(rows: number, cols: number, values: Float64Array | number[], colIndices: Int32Array | number[], rowPointers: Int32Array | number[]);
|
|
458
|
-
/**
|
|
459
|
-
* Create a sparse matrix from a dense matrix
|
|
460
|
-
*
|
|
461
|
-
* @param dense - Dense matrix to convert
|
|
462
|
-
* @param dropTolerance - Values below this threshold are treated as zero
|
|
463
|
-
*/
|
|
464
|
-
static fromDense(dense: DenseMatrix, dropTolerance?: number): SparseMatrix;
|
|
465
|
-
/**
|
|
466
|
-
* Create a sparse matrix from coordinate (COO) format
|
|
467
|
-
*
|
|
468
|
-
* @param rows - Number of rows
|
|
469
|
-
* @param cols - Number of columns
|
|
470
|
-
* @param entries - Array of {row, col, value} entries
|
|
471
|
-
*/
|
|
472
|
-
static fromCOO(rows: number, cols: number, entries: Array<{
|
|
473
|
-
row: number;
|
|
474
|
-
col: number;
|
|
475
|
-
value: number;
|
|
476
|
-
}>): SparseMatrix;
|
|
477
|
-
/**
|
|
478
|
-
* Create a zero sparse matrix
|
|
479
|
-
*/
|
|
480
|
-
static zeros(rows: number, cols: number): SparseMatrix;
|
|
481
|
-
/**
|
|
482
|
-
* Create a sparse identity matrix
|
|
483
|
-
*/
|
|
484
|
-
static identity(n: number): SparseMatrix;
|
|
485
|
-
/**
|
|
486
|
-
* Create a sparse diagonal matrix
|
|
487
|
-
*/
|
|
488
|
-
static diag(values: number[]): SparseMatrix;
|
|
489
|
-
/**
|
|
490
|
-
* Number of non-zero elements
|
|
491
|
-
*/
|
|
492
|
-
get nnz(): number;
|
|
493
|
-
/**
|
|
494
|
-
* Sparsity: fraction of zero elements
|
|
495
|
-
*/
|
|
496
|
-
get sparsity(): number;
|
|
497
|
-
/**
|
|
498
|
-
* Density: fraction of non-zero elements
|
|
499
|
-
*/
|
|
500
|
-
get density(): number;
|
|
501
|
-
/**
|
|
502
|
-
* Get element at (row, col) - O(log(nnz_in_row)) for sorted columns
|
|
503
|
-
*/
|
|
504
|
-
get(row: number, col: number): number;
|
|
505
|
-
/**
|
|
506
|
-
* Set element at (row, col) - returns new matrix (immutable)
|
|
507
|
-
*/
|
|
508
|
-
set(row: number, col: number, value: number): SparseMatrix;
|
|
509
|
-
/**
|
|
510
|
-
* Get a row as a sparse 1×n matrix
|
|
511
|
-
*/
|
|
512
|
-
row(index: number): SparseMatrix;
|
|
513
|
-
/**
|
|
514
|
-
* Get a column as a sparse m×1 matrix
|
|
515
|
-
*/
|
|
516
|
-
column(index: number): SparseMatrix;
|
|
517
|
-
/**
|
|
518
|
-
* Get a submatrix
|
|
519
|
-
*/
|
|
520
|
-
slice(spec: SliceSpec): SparseMatrix;
|
|
521
|
-
/**
|
|
522
|
-
* Get the diagonal elements
|
|
523
|
-
*/
|
|
524
|
-
diagonal(k?: number): SparseMatrix;
|
|
525
|
-
/**
|
|
526
|
-
* Sparse matrix addition
|
|
527
|
-
*/
|
|
528
|
-
add(other: Matrix<number>): SparseMatrix;
|
|
529
|
-
private addSparse;
|
|
530
|
-
/**
|
|
531
|
-
* Sparse matrix subtraction
|
|
532
|
-
*/
|
|
533
|
-
subtract(other: Matrix<number>): SparseMatrix;
|
|
534
|
-
/**
|
|
535
|
-
* Element-wise multiplication (Hadamard product)
|
|
536
|
-
*/
|
|
537
|
-
multiplyElementwise(other: Matrix<number>): SparseMatrix;
|
|
538
|
-
/**
|
|
539
|
-
* Sparse matrix multiplication
|
|
540
|
-
*/
|
|
541
|
-
multiply(other: Matrix<number>): SparseMatrix;
|
|
542
|
-
private multiplySparse;
|
|
543
|
-
/**
|
|
544
|
-
* Scalar multiplication
|
|
545
|
-
*/
|
|
546
|
-
scale(scalar: number): SparseMatrix;
|
|
547
|
-
/**
|
|
548
|
-
* Sparse matrix transpose
|
|
549
|
-
*/
|
|
550
|
-
transpose(): SparseMatrix;
|
|
551
|
-
/**
|
|
552
|
-
* Negate all elements
|
|
553
|
-
*/
|
|
554
|
-
negate(): SparseMatrix;
|
|
555
|
-
/**
|
|
556
|
-
* Sum of all elements
|
|
557
|
-
*/
|
|
558
|
-
sum(): number;
|
|
559
|
-
/**
|
|
560
|
-
* Frobenius norm
|
|
561
|
-
*/
|
|
562
|
-
norm(): number;
|
|
563
|
-
/**
|
|
564
|
-
* Trace (sum of diagonal elements)
|
|
565
|
-
*/
|
|
566
|
-
trace(): number;
|
|
567
|
-
/**
|
|
568
|
-
* Convert to dense matrix
|
|
569
|
-
*/
|
|
570
|
-
toDense(): DenseMatrix;
|
|
571
|
-
/**
|
|
572
|
-
* Convert to nested array
|
|
573
|
-
*/
|
|
574
|
-
toArray(): number[][];
|
|
575
|
-
/**
|
|
576
|
-
* Convert to flat array (row-major)
|
|
577
|
-
*/
|
|
578
|
-
toFlatArray(): number[];
|
|
579
|
-
/**
|
|
580
|
-
* Clone the matrix
|
|
581
|
-
*/
|
|
582
|
-
clone(): SparseMatrix;
|
|
583
|
-
/**
|
|
584
|
-
* Get the CSR components
|
|
585
|
-
*/
|
|
586
|
-
getCSR(): {
|
|
587
|
-
values: Float64Array;
|
|
588
|
-
colIndices: Int32Array;
|
|
589
|
-
rowPointers: Int32Array;
|
|
590
|
-
};
|
|
591
|
-
/**
|
|
592
|
-
* Iterate over non-zero entries
|
|
593
|
-
*/
|
|
594
|
-
entries(): IterableIterator<MatrixEntry<number>>;
|
|
595
|
-
/**
|
|
596
|
-
* Iterate over non-zero values
|
|
597
|
-
*/
|
|
598
|
-
values(): IterableIterator<number>;
|
|
599
|
-
/**
|
|
600
|
-
* Iterate over all values (including zeros, row-major)
|
|
601
|
-
*/
|
|
602
|
-
allValues(): IterableIterator<number>;
|
|
603
|
-
/**
|
|
604
|
-
* Support for...of iteration (iterates over non-zero values)
|
|
605
|
-
*/
|
|
606
|
-
[Symbol.iterator](): IterableIterator<number>;
|
|
607
|
-
/**
|
|
608
|
-
* Apply a function to each non-zero element
|
|
609
|
-
*/
|
|
610
|
-
mapNonZeros(fn: (value: number, row: number, col: number) => number): SparseMatrix;
|
|
611
|
-
/**
|
|
612
|
-
* Apply a function to each element (including zeros)
|
|
613
|
-
* Warning: This may create a dense result
|
|
614
|
-
*/
|
|
615
|
-
map(fn: (value: number, row: number, col: number) => number): SparseMatrix;
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* Type guard for SparseMatrix
|
|
619
|
-
*/
|
|
620
|
-
declare function isSparseMatrix(value: unknown): value is SparseMatrix;
|
|
621
|
-
|
|
622
|
-
/**
|
|
623
|
-
* Matrix Backend Interface
|
|
624
|
-
*
|
|
625
|
-
* Defines the contract for matrix operation backends.
|
|
626
|
-
* Implementations include JSBackend (pure TypeScript),
|
|
627
|
-
* WASMBackend (AssemblyScript), and GPUBackend (WebGPU).
|
|
628
|
-
*
|
|
629
|
-
* @packageDocumentation
|
|
630
|
-
*/
|
|
631
|
-
|
|
632
|
-
/**
|
|
633
|
-
* Backend type identifier
|
|
634
|
-
*/
|
|
635
|
-
type BackendType = 'js' | 'wasm' | 'gpu' | 'parallel';
|
|
636
|
-
/**
|
|
637
|
-
* Backend selection hints
|
|
638
|
-
*/
|
|
639
|
-
interface BackendHints {
|
|
640
|
-
/** Minimum size to use WASM backend */
|
|
641
|
-
wasmThreshold?: number;
|
|
642
|
-
/** Minimum size to use GPU backend */
|
|
643
|
-
gpuThreshold?: number;
|
|
644
|
-
/** Force a specific backend */
|
|
645
|
-
preferredBackend?: BackendType;
|
|
646
|
-
}
|
|
647
|
-
/**
|
|
648
|
-
* Default backend hints
|
|
649
|
-
*/
|
|
650
|
-
declare const DEFAULT_BACKEND_HINTS: Required<BackendHints>;
|
|
651
|
-
/**
|
|
652
|
-
* Abstract backend interface for matrix operations
|
|
653
|
-
*
|
|
654
|
-
* Each backend implements the same operations but may use different
|
|
655
|
-
* underlying implementations (pure JS, WASM SIMD, WebGPU compute shaders).
|
|
656
|
-
*/
|
|
657
|
-
interface MatrixBackend {
|
|
658
|
-
/**
|
|
659
|
-
* Backend identifier
|
|
660
|
-
*/
|
|
661
|
-
readonly type: BackendType;
|
|
662
|
-
/**
|
|
663
|
-
* Whether the backend is available in the current environment
|
|
664
|
-
*/
|
|
665
|
-
isAvailable(): boolean;
|
|
666
|
-
/**
|
|
667
|
-
* Initialize the backend (may be async for WASM/GPU)
|
|
668
|
-
*/
|
|
669
|
-
initialize(): Promise<void>;
|
|
670
|
-
/**
|
|
671
|
-
* Matrix addition: C = A + B
|
|
672
|
-
*/
|
|
673
|
-
add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
674
|
-
/**
|
|
675
|
-
* Matrix subtraction: C = A - B
|
|
676
|
-
*/
|
|
677
|
-
subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
678
|
-
/**
|
|
679
|
-
* Element-wise multiplication: C = A .* B
|
|
680
|
-
*/
|
|
681
|
-
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
682
|
-
/**
|
|
683
|
-
* Element-wise division: C = A ./ B
|
|
684
|
-
*/
|
|
685
|
-
divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
686
|
-
/**
|
|
687
|
-
* Scalar multiplication: C = A * s
|
|
688
|
-
*/
|
|
689
|
-
scale(a: DenseMatrix, scalar: number): DenseMatrix;
|
|
690
|
-
/**
|
|
691
|
-
* Element-wise absolute value
|
|
692
|
-
*/
|
|
693
|
-
abs(a: DenseMatrix): DenseMatrix;
|
|
694
|
-
/**
|
|
695
|
-
* Element-wise negation
|
|
696
|
-
*/
|
|
697
|
-
negate(a: DenseMatrix): DenseMatrix;
|
|
698
|
-
/**
|
|
699
|
-
* Matrix multiplication: C = A * B
|
|
700
|
-
*/
|
|
701
|
-
multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
702
|
-
/**
|
|
703
|
-
* Matrix transpose
|
|
704
|
-
*/
|
|
705
|
-
transpose(a: DenseMatrix): DenseMatrix;
|
|
706
|
-
/**
|
|
707
|
-
* Sum of all elements (may be async for parallel backends)
|
|
708
|
-
*/
|
|
709
|
-
sum(a: DenseMatrix): number | Promise<number>;
|
|
710
|
-
/**
|
|
711
|
-
* Sum along an axis (0 = columns, 1 = rows)
|
|
712
|
-
*/
|
|
713
|
-
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
714
|
-
/**
|
|
715
|
-
* Frobenius norm
|
|
716
|
-
*/
|
|
717
|
-
norm(a: DenseMatrix): number;
|
|
718
|
-
/**
|
|
719
|
-
* Dot product of two vectors (may be async for parallel backends)
|
|
720
|
-
*/
|
|
721
|
-
dot(a: DenseMatrix, b: DenseMatrix): number | Promise<number>;
|
|
722
|
-
}
|
|
723
|
-
/**
|
|
724
|
-
* Registry of available backends
|
|
725
|
-
*/
|
|
726
|
-
declare class BackendRegistry {
|
|
727
|
-
private backends;
|
|
728
|
-
private initialized;
|
|
729
|
-
private hints;
|
|
730
|
-
/**
|
|
731
|
-
* Register a backend
|
|
732
|
-
*/
|
|
733
|
-
register(backend: MatrixBackend): void;
|
|
734
|
-
/**
|
|
735
|
-
* Get a backend by type
|
|
736
|
-
*/
|
|
737
|
-
get(type: BackendType): MatrixBackend | undefined;
|
|
738
|
-
/**
|
|
739
|
-
* Check if a backend is registered and available
|
|
740
|
-
*/
|
|
741
|
-
has(type: BackendType): boolean;
|
|
742
|
-
/**
|
|
743
|
-
* Initialize a backend
|
|
744
|
-
*/
|
|
745
|
-
initialize(type: BackendType): Promise<void>;
|
|
746
|
-
/**
|
|
747
|
-
* Get all available backends
|
|
748
|
-
*/
|
|
749
|
-
available(): BackendType[];
|
|
750
|
-
/**
|
|
751
|
-
* Update selection hints
|
|
752
|
-
*/
|
|
753
|
-
setHints(hints: BackendHints): void;
|
|
754
|
-
/**
|
|
755
|
-
* Get current hints
|
|
756
|
-
*/
|
|
757
|
-
getHints(): Required<BackendHints>;
|
|
758
|
-
/**
|
|
759
|
-
* Select the best backend for a given operation size
|
|
760
|
-
*/
|
|
761
|
-
selectBackend(elementCount: number): MatrixBackend;
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* Global backend registry
|
|
765
|
-
*/
|
|
766
|
-
declare const backendRegistry: BackendRegistry;
|
|
767
|
-
|
|
768
|
-
/**
|
|
769
|
-
* Pure TypeScript Matrix Backend
|
|
770
|
-
*
|
|
771
|
-
* Reference implementation of matrix operations using plain JavaScript.
|
|
772
|
-
* Serves as baseline for correctness and fallback when WASM/GPU unavailable.
|
|
773
|
-
*
|
|
774
|
-
* @packageDocumentation
|
|
775
|
-
*/
|
|
776
|
-
|
|
777
|
-
/**
|
|
778
|
-
* Pure JavaScript matrix backend
|
|
779
|
-
*
|
|
780
|
-
* All operations are implemented in TypeScript without external dependencies.
|
|
781
|
-
* Performance is optimized where possible but prioritizes correctness.
|
|
782
|
-
*/
|
|
783
|
-
declare class JSBackend implements MatrixBackend {
|
|
784
|
-
readonly type: BackendType;
|
|
785
|
-
/**
|
|
786
|
-
* JS backend is always available
|
|
787
|
-
*/
|
|
788
|
-
isAvailable(): boolean;
|
|
789
|
-
/**
|
|
790
|
-
* No initialization needed for JS backend
|
|
791
|
-
*/
|
|
792
|
-
initialize(): Promise<void>;
|
|
793
|
-
/**
|
|
794
|
-
* Matrix addition
|
|
795
|
-
*/
|
|
796
|
-
add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
797
|
-
/**
|
|
798
|
-
* Matrix subtraction
|
|
799
|
-
*/
|
|
800
|
-
subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
801
|
-
/**
|
|
802
|
-
* Element-wise multiplication
|
|
803
|
-
*/
|
|
804
|
-
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
805
|
-
/**
|
|
806
|
-
* Element-wise division
|
|
807
|
-
*/
|
|
808
|
-
divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
809
|
-
/**
|
|
810
|
-
* Scalar multiplication
|
|
811
|
-
*/
|
|
812
|
-
scale(a: DenseMatrix, scalar: number): DenseMatrix;
|
|
813
|
-
/**
|
|
814
|
-
* Element-wise absolute value
|
|
815
|
-
*/
|
|
816
|
-
abs(a: DenseMatrix): DenseMatrix;
|
|
817
|
-
/**
|
|
818
|
-
* Element-wise negation
|
|
819
|
-
*/
|
|
820
|
-
negate(a: DenseMatrix): DenseMatrix;
|
|
821
|
-
/**
|
|
822
|
-
* Matrix multiplication using the naive O(n³) algorithm
|
|
823
|
-
*
|
|
824
|
-
* For larger matrices, consider using blocked/tiled multiplication
|
|
825
|
-
* or Strassen's algorithm (implemented in WASM backend).
|
|
826
|
-
*/
|
|
827
|
-
multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
828
|
-
/**
|
|
829
|
-
* Matrix transpose
|
|
830
|
-
*/
|
|
831
|
-
transpose(a: DenseMatrix): DenseMatrix;
|
|
832
|
-
/**
|
|
833
|
-
* Sum of all elements
|
|
834
|
-
*/
|
|
835
|
-
sum(a: DenseMatrix): number;
|
|
836
|
-
/**
|
|
837
|
-
* Sum along an axis
|
|
838
|
-
* axis=0: sum columns (result is 1×n row vector)
|
|
839
|
-
* axis=1: sum rows (result is m×1 column vector)
|
|
840
|
-
*/
|
|
841
|
-
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
842
|
-
/**
|
|
843
|
-
* Frobenius norm: sqrt(sum of squared elements)
|
|
844
|
-
*/
|
|
845
|
-
norm(a: DenseMatrix): number;
|
|
846
|
-
/**
|
|
847
|
-
* Dot product of two vectors
|
|
848
|
-
*
|
|
849
|
-
* Works with row vectors (1×n) or column vectors (m×1).
|
|
850
|
-
*/
|
|
851
|
-
dot(a: DenseMatrix, b: DenseMatrix): number;
|
|
852
|
-
private checkDimensionsMatch;
|
|
853
|
-
private checkMultiplyDimensions;
|
|
854
|
-
}
|
|
855
|
-
/**
|
|
856
|
-
* Default JS backend instance
|
|
857
|
-
*/
|
|
858
|
-
declare const jsBackend: JSBackend;
|
|
859
|
-
|
|
860
|
-
/**
|
|
861
|
-
* Configuration for ParallelBackend
|
|
862
|
-
*/
|
|
863
|
-
interface ParallelBackendConfig {
|
|
864
|
-
/** Custom ComputePool instance (optional) */
|
|
865
|
-
pool?: ComputePool;
|
|
866
|
-
/** Pool configuration (if creating new pool) */
|
|
867
|
-
poolConfig?: Partial<ComputePoolConfig>;
|
|
868
|
-
/** Threshold for parallel execution (elements) */
|
|
869
|
-
parallelThreshold?: number;
|
|
870
|
-
}
|
|
871
|
-
/**
|
|
872
|
-
* Parallel matrix backend using worker pool
|
|
873
|
-
*
|
|
874
|
-
* Provides parallel implementations of matrix operations for large matrices.
|
|
875
|
-
* Uses automatic chunking and result aggregation for efficient parallel execution.
|
|
876
|
-
*
|
|
877
|
-
* @example
|
|
878
|
-
* ```typescript
|
|
879
|
-
* const backend = new ParallelBackend();
|
|
880
|
-
* await backend.initialize();
|
|
881
|
-
*
|
|
882
|
-
* const result = await backend.multiply(largeMatrixA, largeMatrixB);
|
|
883
|
-
* ```
|
|
884
|
-
*/
|
|
885
|
-
declare class ParallelBackend {
|
|
886
|
-
readonly type: BackendType;
|
|
887
|
-
private pool;
|
|
888
|
-
private initialized;
|
|
889
|
-
private parallelThreshold;
|
|
890
|
-
constructor(config?: ParallelBackendConfig);
|
|
891
|
-
/**
|
|
892
|
-
* Check if parallel backend is available
|
|
893
|
-
*/
|
|
894
|
-
isAvailable(): boolean;
|
|
895
|
-
/**
|
|
896
|
-
* Initialize the worker pool
|
|
897
|
-
*/
|
|
898
|
-
initialize(): Promise<void>;
|
|
899
|
-
/**
|
|
900
|
-
* Check if initialized
|
|
901
|
-
*/
|
|
902
|
-
isReady(): boolean;
|
|
903
|
-
/**
|
|
904
|
-
* Determine if operation should be parallelized
|
|
905
|
-
*/
|
|
906
|
-
private shouldParallelize;
|
|
907
|
-
/**
|
|
908
|
-
* Parallel matrix addition
|
|
909
|
-
*/
|
|
910
|
-
add(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
911
|
-
/**
|
|
912
|
-
* Parallel matrix subtraction
|
|
913
|
-
*/
|
|
914
|
-
subtract(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
915
|
-
/**
|
|
916
|
-
* Parallel element-wise multiplication
|
|
917
|
-
*/
|
|
918
|
-
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
919
|
-
/**
|
|
920
|
-
* Parallel element-wise division
|
|
921
|
-
*/
|
|
922
|
-
divideElementwise(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
923
|
-
/**
|
|
924
|
-
* Parallel scalar multiplication
|
|
925
|
-
*/
|
|
926
|
-
scale(a: DenseMatrix, scalar: number): Promise<DenseMatrix>;
|
|
927
|
-
/**
|
|
928
|
-
* Element-wise absolute value (sequential, as it's simple)
|
|
929
|
-
*/
|
|
930
|
-
abs(a: DenseMatrix): DenseMatrix;
|
|
931
|
-
/**
|
|
932
|
-
* Element-wise negation
|
|
933
|
-
*/
|
|
934
|
-
negate(a: DenseMatrix): DenseMatrix;
|
|
935
|
-
/**
|
|
936
|
-
* Parallel matrix multiplication
|
|
937
|
-
*/
|
|
938
|
-
multiply(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
939
|
-
/**
|
|
940
|
-
* Parallel matrix transpose
|
|
941
|
-
*/
|
|
942
|
-
transpose(a: DenseMatrix): Promise<DenseMatrix>;
|
|
943
|
-
/**
|
|
944
|
-
* Parallel sum of all elements
|
|
945
|
-
*/
|
|
946
|
-
sum(a: DenseMatrix): Promise<number>;
|
|
947
|
-
/**
|
|
948
|
-
* Sum along an axis (sequential)
|
|
949
|
-
*/
|
|
950
|
-
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
951
|
-
/**
|
|
952
|
-
* Frobenius norm (sequential)
|
|
953
|
-
*/
|
|
954
|
-
norm(a: DenseMatrix): number;
|
|
955
|
-
/**
|
|
956
|
-
* Parallel dot product
|
|
957
|
-
*/
|
|
958
|
-
dot(a: DenseMatrix, b: DenseMatrix): Promise<number>;
|
|
959
|
-
/**
|
|
960
|
-
* Terminate the worker pool
|
|
961
|
-
*/
|
|
962
|
-
terminate(): Promise<void>;
|
|
963
|
-
/**
|
|
964
|
-
* Get pool statistics
|
|
965
|
-
*/
|
|
966
|
-
getStats(): workerpool.PoolStats;
|
|
967
|
-
private checkDimensionsMatch;
|
|
968
|
-
private checkMultiplyDimensions;
|
|
969
|
-
}
|
|
970
|
-
/**
|
|
971
|
-
* Default parallel backend instance using global compute pool
|
|
972
|
-
*/
|
|
973
|
-
declare const parallelBackend: ParallelBackend;
|
|
974
|
-
/**
|
|
975
|
-
* Create a new parallel backend with custom configuration
|
|
976
|
-
*/
|
|
977
|
-
declare function createParallelBackend(config?: ParallelBackendConfig): ParallelBackend;
|
|
978
|
-
|
|
979
|
-
/**
|
|
980
|
-
* WASM Feature Detection
|
|
981
|
-
*
|
|
982
|
-
* Detects available WebAssembly features in the current environment.
|
|
983
|
-
* Used to enable/disable SIMD, threads, and other optimizations.
|
|
984
|
-
*
|
|
985
|
-
* @packageDocumentation
|
|
986
|
-
*/
|
|
987
|
-
/**
|
|
988
|
-
* Available WASM features
|
|
989
|
-
*/
|
|
990
|
-
interface WasmFeatures {
|
|
991
|
-
/** Basic WebAssembly support */
|
|
992
|
-
webAssembly: boolean;
|
|
993
|
-
/** WASM SIMD (v128) support */
|
|
994
|
-
simd: boolean;
|
|
995
|
-
/** SharedArrayBuffer available */
|
|
996
|
-
sharedMemory: boolean;
|
|
997
|
-
/** Atomics API available */
|
|
998
|
-
atomics: boolean;
|
|
999
|
-
/** WASM threads (requires SharedArrayBuffer) */
|
|
1000
|
-
threads: boolean;
|
|
1001
|
-
/** Bulk memory operations */
|
|
1002
|
-
bulkMemory: boolean;
|
|
1003
|
-
/** Reference types */
|
|
1004
|
-
referenceTypes: boolean;
|
|
1005
|
-
/** WASM exception handling */
|
|
1006
|
-
exceptions: boolean;
|
|
1007
|
-
/** Tail call optimization */
|
|
1008
|
-
tailCall: boolean;
|
|
1009
|
-
}
|
|
1010
|
-
/**
|
|
1011
|
-
* Detect all WASM features
|
|
1012
|
-
*
|
|
1013
|
-
* @returns Promise resolving to detected features
|
|
1014
|
-
*/
|
|
1015
|
-
declare function detectWasmFeatures(): Promise<WasmFeatures>;
|
|
1016
|
-
/**
|
|
1017
|
-
* Synchronously check if WASM is available (basic check only)
|
|
1018
|
-
*/
|
|
1019
|
-
declare function isWasmAvailable(): boolean;
|
|
1020
|
-
/**
|
|
1021
|
-
* Synchronously check if SharedArrayBuffer is available
|
|
1022
|
-
*/
|
|
1023
|
-
declare function isSharedMemoryAvailable(): boolean;
|
|
1024
|
-
/**
|
|
1025
|
-
* Synchronously check if Atomics are available
|
|
1026
|
-
*/
|
|
1027
|
-
declare function isAtomicsAvailable(): boolean;
|
|
1028
|
-
/**
|
|
1029
|
-
* Clear the feature detection cache (useful for testing)
|
|
1030
|
-
*/
|
|
1031
|
-
declare function clearFeatureCache(): void;
|
|
1032
|
-
/**
|
|
1033
|
-
* Get cached features if available
|
|
1034
|
-
*/
|
|
1035
|
-
declare function getCachedFeatures(): WasmFeatures | null;
|
|
1036
|
-
|
|
1037
|
-
/**
|
|
1038
|
-
* WASM Matrix Backend (AssemblyScript)
|
|
1039
|
-
*
|
|
1040
|
-
* Implements MatrixBackend using the AssemblyScript-compiled WebAssembly
|
|
1041
|
-
* module (`dist/wasm/mathts-as.wasm`, ~65 KB). Falls back to JSBackend when
|
|
1042
|
-
* the WASM module is unavailable or for small matrices.
|
|
1043
|
-
*
|
|
1044
|
-
* ABI:
|
|
1045
|
-
* The AS module exports a managed runtime (`__new` / `__pin` / `__unpin` /
|
|
1046
|
-
* `__collect`) and its matrix/array functions accept managed `Float64Array`
|
|
1047
|
-
* *header* pointers — NOT raw flat-memory pointers. A typed-array header is
|
|
1048
|
-
* a 12-byte block at some offset that stores (buffer, buffer, byteLength).
|
|
1049
|
-
*
|
|
1050
|
-
* To call e.g. `matrix_add(a, b, result)` from JS, we have to:
|
|
1051
|
-
* 1. `__new(byteLength, 1)` — allocate the data buffer (id=1 = ArrayBuffer)
|
|
1052
|
-
* 2. `__pin(buffer)` — pin so it survives GC while building header
|
|
1053
|
-
* 3. `__new(12, 5)` — allocate the Float64Array header (id=5)
|
|
1054
|
-
* 4. Write [buffer, buffer, byteLength] into the header
|
|
1055
|
-
* 5. Copy caller data into the buffer
|
|
1056
|
-
* 6. `__unpin(buffer)` — the header now owns the buffer reference
|
|
1057
|
-
*
|
|
1058
|
-
* The legacy artifact had no managed runtime and used a completely
|
|
1059
|
-
* different ABI (camelCase exports, flat-memory raw pointers). It was
|
|
1060
|
-
* retired in Phase 7b; the AssemblyScript binary is now the only WASM
|
|
1061
|
-
* backend.
|
|
1062
|
-
*
|
|
1063
|
-
* Naming map (legacy backend ↔ AssemblyScript) — kept for historical reference:
|
|
1064
|
-
*
|
|
1065
|
-
* Op Legacy (camelCase, flat ptrs) AS (snake_case, header refs)
|
|
1066
|
-
* ------------------ --------------------------------- -------------------------------
|
|
1067
|
-
* add add(aPtr,bPtr,n,resPtr) matrix_add(a,b,result)
|
|
1068
|
-
* subtract subtract(aPtr,bPtr,n,resPtr) matrix_sub(a,b,result)
|
|
1069
|
-
* multiplyElementwise simdMulF64(aPtr,bPtr,resPtr,n) matrix_mul_elementwise(a,b,result)
|
|
1070
|
-
* divideElementwise (none) matrix_div_elementwise(a,b,result)
|
|
1071
|
-
* scale simdScaleF64(aPtr,s,resPtr,n) matrix_scale(a,scalar,result)
|
|
1072
|
-
* abs simdAbsF64(aPtr,resPtr,n) array_abs(a,result)
|
|
1073
|
-
* negate simdScaleF64(aPtr,-1,resPtr,n) matrix_neg(a,result)
|
|
1074
|
-
* multiply multiplyDense(a,r,c,b,r,c,res) matrix_multiply(a,aRows,aCols,b,bCols,result)
|
|
1075
|
-
* transpose transpose(a,r,c,res) matrix_transpose(a,rows,cols,result)
|
|
1076
|
-
* sum simdSumF64(aPtr,n) array_sum(a)
|
|
1077
|
-
* norm simdNormF64(aPtr,n) array_norm(a)
|
|
1078
|
-
* dot dotProduct(aPtr,bPtr,n) array_dot(a,b)
|
|
1079
|
-
*
|
|
1080
|
-
* AssemblyScript also exports LU / QR / Cholesky / inverse / determinant
|
|
1081
|
-
* (see assembly/src/algebra/decomposition.ts); the methods below dispatch
|
|
1082
|
-
* to them when present and fall back to the in-process JS implementation
|
|
1083
|
-
* otherwise.
|
|
1084
|
-
*
|
|
1085
|
-
* @packageDocumentation
|
|
1086
|
-
*/
|
|
1087
|
-
|
|
1088
|
-
/**
|
|
1089
|
-
* WASM Backend configuration
|
|
1090
|
-
*/
|
|
1091
|
-
interface WASMBackendConfig {
|
|
1092
|
-
/** Minimum elements to use WASM (default: 100) */
|
|
1093
|
-
minElements?: number;
|
|
1094
|
-
/** Path to WASM file (defaults to the packaged AS artifact, `dist/wasm/mathts-as.wasm`) */
|
|
1095
|
-
wasmPath?: string;
|
|
1096
|
-
/** Enable SIMD code paths if the AS module exposes them (currently a no-op
|
|
1097
|
-
* — AS export surface does not include `simd*` ops; kept for API stability). */
|
|
1098
|
-
useSIMD?: boolean;
|
|
1099
|
-
}
|
|
1100
|
-
/**
|
|
1101
|
-
* WASM Backend for matrix operations (AssemblyScript path).
|
|
1102
|
-
*
|
|
1103
|
-
* This is the sole WASM backend after Phase 7b retired the legacy path; it
|
|
1104
|
-
* reports `type: 'wasm'` and is registered by `register-backends.ts`.
|
|
1105
|
-
*/
|
|
1106
|
-
declare class WASMBackend implements MatrixBackend {
|
|
1107
|
-
readonly type: BackendType;
|
|
1108
|
-
private config;
|
|
1109
|
-
private wasmModule;
|
|
1110
|
-
private features;
|
|
1111
|
-
private initPromise;
|
|
1112
|
-
/** Pooled allocations — see `AsAllocCache` for rationale. */
|
|
1113
|
-
private allocCache;
|
|
1114
|
-
constructor(config?: WASMBackendConfig);
|
|
1115
|
-
isAvailable(): boolean;
|
|
1116
|
-
initialize(): Promise<void>;
|
|
1117
|
-
private doInitialize;
|
|
1118
|
-
/**
|
|
1119
|
-
* Resolve the path to the AssemblyScript artifact (`dist/wasm/mathts-as.wasm`).
|
|
1120
|
-
*/
|
|
1121
|
-
private resolveAsWasmPath;
|
|
1122
|
-
/**
|
|
1123
|
-
* Compile + instantiate the AssemblyScript WASM artifact. Each
|
|
1124
|
-
* WASMBackend instance owns its own instance — this is intentional so
|
|
1125
|
-
* tests and multiple backend instances can coexist in the same process.
|
|
1126
|
-
*/
|
|
1127
|
-
private loadAsModule;
|
|
1128
|
-
/**
|
|
1129
|
-
* WASM is only worth its copy/alloc overhead for compute-dense ops. The 2026-07-01
|
|
1130
|
-
* backend audit (`tools/benchmarks/backend-audit`) measured element-wise / transpose
|
|
1131
|
-
* ops 4–6× SLOWER on WASM than JS (memory-bound; the SIMD matmul kernel is the only
|
|
1132
|
-
* clear win, 9–12×). So WASM is reserved for `opKind: 'matmul'`; everything else stays
|
|
1133
|
-
* on JS via each method's existing `jsBackend` fallback.
|
|
1134
|
-
*/
|
|
1135
|
-
private shouldUseWasm;
|
|
1136
|
-
getFeatures(): WasmFeatures | null;
|
|
1137
|
-
add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1138
|
-
subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1139
|
-
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1140
|
-
divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1141
|
-
scale(a: DenseMatrix, scalar: number): DenseMatrix;
|
|
1142
|
-
abs(a: DenseMatrix): DenseMatrix;
|
|
1143
|
-
negate(a: DenseMatrix): DenseMatrix;
|
|
1144
|
-
multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1145
|
-
transpose(a: DenseMatrix): DenseMatrix;
|
|
1146
|
-
sum(a: DenseMatrix): number;
|
|
1147
|
-
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
1148
|
-
norm(a: DenseMatrix): number;
|
|
1149
|
-
dot(a: DenseMatrix, b: DenseMatrix): number;
|
|
1150
|
-
luDecomposition(a: DenseMatrix): Promise<{
|
|
1151
|
-
lu: DenseMatrix;
|
|
1152
|
-
perm: Int32Array;
|
|
1153
|
-
singular: boolean;
|
|
1154
|
-
}>;
|
|
1155
|
-
private luDecompositionJS;
|
|
1156
|
-
qrDecomposition(a: DenseMatrix): Promise<{
|
|
1157
|
-
q: DenseMatrix;
|
|
1158
|
-
r: DenseMatrix;
|
|
1159
|
-
}>;
|
|
1160
|
-
private qrDecompositionJS;
|
|
1161
|
-
inverse(a: DenseMatrix): Promise<{
|
|
1162
|
-
inverse: DenseMatrix;
|
|
1163
|
-
singular: boolean;
|
|
1164
|
-
}>;
|
|
1165
|
-
private inverseJS;
|
|
1166
|
-
determinantWasm(a: DenseMatrix): Promise<number>;
|
|
1167
|
-
private determinantJS;
|
|
1168
|
-
choleskyDecomposition(a: DenseMatrix): Promise<{
|
|
1169
|
-
l: DenseMatrix;
|
|
1170
|
-
positiveDefinite: boolean;
|
|
1171
|
-
}>;
|
|
1172
|
-
private choleskyDecompositionJS;
|
|
1173
|
-
/**
|
|
1174
|
-
* Update configuration
|
|
1175
|
-
*/
|
|
1176
|
-
updateConfig(config: Partial<WASMBackendConfig>): void;
|
|
1177
|
-
/**
|
|
1178
|
-
* Get current configuration
|
|
1179
|
-
*/
|
|
1180
|
-
getConfig(): Required<WASMBackendConfig>;
|
|
1181
|
-
}
|
|
1182
|
-
/**
|
|
1183
|
-
* Global WASM backend instance (AssemblyScript path)
|
|
1184
|
-
*/
|
|
1185
|
-
declare const wasmBackend: WASMBackend;
|
|
1186
|
-
/**
|
|
1187
|
-
* Create a WASM backend (AssemblyScript path) with custom configuration
|
|
1188
|
-
*/
|
|
1189
|
-
declare function createWASMBackend(config?: WASMBackendConfig): WASMBackend;
|
|
1190
|
-
|
|
1191
|
-
/**
|
|
1192
|
-
* GPU Backend for Matrix Operations
|
|
1193
|
-
*
|
|
1194
|
-
* WebGPU-accelerated matrix operations for large matrices.
|
|
1195
|
-
*/
|
|
1196
|
-
|
|
1197
|
-
/**
|
|
1198
|
-
* GPU Backend status
|
|
1199
|
-
*/
|
|
1200
|
-
type GPUBackendStatus = 'uninitialized' | 'initializing' | 'ready' | 'error' | 'unsupported';
|
|
1201
|
-
/**
|
|
1202
|
-
* Options for GPU backend
|
|
1203
|
-
*/
|
|
1204
|
-
interface GPUBackendOptions extends GPUContextOptions {
|
|
1205
|
-
/** Use global GPU context instead of creating a new one */
|
|
1206
|
-
useGlobalContext?: boolean;
|
|
1207
|
-
/** Buffer pool options */
|
|
1208
|
-
bufferPoolOptions?: {
|
|
1209
|
-
maxCacheSize?: number;
|
|
1210
|
-
evictionTimeout?: number;
|
|
1211
|
-
};
|
|
1212
|
-
/** Threshold for using GPU (matrix size) */
|
|
1213
|
-
threshold?: number;
|
|
1214
|
-
}
|
|
1215
|
-
/**
|
|
1216
|
-
* GPU Backend for accelerated matrix operations
|
|
1217
|
-
*/
|
|
1218
|
-
declare class GPUBackend {
|
|
1219
|
-
private context;
|
|
1220
|
-
private bufferPool;
|
|
1221
|
-
private shaderManager;
|
|
1222
|
-
private _status;
|
|
1223
|
-
private _capabilities;
|
|
1224
|
-
private _lastError;
|
|
1225
|
-
private threshold;
|
|
1226
|
-
private workgroupSize;
|
|
1227
|
-
private useGlobalContext;
|
|
1228
|
-
constructor(options?: GPUBackendOptions);
|
|
1229
|
-
/**
|
|
1230
|
-
* Get the current status
|
|
1231
|
-
*/
|
|
1232
|
-
get status(): GPUBackendStatus;
|
|
1233
|
-
/**
|
|
1234
|
-
* Check if backend is ready
|
|
1235
|
-
*/
|
|
1236
|
-
get isReady(): boolean;
|
|
1237
|
-
/**
|
|
1238
|
-
* Get capabilities
|
|
1239
|
-
*/
|
|
1240
|
-
get capabilities(): GPUCapabilities | null;
|
|
1241
|
-
/**
|
|
1242
|
-
* Get last error
|
|
1243
|
-
*/
|
|
1244
|
-
get lastError(): Error | null;
|
|
1245
|
-
/**
|
|
1246
|
-
* Initialize the GPU backend
|
|
1247
|
-
*/
|
|
1248
|
-
initialize(options?: GPUBackendOptions): Promise<boolean>;
|
|
1249
|
-
/**
|
|
1250
|
-
* Check if GPU should be used for the given matrix size
|
|
1251
|
-
*/
|
|
1252
|
-
shouldUseGPU(rows: number, cols: number): boolean;
|
|
1253
|
-
/**
|
|
1254
|
-
* Calculate workgroup counts for a matrix
|
|
1255
|
-
*/
|
|
1256
|
-
calculateWorkgroups(rows: number, cols: number): [number, number, number];
|
|
1257
|
-
/**
|
|
1258
|
-
* Get the GPU context
|
|
1259
|
-
*/
|
|
1260
|
-
getContext(): GPUContext;
|
|
1261
|
-
/**
|
|
1262
|
-
* Get the buffer pool
|
|
1263
|
-
*/
|
|
1264
|
-
getBufferPool(): BufferPool;
|
|
1265
|
-
/**
|
|
1266
|
-
* Get the shader manager
|
|
1267
|
-
*/
|
|
1268
|
-
getShaderManager(): ShaderManager;
|
|
1269
|
-
/**
|
|
1270
|
-
* Add two matrices element-wise
|
|
1271
|
-
*/
|
|
1272
|
-
add(a: Float32Array, b: Float32Array, rows: number, cols: number): Promise<Float32Array>;
|
|
1273
|
-
/**
|
|
1274
|
-
* Multiply two matrices
|
|
1275
|
-
*/
|
|
1276
|
-
matmul(a: Float32Array, b: Float32Array, M: number, K: number, N: number): Promise<Float32Array>;
|
|
1277
|
-
/**
|
|
1278
|
-
* Transpose a matrix
|
|
1279
|
-
*/
|
|
1280
|
-
transpose(a: Float32Array, rows: number, cols: number): Promise<Float32Array>;
|
|
1281
|
-
/**
|
|
1282
|
-
* Scale a matrix by a scalar
|
|
1283
|
-
*/
|
|
1284
|
-
scale(a: Float32Array, scalar: number): Promise<Float32Array>;
|
|
1285
|
-
/**
|
|
1286
|
-
* Get backend statistics
|
|
1287
|
-
*/
|
|
1288
|
-
getStats(): {
|
|
1289
|
-
status: GPUBackendStatus;
|
|
1290
|
-
capabilities: GPUCapabilities | null;
|
|
1291
|
-
bufferPool: {
|
|
1292
|
-
totalBuffers: number;
|
|
1293
|
-
inUseBuffers: number;
|
|
1294
|
-
cachedBuffers: number;
|
|
1295
|
-
} | null;
|
|
1296
|
-
shaders: {
|
|
1297
|
-
cachedShaders: number;
|
|
1298
|
-
cachedPipelines: number;
|
|
1299
|
-
} | null;
|
|
1300
|
-
};
|
|
1301
|
-
/**
|
|
1302
|
-
* Destroy the backend
|
|
1303
|
-
*/
|
|
1304
|
-
destroy(): void;
|
|
1305
|
-
}
|
|
1306
|
-
/**
|
|
1307
|
-
* Get the global GPU backend
|
|
1308
|
-
*/
|
|
1309
|
-
declare function getGlobalGPUBackend(): GPUBackend;
|
|
1310
|
-
/**
|
|
1311
|
-
* Initialize the global GPU backend
|
|
1312
|
-
*/
|
|
1313
|
-
declare function initializeGlobalGPUBackend(options?: GPUBackendOptions): Promise<boolean>;
|
|
1314
|
-
/**
|
|
1315
|
-
* Destroy the global GPU backend
|
|
1316
|
-
*/
|
|
1317
|
-
declare function destroyGlobalGPUBackend(): void;
|
|
1318
|
-
|
|
1319
|
-
/**
|
|
1320
|
-
* GPU Matrix Backend Adapter
|
|
1321
|
-
*
|
|
1322
|
-
* Adapts GPUBackend to implement the MatrixBackend interface,
|
|
1323
|
-
* enabling seamless integration with the backend selection system.
|
|
1324
|
-
*
|
|
1325
|
-
* @packageDocumentation
|
|
1326
|
-
*/
|
|
1327
|
-
|
|
1328
|
-
/**
|
|
1329
|
-
* Configuration for GPU Matrix Backend
|
|
1330
|
-
*/
|
|
1331
|
-
interface GPUMatrixBackendConfig {
|
|
1332
|
-
/** Minimum elements to use GPU (default: 65536 = 256x256) */
|
|
1333
|
-
minElements?: number;
|
|
1334
|
-
/** Use global GPU backend instance */
|
|
1335
|
-
useGlobalBackend?: boolean;
|
|
1336
|
-
/** GPU backend options */
|
|
1337
|
-
gpuOptions?: GPUBackendOptions;
|
|
1338
|
-
/** Fall back to JS on GPU errors */
|
|
1339
|
-
fallbackOnError?: boolean;
|
|
1340
|
-
}
|
|
1341
|
-
/**
|
|
1342
|
-
* GPU Matrix Backend
|
|
1343
|
-
*
|
|
1344
|
-
* Implements MatrixBackend interface using WebGPU compute shaders.
|
|
1345
|
-
* Provides significant acceleration for large matrices.
|
|
1346
|
-
*
|
|
1347
|
-
* @example
|
|
1348
|
-
* ```typescript
|
|
1349
|
-
* const gpu = new GPUMatrixBackend();
|
|
1350
|
-
* await gpu.initialize();
|
|
1351
|
-
*
|
|
1352
|
-
* const result = gpu.multiply(matrixA, matrixB);
|
|
1353
|
-
* ```
|
|
1354
|
-
*/
|
|
1355
|
-
declare class GPUMatrixBackend implements MatrixBackend {
|
|
1356
|
-
readonly type: BackendType;
|
|
1357
|
-
private config;
|
|
1358
|
-
private backend;
|
|
1359
|
-
private capabilities;
|
|
1360
|
-
private initPromise;
|
|
1361
|
-
private _available;
|
|
1362
|
-
constructor(config?: GPUMatrixBackendConfig);
|
|
1363
|
-
/**
|
|
1364
|
-
* Check if GPU is available in the current environment
|
|
1365
|
-
*/
|
|
1366
|
-
isAvailable(): boolean;
|
|
1367
|
-
/**
|
|
1368
|
-
* Initialize the GPU backend
|
|
1369
|
-
*/
|
|
1370
|
-
initialize(): Promise<void>;
|
|
1371
|
-
private doInitialize;
|
|
1372
|
-
/**
|
|
1373
|
-
* Check if operation should use GPU
|
|
1374
|
-
*/
|
|
1375
|
-
private shouldUseGPU;
|
|
1376
|
-
/**
|
|
1377
|
-
* Execute GPU operation with fallback
|
|
1378
|
-
*/
|
|
1379
|
-
private executeWithFallback;
|
|
1380
|
-
/**
|
|
1381
|
-
* Get GPU capabilities
|
|
1382
|
-
*/
|
|
1383
|
-
getCapabilities(): GPUCapabilities | null;
|
|
1384
|
-
/**
|
|
1385
|
-
* Get backend statistics
|
|
1386
|
-
*/
|
|
1387
|
-
getStats(): ReturnType<GPUBackend['getStats']> | null;
|
|
1388
|
-
add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1389
|
-
/**
|
|
1390
|
-
* Async add operation using GPU
|
|
1391
|
-
*/
|
|
1392
|
-
addAsync(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
1393
|
-
subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1394
|
-
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1395
|
-
divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1396
|
-
scale(a: DenseMatrix, scalar: number): DenseMatrix;
|
|
1397
|
-
/**
|
|
1398
|
-
* Async scale operation using GPU
|
|
1399
|
-
*/
|
|
1400
|
-
scaleAsync(a: DenseMatrix, scalar: number): Promise<DenseMatrix>;
|
|
1401
|
-
abs(a: DenseMatrix): DenseMatrix;
|
|
1402
|
-
negate(a: DenseMatrix): DenseMatrix;
|
|
1403
|
-
multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1404
|
-
/**
|
|
1405
|
-
* Async matrix multiplication using GPU
|
|
1406
|
-
*/
|
|
1407
|
-
multiplyAsync(a: DenseMatrix, b: DenseMatrix): Promise<DenseMatrix>;
|
|
1408
|
-
transpose(a: DenseMatrix): DenseMatrix;
|
|
1409
|
-
/**
|
|
1410
|
-
* Async transpose using GPU
|
|
1411
|
-
*/
|
|
1412
|
-
transposeAsync(a: DenseMatrix): Promise<DenseMatrix>;
|
|
1413
|
-
sum(a: DenseMatrix): number;
|
|
1414
|
-
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
1415
|
-
norm(a: DenseMatrix): number;
|
|
1416
|
-
dot(a: DenseMatrix, b: DenseMatrix): number;
|
|
1417
|
-
/**
|
|
1418
|
-
* Update configuration
|
|
1419
|
-
*/
|
|
1420
|
-
updateConfig(config: Partial<GPUMatrixBackendConfig>): void;
|
|
1421
|
-
/**
|
|
1422
|
-
* Get current configuration
|
|
1423
|
-
*/
|
|
1424
|
-
getConfig(): Required<GPUMatrixBackendConfig>;
|
|
1425
|
-
/**
|
|
1426
|
-
* Destroy the backend
|
|
1427
|
-
*/
|
|
1428
|
-
destroy(): void;
|
|
1429
|
-
}
|
|
1430
|
-
/**
|
|
1431
|
-
* Global GPU matrix backend instance
|
|
1432
|
-
*/
|
|
1433
|
-
declare const gpuMatrixBackend: GPUMatrixBackend;
|
|
1434
|
-
/**
|
|
1435
|
-
* Create a GPU matrix backend with custom configuration
|
|
1436
|
-
*/
|
|
1437
|
-
declare function createGPUMatrixBackend(config?: GPUMatrixBackendConfig): GPUMatrixBackend;
|
|
1438
|
-
|
|
1439
|
-
/**
|
|
1440
|
-
* MathTS Matrix Configuration
|
|
1441
|
-
*
|
|
1442
|
-
* Centralized configuration for matrix operations, backend selection,
|
|
1443
|
-
* and performance tuning.
|
|
1444
|
-
*
|
|
1445
|
-
* @packageDocumentation
|
|
1446
|
-
*/
|
|
1447
|
-
|
|
1448
|
-
/**
|
|
1449
|
-
* Operation type hints for backend selection.
|
|
1450
|
-
*
|
|
1451
|
-
* Defined here rather than in `BackendManager.ts` so that `config.ts` (the
|
|
1452
|
-
* lower-level module) owns it — `BackendManager` already imports `config`, so
|
|
1453
|
-
* sourcing the type the other way round closed an import cycle.
|
|
1454
|
-
*/
|
|
1455
|
-
type OperationType = 'add' | 'subtract' | 'multiply' | 'multiplyElementwise' | 'transpose' | 'scale' | 'decomposition' | 'solve' | 'fft' | 'eig' | 'svd';
|
|
1456
|
-
|
|
1457
|
-
/**
|
|
1458
|
-
* Backend Manager
|
|
1459
|
-
*
|
|
1460
|
-
* Centralized management for matrix operation backends with automatic
|
|
1461
|
-
* selection based on matrix size, operation type, and availability.
|
|
1462
|
-
* Includes adaptive threshold tuning based on runtime profiling.
|
|
1463
|
-
*
|
|
1464
|
-
* @packageDocumentation
|
|
1465
|
-
*/
|
|
1466
|
-
|
|
1467
|
-
/**
|
|
1468
|
-
* Extended backend hints with operation-specific thresholds
|
|
1469
|
-
*/
|
|
1470
|
-
interface ExtendedBackendHints extends BackendHints {
|
|
1471
|
-
/** Specific thresholds by operation type */
|
|
1472
|
-
operationThresholds?: Partial<Record<OperationType, {
|
|
1473
|
-
wasm?: number;
|
|
1474
|
-
gpu?: number;
|
|
1475
|
-
}>>;
|
|
1476
|
-
/** Enable automatic SIMD detection for WASM */
|
|
1477
|
-
autoSIMD?: boolean;
|
|
1478
|
-
/** Fallback to JS on backend failure */
|
|
1479
|
-
fallbackOnError?: boolean;
|
|
1480
|
-
}
|
|
1481
|
-
/**
|
|
1482
|
-
* Default extended hints
|
|
1483
|
-
*/
|
|
1484
|
-
declare const DEFAULT_EXTENDED_HINTS: Required<ExtendedBackendHints>;
|
|
1485
|
-
/**
|
|
1486
|
-
* Centralized Backend Manager
|
|
1487
|
-
*
|
|
1488
|
-
* Provides a unified interface for executing matrix operations with
|
|
1489
|
-
* automatic backend selection based on matrix size and operation type.
|
|
1490
|
-
* Features adaptive threshold tuning based on runtime profiling.
|
|
1491
|
-
*/
|
|
1492
|
-
declare class BackendManager {
|
|
1493
|
-
private hints;
|
|
1494
|
-
private initialized;
|
|
1495
|
-
private initializationPromise;
|
|
1496
|
-
private adaptiveState;
|
|
1497
|
-
private configUnsubscribe;
|
|
1498
|
-
constructor(hints?: ExtendedBackendHints);
|
|
1499
|
-
/**
|
|
1500
|
-
* Sync manager state with global config
|
|
1501
|
-
*/
|
|
1502
|
-
private syncWithConfig;
|
|
1503
|
-
/**
|
|
1504
|
-
* Initialize all available backends
|
|
1505
|
-
*/
|
|
1506
|
-
initialize(): Promise<void>;
|
|
1507
|
-
private doInitialize;
|
|
1508
|
-
/**
|
|
1509
|
-
* Update backend hints
|
|
1510
|
-
*/
|
|
1511
|
-
setHints(hints: ExtendedBackendHints): void;
|
|
1512
|
-
/**
|
|
1513
|
-
* Get current hints
|
|
1514
|
-
*/
|
|
1515
|
-
getHints(): Required<ExtendedBackendHints>;
|
|
1516
|
-
/**
|
|
1517
|
-
* Get the best backend for a given operation and matrix size.
|
|
1518
|
-
*
|
|
1519
|
-
* Selection priority:
|
|
1520
|
-
* 1. Preferred backend (if explicitly set)
|
|
1521
|
-
* 2. Elements > gpuThreshold -> GPU (if available)
|
|
1522
|
-
* 3. Elements > wasmThreshold -> AS WASM (if loaded)
|
|
1523
|
-
* 4. JS fallback
|
|
1524
|
-
*/
|
|
1525
|
-
selectBackend(elementCount: number, operation?: OperationType): MatrixBackend;
|
|
1526
|
-
/**
|
|
1527
|
-
* Execute an operation with automatic backend selection
|
|
1528
|
-
*/
|
|
1529
|
-
private executeWithFallback;
|
|
1530
|
-
/**
|
|
1531
|
-
* Matrix addition with auto backend selection
|
|
1532
|
-
*/
|
|
1533
|
-
add(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1534
|
-
/**
|
|
1535
|
-
* Matrix subtraction with auto backend selection
|
|
1536
|
-
*/
|
|
1537
|
-
subtract(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1538
|
-
/**
|
|
1539
|
-
* Element-wise multiplication with auto backend selection
|
|
1540
|
-
*/
|
|
1541
|
-
multiplyElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1542
|
-
/**
|
|
1543
|
-
* Element-wise division with auto backend selection
|
|
1544
|
-
*/
|
|
1545
|
-
divideElementwise(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1546
|
-
/**
|
|
1547
|
-
* Scalar multiplication with auto backend selection
|
|
1548
|
-
*/
|
|
1549
|
-
scale(a: DenseMatrix, scalar: number): DenseMatrix;
|
|
1550
|
-
/**
|
|
1551
|
-
* Element-wise absolute value with auto backend selection
|
|
1552
|
-
*/
|
|
1553
|
-
abs(a: DenseMatrix): DenseMatrix;
|
|
1554
|
-
/**
|
|
1555
|
-
* Element-wise negation with auto backend selection
|
|
1556
|
-
*/
|
|
1557
|
-
negate(a: DenseMatrix): DenseMatrix;
|
|
1558
|
-
/**
|
|
1559
|
-
* Matrix multiplication with auto backend selection
|
|
1560
|
-
*/
|
|
1561
|
-
multiply(a: DenseMatrix, b: DenseMatrix): DenseMatrix;
|
|
1562
|
-
/**
|
|
1563
|
-
* Matrix transpose with auto backend selection
|
|
1564
|
-
*/
|
|
1565
|
-
transpose(a: DenseMatrix): DenseMatrix;
|
|
1566
|
-
/**
|
|
1567
|
-
* Sum of all elements with auto backend selection
|
|
1568
|
-
*/
|
|
1569
|
-
sum(a: DenseMatrix): Promise<number>;
|
|
1570
|
-
/**
|
|
1571
|
-
* Sum along axis with auto backend selection
|
|
1572
|
-
*/
|
|
1573
|
-
sumAxis(a: DenseMatrix, axis: 0 | 1): DenseMatrix;
|
|
1574
|
-
/**
|
|
1575
|
-
* Frobenius norm with auto backend selection
|
|
1576
|
-
*/
|
|
1577
|
-
norm(a: DenseMatrix): number;
|
|
1578
|
-
/**
|
|
1579
|
-
* Dot product with auto backend selection
|
|
1580
|
-
*/
|
|
1581
|
-
dot(a: DenseMatrix, b: DenseMatrix): Promise<number>;
|
|
1582
|
-
/**
|
|
1583
|
-
* Get list of available backends
|
|
1584
|
-
*/
|
|
1585
|
-
getAvailableBackends(): BackendType[];
|
|
1586
|
-
/**
|
|
1587
|
-
* Check if a specific backend is available
|
|
1588
|
-
*/
|
|
1589
|
-
hasBackend(type: BackendType): boolean;
|
|
1590
|
-
/**
|
|
1591
|
-
* Get current active backend for a given operation size
|
|
1592
|
-
*/
|
|
1593
|
-
getActiveBackend(elementCount: number, operation?: OperationType): BackendType;
|
|
1594
|
-
/**
|
|
1595
|
-
* Force a specific backend for all operations
|
|
1596
|
-
*/
|
|
1597
|
-
forceBackend(type: BackendType | null): void;
|
|
1598
|
-
/**
|
|
1599
|
-
* Record a performance sample for adaptive tuning
|
|
1600
|
-
*/
|
|
1601
|
-
recordSample(operation: OperationType, elementCount: number, backend: BackendType, durationMs: number): void;
|
|
1602
|
-
/**
|
|
1603
|
-
* Adjust thresholds based on collected samples
|
|
1604
|
-
*/
|
|
1605
|
-
private maybeAdjustThresholds;
|
|
1606
|
-
/**
|
|
1607
|
-
* Get current adaptive thresholds
|
|
1608
|
-
*/
|
|
1609
|
-
getAdaptiveThresholds(): Map<OperationType, {
|
|
1610
|
-
wasm: number;
|
|
1611
|
-
gpu: number;
|
|
1612
|
-
}>;
|
|
1613
|
-
/**
|
|
1614
|
-
* Reset adaptive tuning state
|
|
1615
|
-
*/
|
|
1616
|
-
resetAdaptiveState(): void;
|
|
1617
|
-
/**
|
|
1618
|
-
* Get performance statistics
|
|
1619
|
-
*/
|
|
1620
|
-
getPerformanceStats(): {
|
|
1621
|
-
sampleCount: number;
|
|
1622
|
-
operationStats: Map<OperationType, {
|
|
1623
|
-
avgDuration: number;
|
|
1624
|
-
samples: number;
|
|
1625
|
-
backendUsage: Record<BackendType, number>;
|
|
1626
|
-
}>;
|
|
1627
|
-
};
|
|
1628
|
-
/**
|
|
1629
|
-
* Cleanup resources
|
|
1630
|
-
*/
|
|
1631
|
-
destroy(): void;
|
|
1632
|
-
}
|
|
1633
|
-
/**
|
|
1634
|
-
* Default backend manager instance
|
|
1635
|
-
*/
|
|
1636
|
-
declare const backendManager: BackendManager;
|
|
1637
|
-
/**
|
|
1638
|
-
* Create a new backend manager with custom hints
|
|
1639
|
-
*/
|
|
1640
|
-
declare function createBackendManager(hints?: ExtendedBackendHints): BackendManager;
|
|
1641
|
-
|
|
1642
|
-
/**
|
|
1643
|
-
* Matrix-domain WGSL kernels.
|
|
1644
|
-
*
|
|
1645
|
-
* These live in matrix — the @danielsimonjr/mathts-gpu foundation ships no
|
|
1646
|
-
* domain kernels. GPUBackend registers them onto a ShaderManager at init.
|
|
1647
|
-
*/
|
|
1648
|
-
|
|
1649
|
-
declare const BUILTIN_SHADERS: {
|
|
1650
|
-
/** Matrix addition shader */
|
|
1651
|
-
readonly matrixAdd: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>; // rows, cols, _, _\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n let idx = row * cols + col;\n result[idx] = a[idx] + b[idx];\n }\n ";
|
|
1652
|
-
/** Matrix subtraction shader */
|
|
1653
|
-
readonly matrixSub: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>;\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n let idx = row * cols + col;\n result[idx] = a[idx] - b[idx];\n }\n ";
|
|
1654
|
-
/** Element-wise multiplication shader */
|
|
1655
|
-
readonly matrixMul: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>;\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n let idx = row * cols + col;\n result[idx] = a[idx] * b[idx];\n }\n ";
|
|
1656
|
-
/** Scalar multiplication shader */
|
|
1657
|
-
readonly scalarMul: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read_write> result: array<f32>;\n @group(0) @binding(2) var<uniform> params: vec4<f32>; // scalar, length, _, _\n\n @compute @workgroup_size(256)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let scalar = params.x;\n let length = u32(params.y);\n let idx = gid.x;\n\n if (idx >= length) { return; }\n\n result[idx] = a[idx] * scalar;\n }\n ";
|
|
1658
|
-
/** Matrix multiplication (naive) shader */
|
|
1659
|
-
readonly matmul: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read> b: array<f32>;\n @group(0) @binding(2) var<storage, read_write> result: array<f32>;\n @group(0) @binding(3) var<uniform> params: vec4<u32>; // M, N, K, _\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let M = params.x;\n let N = params.y;\n let K = params.z;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= M || col >= N) { return; }\n\n var sum: f32 = 0.0;\n for (var k: u32 = 0u; k < K; k = k + 1u) {\n sum = sum + a[row * K + k] * b[k * N + col];\n }\n\n result[row * N + col] = sum;\n }\n ";
|
|
1660
|
-
/** Matrix transpose shader */
|
|
1661
|
-
readonly transpose: "\n @group(0) @binding(0) var<storage, read> a: array<f32>;\n @group(0) @binding(1) var<storage, read_write> result: array<f32>;\n @group(0) @binding(2) var<uniform> params: vec4<u32>; // rows, cols, _, _\n\n @compute @workgroup_size(16, 16)\n fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n let rows = params.x;\n let cols = params.y;\n let row = gid.y;\n let col = gid.x;\n\n if (row >= rows || col >= cols) { return; }\n\n result[col * rows + row] = a[row * cols + col];\n }\n ";
|
|
1662
|
-
/** Sum reduction shader (first pass) */
|
|
1663
|
-
readonly sumReduce: "\n @group(0) @binding(0) var<storage, read> input: array<f32>;\n @group(0) @binding(1) var<storage, read_write> output: array<f32>;\n @group(0) @binding(2) var<uniform> params: vec4<u32>; // inputLength, outputLength, _, _\n\n // NOTE: 'shared' is a RESERVED KEYWORD in WGSL — naming this workgroup\n // array 'shared' made this shader fail to compile, which (because\n // GPUBackend.initialize() precompiles every registered shader) poisoned\n // backend init and silently forced ALL GPU ops onto the CPU fallback.\n var<workgroup> sdata: array<f32, 256>;\n\n @compute @workgroup_size(256)\n fn main(\n @builtin(local_invocation_id) lid: vec3<u32>,\n @builtin(workgroup_id) wid: vec3<u32>\n ) {\n let inputLength = params.x;\n let idx = wid.x * 512u + lid.x;\n\n // Load two elements and sum\n var sum: f32 = 0.0;\n if (idx < inputLength) {\n sum = input[idx];\n }\n if (idx + 256u < inputLength) {\n sum = sum + input[idx + 256u];\n }\n sdata[lid.x] = sum;\n\n workgroupBarrier();\n\n // Reduce within workgroup\n for (var s: u32 = 128u; s > 0u; s = s >> 1u) {\n if (lid.x < s) {\n sdata[lid.x] = sdata[lid.x] + sdata[lid.x + s];\n }\n workgroupBarrier();\n }\n\n // Write result\n if (lid.x == 0u) {\n output[wid.x] = sdata[0];\n }\n }\n ";
|
|
1664
|
-
};
|
|
1665
|
-
|
|
1666
|
-
/**
|
|
1667
|
-
* GPU Batch Executor
|
|
1668
|
-
*
|
|
1669
|
-
* Manages batched GPU command submission for reduced overhead.
|
|
1670
|
-
* Queues multiple operations and executes them together for efficiency.
|
|
1671
|
-
*
|
|
1672
|
-
* @packageDocumentation
|
|
1673
|
-
*/
|
|
1674
|
-
|
|
1675
|
-
/**
|
|
1676
|
-
* Result of batch execution
|
|
1677
|
-
*/
|
|
1678
|
-
interface BatchResult {
|
|
1679
|
-
/** Success status */
|
|
1680
|
-
success: boolean;
|
|
1681
|
-
/** Number of operations executed */
|
|
1682
|
-
operationCount: number;
|
|
1683
|
-
/** Execution time in milliseconds */
|
|
1684
|
-
duration: number;
|
|
1685
|
-
/** Error message if failed */
|
|
1686
|
-
error?: string;
|
|
1687
|
-
}
|
|
1688
|
-
/**
|
|
1689
|
-
* Options for batch execution
|
|
1690
|
-
*/
|
|
1691
|
-
interface BatchOptions {
|
|
1692
|
-
/** Maximum operations per batch before auto-flush */
|
|
1693
|
-
maxBatchSize?: number;
|
|
1694
|
-
/** Auto-flush when batch is full */
|
|
1695
|
-
autoFlush?: boolean;
|
|
1696
|
-
/** Wait for GPU completion before returning */
|
|
1697
|
-
waitForCompletion?: boolean;
|
|
1698
|
-
}
|
|
1699
|
-
/**
|
|
1700
|
-
* GPU Batch Executor
|
|
1701
|
-
*
|
|
1702
|
-
* Accumulates GPU operations and executes them in batches to reduce
|
|
1703
|
-
* command submission overhead. Uses command buffers efficiently.
|
|
1704
|
-
*
|
|
1705
|
-
* @example
|
|
1706
|
-
* ```typescript
|
|
1707
|
-
* const executor = new BatchExecutor(context, shaders, bufferPool);
|
|
1708
|
-
*
|
|
1709
|
-
* executor.add(a, b, output, { rows: 100, cols: 100 });
|
|
1710
|
-
* executor.matmul(a, b, output, { rows: 100, cols: 100, k: 100 });
|
|
1711
|
-
*
|
|
1712
|
-
* await executor.flush();
|
|
1713
|
-
* ```
|
|
1714
|
-
*/
|
|
1715
|
-
declare class BatchExecutor {
|
|
1716
|
-
private context;
|
|
1717
|
-
private shaders;
|
|
1718
|
-
private bufferPool;
|
|
1719
|
-
private operations;
|
|
1720
|
-
private options;
|
|
1721
|
-
/**
|
|
1722
|
-
* Create a new batch executor
|
|
1723
|
-
*/
|
|
1724
|
-
constructor(context: GPUContext, shaders: ShaderManager, bufferPool: BufferPool, options?: BatchOptions);
|
|
1725
|
-
/**
|
|
1726
|
-
* Get current batch size
|
|
1727
|
-
*/
|
|
1728
|
-
get size(): number;
|
|
1729
|
-
/**
|
|
1730
|
-
* Check if batch is empty
|
|
1731
|
-
*/
|
|
1732
|
-
get isEmpty(): boolean;
|
|
1733
|
-
/**
|
|
1734
|
-
* Check if batch is full
|
|
1735
|
-
*/
|
|
1736
|
-
get isFull(): boolean;
|
|
1737
|
-
/**
|
|
1738
|
-
* Queue an add operation
|
|
1739
|
-
*/
|
|
1740
|
-
add(inputA: GPUBuffer, inputB: GPUBuffer, output: GPUBuffer, dimensions: {
|
|
1741
|
-
rows: number;
|
|
1742
|
-
cols: number;
|
|
1743
|
-
}): void;
|
|
1744
|
-
/**
|
|
1745
|
-
* Queue a subtract operation
|
|
1746
|
-
*/
|
|
1747
|
-
subtract(inputA: GPUBuffer, inputB: GPUBuffer, output: GPUBuffer, dimensions: {
|
|
1748
|
-
rows: number;
|
|
1749
|
-
cols: number;
|
|
1750
|
-
}): void;
|
|
1751
|
-
/**
|
|
1752
|
-
* Queue an element-wise multiply operation
|
|
1753
|
-
*/
|
|
1754
|
-
multiply(inputA: GPUBuffer, inputB: GPUBuffer, output: GPUBuffer, dimensions: {
|
|
1755
|
-
rows: number;
|
|
1756
|
-
cols: number;
|
|
1757
|
-
}): void;
|
|
1758
|
-
/**
|
|
1759
|
-
* Queue a scale operation
|
|
1760
|
-
*/
|
|
1761
|
-
scale(input: GPUBuffer, output: GPUBuffer, scalar: number, dimensions: {
|
|
1762
|
-
rows: number;
|
|
1763
|
-
cols: number;
|
|
1764
|
-
}): void;
|
|
1765
|
-
/**
|
|
1766
|
-
* Queue a matrix multiplication operation
|
|
1767
|
-
*/
|
|
1768
|
-
matmul(inputA: GPUBuffer, inputB: GPUBuffer, output: GPUBuffer, dimensions: {
|
|
1769
|
-
rows: number;
|
|
1770
|
-
cols: number;
|
|
1771
|
-
k: number;
|
|
1772
|
-
}): void;
|
|
1773
|
-
/**
|
|
1774
|
-
* Queue a transpose operation
|
|
1775
|
-
*/
|
|
1776
|
-
transpose(input: GPUBuffer, output: GPUBuffer, dimensions: {
|
|
1777
|
-
rows: number;
|
|
1778
|
-
cols: number;
|
|
1779
|
-
}): void;
|
|
1780
|
-
/**
|
|
1781
|
-
* Queue a sum reduction operation
|
|
1782
|
-
*/
|
|
1783
|
-
reduceSum(input: GPUBuffer, output: GPUBuffer, dimensions: {
|
|
1784
|
-
rows: number;
|
|
1785
|
-
cols: number;
|
|
1786
|
-
}): void;
|
|
1787
|
-
/**
|
|
1788
|
-
* Queue operation (internal)
|
|
1789
|
-
*/
|
|
1790
|
-
private queueOperation;
|
|
1791
|
-
/**
|
|
1792
|
-
* Flush all queued operations (async)
|
|
1793
|
-
*/
|
|
1794
|
-
flush(): Promise<BatchResult>;
|
|
1795
|
-
/**
|
|
1796
|
-
* Flush synchronously (fire and forget)
|
|
1797
|
-
*/
|
|
1798
|
-
flushSync(): void;
|
|
1799
|
-
/**
|
|
1800
|
-
* Encode a single operation into the command encoder
|
|
1801
|
-
*/
|
|
1802
|
-
private encodeOperation;
|
|
1803
|
-
/**
|
|
1804
|
-
* Get pipeline name for operation type
|
|
1805
|
-
*/
|
|
1806
|
-
private getPipelineName;
|
|
1807
|
-
/**
|
|
1808
|
-
* Create params buffer for operation
|
|
1809
|
-
*/
|
|
1810
|
-
private createParamsBuffer;
|
|
1811
|
-
/**
|
|
1812
|
-
* Calculate workgroup dispatch counts
|
|
1813
|
-
*/
|
|
1814
|
-
private calculateWorkgroups;
|
|
1815
|
-
/**
|
|
1816
|
-
* Clear all queued operations without executing
|
|
1817
|
-
*/
|
|
1818
|
-
clear(): void;
|
|
1819
|
-
/**
|
|
1820
|
-
* Get statistics about the batch executor
|
|
1821
|
-
*/
|
|
1822
|
-
getStats(): {
|
|
1823
|
-
queuedOperations: number;
|
|
1824
|
-
maxBatchSize: number;
|
|
1825
|
-
autoFlush: boolean;
|
|
1826
|
-
};
|
|
1827
|
-
}
|
|
1828
|
-
|
|
1829
|
-
/**
|
|
1830
|
-
* GPU-CPU Synchronization Strategy
|
|
1831
|
-
*
|
|
1832
|
-
* Implements efficient patterns for synchronizing data between
|
|
1833
|
-
* CPU and GPU memory. Minimizes transfer latency and enables
|
|
1834
|
-
* overlapping of CPU and GPU work.
|
|
1835
|
-
*
|
|
1836
|
-
* @packageDocumentation
|
|
1837
|
-
*/
|
|
1838
|
-
|
|
1839
|
-
/**
|
|
1840
|
-
* Synchronization strategy type
|
|
1841
|
-
*/
|
|
1842
|
-
type SyncStrategy = 'immediate' | 'lazy' | 'double-buffer' | 'streaming';
|
|
1843
|
-
/**
|
|
1844
|
-
* Transfer direction
|
|
1845
|
-
*/
|
|
1846
|
-
type TransferDirection = 'cpu-to-gpu' | 'gpu-to-cpu';
|
|
1847
|
-
/**
|
|
1848
|
-
* Transfer result
|
|
1849
|
-
*/
|
|
1850
|
-
interface TransferResult {
|
|
1851
|
-
/** Request ID */
|
|
1852
|
-
id: number;
|
|
1853
|
-
/** Success status */
|
|
1854
|
-
success: boolean;
|
|
1855
|
-
/** Transfer time in milliseconds */
|
|
1856
|
-
duration: number;
|
|
1857
|
-
/** Bytes transferred */
|
|
1858
|
-
bytesTransferred: number;
|
|
1859
|
-
/** Error message if failed */
|
|
1860
|
-
error?: string;
|
|
1861
|
-
}
|
|
1862
|
-
/**
|
|
1863
|
-
* Sync configuration
|
|
1864
|
-
*/
|
|
1865
|
-
interface SyncConfig {
|
|
1866
|
-
/** Default strategy */
|
|
1867
|
-
strategy: SyncStrategy;
|
|
1868
|
-
/** Chunk size for streaming (bytes) */
|
|
1869
|
-
chunkSize?: number;
|
|
1870
|
-
/** Maximum pending transfers */
|
|
1871
|
-
maxPendingTransfers?: number;
|
|
1872
|
-
/** Auto-coalesce nearby transfers */
|
|
1873
|
-
coalesceTransfers?: boolean;
|
|
1874
|
-
}
|
|
1875
|
-
/**
|
|
1876
|
-
* GPU-CPU Synchronization Manager
|
|
1877
|
-
*
|
|
1878
|
-
* Manages data transfer between CPU and GPU with various strategies
|
|
1879
|
-
* optimized for different use cases.
|
|
1880
|
-
*
|
|
1881
|
-
* @example
|
|
1882
|
-
* ```typescript
|
|
1883
|
-
* const sync = new SyncManager(context, bufferPool, {
|
|
1884
|
-
* strategy: 'double-buffer',
|
|
1885
|
-
* });
|
|
1886
|
-
*
|
|
1887
|
-
* // Upload data to GPU
|
|
1888
|
-
* await sync.upload(cpuData, gpuBuffer);
|
|
1889
|
-
*
|
|
1890
|
-
* // Download results from GPU
|
|
1891
|
-
* const result = await sync.download(gpuBuffer);
|
|
1892
|
-
* ```
|
|
1893
|
-
*/
|
|
1894
|
-
declare class SyncManager {
|
|
1895
|
-
private context;
|
|
1896
|
-
private config;
|
|
1897
|
-
private pendingTransfers;
|
|
1898
|
-
private nextRequestId;
|
|
1899
|
-
private stagingBuffers;
|
|
1900
|
-
private totalUploads;
|
|
1901
|
-
private totalDownloads;
|
|
1902
|
-
private totalBytesUploaded;
|
|
1903
|
-
private totalBytesDownloaded;
|
|
1904
|
-
constructor(context: GPUContext, _bufferPool: BufferPool, // Reserved for future pool integration
|
|
1905
|
-
config?: Partial<SyncConfig>);
|
|
1906
|
-
/**
|
|
1907
|
-
* Upload data from CPU to GPU
|
|
1908
|
-
*/
|
|
1909
|
-
upload(cpuData: Float32Array | Float64Array | Uint32Array | Int32Array, gpuBuffer: GPUBuffer, options?: {
|
|
1910
|
-
offset?: number;
|
|
1911
|
-
size?: number;
|
|
1912
|
-
}): Promise<TransferResult>;
|
|
1913
|
-
/**
|
|
1914
|
-
* Download data from GPU to CPU
|
|
1915
|
-
*/
|
|
1916
|
-
download(gpuBuffer: GPUBuffer, options?: {
|
|
1917
|
-
offset?: number;
|
|
1918
|
-
size?: number;
|
|
1919
|
-
}): Promise<Float32Array>;
|
|
1920
|
-
/**
|
|
1921
|
-
* Download data using double-buffering for overlap
|
|
1922
|
-
*/
|
|
1923
|
-
downloadDoubleBuffered(gpuBuffer: GPUBuffer, size: number): Promise<Float32Array>;
|
|
1924
|
-
/**
|
|
1925
|
-
* Stream large data in chunks
|
|
1926
|
-
*/
|
|
1927
|
-
uploadStreaming(cpuData: Float32Array, gpuBuffer: GPUBuffer, onProgress?: (progress: number) => void): Promise<TransferResult>;
|
|
1928
|
-
/**
|
|
1929
|
-
* Download large data in chunks
|
|
1930
|
-
*/
|
|
1931
|
-
downloadStreaming(gpuBuffer: GPUBuffer, totalSize: number, onProgress?: (progress: number) => void): Promise<Float32Array>;
|
|
1932
|
-
/**
|
|
1933
|
-
* Batch multiple transfers
|
|
1934
|
-
*/
|
|
1935
|
-
batchTransfer(requests: Array<{
|
|
1936
|
-
data: Float32Array;
|
|
1937
|
-
buffer: GPUBuffer;
|
|
1938
|
-
direction: TransferDirection;
|
|
1939
|
-
}>): Promise<TransferResult[]>;
|
|
1940
|
-
/**
|
|
1941
|
-
* Create or reuse a staging buffer
|
|
1942
|
-
*/
|
|
1943
|
-
private getOrCreateStagingBuffer;
|
|
1944
|
-
/**
|
|
1945
|
-
* Round up to next power of 2
|
|
1946
|
-
*/
|
|
1947
|
-
private roundToPowerOf2;
|
|
1948
|
-
/**
|
|
1949
|
-
* Wait for all pending transfers to complete
|
|
1950
|
-
*/
|
|
1951
|
-
flush(): Promise<void>;
|
|
1952
|
-
/**
|
|
1953
|
-
* Get synchronization statistics
|
|
1954
|
-
*/
|
|
1955
|
-
getStats(): {
|
|
1956
|
-
totalUploads: number;
|
|
1957
|
-
totalDownloads: number;
|
|
1958
|
-
totalBytesUploaded: number;
|
|
1959
|
-
totalBytesDownloaded: number;
|
|
1960
|
-
pendingTransfers: number;
|
|
1961
|
-
stagingBuffersCount: number;
|
|
1962
|
-
strategy: SyncStrategy;
|
|
1963
|
-
};
|
|
1964
|
-
/**
|
|
1965
|
-
* Destroy sync manager and release resources
|
|
1966
|
-
*/
|
|
1967
|
-
destroy(): void;
|
|
1968
|
-
}
|
|
1969
|
-
/**
|
|
1970
|
-
* Create a sync manager with the recommended configuration
|
|
1971
|
-
*/
|
|
1972
|
-
declare function createSyncManager(context: GPUContext, bufferPool: BufferPool, strategy?: SyncStrategy): SyncManager;
|
|
1973
|
-
|
|
1974
|
-
/**
|
|
1975
|
-
* Eigenvalue and Eigenvector Decomposition
|
|
1976
|
-
*
|
|
1977
|
-
* Implements eigenvalue computation using QR algorithm and
|
|
1978
|
-
* eigenvector extraction using inverse iteration.
|
|
1979
|
-
*/
|
|
1980
|
-
/**
|
|
1981
|
-
* Result of eigenvalue decomposition
|
|
1982
|
-
*/
|
|
1983
|
-
interface EigResult {
|
|
1984
|
-
/** Eigenvalues (may be complex for non-symmetric matrices) */
|
|
1985
|
-
values: Array<{
|
|
1986
|
-
re: number;
|
|
1987
|
-
im: number;
|
|
1988
|
-
}>;
|
|
1989
|
-
/**
|
|
1990
|
-
* Eigenvectors as columns (each column is an eigenvector). For a real
|
|
1991
|
-
* eigenvalue this is the full (real) eigenvector, unit-normalised. For a
|
|
1992
|
-
* complex-conjugate eigenvalue pair this holds the REAL PART of the
|
|
1993
|
-
* corresponding complex eigenvector — see {@link vectorsIm} for the
|
|
1994
|
-
* imaginary part.
|
|
1995
|
-
*/
|
|
1996
|
-
vectors: number[][];
|
|
1997
|
-
/**
|
|
1998
|
-
* Imaginary parts of the eigenvector columns (same shape as {@link vectors}).
|
|
1999
|
-
* All-zero for real eigenvalues. For a complex-conjugate eigenvalue pair at
|
|
2000
|
-
* indices `j`/`j+1`, the full complex eigenvectors are
|
|
2001
|
-
* `vectors[j] + i*vectorsIm[j]` (for `values[j]`) and
|
|
2002
|
-
* `vectors[j+1] - i*vectorsIm[j] === vectors[j+1] + i*vectorsIm[j+1]` (for
|
|
2003
|
-
* the conjugate `values[j+1]`), each unit-normalised by the complex 2-norm
|
|
2004
|
-
* `sqrt(sum(re_i^2 + im_i^2))`.
|
|
2005
|
-
*/
|
|
2006
|
-
vectorsIm: number[][];
|
|
2007
|
-
/** Whether the matrix was symmetric */
|
|
2008
|
-
isSymmetric: boolean;
|
|
2009
|
-
}
|
|
2010
|
-
/**
|
|
2011
|
-
* Options for eigenvalue computation
|
|
2012
|
-
*/
|
|
2013
|
-
interface EigOptions {
|
|
2014
|
-
/** Maximum number of QR iterations */
|
|
2015
|
-
maxIterations?: number;
|
|
2016
|
-
/** Convergence tolerance */
|
|
2017
|
-
tolerance?: number;
|
|
2018
|
-
/** Whether to compute eigenvectors */
|
|
2019
|
-
computeVectors?: boolean;
|
|
2020
|
-
}
|
|
2021
|
-
/**
|
|
2022
|
-
* Compute eigenvalues and eigenvectors of a square matrix
|
|
2023
|
-
* Uses QR algorithm with implicit shifts
|
|
2024
|
-
*
|
|
2025
|
-
* @param matrix - Square matrix (n x n)
|
|
2026
|
-
* @param options - Computation options
|
|
2027
|
-
* @returns Eigenvalues and eigenvectors
|
|
2028
|
-
*/
|
|
2029
|
-
declare function eig(matrix: number[][] | Float64Array, options?: EigOptions): EigResult;
|
|
2030
|
-
/**
|
|
2031
|
-
* Compute only eigenvalues (faster, no eigenvector computation)
|
|
2032
|
-
*/
|
|
2033
|
-
declare function eigvals(matrix: number[][] | Float64Array, options?: Omit<EigOptions, 'computeVectors'>): Array<{
|
|
2034
|
-
re: number;
|
|
2035
|
-
im: number;
|
|
2036
|
-
}>;
|
|
2037
|
-
/**
|
|
2038
|
-
* Power iteration for dominant eigenvalue
|
|
2039
|
-
* Faster than full eigendecomposition when only largest eigenvalue needed
|
|
2040
|
-
*/
|
|
2041
|
-
declare function powerIteration(matrix: number[][], options?: {
|
|
2042
|
-
maxIterations?: number;
|
|
2043
|
-
tolerance?: number;
|
|
2044
|
-
}): {
|
|
2045
|
-
value: number;
|
|
2046
|
-
vector: number[];
|
|
2047
|
-
};
|
|
2048
|
-
|
|
2049
|
-
/**
|
|
2050
|
-
* Singular Value Decomposition (SVD)
|
|
2051
|
-
*
|
|
2052
|
-
* Implements SVD using Golub-Reinsch bidiagonalization and implicit QR.
|
|
2053
|
-
* A = U * S * V^T where:
|
|
2054
|
-
* - U: m x m orthogonal matrix (left singular vectors)
|
|
2055
|
-
* - S: m x n diagonal matrix (singular values)
|
|
2056
|
-
* - V: n x n orthogonal matrix (right singular vectors)
|
|
2057
|
-
*/
|
|
2058
|
-
/**
|
|
2059
|
-
* Result of SVD decomposition
|
|
2060
|
-
*/
|
|
2061
|
-
interface SVDResult {
|
|
2062
|
-
/** Left singular vectors (m x m) */
|
|
2063
|
-
U: number[][];
|
|
2064
|
-
/** Singular values (min(m,n) values) */
|
|
2065
|
-
S: number[];
|
|
2066
|
-
/** Right singular vectors (n x n) */
|
|
2067
|
-
V: number[][];
|
|
2068
|
-
/** Rank estimate */
|
|
2069
|
-
rank: number;
|
|
2070
|
-
}
|
|
2071
|
-
/**
|
|
2072
|
-
* Options for SVD computation
|
|
2073
|
-
*/
|
|
2074
|
-
interface SVDOptions {
|
|
2075
|
-
/** Maximum number of iterations */
|
|
2076
|
-
maxIterations?: number;
|
|
2077
|
-
/** Convergence tolerance */
|
|
2078
|
-
tolerance?: number;
|
|
2079
|
-
/**
|
|
2080
|
-
* Complete the thin factor to a square orthonormal basis (U → m×m for tall
|
|
2081
|
-
* inputs, V → n×n for wide ones), numpy `full_matrices=True` style, so the
|
|
2082
|
-
* rectangular-Σ reconstruction `A = U·Σ·Vᵀ` holds. Default `false` = thin
|
|
2083
|
-
* factors (the library's long-standing behavior; the option was previously
|
|
2084
|
-
* accepted but ignored — B-4).
|
|
2085
|
-
*/
|
|
2086
|
-
fullMatrices?: boolean;
|
|
2087
|
-
/** Threshold for rank determination */
|
|
2088
|
-
rankTolerance?: number;
|
|
2089
|
-
}
|
|
2090
|
-
declare function svd(matrix: number[][] | Float64Array, options?: SVDOptions): SVDResult;
|
|
2091
|
-
/**
|
|
2092
|
-
* Compute only singular values (faster than full SVD)
|
|
2093
|
-
*/
|
|
2094
|
-
declare function singularValues(matrix: number[][] | Float64Array, options?: Omit<SVDOptions, 'fullMatrices'>): number[];
|
|
2095
|
-
/**
|
|
2096
|
-
* Compute the pseudoinverse (Moore-Penrose inverse) using SVD
|
|
2097
|
-
*/
|
|
2098
|
-
declare function pinv$1(matrix: number[][], options?: SVDOptions): number[][];
|
|
2099
|
-
/**
|
|
2100
|
-
* Low-rank approximation using SVD
|
|
2101
|
-
* Keeps only the top r singular values
|
|
2102
|
-
*/
|
|
2103
|
-
declare function lowRankApprox(matrix: number[][], r: number, options?: SVDOptions): number[][];
|
|
2104
|
-
/**
|
|
2105
|
-
* Compute condition number using SVD
|
|
2106
|
-
*/
|
|
2107
|
-
declare function cond(matrix: number[][], options?: SVDOptions): number;
|
|
2108
|
-
/**
|
|
2109
|
-
* Compute matrix norm using SVD
|
|
2110
|
-
* Returns the spectral norm (largest singular value)
|
|
2111
|
-
*/
|
|
2112
|
-
declare function norm2(matrix: number[][], options?: SVDOptions): number;
|
|
2113
|
-
/**
|
|
2114
|
-
* Compute Frobenius norm using SVD
|
|
2115
|
-
* Equals sqrt(sum of squared singular values)
|
|
2116
|
-
*/
|
|
2117
|
-
declare function normFro(matrix: number[][]): number;
|
|
2118
|
-
|
|
2119
|
-
/**
|
|
2120
|
-
* `eigWasm` / `eigvalsWasm` / `spectralRadiusWasm` — thin wrappers kept for API
|
|
2121
|
-
* compatibility (consumed by `tensor` and `linalg`).
|
|
2122
|
-
*
|
|
2123
|
-
* The WASM eig path was RETIRED and REMOVED (2026-07-01): the AssemblyScript Jacobi
|
|
2124
|
-
* (symmetric) / Hessenberg+Francis (general) kernels were scalar + async and measured
|
|
2125
|
-
* 0.2–0.7× of the pure-JS path, worsening with size (~5× slower at 128²) — see
|
|
2126
|
-
* `tools/benchmarks/decomp-audit`. Since a genuinely fast version would be a from-scratch
|
|
2127
|
-
* SIMD implementation (not the removed scalar code), these now simply delegate to the
|
|
2128
|
-
* synchronous JS `eig` / `eigvals` / `powerIteration`. The `async` signature is retained so
|
|
2129
|
-
* existing callers (which `await`) are unaffected.
|
|
2130
|
-
*
|
|
2131
|
-
* @packageDocumentation
|
|
2132
|
-
*/
|
|
2133
|
-
|
|
2134
|
-
/**
|
|
2135
|
-
* Eigendecomposition. Delegates to the JS {@link eig} (WASM path retired — see file header).
|
|
2136
|
-
*/
|
|
2137
|
-
declare function eigWasm(matrix: number[][], options?: EigOptions): Promise<EigResult>;
|
|
2138
|
-
/**
|
|
2139
|
-
* Eigenvalues only. Delegates to the JS {@link eigvals} (WASM path retired).
|
|
2140
|
-
*/
|
|
2141
|
-
declare function eigvalsWasm(matrix: number[][], options?: Omit<EigOptions, 'computeVectors'>): Promise<Array<{
|
|
2142
|
-
re: number;
|
|
2143
|
-
im: number;
|
|
2144
|
-
}>>;
|
|
2145
|
-
/**
|
|
2146
|
-
* Spectral radius (|largest eigenvalue|). Delegates to the JS {@link powerIteration}
|
|
2147
|
-
* (WASM path retired).
|
|
2148
|
-
*/
|
|
2149
|
-
declare function spectralRadiusWasm(matrix: number[][], options?: {
|
|
2150
|
-
maxIterations?: number;
|
|
2151
|
-
tolerance?: number;
|
|
2152
|
-
}): Promise<number>;
|
|
2153
|
-
|
|
2154
|
-
/**
|
|
2155
|
-
* `svdWasm` — thin wrapper kept for API compatibility (consumed by `tensor` and `linalg`).
|
|
2156
|
-
*
|
|
2157
|
-
* The WASM SVD path was RETIRED and REMOVED (2026-07-01): the AssemblyScript one-sided
|
|
2158
|
-
* Jacobi kernel was scalar + async and measured 0.4–0.7× of the pure-JS Golub-Reinsch
|
|
2159
|
-
* {@link svd}, worsening with size — see `tools/benchmarks/decomp-audit`. It now delegates
|
|
2160
|
-
* to the JS `svd`, truncated to the **thin / economy** form (`U` is `m×k`, `V` is `n×k`,
|
|
2161
|
-
* `k = min(m, n)`) so the result shape is unchanged. The `async` signature is retained so
|
|
2162
|
-
* existing callers (which `await`) are unaffected.
|
|
2163
|
-
*
|
|
2164
|
-
* @packageDocumentation
|
|
2165
|
-
*/
|
|
2166
|
-
|
|
2167
|
-
/**
|
|
2168
|
-
* Thin SVD. Delegates to the JS {@link svd} and truncates to the economy form
|
|
2169
|
-
* (WASM path retired — see file header).
|
|
2170
|
-
*
|
|
2171
|
-
* @param matrix - Input matrix (m x n) as a row-major 2D array
|
|
2172
|
-
* @param options - SVD options; `rankTolerance` controls the rank estimate
|
|
2173
|
-
* @returns thin SVD `{ U (m x k), S (k), V (n x k), rank }`
|
|
2174
|
-
*/
|
|
2175
|
-
declare function svdWasm(matrix: number[][], options?: SVDOptions): Promise<SVDResult>;
|
|
2176
|
-
|
|
2177
|
-
/**
|
|
2178
|
-
* Moore-Penrose Pseudoinverse (DenseMatrix primitive)
|
|
2179
|
-
*
|
|
2180
|
-
* Computes the pseudoinverse A⁺ of a DenseMatrix A via full SVD with
|
|
2181
|
-
* rcond·max(S) singular-value thresholding.
|
|
2182
|
-
*
|
|
2183
|
-
* Algorithm:
|
|
2184
|
-
* 1. Full SVD: A = U · diag(S) · Vᵀ (A is m×n)
|
|
2185
|
-
* U: m×m orthogonal, V: n×n orthogonal, S: min(m,n) values (descending).
|
|
2186
|
-
* 2. Threshold: s_inv_i = 1/s_i if s_i > rcond·max(S), else 0.
|
|
2187
|
-
* 3. Build: A⁺ = V · diag(s_inv) · Uᵀ (shape: n×m).
|
|
2188
|
-
*
|
|
2189
|
-
* Satisfies all four Moore-Penrose identities within numerical precision:
|
|
2190
|
-
* A · A⁺ · A = A
|
|
2191
|
-
* A⁺ · A · A⁺ = A⁺
|
|
2192
|
-
* (A · A⁺)ᵀ = A · A⁺
|
|
2193
|
-
* (A⁺ · A)ᵀ = A⁺ · A
|
|
2194
|
-
*/
|
|
2195
|
-
|
|
2196
|
-
interface PinvOptions {
|
|
2197
|
-
/**
|
|
2198
|
-
* Relative condition threshold for singular-value truncation.
|
|
2199
|
-
* A singular value s_i is treated as zero when s_i ≤ rcond · max(S).
|
|
2200
|
-
* Default: 1e-10.
|
|
2201
|
-
*/
|
|
2202
|
-
rcond?: number;
|
|
2203
|
-
}
|
|
2204
|
-
/**
|
|
2205
|
-
* Compute the Moore-Penrose pseudoinverse of a DenseMatrix.
|
|
2206
|
-
*
|
|
2207
|
-
* @param A - Input matrix (m × n).
|
|
2208
|
-
* @param opts - Optional rcond threshold (default 1e-10).
|
|
2209
|
-
* @returns A⁺, the pseudoinverse of A (n × m).
|
|
2210
|
-
*/
|
|
2211
|
-
declare function pinv(A: DenseMatrix, opts?: PinvOptions): DenseMatrix;
|
|
2212
|
-
|
|
2213
|
-
/**
|
|
2214
|
-
* QR Decomposition (DenseMatrix primitive)
|
|
2215
|
-
*
|
|
2216
|
-
* Classical Gram-Schmidt with re-orthogonalisation. Adequate for the
|
|
2217
|
-
* well-conditioned matrices that arise in the tensor wrappers + the
|
|
2218
|
-
* `randomTensor` orthogonal-distribution code path. For pathological
|
|
2219
|
-
* matrices the Householder QR implemented in `WASMBackend.qrDecompositionJS`
|
|
2220
|
-
* is preferable, but Gram-Schmidt suffices for the current call sites.
|
|
2221
|
-
*
|
|
2222
|
-
* Two modes:
|
|
2223
|
-
* - 'reduced' (thin QR): Q is (m × k), R is (k × n), k = min(m, n).
|
|
2224
|
-
* - 'full' : Q is (m × m), R is (m × n).
|
|
2225
|
-
*
|
|
2226
|
-
* In 'reduced' mode A = Q · R reconstructs A to within `1e-12`.
|
|
2227
|
-
* Q's columns are orthonormal (Qᵀ · Q = I_k); R is upper-triangular.
|
|
2228
|
-
*/
|
|
2229
|
-
|
|
2230
|
-
interface QRResult {
|
|
2231
|
-
/** Orthonormal Q factor. Reduced: (m × k); full: (m × m). */
|
|
2232
|
-
Q: DenseMatrix;
|
|
2233
|
-
/** Upper-triangular R factor. Reduced: (k × n); full: (m × n). */
|
|
2234
|
-
R: DenseMatrix;
|
|
2235
|
-
}
|
|
2236
|
-
interface QROptions {
|
|
2237
|
-
/** 'reduced' (default, thin QR) or 'full'. */
|
|
2238
|
-
mode?: 'reduced' | 'full';
|
|
2239
|
-
}
|
|
2240
|
-
/**
|
|
2241
|
-
* Compute the QR decomposition of `A` such that `A = Q · R`.
|
|
2242
|
-
*
|
|
2243
|
-
* @param A - Input matrix (m × n, m ≥ 0, n ≥ 0).
|
|
2244
|
-
* @param opts - Optional mode selector (`'reduced'` thin QR or `'full'` square Q).
|
|
2245
|
-
*/
|
|
2246
|
-
declare function qr(A: DenseMatrix, opts?: QROptions): QRResult;
|
|
2247
|
-
|
|
2248
|
-
/**
|
|
2249
|
-
* LU Decomposition (DenseMatrix primitive)
|
|
2250
|
-
*
|
|
2251
|
-
* Doolittle's algorithm with partial (row) pivoting. Factorises a square
|
|
2252
|
-
* matrix A into P · A = L · U where:
|
|
2253
|
-
* - L is unit lower-triangular (1s on the diagonal).
|
|
2254
|
-
* - U is upper-triangular.
|
|
2255
|
-
* - P is returned as a row-permutation array: P[i] is the original-row
|
|
2256
|
-
* index now at position i after pivoting.
|
|
2257
|
-
*
|
|
2258
|
-
* Usage:
|
|
2259
|
-
* const { L, U, P } = lu(A);
|
|
2260
|
-
* // P[i] gives the original row moved to position i, so P · A = L · U.
|
|
2261
|
-
*/
|
|
2262
|
-
|
|
2263
|
-
interface LUResult {
|
|
2264
|
-
/** Unit lower-triangular factor L. */
|
|
2265
|
-
L: DenseMatrix;
|
|
2266
|
-
/** Upper-triangular factor U. */
|
|
2267
|
-
U: DenseMatrix;
|
|
2268
|
-
/** Row permutation array of length n. P[i] is the original-row index now at position i. */
|
|
2269
|
-
P: number[];
|
|
2270
|
-
}
|
|
2271
|
-
/**
|
|
2272
|
-
* Perform LU decomposition with partial pivoting on a square `DenseMatrix`.
|
|
2273
|
-
*
|
|
2274
|
-
* Implements Doolittle's algorithm in compact form: the working array stores
|
|
2275
|
-
* L below the diagonal (unit diagonal implicit) and U on and above it.
|
|
2276
|
-
*
|
|
2277
|
-
* @throws {Error} if `A` is not square.
|
|
2278
|
-
* @throws {Error} if `A` is singular (zero pivot encountered).
|
|
2279
|
-
*/
|
|
2280
|
-
declare function lu(A: DenseMatrix): LUResult;
|
|
2281
|
-
/**
|
|
2282
|
-
* Solve the dense linear system `A · x = b` using a precomputed LU factorisation
|
|
2283
|
-
* (from {@link lu}), where `P · A = L · U`.
|
|
2284
|
-
*
|
|
2285
|
-
* Applies the row permutation to `b` (`(P·b)[i] = b[P[i]]`), forward-substitutes
|
|
2286
|
-
* the unit-lower-triangular `L`, then back-substitutes the upper-triangular `U`.
|
|
2287
|
-
* Each call is O(n²), so factor **once** with `lu()` and call `luSolve` repeatedly
|
|
2288
|
-
* to solve many right-hand sides against the same matrix — the pattern the stiff
|
|
2289
|
-
* Rosenbrock/RODAS ODE steps use (the iteration matrix is solved against 3–6
|
|
2290
|
-
* successive RHS per step).
|
|
2291
|
-
*
|
|
2292
|
-
* @param fac An `LUResult` produced by {@link lu}.
|
|
2293
|
-
* @param b Right-hand side vector of length `n` (`number[]` or `Float64Array`).
|
|
2294
|
-
* @returns The solution vector `x` as a `number[]` of length `n`.
|
|
2295
|
-
* @throws {Error} if `b.length` does not equal the factor dimension `n`.
|
|
2296
|
-
*/
|
|
2297
|
-
declare function luSolve(fac: LUResult, b: ArrayLike<number>): number[];
|
|
2298
|
-
|
|
2299
|
-
/**
|
|
2300
|
-
* Cholesky Decomposition (DenseMatrix primitive)
|
|
2301
|
-
*
|
|
2302
|
-
* Right-looking algorithm. Factorises a symmetric positive-definite (SPD)
|
|
2303
|
-
* matrix A into A = L · Lᵀ where L is lower-triangular.
|
|
2304
|
-
*
|
|
2305
|
-
* Usage:
|
|
2306
|
-
* const { L } = cholesky(A);
|
|
2307
|
-
* // A ≈ L · L^T (to floating-point precision)
|
|
2308
|
-
*/
|
|
2309
|
-
|
|
2310
|
-
interface CholeskyResult {
|
|
2311
|
-
/** Lower-triangular Cholesky factor L such that A = L · Lᵀ. */
|
|
2312
|
-
L: DenseMatrix;
|
|
2313
|
-
}
|
|
2314
|
-
/**
|
|
2315
|
-
* Compute the Cholesky decomposition of the symmetric positive-definite
|
|
2316
|
-
* `DenseMatrix` A such that A = L · Lᵀ.
|
|
2317
|
-
*
|
|
2318
|
-
* @throws {Error} if `A` is not square.
|
|
2319
|
-
* @throws {Error} if `A` is not positive definite (non-positive diagonal pivot).
|
|
2320
|
-
*/
|
|
2321
|
-
declare function cholesky(A: DenseMatrix): CholeskyResult;
|
|
2322
|
-
|
|
2323
|
-
/**
|
|
2324
|
-
* Matrix Exponential — Scaling-and-Squaring with Padé-13 Approximant
|
|
2325
|
-
*
|
|
2326
|
-
* Implements Algorithm 10.20 from:
|
|
2327
|
-
* Higham, N. J. (2005). "The scaling and squaring method for the matrix
|
|
2328
|
-
* exponential revisited." SIAM J. Matrix Anal. Appl., 26(4), 1179–1193.
|
|
2329
|
-
*
|
|
2330
|
-
* Algorithm:
|
|
2331
|
-
* 1. Compute s = max(0, ceil(log2(||A||_1 / θ_13))) where θ_13 ≈ 5.372.
|
|
2332
|
-
* 2. Scale: B = A / 2^s.
|
|
2333
|
-
* 3. Compute the [13/13] Padé approximant R(B) = (U + V)^{-1} * (-U + V).
|
|
2334
|
-
* 4. Square s times: expm(A) = R(B)^(2^s).
|
|
2335
|
-
*
|
|
2336
|
-
* Works for any real square DenseMatrix. For large ||A||, uses scaling to
|
|
2337
|
-
* stay within the Padé approximant's accuracy radius.
|
|
2338
|
-
*/
|
|
2339
|
-
|
|
2340
|
-
interface ExpmOptions {
|
|
2341
|
-
/**
|
|
2342
|
-
* When true, return the result as a DenseMatrix.
|
|
2343
|
-
* When false (default), work directly on the internal number[][] and
|
|
2344
|
-
* return as DenseMatrix anyway — this option is reserved for future use.
|
|
2345
|
-
*/
|
|
2346
|
-
_reserved?: never;
|
|
2347
|
-
}
|
|
2348
|
-
/**
|
|
2349
|
-
* Compute the matrix exponential of a square DenseMatrix.
|
|
2350
|
-
*
|
|
2351
|
-
* Uses the Padé-13 scaling-and-squaring algorithm (Higham 2005).
|
|
2352
|
-
* Accurate to near machine precision for general real matrices.
|
|
2353
|
-
*
|
|
2354
|
-
* @param A - Square DenseMatrix (n × n).
|
|
2355
|
-
* @returns expm(A) as a DenseMatrix (n × n).
|
|
2356
|
-
* @throws Error if A is not square.
|
|
2357
|
-
*
|
|
2358
|
-
* @example
|
|
2359
|
-
* // expm(0) = I
|
|
2360
|
-
* matrixExpm(DenseMatrix.zeros(3, 3)) // => identity 3×3
|
|
2361
|
-
*
|
|
2362
|
-
* @example
|
|
2363
|
-
* // expm(t*I) = e^t * I
|
|
2364
|
-
* const tI = DenseMatrix.fromArray([[2,0],[0,2]]);
|
|
2365
|
-
* matrixExpm(tI) // => [[e^2, 0], [0, e^2]]
|
|
2366
|
-
*/
|
|
2367
|
-
declare function matrixExpm(A: DenseMatrix): DenseMatrix;
|
|
2368
|
-
|
|
2369
|
-
/**
|
|
2370
|
-
* Matrix Logarithm — Schur-Padé inverse scaling-and-squaring (Slices 5.9a + 6.1)
|
|
2371
|
-
*
|
|
2372
|
-
* Implements the Schur-Padé algorithm based on:
|
|
2373
|
-
* Higham (2008) "Functions of Matrices: Theory and Computation," Chapter 11
|
|
2374
|
-
* (Algorithm 11.10).
|
|
2375
|
-
*
|
|
2376
|
-
* Algorithm (general Schur-based path, Slice 6.1):
|
|
2377
|
-
* 1. Schur decompose: A = Q · T · Q^T.
|
|
2378
|
-
* 2. Repeatedly take matrix square roots of T (Björck recurrence on upper-
|
|
2379
|
-
* triangular matrices) until ||T^{1/2^k} - I||_1 < 0.25.
|
|
2380
|
-
* 3. Evaluate log(I + X) via 16-point Gauss-Legendre quadrature.
|
|
2381
|
-
* 4. Scale back by 2^k; rotate by Q: logm(A) = Q · (2^k · log(T^{1/2^k})) · Q^T.
|
|
2382
|
-
*
|
|
2383
|
-
* For the eig-based fallback (diagonalisable matrices with positive real
|
|
2384
|
-
* eigenvalues): logm(A) = V · diag(log(λ)) · V^{-1}.
|
|
2385
|
-
*/
|
|
2386
|
-
|
|
2387
|
-
interface LogmOptions {
|
|
2388
|
-
/**
|
|
2389
|
-
* Tolerance for the "near-identity" test.
|
|
2390
|
-
* Default: 0.25 (||M - I||_1 < tol triggers Padé path).
|
|
2391
|
-
*/
|
|
2392
|
-
nearIdentityTol?: number;
|
|
2393
|
-
}
|
|
2394
|
-
/**
|
|
2395
|
-
* Compute the principal matrix logarithm of a square DenseMatrix.
|
|
2396
|
-
*
|
|
2397
|
-
* Algorithm (Slice 5.9a — inverse scaling-and-squaring + Padé quadrature):
|
|
2398
|
-
* 1. Repeatedly take matrix square roots until ||M - I||_1 < nearIdentityTol.
|
|
2399
|
-
* 2. Evaluate log(I + X) = X * ∫₀¹ (I + tX)^{-1} dt via 7-pt Gauss-Legendre.
|
|
2400
|
-
* 3. Scale back: logm(A) = 2^k * log(A^{1/2^k}).
|
|
2401
|
-
*
|
|
2402
|
-
* Falls back to eigendecomposition if the square-root sequence diverges
|
|
2403
|
-
* (i.e., if the matrix has no non-positive eigenvalues and is diagonalisable).
|
|
2404
|
-
*
|
|
2405
|
-
* Limitations (deferred to Slice 5.9b):
|
|
2406
|
-
* - Matrices with non-positive real eigenvalues: principal log is undefined.
|
|
2407
|
-
* - Matrices with complex eigenvalues: throws.
|
|
2408
|
-
* - Non-diagonalisable (defective) matrices: eig-fallback also throws.
|
|
2409
|
-
* - Full Schur-based inverse-scaling-and-squaring for general non-normal
|
|
2410
|
-
* matrices.
|
|
2411
|
-
*
|
|
2412
|
-
* @param A - Square DenseMatrix with positive real eigenvalues.
|
|
2413
|
-
* @returns logm(A) as a DenseMatrix.
|
|
2414
|
-
* @throws Error for matrices with non-positive or complex eigenvalues.
|
|
2415
|
-
*
|
|
2416
|
-
* @example
|
|
2417
|
-
* // logm(I) = 0
|
|
2418
|
-
* matrixLogm(DenseMatrix.eye(3)) // => zero matrix
|
|
2419
|
-
*
|
|
2420
|
-
* @example
|
|
2421
|
-
* // Round-trip: expm(logm(A)) ≈ A
|
|
2422
|
-
* const A = DenseMatrix.fromArray([[4, 1], [0, 9]]);
|
|
2423
|
-
* const logA = matrixLogm(A);
|
|
2424
|
-
* // expm(logA) ≈ A (to machine precision for diagonalisable A)
|
|
2425
|
-
*/
|
|
2426
|
-
declare function matrixLogm(A: DenseMatrix, opts?: LogmOptions): DenseMatrix;
|
|
2427
|
-
|
|
2428
|
-
/**
|
|
2429
|
-
* Matrix Square Root — Hybrid approach (Slices 5.9a + 6.1)
|
|
2430
|
-
*
|
|
2431
|
-
* Computes the principal square root of a square matrix.
|
|
2432
|
-
*
|
|
2433
|
-
* Algorithm selection:
|
|
2434
|
-
* 1. For symmetric positive semi-definite matrices: symmetric eigendecomposition
|
|
2435
|
-
* with Gram-Schmidt re-orthogonalisation (handles repeated eigenvalues).
|
|
2436
|
-
* 2. For general matrices: Björck-Hammarling Schur-based algorithm
|
|
2437
|
-
* (Higham 2008, Algorithm 6.3):
|
|
2438
|
-
* a. Schur decompose: A = Q · T · Q^T.
|
|
2439
|
-
* b. Compute upper-triangular U with U^2 = T via back-substitution.
|
|
2440
|
-
* c. Rotate back: sqrtm(A) = Q · U · Q^T.
|
|
2441
|
-
* Falls back to Newton iteration for diagonalisable matrices when the
|
|
2442
|
-
* Schur path is unsuitable (e.g. negative eigenvalues in 1×1 blocks).
|
|
2443
|
-
*
|
|
2444
|
-
* References:
|
|
2445
|
-
* - Higham (2008) "Functions of Matrices" §6 (Algorithm 6.3).
|
|
2446
|
-
* - Björck & Hammarling (1983) "A Schur method for the square root of a matrix."
|
|
2447
|
-
*/
|
|
2448
|
-
|
|
2449
|
-
interface SqrtmOptions {
|
|
2450
|
-
/**
|
|
2451
|
-
* Force the symmetric-eigendecomposition path even for non-SPD matrices.
|
|
2452
|
-
* By default, symmetry is auto-detected.
|
|
2453
|
-
*/
|
|
2454
|
-
assumeSymmetric?: boolean;
|
|
2455
|
-
}
|
|
2456
|
-
/**
|
|
2457
|
-
* Compute the principal square root of a square DenseMatrix.
|
|
2458
|
-
*
|
|
2459
|
-
* For symmetric positive semi-definite A:
|
|
2460
|
-
* Uses symmetric eigendecomposition with Gram-Schmidt re-orthogonalisation.
|
|
2461
|
-
*
|
|
2462
|
-
* For general diagonalisable A with non-negative eigenvalues:
|
|
2463
|
-
* Uses Newton iteration Y_{k+1} = (Y_k + A * Y_k^{-1}) / 2, falling back
|
|
2464
|
-
* to the eig-based formula if Newton fails to converge.
|
|
2465
|
-
*
|
|
2466
|
-
* Slice 5.9a limitations:
|
|
2467
|
-
* - Matrices with negative real eigenvalues: throws.
|
|
2468
|
-
* - Matrices with complex eigenvalues: throws.
|
|
2469
|
-
* - Non-diagonalisable (defective) matrices: Newton may fail + eig fallback
|
|
2470
|
-
* may also fail. Full Schur-based Björck-Hammarling deferred to Slice 5.9b.
|
|
2471
|
-
*
|
|
2472
|
-
* @param A - Square DenseMatrix (n × n).
|
|
2473
|
-
* @param opts - Optional flags.
|
|
2474
|
-
* @returns sqrtm(A), the principal square root.
|
|
2475
|
-
* @throws Error for matrices with negative or complex eigenvalues.
|
|
2476
|
-
*
|
|
2477
|
-
* @example
|
|
2478
|
-
* // sqrtm(I) = I
|
|
2479
|
-
* matrixSqrtm(DenseMatrix.eye(3)) // => identity 3×3
|
|
2480
|
-
*
|
|
2481
|
-
* @example
|
|
2482
|
-
* // sqrtm(4*I) = 2*I
|
|
2483
|
-
* const A = DenseMatrix.fromArray([[4,0],[0,4]]);
|
|
2484
|
-
* matrixSqrtm(A) // => [[2,0],[0,2]]
|
|
2485
|
-
*
|
|
2486
|
-
* @example
|
|
2487
|
-
* // Round-trip: sqrtm(A)^2 ≈ A
|
|
2488
|
-
* const S = DenseMatrix.fromArray([[4,2],[2,3]]);
|
|
2489
|
-
* const R = matrixSqrtm(S);
|
|
2490
|
-
* // R * R ≈ S (to machine precision for SPD matrices)
|
|
2491
|
-
*/
|
|
2492
|
-
declare function matrixSqrtm(A: DenseMatrix, opts?: SqrtmOptions): DenseMatrix;
|
|
2493
|
-
|
|
2494
|
-
/**
|
|
2495
|
-
* Schur Decomposition — Francis QR with double shifts (Slice 6.1)
|
|
2496
|
-
*
|
|
2497
|
-
* Computes the real Schur decomposition A = Q · T · Q^T where:
|
|
2498
|
-
* - Q is orthogonal (Q^T · Q = I)
|
|
2499
|
-
* - T is quasi-upper-triangular (real Schur form): diagonal blocks are
|
|
2500
|
-
* 1×1 (real eigenvalue) or 2×2 (complex-conjugate eigenvalue pair)
|
|
2501
|
-
*
|
|
2502
|
-
* Algorithm: Householder reduction to upper Hessenberg form followed by
|
|
2503
|
-
* Francis double-shift implicit QR iteration (Golub & Van Loan §7.5).
|
|
2504
|
-
*
|
|
2505
|
-
* References:
|
|
2506
|
-
* Golub & Van Loan (2013) "Matrix Computations," 4th ed., §7.5.
|
|
2507
|
-
* Higham (2008) "Functions of Matrices: Theory and Computation," §1.
|
|
2508
|
-
*/
|
|
2509
|
-
|
|
2510
|
-
interface SchurResult {
|
|
2511
|
-
/** Orthogonal factor Q (Q^T · Q = I, Q · Q^T = I). */
|
|
2512
|
-
Q: DenseMatrix;
|
|
2513
|
-
/** Quasi-upper-triangular Schur form T (real Schur form). */
|
|
2514
|
-
T: DenseMatrix;
|
|
2515
|
-
}
|
|
2516
|
-
interface SchurOptions {
|
|
2517
|
-
/** Maximum QR iterations (default: 1000). */
|
|
2518
|
-
maxIterations?: number;
|
|
2519
|
-
/** Deflation tolerance (default: 1e-12). */
|
|
2520
|
-
tolerance?: number;
|
|
2521
|
-
}
|
|
2522
|
-
/**
|
|
2523
|
-
* Compute the real Schur decomposition of a square matrix:
|
|
2524
|
-
* A = Q · T · Q^T
|
|
2525
|
-
*
|
|
2526
|
-
* T is quasi-upper-triangular (real Schur form): 1×1 diagonal blocks for
|
|
2527
|
-
* real eigenvalues and 2×2 blocks for complex-conjugate pairs. Q is
|
|
2528
|
-
* orthogonal.
|
|
2529
|
-
*
|
|
2530
|
-
* For matrices with all real eigenvalues, T is upper triangular with
|
|
2531
|
-
* eigenvalues on the diagonal. For matrices with complex-conjugate eigenvalue
|
|
2532
|
-
* pairs, the corresponding 2×2 diagonal block has the form
|
|
2533
|
-
* [[a, b], [-b, a]] (approximately)
|
|
2534
|
-
* and the complex pair is a ± bi.
|
|
2535
|
-
*
|
|
2536
|
-
* @param A - Square DenseMatrix (n × n, real entries).
|
|
2537
|
-
* @param opts - Optional algorithm parameters.
|
|
2538
|
-
* @returns { Q, T } satisfying Q · T · Q^T ≈ A.
|
|
2539
|
-
* @throws Error if A is not square.
|
|
2540
|
-
*/
|
|
2541
|
-
declare function matrixSchur(A: DenseMatrix, opts?: SchurOptions): SchurResult;
|
|
2542
|
-
|
|
2543
|
-
/**
|
|
2544
|
-
* Rank-Revealing Pivoted QR Decomposition (Businger-Golub column pivoting)
|
|
2545
|
-
*
|
|
2546
|
-
* Computes `A[:, P] = Q · R` via Householder reflections, choosing at each
|
|
2547
|
-
* step the remaining column of largest norm as the next pivot. This is the
|
|
2548
|
-
* classical column-pivoted QR (LAPACK `dgeqp3`'s algorithm, simplified —
|
|
2549
|
-
* exact remaining-column-norm recomputation rather than the cheaper
|
|
2550
|
-
* downdating formula, since these matrices are small): it guarantees
|
|
2551
|
-
* `|R[0,0]| ≥ |R[1,1]| ≥ … ≥ |R[k-1,k-1]|`, which makes `R`'s diagonal a
|
|
2552
|
-
* reliable numerical-rank indicator — unlike the plain (unpivoted)
|
|
2553
|
-
* Gram-Schmidt `qr()` in this directory, whose diagonal can be small for an
|
|
2554
|
-
* early column purely by column order, not by rank deficiency.
|
|
2555
|
-
*
|
|
2556
|
-
* Reuses the Householder helpers in `./common.js` (already exercised by
|
|
2557
|
-
* `svd.ts`/`schur.ts`) rather than re-deriving reflection algebra here.
|
|
2558
|
-
*/
|
|
2559
|
-
|
|
2560
|
-
interface QRPivotedResult {
|
|
2561
|
-
/** Orthonormal Q factor (m × k, k = min(m, n)). */
|
|
2562
|
-
Q: DenseMatrix;
|
|
2563
|
-
/** Upper-triangular R factor (k × n) with |diag(R)| non-increasing. */
|
|
2564
|
-
R: DenseMatrix;
|
|
2565
|
-
/** Column permutation: `P[j]` is the original column index now at position `j`, so `A[:, P] = Q · R`. */
|
|
2566
|
-
P: number[];
|
|
2567
|
-
/** Numerical rank: count of `i` with `|R[i,i]| > tolerance · |R[0,0]|`. */
|
|
2568
|
-
rank: number;
|
|
2569
|
-
}
|
|
2570
|
-
interface QRPivotedOptions {
|
|
2571
|
-
/** Relative rank tolerance (default `1e-10`). */
|
|
2572
|
-
tolerance?: number;
|
|
2573
|
-
}
|
|
2574
|
-
/**
|
|
2575
|
-
* Compute the column-pivoted rank-revealing QR decomposition of `A`.
|
|
2576
|
-
*
|
|
2577
|
-
* @param A - Input matrix (m × n).
|
|
2578
|
-
* @param opts - Optional rank tolerance.
|
|
2579
|
-
*/
|
|
2580
|
-
declare function qrPivoted(A: DenseMatrix, opts?: QRPivotedOptions): QRPivotedResult;
|
|
2581
|
-
|
|
2582
|
-
/**
|
|
2583
|
-
* QR-family decompositions: LQ, RQ, QL
|
|
2584
|
-
*
|
|
2585
|
-
* Each is derived from the existing Gram-Schmidt `qr()` primitive in this
|
|
2586
|
-
* directory via the standard flip/transpose reductions (Golub & Van Loan,
|
|
2587
|
-
* *Matrix Computations*, §5.2):
|
|
2588
|
-
*
|
|
2589
|
-
* - **LQ** (`A = L·Q`, L lower-triangular): `qr(Aᵀ)` transposed — no flips
|
|
2590
|
-
* needed. If `Aᵀ = Q₁·R₁` then `A = R₁ᵀ·Q₁ᵀ`, so `L = R₁ᵀ`, `Q = Q₁ᵀ`.
|
|
2591
|
-
* - **RQ** (`A = R·Q`, R upper-triangular): reverse `A`'s row order,
|
|
2592
|
-
* transpose, run `qr()`, then un-flip the resulting factors.
|
|
2593
|
-
* - **QL** (`A = Q·L`, L lower-triangular): reverse `A`'s column order,
|
|
2594
|
-
* run `qr()`, then un-flip the resulting factors.
|
|
2595
|
-
*
|
|
2596
|
-
* All three share `qr()`'s reduced-QR convention: for an m×n input the thin
|
|
2597
|
-
* factor pairing is (m×k, k×n) with k = min(m, n).
|
|
2598
|
-
*/
|
|
2599
|
-
|
|
2600
|
-
interface LQResult {
|
|
2601
|
-
/** Lower-triangular (trapezoidal) factor. m × k, k = min(m, n). */
|
|
2602
|
-
L: DenseMatrix;
|
|
2603
|
-
/** Orthonormal-row factor (Q · Qᵀ = I). k × n. */
|
|
2604
|
-
Q: DenseMatrix;
|
|
2605
|
-
}
|
|
2606
|
-
interface RQResult {
|
|
2607
|
-
/** Upper-triangular (trapezoidal) factor. m × k, k = min(m, n). */
|
|
2608
|
-
R: DenseMatrix;
|
|
2609
|
-
/** Orthonormal-row factor (Q · Qᵀ = I). k × n. */
|
|
2610
|
-
Q: DenseMatrix;
|
|
2611
|
-
}
|
|
2612
|
-
interface QLResult {
|
|
2613
|
-
/** Orthonormal-column factor (Qᵀ · Q = I). m × k, k = min(m, n). */
|
|
2614
|
-
Q: DenseMatrix;
|
|
2615
|
-
/** Lower-triangular (trapezoidal) factor. k × n. */
|
|
2616
|
-
L: DenseMatrix;
|
|
2617
|
-
}
|
|
2618
|
-
/**
|
|
2619
|
-
* LQ decomposition: `A = L · Q` with `L` lower-triangular and `Q` having
|
|
2620
|
-
* orthonormal rows (`Q · Qᵀ = I`).
|
|
2621
|
-
*/
|
|
2622
|
-
declare function lq(A: DenseMatrix): LQResult;
|
|
2623
|
-
/**
|
|
2624
|
-
* RQ decomposition: `A = R · Q` with `R` upper-triangular and `Q` having
|
|
2625
|
-
* orthonormal rows (`Q · Qᵀ = I`).
|
|
2626
|
-
*/
|
|
2627
|
-
declare function rq(A: DenseMatrix): RQResult;
|
|
2628
|
-
/**
|
|
2629
|
-
* QL decomposition: `A = Q · L` with `Q` having orthonormal columns
|
|
2630
|
-
* (`Qᵀ · Q = I`) and `L` lower-triangular.
|
|
2631
|
-
*/
|
|
2632
|
-
declare function ql(A: DenseMatrix): QLResult;
|
|
2633
|
-
|
|
2634
|
-
/**
|
|
2635
|
-
* Condition-number ESTIMATE (Hager/Higham 1-norm power-iteration estimator)
|
|
2636
|
-
*
|
|
2637
|
-
* Estimates `‖A‖₁ · ‖A⁻¹‖₁` without ever forming `A⁻¹` explicitly — Hager's
|
|
2638
|
-
* algorithm (Hager 1984; Higham 1988's practical refinement) needs only the
|
|
2639
|
-
* ability to apply `A⁻¹` and `A⁻ᵀ` to a vector, which is done here via the
|
|
2640
|
-
* existing `lu()` primitive's triangular factors (forward/back substitution,
|
|
2641
|
-
* O(n²) per application vs. O(n³) to form `A⁻¹` or run the SVD-based exact
|
|
2642
|
-
* `cond()` in `svd.ts`). The result is an ESTIMATE — typically a lower bound
|
|
2643
|
-
* on the true `‖A⁻¹‖₁`, usually within a small constant factor — not the
|
|
2644
|
-
* exact value.
|
|
2645
|
-
*/
|
|
2646
|
-
|
|
2647
|
-
/**
|
|
2648
|
-
* Estimate the 1-norm condition number `‖A‖₁ · ‖A⁻¹‖₁` of a square matrix.
|
|
2649
|
-
*
|
|
2650
|
-
* This is an ESTIMATE (Hager/Higham power iteration), not the exact
|
|
2651
|
-
* condition number that `cond()` (in `svd.ts`) computes via a full SVD —
|
|
2652
|
-
* `condest` is O(n²)-per-iteration and avoids forming `A⁻¹`, at the cost of
|
|
2653
|
-
* being a (typically close) lower-bound estimate rather than an exact value.
|
|
2654
|
-
*
|
|
2655
|
-
* @param A - Square input matrix.
|
|
2656
|
-
* @param p - Only the 1-norm (`p = 1`, the default) is currently implemented.
|
|
2657
|
-
* @throws {Error} if `A` is not square, or `p !== 1`.
|
|
2658
|
-
*/
|
|
2659
|
-
declare function condest(A: DenseMatrix, p?: number): number;
|
|
2660
|
-
|
|
2661
|
-
/**
|
|
2662
|
-
* Typed Matrix Operations
|
|
2663
|
-
*
|
|
2664
|
-
* Polymorphic matrix operations using typed-function for runtime dispatch.
|
|
2665
|
-
* Supports operations on DenseMatrix with automatic type coercion.
|
|
2666
|
-
*
|
|
2667
|
-
* @packageDocumentation
|
|
2668
|
-
*/
|
|
2669
|
-
/**
|
|
2670
|
-
* Create a matrix from various input types
|
|
2671
|
-
*/
|
|
2672
|
-
declare const matrix: typed_function.TypedFunction;
|
|
2673
|
-
/**
|
|
2674
|
-
* Create an identity matrix
|
|
2675
|
-
*/
|
|
2676
|
-
declare const identity: typed_function.TypedFunction;
|
|
2677
|
-
/**
|
|
2678
|
-
* Create a matrix of zeros
|
|
2679
|
-
*/
|
|
2680
|
-
declare const zeros: typed_function.TypedFunction;
|
|
2681
|
-
/**
|
|
2682
|
-
* Create a matrix of ones
|
|
2683
|
-
*/
|
|
2684
|
-
declare const ones: typed_function.TypedFunction;
|
|
2685
|
-
/**
|
|
2686
|
-
* Create a diagonal matrix
|
|
2687
|
-
*/
|
|
2688
|
-
declare const diag: typed_function.TypedFunction;
|
|
2689
|
-
/**
|
|
2690
|
-
* Create a random matrix
|
|
2691
|
-
*/
|
|
2692
|
-
declare const random: typed_function.TypedFunction;
|
|
2693
|
-
/**
|
|
2694
|
-
* Matrix addition - polymorphic add function
|
|
2695
|
-
*/
|
|
2696
|
-
declare const add: typed_function.TypedFunction;
|
|
2697
|
-
/**
|
|
2698
|
-
* Matrix subtraction
|
|
2699
|
-
*/
|
|
2700
|
-
declare const subtract: typed_function.TypedFunction;
|
|
2701
|
-
/**
|
|
2702
|
-
* Matrix multiplication (matmul)
|
|
2703
|
-
*/
|
|
2704
|
-
declare const multiply: typed_function.TypedFunction;
|
|
2705
|
-
/**
|
|
2706
|
-
* Element-wise multiplication (Hadamard product)
|
|
2707
|
-
*/
|
|
2708
|
-
declare const dotMultiply: typed_function.TypedFunction;
|
|
2709
|
-
/**
|
|
2710
|
-
* Matrix division (A / B where B is scalar or element-wise)
|
|
2711
|
-
*/
|
|
2712
|
-
declare const divide: typed_function.TypedFunction;
|
|
2713
|
-
/**
|
|
2714
|
-
* Matrix negation
|
|
2715
|
-
*/
|
|
2716
|
-
declare const unaryMinus: typed_function.TypedFunction;
|
|
2717
|
-
/**
|
|
2718
|
-
* Matrix transpose
|
|
2719
|
-
*/
|
|
2720
|
-
declare const transpose: typed_function.TypedFunction;
|
|
2721
|
-
/**
|
|
2722
|
-
* Sum of all elements
|
|
2723
|
-
*/
|
|
2724
|
-
declare const sum: typed_function.TypedFunction;
|
|
2725
|
-
/**
|
|
2726
|
-
* Mean of all elements
|
|
2727
|
-
*/
|
|
2728
|
-
declare const mean: typed_function.TypedFunction;
|
|
2729
|
-
/**
|
|
2730
|
-
* Minimum element
|
|
2731
|
-
*/
|
|
2732
|
-
declare const min: typed_function.TypedFunction;
|
|
2733
|
-
/**
|
|
2734
|
-
* Maximum element
|
|
2735
|
-
*/
|
|
2736
|
-
declare const max: typed_function.TypedFunction;
|
|
2737
|
-
/**
|
|
2738
|
-
* Frobenius norm
|
|
2739
|
-
*/
|
|
2740
|
-
declare const norm: typed_function.TypedFunction;
|
|
2741
|
-
/**
|
|
2742
|
-
* Matrix trace (sum of diagonal elements)
|
|
2743
|
-
*/
|
|
2744
|
-
declare const trace: typed_function.TypedFunction;
|
|
2745
|
-
/**
|
|
2746
|
-
* Element-wise absolute value
|
|
2747
|
-
*/
|
|
2748
|
-
declare const abs: typed_function.TypedFunction;
|
|
2749
|
-
/**
|
|
2750
|
-
* Element-wise square root
|
|
2751
|
-
*/
|
|
2752
|
-
declare const sqrt: typed_function.TypedFunction;
|
|
2753
|
-
/**
|
|
2754
|
-
* Element-wise square
|
|
2755
|
-
*/
|
|
2756
|
-
declare const square: typed_function.TypedFunction;
|
|
2757
|
-
/**
|
|
2758
|
-
* Element-wise exponential
|
|
2759
|
-
*/
|
|
2760
|
-
declare const exp: typed_function.TypedFunction;
|
|
2761
|
-
/**
|
|
2762
|
-
* Element-wise natural logarithm
|
|
2763
|
-
*/
|
|
2764
|
-
declare const log: typed_function.TypedFunction;
|
|
2765
|
-
/**
|
|
2766
|
-
* Element-wise power
|
|
2767
|
-
*/
|
|
2768
|
-
declare const pow: typed_function.TypedFunction;
|
|
2769
|
-
/**
|
|
2770
|
-
* Get matrix dimensions
|
|
2771
|
-
*/
|
|
2772
|
-
declare const size: typed_function.TypedFunction;
|
|
2773
|
-
/**
|
|
2774
|
-
* Get element at position
|
|
2775
|
-
*/
|
|
2776
|
-
declare const subset: typed_function.TypedFunction;
|
|
2777
|
-
/**
|
|
2778
|
-
* Get row from matrix
|
|
2779
|
-
*/
|
|
2780
|
-
declare const row: typed_function.TypedFunction;
|
|
2781
|
-
/**
|
|
2782
|
-
* Get column from matrix
|
|
2783
|
-
*/
|
|
2784
|
-
declare const column: typed_function.TypedFunction;
|
|
2785
|
-
/**
|
|
2786
|
-
* Get diagonal from matrix
|
|
2787
|
-
*/
|
|
2788
|
-
declare const diagonal: typed_function.TypedFunction;
|
|
2789
|
-
declare const typedMatrixOperations: {
|
|
2790
|
-
matrix: typed_function.TypedFunction;
|
|
2791
|
-
identity: typed_function.TypedFunction;
|
|
2792
|
-
zeros: typed_function.TypedFunction;
|
|
2793
|
-
ones: typed_function.TypedFunction;
|
|
2794
|
-
diag: typed_function.TypedFunction;
|
|
2795
|
-
random: typed_function.TypedFunction;
|
|
2796
|
-
add: typed_function.TypedFunction;
|
|
2797
|
-
subtract: typed_function.TypedFunction;
|
|
2798
|
-
multiply: typed_function.TypedFunction;
|
|
2799
|
-
dotMultiply: typed_function.TypedFunction;
|
|
2800
|
-
divide: typed_function.TypedFunction;
|
|
2801
|
-
unaryMinus: typed_function.TypedFunction;
|
|
2802
|
-
transpose: typed_function.TypedFunction;
|
|
2803
|
-
sum: typed_function.TypedFunction;
|
|
2804
|
-
mean: typed_function.TypedFunction;
|
|
2805
|
-
min: typed_function.TypedFunction;
|
|
2806
|
-
max: typed_function.TypedFunction;
|
|
2807
|
-
norm: typed_function.TypedFunction;
|
|
2808
|
-
trace: typed_function.TypedFunction;
|
|
2809
|
-
abs: typed_function.TypedFunction;
|
|
2810
|
-
sqrt: typed_function.TypedFunction;
|
|
2811
|
-
square: typed_function.TypedFunction;
|
|
2812
|
-
exp: typed_function.TypedFunction;
|
|
2813
|
-
log: typed_function.TypedFunction;
|
|
2814
|
-
pow: typed_function.TypedFunction;
|
|
2815
|
-
size: typed_function.TypedFunction;
|
|
2816
|
-
subset: typed_function.TypedFunction;
|
|
2817
|
-
row: typed_function.TypedFunction;
|
|
2818
|
-
column: typed_function.TypedFunction;
|
|
2819
|
-
diagonal: typed_function.TypedFunction;
|
|
2820
|
-
};
|
|
2821
|
-
|
|
2822
|
-
/**
|
|
2823
|
-
* Parallel-First Matrix Operations
|
|
2824
|
-
*
|
|
2825
|
-
* AssemblyScript-friendly TypeScript implementations with typed-function
|
|
2826
|
-
* integration and workerpool parallel execution via @danielsimonjr/mathts-parallel.
|
|
2827
|
-
*
|
|
2828
|
-
* These operations use Float64Array flat row-major format for efficient
|
|
2829
|
-
* parallel processing through the ComputePool worker infrastructure.
|
|
4
|
+
* Matrix operations for MathTS with pluggable backends
|
|
5
|
+
* (JS, WASM, WebGPU) and typed-function polymorphic dispatch.
|
|
2830
6
|
*
|
|
2831
7
|
* Following the parallel-first philosophy per CLAUDE.md:
|
|
2832
|
-
* -
|
|
2833
|
-
* -
|
|
2834
|
-
*
|
|
8
|
+
* - Sequential typed-operations.ts for DenseMatrix class operations
|
|
9
|
+
* - parallel-matrix.ts for worker pool-based Float64Array operations
|
|
10
|
+
*
|
|
11
|
+
* For Float64Array-based parallel matrix operations, use @danielsimonjr/mathts-parallel
|
|
12
|
+
* directly or import from './parallel-matrix.js'.
|
|
2835
13
|
*
|
|
2836
14
|
* @packageDocumentation
|
|
2837
15
|
*/
|
|
2838
|
-
|
|
2839
|
-
*
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
*/
|
|
2845
|
-
declare const parallelIdentity: typed_function.TypedFunction;
|
|
2846
|
-
/**
|
|
2847
|
-
* Create a matrix of zeros
|
|
2848
|
-
*/
|
|
2849
|
-
declare const parallelZeros: typed_function.TypedFunction;
|
|
2850
|
-
/**
|
|
2851
|
-
* Create a matrix of ones
|
|
2852
|
-
*/
|
|
2853
|
-
declare const parallelOnes: typed_function.TypedFunction;
|
|
2854
|
-
/**
|
|
2855
|
-
* Create a diagonal matrix
|
|
2856
|
-
*/
|
|
2857
|
-
declare const parallelDiag: typed_function.TypedFunction;
|
|
2858
|
-
/**
|
|
2859
|
-
* Create a random matrix
|
|
2860
|
-
*/
|
|
2861
|
-
declare const parallelRandom: typed_function.TypedFunction;
|
|
2862
|
-
/**
|
|
2863
|
-
* Parallel matrix addition with typed-function dispatch
|
|
2864
|
-
*/
|
|
2865
|
-
declare const parallelMatrixAdd: typed_function.TypedFunction;
|
|
2866
|
-
/**
|
|
2867
|
-
* Parallel matrix subtraction with typed-function dispatch
|
|
2868
|
-
*/
|
|
2869
|
-
declare const parallelMatrixSubtract: typed_function.TypedFunction;
|
|
2870
|
-
/**
|
|
2871
|
-
* Parallel matrix multiplication (matmul) with typed-function dispatch
|
|
2872
|
-
*/
|
|
2873
|
-
declare const parallelMatrixMultiply: typed_function.TypedFunction;
|
|
2874
|
-
/**
|
|
2875
|
-
* Element-wise multiplication (Hadamard product) - parallel execution
|
|
2876
|
-
*/
|
|
2877
|
-
declare const parallelDotMultiply: typed_function.TypedFunction;
|
|
2878
|
-
/**
|
|
2879
|
-
* Parallel matrix division
|
|
2880
|
-
*/
|
|
2881
|
-
declare const parallelMatrixDivide: typed_function.TypedFunction;
|
|
2882
|
-
/**
|
|
2883
|
-
* Matrix negation - parallel execution
|
|
2884
|
-
*/
|
|
2885
|
-
declare const parallelUnaryMinus: typed_function.TypedFunction;
|
|
2886
|
-
/**
|
|
2887
|
-
* Matrix transpose - parallel execution
|
|
2888
|
-
*/
|
|
2889
|
-
declare const parallelMatrixTranspose: typed_function.TypedFunction;
|
|
2890
|
-
/**
|
|
2891
|
-
* Parallel sum of all elements
|
|
2892
|
-
*/
|
|
2893
|
-
declare const parallelMatrixSum: typed_function.TypedFunction;
|
|
2894
|
-
/**
|
|
2895
|
-
* Parallel mean of all elements
|
|
2896
|
-
*/
|
|
2897
|
-
declare const parallelMatrixMean: typed_function.TypedFunction;
|
|
2898
|
-
/**
|
|
2899
|
-
* Parallel minimum element
|
|
2900
|
-
*/
|
|
2901
|
-
declare const parallelMatrixMin: typed_function.TypedFunction;
|
|
2902
|
-
/**
|
|
2903
|
-
* Parallel maximum element
|
|
2904
|
-
*/
|
|
2905
|
-
declare const parallelMatrixMax: typed_function.TypedFunction;
|
|
2906
|
-
/**
|
|
2907
|
-
* Parallel variance computation
|
|
2908
|
-
*/
|
|
2909
|
-
declare const parallelMatrixVariance: typed_function.TypedFunction;
|
|
2910
|
-
/**
|
|
2911
|
-
* Parallel standard deviation
|
|
2912
|
-
*/
|
|
2913
|
-
declare const parallelMatrixStd: typed_function.TypedFunction;
|
|
2914
|
-
/**
|
|
2915
|
-
* Parallel Frobenius norm
|
|
2916
|
-
*/
|
|
2917
|
-
declare const parallelMatrixNorm: typed_function.TypedFunction;
|
|
2918
|
-
/**
|
|
2919
|
-
* Parallel dot product
|
|
2920
|
-
*/
|
|
2921
|
-
declare const parallelMatrixDot: typed_function.TypedFunction;
|
|
2922
|
-
/**
|
|
2923
|
-
* Matrix trace (sum of diagonal elements)
|
|
2924
|
-
*/
|
|
2925
|
-
declare const parallelMatrixTrace: typed_function.TypedFunction;
|
|
2926
|
-
/**
|
|
2927
|
-
* Parallel Euclidean distance
|
|
2928
|
-
*/
|
|
2929
|
-
declare const parallelMatrixDistance: typed_function.TypedFunction;
|
|
2930
|
-
/**
|
|
2931
|
-
* Parallel element-wise absolute value
|
|
2932
|
-
*/
|
|
2933
|
-
declare const parallelMatrixAbs: typed_function.TypedFunction;
|
|
2934
|
-
/**
|
|
2935
|
-
* Parallel element-wise square root
|
|
2936
|
-
*/
|
|
2937
|
-
declare const parallelMatrixSqrt: typed_function.TypedFunction;
|
|
2938
|
-
/**
|
|
2939
|
-
* Parallel element-wise square
|
|
2940
|
-
*/
|
|
2941
|
-
declare const parallelMatrixSquare: typed_function.TypedFunction;
|
|
2942
|
-
/**
|
|
2943
|
-
* Parallel element-wise exponential
|
|
2944
|
-
*/
|
|
2945
|
-
declare const parallelMatrixExp: typed_function.TypedFunction;
|
|
2946
|
-
/**
|
|
2947
|
-
* Parallel element-wise natural logarithm
|
|
2948
|
-
*/
|
|
2949
|
-
declare const parallelMatrixLog: typed_function.TypedFunction;
|
|
2950
|
-
/**
|
|
2951
|
-
* Parallel element-wise sine
|
|
2952
|
-
*/
|
|
2953
|
-
declare const parallelMatrixSin: typed_function.TypedFunction;
|
|
2954
|
-
/**
|
|
2955
|
-
* Parallel element-wise cosine
|
|
2956
|
-
*/
|
|
2957
|
-
declare const parallelMatrixCos: typed_function.TypedFunction;
|
|
2958
|
-
/**
|
|
2959
|
-
* Parallel element-wise tangent
|
|
2960
|
-
*/
|
|
2961
|
-
declare const parallelMatrixTan: typed_function.TypedFunction;
|
|
2962
|
-
/**
|
|
2963
|
-
* Get matrix dimensions
|
|
2964
|
-
*/
|
|
2965
|
-
declare const parallelMatrixSize: typed_function.TypedFunction;
|
|
2966
|
-
/**
|
|
2967
|
-
* Get element at position
|
|
2968
|
-
*/
|
|
2969
|
-
declare const parallelMatrixSubset: typed_function.TypedFunction;
|
|
2970
|
-
/**
|
|
2971
|
-
* Get row from matrix
|
|
2972
|
-
*/
|
|
2973
|
-
declare const parallelMatrixRow: typed_function.TypedFunction;
|
|
2974
|
-
/**
|
|
2975
|
-
* Get column from matrix
|
|
2976
|
-
*/
|
|
2977
|
-
declare const parallelMatrixColumn: typed_function.TypedFunction;
|
|
2978
|
-
/**
|
|
2979
|
-
* Get diagonal from matrix
|
|
2980
|
-
*/
|
|
2981
|
-
declare const parallelMatrixDiagonal: typed_function.TypedFunction;
|
|
2982
|
-
/**
|
|
2983
|
-
* Parallel matrix-vector multiplication
|
|
2984
|
-
*/
|
|
2985
|
-
declare const parallelMatrixMatvec: typed_function.TypedFunction;
|
|
2986
|
-
/**
|
|
2987
|
-
* Parallel outer product
|
|
2988
|
-
*/
|
|
2989
|
-
declare const parallelMatrixOuter: typed_function.TypedFunction;
|
|
2990
|
-
/**
|
|
2991
|
-
* Parallel histogram computation
|
|
2992
|
-
*/
|
|
2993
|
-
declare const parallelMatrixHistogram: typed_function.TypedFunction;
|
|
2994
|
-
declare const parallelMatrixOperations: {
|
|
2995
|
-
matrix: typed_function.TypedFunction;
|
|
2996
|
-
identity: typed_function.TypedFunction;
|
|
2997
|
-
zeros: typed_function.TypedFunction;
|
|
2998
|
-
ones: typed_function.TypedFunction;
|
|
2999
|
-
diag: typed_function.TypedFunction;
|
|
3000
|
-
random: typed_function.TypedFunction;
|
|
3001
|
-
add: typed_function.TypedFunction;
|
|
3002
|
-
subtract: typed_function.TypedFunction;
|
|
3003
|
-
multiply: typed_function.TypedFunction;
|
|
3004
|
-
dotMultiply: typed_function.TypedFunction;
|
|
3005
|
-
divide: typed_function.TypedFunction;
|
|
3006
|
-
unaryMinus: typed_function.TypedFunction;
|
|
3007
|
-
transpose: typed_function.TypedFunction;
|
|
3008
|
-
sum: typed_function.TypedFunction;
|
|
3009
|
-
mean: typed_function.TypedFunction;
|
|
3010
|
-
min: typed_function.TypedFunction;
|
|
3011
|
-
max: typed_function.TypedFunction;
|
|
3012
|
-
variance: typed_function.TypedFunction;
|
|
3013
|
-
std: typed_function.TypedFunction;
|
|
3014
|
-
norm: typed_function.TypedFunction;
|
|
3015
|
-
dot: typed_function.TypedFunction;
|
|
3016
|
-
trace: typed_function.TypedFunction;
|
|
3017
|
-
distance: typed_function.TypedFunction;
|
|
3018
|
-
abs: typed_function.TypedFunction;
|
|
3019
|
-
sqrt: typed_function.TypedFunction;
|
|
3020
|
-
square: typed_function.TypedFunction;
|
|
3021
|
-
exp: typed_function.TypedFunction;
|
|
3022
|
-
log: typed_function.TypedFunction;
|
|
3023
|
-
sin: typed_function.TypedFunction;
|
|
3024
|
-
cos: typed_function.TypedFunction;
|
|
3025
|
-
tan: typed_function.TypedFunction;
|
|
3026
|
-
size: typed_function.TypedFunction;
|
|
3027
|
-
subset: typed_function.TypedFunction;
|
|
3028
|
-
row: typed_function.TypedFunction;
|
|
3029
|
-
column: typed_function.TypedFunction;
|
|
3030
|
-
diagonal: typed_function.TypedFunction;
|
|
3031
|
-
matvec: typed_function.TypedFunction;
|
|
3032
|
-
outer: typed_function.TypedFunction;
|
|
3033
|
-
histogram: typed_function.TypedFunction;
|
|
3034
|
-
};
|
|
3035
|
-
/**
|
|
3036
|
-
* Initialize parallel matrix operations
|
|
3037
|
-
*/
|
|
3038
|
-
declare function initializeParallelMatrix(): Promise<void>;
|
|
3039
|
-
/**
|
|
3040
|
-
* Terminate parallel matrix operations pool
|
|
3041
|
-
*/
|
|
3042
|
-
declare function terminateParallelMatrix(): Promise<void>;
|
|
3043
|
-
|
|
3044
|
-
export { BUILTIN_SHADERS, type BackendHints, BackendManager, BackendRegistry, type BackendType, BatchExecutor, type CholeskyResult, DEFAULT_BACKEND_HINTS, DEFAULT_EXTENDED_HINTS, DenseMatrix, type EigOptions, type EigResult, type ExpmOptions, type ExtendedBackendHints, GPUBackend, type GPUBackendOptions, type GPUBackendStatus, GPUMatrixBackend, type GPUMatrixBackendConfig, JSBackend, type LQResult, type LUResult, type LogmOptions, Matrix, type MatrixBackend, type MatrixEntry, type MatrixIndex, type MatrixType, type OperationType, ParallelBackend, type ParallelBackendConfig, type PinvOptions, type QLResult, type QROptions, type QRPivotedOptions, type QRPivotedResult, type QRResult, type RQResult, type SVDOptions, type SVDResult, type SchurOptions, type SchurResult, type SliceSpec, SparseMatrix, type SqrtmOptions, type SyncConfig, SyncManager, type SyncStrategy, WASMBackend, type WASMBackendConfig, type WasmFeatures, abs, add, backendManager, backendRegistry, cholesky, clearFeatureCache, column, cond, condest, createBackendManager, createGPUMatrixBackend, createParallelBackend, createSyncManager, createWASMBackend, destroyGlobalGPUBackend, detectWasmFeatures, diag, diagonal, divide, dotMultiply, eig, eigWasm, eigvals, eigvalsWasm, exp, getCachedFeatures, getGlobalGPUBackend, gpuMatrixBackend, identity, initializeGlobalGPUBackend, initializeParallelMatrix, isAtomicsAvailable, isDenseMatrix, isMatrix, isSharedMemoryAvailable, isSparseMatrix, isWasmAvailable, jsBackend, log, lowRankApprox, lq, lu, luSolve, matrix, matrixExpm, matrixLogm, pinv as matrixPinv, matrixSchur, matrixSqrtm, max, mean, min, multiply, norm, norm2, normFro, ones, parallelBackend, parallelDiag, parallelDotMultiply, parallelIdentity, parallelMatrix, parallelMatrixAbs, parallelMatrixAdd, parallelMatrixColumn, parallelMatrixCos, parallelMatrixDiagonal, parallelMatrixDistance, parallelMatrixDivide, parallelMatrixDot, parallelMatrixExp, parallelMatrixHistogram, parallelMatrixLog, parallelMatrixMatvec, parallelMatrixMax, parallelMatrixMean, parallelMatrixMin, parallelMatrixMultiply, parallelMatrixNorm, parallelMatrixOperations, parallelMatrixOuter, parallelMatrixRow, parallelMatrixSin, parallelMatrixSize, parallelMatrixSqrt, parallelMatrixSquare, parallelMatrixStd, parallelMatrixSubset, parallelMatrixSubtract, parallelMatrixSum, parallelMatrixTan, parallelMatrixTrace, parallelMatrixTranspose, parallelMatrixVariance, parallelOnes, parallelRandom, parallelUnaryMinus, parallelZeros, pinv$1 as pinv, pow, powerIteration, ql, qr, qrPivoted, random, row, rq, singularValues, size, spectralRadiusWasm, sqrt, square, subset, subtract, sum, svd, svdWasm, terminateParallelMatrix, trace, transpose, typedMatrixOperations, unaryMinus, wasmBackend, zeros };
|
|
16
|
+
export * from './types/index.js';
|
|
17
|
+
export * from './backends/index.js';
|
|
18
|
+
export * from './operations/index.js';
|
|
19
|
+
export * from './typed-operations.js';
|
|
20
|
+
export * from './parallel-matrix.js';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|