sublinear 0.2.0

High-performance sublinear-time solver for asymmetric diagonally dominant systems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
 * Optimized matrix operations with memory pooling and SIMD-friendly patterns
 * Target: 50% memory reduction and improved cache locality
 */
// Memory pool for vector allocations
class VectorPool {
    pools = new Map();
    maxPoolSize = 100;
    acquire(size) {
        const pool = this.pools.get(size);
        if (pool && pool.length > 0) {
            return pool.pop();
        }
        return new Array(size);
    }
    release(vector) {
        const size = vector.length;
        vector.fill(0); // Clear for reuse
        let pool = this.pools.get(size);
        if (!pool) {
            pool = [];
            this.pools.set(size, pool);
        }
        if (pool.length < this.maxPoolSize) {
            pool.push(vector);
        }
    }
    clear() {
        this.pools.clear();
    }
    getStats() {
        const poolSizes = {};
        let totalVectors = 0;
        for (const [size, pool] of this.pools) {
            poolSizes[size] = pool.length;
            totalVectors += pool.length;
        }
        return { poolSizes, totalVectors };
    }
}
// Compressed Sparse Row (CSR) format for JavaScript
export class CSRMatrix {
    values;
    colIndices;
    rowPtr;
    rows;
    cols;
    constructor(rows, cols, nnz) {
        this.rows = rows;
        this.cols = cols;
        this.values = new Float64Array(nnz);
        this.colIndices = new Uint32Array(nnz);
        this.rowPtr = new Uint32Array(rows + 1);
    }
    static fromCOO(matrix) {
        const { values, rowIndices, colIndices } = matrix;
        const nnz = values.length;
        const csr = new CSRMatrix(matrix.rows, matrix.cols, nnz);
        // Sort by row, then column
        const triplets = Array.from({ length: nnz }, (_, i) => ({
            row: rowIndices[i],
            col: colIndices[i],
            val: values[i],
            index: i
        }));
        triplets.sort((a, b) => a.row - b.row || a.col - b.col);
        // Build CSR structure
        let currentRow = 0;
        let nnzCount = 0;
        for (const triplet of triplets) {
            // Skip zeros
            if (triplet.val === 0)
                continue;
            // Update row pointers
            while (currentRow < triplet.row) {
                csr.rowPtr[++currentRow] = nnzCount;
            }
            csr.values[nnzCount] = triplet.val;
            csr.colIndices[nnzCount] = triplet.col;
            nnzCount++;
        }
        // Finalize row pointers
        while (currentRow < matrix.rows) {
            csr.rowPtr[++currentRow] = nnzCount;
        }
        return csr;
    }
    // Cache-friendly matrix-vector multiplication with SIMD hints
    multiplyVector(x, result) {
        result.fill(0);
        // Process 4 rows at a time for better cache locality
        const blockSize = 4;
        let rowBlock = 0;
        while (rowBlock < this.rows) {
            const endBlock = Math.min(rowBlock + blockSize, this.rows);
            for (let row = rowBlock; row < endBlock; row++) {
                const start = this.rowPtr[row];
                const end = this.rowPtr[row + 1];
                let sum = 0;
                // Unroll loop for SIMD optimization hints
                let i = start;
                for (; i < end - 3; i += 4) {
                    sum += this.values[i] * x[this.colIndices[i]] +
                        this.values[i + 1] * x[this.colIndices[i + 1]] +
                        this.values[i + 2] * x[this.colIndices[i + 2]] +
                        this.values[i + 3] * x[this.colIndices[i + 3]];
                }
                // Handle remaining elements
                for (; i < end; i++) {
                    sum += this.values[i] * x[this.colIndices[i]];
                }
                result[row] = sum;
            }
            rowBlock = endBlock;
        }
    }
    getEntry(row, col) {
        const start = this.rowPtr[row];
        const end = this.rowPtr[row + 1];
        // Binary search for column
        let left = start;
        let right = end - 1;
        while (left <= right) {
            const mid = Math.floor((left + right) / 2);
            const midCol = this.colIndices[mid];
            if (midCol === col) {
                return this.values[mid];
            }
            else if (midCol < col) {
                left = mid + 1;
            }
            else {
                right = mid - 1;
            }
        }
        return 0;
    }
    // Memory-efficient row iteration
    *rowEntries(row) {
        const start = this.rowPtr[row];
        const end = this.rowPtr[row + 1];
        for (let i = start; i < end; i++) {
            yield { col: this.colIndices[i], val: this.values[i] };
        }
    }
    getMemoryUsage() {
        return this.values.byteLength +
            this.colIndices.byteLength +
            this.rowPtr.byteLength;
    }
    getNnz() {
        return this.values.length;
    }
    getRows() {
        return this.rows;
    }
    getCols() {
        return this.cols;
    }
}
// Compressed Sparse Column (CSC) format for column-wise operations
export class CSCMatrix {
    values;
    rowIndices;
    colPtr;
    rows;
    cols;
    constructor(rows, cols, nnz) {
        this.rows = rows;
        this.cols = cols;
        this.values = new Float64Array(nnz);
        this.rowIndices = new Uint32Array(nnz);
        this.colPtr = new Uint32Array(cols + 1);
    }
    static fromCSR(csr) {
        const nnz = csr.getNnz();
        const csc = new CSCMatrix(csr.getRows(), csr.getCols(), nnz);
        // Convert CSR to triplets, then sort by column
        const triplets = [];
        for (let row = 0; row < csr.getRows(); row++) {
            for (const entry of csr.rowEntries(row)) {
                triplets.push({ row, col: entry.col, val: entry.val });
            }
        }
        triplets.sort((a, b) => a.col - b.col || a.row - b.row);
        // Build CSC structure
        let currentCol = 0;
        let nnzCount = 0;
        for (const triplet of triplets) {
            while (currentCol < triplet.col) {
                csc.colPtr[++currentCol] = nnzCount;
            }
            csc.values[nnzCount] = triplet.val;
            csc.rowIndices[nnzCount] = triplet.row;
            nnzCount++;
        }
        while (currentCol < csc.cols) {
            csc.colPtr[++currentCol] = nnzCount;
        }
        return csc;
    }
    // Column-wise matrix-vector multiplication
    multiplyVector(x, result) {
        result.fill(0);
        for (let col = 0; col < this.cols; col++) {
            const xCol = x[col];
            if (xCol === 0)
                continue;
            const start = this.colPtr[col];
            const end = this.colPtr[col + 1];
            // Vectorized accumulation
            for (let i = start; i < end; i++) {
                result[this.rowIndices[i]] += this.values[i] * xCol;
            }
        }
    }
    getMemoryUsage() {
        return this.values.byteLength +
            this.rowIndices.byteLength +
            this.colPtr.byteLength;
    }
    getNnz() {
        return this.values.length;
    }
    getRows() {
        return this.rows;
    }
    getCols() {
        return this.cols;
    }
}
// Memory streaming for large matrices
export class StreamingMatrix {
    chunks = new Map();
    chunkSize;
    rows;
    cols;
    maxCachedChunks;
    constructor(rows, cols, chunkSize = 1000, maxCachedChunks = 10) {
        this.rows = rows;
        this.cols = cols;
        this.chunkSize = chunkSize;
        this.maxCachedChunks = maxCachedChunks;
    }
    static fromMatrix(matrix, chunkSize = 1000) {
        const streaming = new StreamingMatrix(matrix.rows, matrix.cols, chunkSize);
        if (matrix.format === 'coo') {
            const sparse = matrix;
            const chunkData = new Map();
            for (let i = 0; i < sparse.values.length; i++) {
                const row = sparse.rowIndices[i];
                const chunkId = Math.floor(row / chunkSize);
                if (!chunkData.has(chunkId)) {
                    chunkData.set(chunkId, []);
                }
                chunkData.get(chunkId).push({
                    col: sparse.colIndices[i],
                    val: sparse.values[i]
                });
            }
            // Convert each chunk to CSR
            for (const [chunkId, entries] of chunkData) {
                const chunkRows = Math.min(chunkSize, streaming.rows - chunkId * chunkSize);
                const chunkCSR = new CSRMatrix(chunkRows, streaming.cols, entries.length);
                // Build CSR for this chunk
                const rowData = new Map();
                for (const entry of entries) {
                    const localRow = (chunkId * chunkSize) % chunkSize;
                    if (!rowData.has(localRow)) {
                        rowData.set(localRow, []);
                    }
                    rowData.get(localRow).push(entry);
                }
                // Fill CSR arrays
                let nnzCount = 0;
                for (let row = 0; row < chunkRows; row++) {
                    chunkCSR.rowPtr[row] = nnzCount;
                    const rowEntries = rowData.get(row) || [];
                    rowEntries.sort((a, b) => a.col - b.col);
                    for (const entry of rowEntries) {
                        chunkCSR.values[nnzCount] = entry.val;
                        chunkCSR.colIndices[nnzCount] = entry.col;
                        nnzCount++;
                    }
                }
                chunkCSR.rowPtr[chunkRows] = nnzCount;
                streaming.chunks.set(chunkId, chunkCSR);
            }
        }
        return streaming;
    }
    getChunk(chunkId) {
        return this.chunks.get(chunkId) || null;
    }
    // Streaming matrix-vector multiplication
    multiplyVector(x, result) {
        result.fill(0);
        const totalChunks = Math.ceil(this.rows / this.chunkSize);
        for (let chunkId = 0; chunkId < totalChunks; chunkId++) {
            const chunk = this.getChunk(chunkId);
            if (!chunk)
                continue;
            const startRow = chunkId * this.chunkSize;
            const chunkResult = new Array(chunk.getRows()).fill(0);
            chunk.multiplyVector(x, chunkResult);
            // Copy back to result
            for (let i = 0; i < chunkResult.length && startRow + i < this.rows; i++) {
                result[startRow + i] = chunkResult[i];
            }
            // Memory management: remove old chunks if cache is full
            if (this.chunks.size > this.maxCachedChunks) {
                const oldestChunk = Math.max(0, chunkId - this.maxCachedChunks);
                this.chunks.delete(oldestChunk);
            }
        }
    }
    getMemoryUsage() {
        let total = 0;
        for (const chunk of this.chunks.values()) {
            total += chunk.getMemoryUsage();
        }
        return total;
    }
}
// Optimized matrix operations with memory pooling
export class OptimizedMatrixOperations {
    static vectorPool = new VectorPool();
    static getVectorPool() {
        return this.vectorPool;
    }
    // SIMD-optimized vector operations
    static vectorAdd(a, b, result) {
        const n = a.length;
        const out = result || this.vectorPool.acquire(n);
        // Process 4 elements at a time for SIMD
        let i = 0;
        for (; i < n - 3; i += 4) {
            out[i] = a[i] + b[i];
            out[i + 1] = a[i + 1] + b[i + 1];
            out[i + 2] = a[i + 2] + b[i + 2];
            out[i + 3] = a[i + 3] + b[i + 3];
        }
        // Handle remaining elements
        for (; i < n; i++) {
            out[i] = a[i] + b[i];
        }
        return out;
    }
    static vectorScale(vector, scalar, result) {
        const n = vector.length;
        const out = result || this.vectorPool.acquire(n);
        // SIMD-friendly unrolled loop
        let i = 0;
        for (; i < n - 3; i += 4) {
            out[i] = vector[i] * scalar;
            out[i + 1] = vector[i + 1] * scalar;
            out[i + 2] = vector[i + 2] * scalar;
            out[i + 3] = vector[i + 3] * scalar;
        }
        for (; i < n; i++) {
            out[i] = vector[i] * scalar;
        }
        return out;
    }
    static vectorDot(a, b) {
        const n = a.length;
        let sum = 0;
        // Unrolled loop for SIMD optimization
        let i = 0;
        for (; i < n - 3; i += 4) {
            sum += a[i] * b[i] +
                a[i + 1] * b[i + 1] +
                a[i + 2] * b[i + 2] +
                a[i + 3] * b[i + 3];
        }
        for (; i < n; i++) {
            sum += a[i] * b[i];
        }
        return sum;
    }
    static vectorNorm2(vector) {
        return Math.sqrt(this.vectorDot(vector, vector));
    }
    // Memory-efficient matrix format conversion
    static convertToOptimalFormat(matrix) {
        if (matrix.format === 'coo') {
            const sparse = matrix;
            // Choose format based on sparsity pattern and expected access
            const sparsity = sparse.values.length / (matrix.rows * matrix.cols);
            // CSR is generally better for row-wise access and matrix-vector multiplication
            return CSRMatrix.fromCOO(sparse);
        }
        else {
            // Convert dense to sparse first
            const sparse = this.denseToSparse(matrix);
            return CSRMatrix.fromCOO(sparse);
        }
    }
    static denseToSparse(dense, tolerance = 1e-15) {
        const values = [];
        const rowIndices = [];
        const colIndices = [];
        for (let i = 0; i < dense.rows; i++) {
            for (let j = 0; j < dense.cols; j++) {
                const value = dense.data[i][j];
                if (Math.abs(value) > tolerance) {
                    values.push(value);
                    rowIndices.push(i);
                    colIndices.push(j);
                }
            }
        }
        return {
            rows: dense.rows,
            cols: dense.cols,
            values,
            rowIndices,
            colIndices,
            format: 'coo'
        };
    }
    // Memory usage profiling
    static profileMemoryUsage(matrix) {
        const memoryUsed = matrix.getMemoryUsage();
        let nnz;
        let rows;
        let cols;
        if (matrix instanceof CSRMatrix || matrix instanceof CSCMatrix) {
            nnz = matrix.getNnz();
            rows = matrix.getRows();
            cols = matrix.getCols();
        }
        else {
            nnz = 0;
            rows = matrix['rows'];
            cols = matrix['cols'];
        }
        const denseMemory = rows * cols * 8; // 8 bytes per double
        const compressionRatio = denseMemory / memoryUsed;
        return {
            matrixSize: rows * cols,
            nnz,
            memoryUsed,
            compressionRatio
        };
    }
    // Cleanup memory pools
    static cleanup() {
        this.vectorPool.clear();
    }
}