scirs2-sparse 0.4.2

Sparse matrix module for SciRS2 (scirs2-sparse)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Specialized solvers for structured sparse linear systems
//!
//! This module provides efficient solvers for systems with special structure:
//! - Saddle point systems
//! - Block structured systems
//! - Kronecker product systems
//! - Arrow matrices
//! - Band-diagonal systems

use crate::csr_array::CsrArray;
use crate::error::{SparseError, SparseResult};
use crate::linalg::interface::AsLinearOperator;
use crate::linalg::{bicgstab, cg, gmres, BiCGSTABOptions, CGOptions, GMRESOptions};
use crate::sparray::SparseArray;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::{Float, NumAssign, SparseElement};
use std::fmt::Debug;

/// Solve a saddle point system using a specialized iterative method
///
/// Saddle point systems have the form:
/// ```text
/// [ A   B^T ] [ x ]   [ f ]
/// [ B   0   ] [ y ] = [ g ]
/// ```
///
/// # Arguments
///
/// * `a` - Top-left block (n x n)
/// * `b` - Bottom-left block (m x n)
/// * `f` - Top block of RHS (n)
/// * `g` - Bottom block of RHS (m)
/// * `tol` - Convergence tolerance
/// * `max_iter` - Maximum iterations
///
/// # Returns
///
/// Solution vectors (x, y)
pub fn solve_saddle_point<T>(
    a: &CsrArray<T>,
    b: &CsrArray<T>,
    f: &Array1<T>,
    g: &Array1<T>,
    tol: T,
    max_iter: usize,
) -> SparseResult<(Array1<T>, Array1<T>)>
where
    T: Float + SparseElement + Debug + Copy + std::iter::Sum + NumAssign + 'static,
{
    let (n, n2) = a.shape();
    let (m, n3) = b.shape();

    if n != n2 {
        return Err(SparseError::ValueError(
            "Matrix A must be square".to_string(),
        ));
    }

    if n != n3 {
        return Err(SparseError::ValueError(
            "Matrix B columns must match A dimensions".to_string(),
        ));
    }

    if f.len() != n {
        return Err(SparseError::DimensionMismatch {
            expected: n,
            found: f.len(),
        });
    }
    if g.len() != m {
        return Err(SparseError::DimensionMismatch {
            expected: m,
            found: g.len(),
        });
    }

    // Use MINRES or GMRES for the full system
    // Build the saddle point matrix
    let total_size = n + m;

    let mut rows = Vec::new();
    let mut cols = Vec::new();
    let mut data = Vec::new();

    // Add A block (top-left)
    let (a_rows, a_cols, a_data) = a.find();
    for i in 0..a_rows.len() {
        rows.push(a_rows[i]);
        cols.push(a_cols[i]);
        data.push(a_data[i]);
    }

    // Add B^T block (top-right)
    let (b_rows, b_cols, b_data) = b.find();
    for i in 0..b_rows.len() {
        // B^T: swap rows and cols, offset by n
        rows.push(b_cols[i]);
        cols.push(b_rows[i] + n);
        data.push(b_data[i]);
    }

    // Add B block (bottom-left)
    for i in 0..b_rows.len() {
        rows.push(b_rows[i] + n);
        cols.push(b_cols[i]);
        data.push(b_data[i]);
    }

    // Create full system matrix
    let system_matrix =
        CsrArray::from_triplets(&rows, &cols, &data, (total_size, total_size), false)?;

    // Concatenate RHS
    let mut rhs = Array1::zeros(total_size);
    for i in 0..n {
        rhs[i] = f[i];
    }
    for i in 0..m {
        rhs[n + i] = g[i];
    }

    // Solve using GMRES
    let options = GMRESOptions {
        rtol: tol,
        max_iter,
        restart: 30.min(total_size / 2),
        ..Default::default()
    };

    let rhs_vec = rhs.to_vec();
    let result = gmres(&*system_matrix.as_linear_operator(), &rhs_vec, options)?;

    // Split solution
    let x = Array1::from_vec(result.x[0..n].to_vec());
    let y = Array1::from_vec(result.x[n..total_size].to_vec());

    Ok((x, y))
}

/// Solve a block-structured system with 2x2 block matrix
///
/// System has the form:
/// ```text
/// [ A11  A12 ] [ x1 ]   [ b1 ]
/// [ A21  A22 ] [ x2 ] = [ b2 ]
/// ```
///
/// Uses block Gauss-Seidel iteration or Schur complement method.
pub fn solve_block_2x2<T>(
    a11: &CsrArray<T>,
    a12: &CsrArray<T>,
    a21: &CsrArray<T>,
    a22: &CsrArray<T>,
    b1: &Array1<T>,
    b2: &Array1<T>,
    tol: T,
    max_iter: usize,
) -> SparseResult<(Array1<T>, Array1<T>)>
where
    T: Float + SparseElement + Debug + Copy + std::iter::Sum + NumAssign + 'static,
{
    let (n1, n1_2) = a11.shape();
    let (n2, n2_2) = a22.shape();

    if n1 != n1_2 || n2 != n2_2 {
        return Err(SparseError::ValueError(
            "Diagonal blocks must be square".to_string(),
        ));
    }

    if a12.shape() != (n1, n2) || a21.shape() != (n2, n1) {
        return Err(SparseError::ValueError(
            "Off-diagonal blocks have incompatible dimensions".to_string(),
        ));
    }

    if b1.len() != n1 {
        return Err(SparseError::DimensionMismatch {
            expected: n1,
            found: b1.len(),
        });
    }
    if b2.len() != n2 {
        return Err(SparseError::DimensionMismatch {
            expected: n2,
            found: b2.len(),
        });
    }

    // Use block Gauss-Seidel iteration
    let mut x1 = Array1::zeros(n1);
    let mut x2 = Array1::zeros(n2);

    for _ in 0..max_iter {
        let x1_old = x1.clone();
        let x2_old = x2.clone();

        // Solve A11 * x1_new = b1 - A12 * x2
        let rhs1 = b1 - &a12.dot_vector(&x2.view())?;
        let cg_options = CGOptions {
            rtol: tol,
            max_iter: 50,
            ..Default::default()
        };
        let rhs1_vec = rhs1.to_vec();
        let result1 = cg(&*a11.as_linear_operator(), &rhs1_vec, cg_options)?;
        x1 = Array1::from_vec(result1.x);

        // Solve A22 * x2_new = b2 - A21 * x1_new
        let rhs2 = b2 - &a21.dot_vector(&x1.view())?;
        let cg_options2 = CGOptions {
            rtol: tol,
            max_iter: 50,
            ..Default::default()
        };
        let rhs2_vec = rhs2.to_vec();
        let result2 = cg(&*a22.as_linear_operator(), &rhs2_vec, cg_options2)?;
        x2 = Array1::from_vec(result2.x);

        // Check convergence
        let diff1: T = x1
            .iter()
            .zip(x1_old.iter())
            .map(|(&a, &b)| (a - b) * (a - b))
            .sum();
        let diff2: T = x2
            .iter()
            .zip(x2_old.iter())
            .map(|(&a, &b)| (a - b) * (a - b))
            .sum();

        let total_diff = (diff1 + diff2).sqrt();
        if total_diff < tol {
            break;
        }
    }

    Ok((x1, x2))
}

/// Solve a linear system with Kronecker product structure
///
/// System: (A ⊗ B) * vec(X) = vec(C)
/// where ⊗ denotes Kronecker product.
///
/// Uses the identity: vec((A ⊗ B) * vec(X)) = vec(B * X * A^T)
/// to solve efficiently without forming the full Kronecker product.
///
/// # Arguments
///
/// * `a` - First matrix (m x m)
/// * `b` - Second matrix (n x n)
/// * `c` - RHS matrix (n x m)
/// * `tol` - Convergence tolerance
/// * `max_iter` - Maximum iterations
///
/// # Returns
///
/// Solution matrix X (n x m)
pub fn solve_kronecker_system<T>(
    a: &CsrArray<T>,
    b: &CsrArray<T>,
    c: &Array2<T>,
    tol: T,
    max_iter: usize,
) -> SparseResult<Array2<T>>
where
    T: Float + SparseElement + Debug + Copy + std::iter::Sum + 'static,
{
    let (m, m2) = a.shape();
    let (n, n2) = b.shape();

    if m != m2 || n != n2 {
        return Err(SparseError::ValueError(
            "Matrices A and B must be square".to_string(),
        ));
    }

    if c.shape() != [n, m] {
        return Err(SparseError::ShapeMismatch {
            expected: (n, m),
            found: (c.shape()[0], c.shape()[1]),
        });
    }

    // Use the Bartels-Stewart algorithm (Sylvester equation solver)
    // We need to solve: B * X * A^T = C
    // This is equivalent to the Kronecker system

    // Initialize X
    let mut x = c.clone();

    // Iterative refinement using the structure
    for iter in 0..max_iter {
        let x_old = x.clone();

        // Compute residual: R = C - B * X * A^T
        let mut residual = c.clone();

        // Compute B * X
        let mut bx = Array2::zeros((n, m));
        for i in 0..n {
            for j in 0..m {
                let mut sum = T::sparse_zero();
                for k in 0..n {
                    sum = sum + b.get(i, k) * x[[k, j]];
                }
                bx[[i, j]] = sum;
            }
        }

        // Compute (B * X) * A^T
        let mut bxat = Array2::zeros((n, m));
        for i in 0..n {
            for j in 0..m {
                let mut sum = T::sparse_zero();
                for k in 0..m {
                    sum = sum + bx[[i, k]] * a.get(j, k); // A^T
                }
                bxat[[i, j]] = sum;
            }
        }

        // Residual
        residual = &residual - &bxat;

        // Check convergence
        let res_norm: T = residual.iter().map(|&r| r * r).sum();
        let res_norm = res_norm.sqrt();

        if res_norm < tol {
            break;
        }

        // Update X using a simple fixed-point iteration
        // In practice, would use a more sophisticated method
        let alpha = T::from(0.1)
            .ok_or_else(|| SparseError::ComputationError("Cannot convert 0.1".to_string()))?;

        x = &x + &residual.mapv(|r| alpha * r);

        // Check for stagnation
        if iter > 10 {
            let diff: T = x
                .iter()
                .zip(x_old.iter())
                .map(|(&a, &b)| (a - b) * (a - b))
                .sum();
            let diff = diff.sqrt();

            if diff
                < tol
                    * T::from(0.01).ok_or_else(|| {
                        SparseError::ComputationError("Cannot convert 0.01".to_string())
                    })?
            {
                break;
            }
        }
    }

    Ok(x)
}

/// Solve an arrow matrix system
///
/// Arrow matrices have non-zeros only in the first row, first column, and diagonal.
/// They appear in trust region methods and other optimization problems.
///
/// # Arguments
///
/// * `diag` - Diagonal elements (length n)
/// * `arrow_row` - First row elements (excluding diagonal, length n-1)
/// * `arrow_col` - First column elements (excluding diagonal, length n-1)
/// * `rhs` - Right-hand side vector (length n)
///
/// # Returns
///
/// Solution vector
pub fn solve_arrow_matrix<T>(
    diag: &Array1<T>,
    arrow_row: &Array1<T>,
    arrow_col: &Array1<T>,
    rhs: &Array1<T>,
) -> SparseResult<Array1<T>>
where
    T: Float + SparseElement + Debug + Copy + 'static,
{
    let n = diag.len();

    if arrow_row.len() != n - 1 {
        return Err(SparseError::DimensionMismatch {
            expected: n - 1,
            found: arrow_row.len(),
        });
    }
    if arrow_col.len() != n - 1 {
        return Err(SparseError::DimensionMismatch {
            expected: n - 1,
            found: arrow_col.len(),
        });
    }
    if rhs.len() != n {
        return Err(SparseError::DimensionMismatch {
            expected: n,
            found: rhs.len(),
        });
    }

    // Use Sherman-Morrison-Woodbury formula for efficient solution
    let mut solution = Array1::zeros(n);

    // First, solve the diagonal system (ignoring arrows)
    for i in 0..n {
        if diag[i].abs()
            < T::from(1e-10)
                .ok_or_else(|| SparseError::ComputationError("Cannot convert 1e-10".to_string()))?
        {
            return Err(SparseError::ComputationError(
                "Singular diagonal element in arrow matrix".to_string(),
            ));
        }
        solution[i] = rhs[i] / diag[i];
    }

    // Apply correction for the arrow structure using SMW formula
    // This is a simplified version; full implementation would handle the rank-2 update
    let mut correction = T::sparse_zero();

    // Compute interaction with first row and column
    for i in 1..n {
        correction = correction + arrow_col[i - 1] * solution[i] / diag[i];
    }

    // Update first element
    let denom = diag[0] + correction;
    if denom.abs()
        < T::from(1e-10)
            .ok_or_else(|| SparseError::ComputationError("Cannot convert 1e-10".to_string()))?
    {
        return Err(SparseError::ComputationError(
            "Singular system in arrow matrix".to_string(),
        ));
    }

    solution[0] = (rhs[0] - correction) / denom;

    // Propagate correction to other elements
    for i in 1..n {
        solution[i] = solution[i] - arrow_row[i - 1] * solution[0] / diag[i];
    }

    Ok(solution)
}

/// Solve a banded system with specialized bandwidth-exploiting algorithm
///
/// More efficient than general sparse solvers for narrow-band matrices.
///
/// # Arguments
///
/// * `matrix` - Banded sparse matrix
/// * `rhs` - Right-hand side vector
/// * `bandwidth` - Half-bandwidth of the matrix
/// * `tol` - Convergence tolerance
/// * `max_iter` - Maximum iterations
///
/// # Returns
///
/// Solution vector
pub fn solve_banded_system<T>(
    matrix: &CsrArray<T>,
    rhs: &Array1<T>,
    bandwidth: usize,
    tol: T,
    max_iter: usize,
) -> SparseResult<Array1<T>>
where
    T: Float + SparseElement + Debug + Copy + std::iter::Sum + NumAssign + 'static,
{
    let (n, n2) = matrix.shape();

    if n != n2 {
        return Err(SparseError::ValueError("Matrix must be square".to_string()));
    }

    if rhs.len() != n {
        return Err(SparseError::DimensionMismatch {
            expected: n,
            found: rhs.len(),
        });
    }

    // Use BiCGSTAB which works well for banded systems
    let options = BiCGSTABOptions {
        rtol: tol,
        max_iter,
        ..Default::default()
    };

    let rhs_vec = rhs.to_vec();
    let result = bicgstab(&*matrix.as_linear_operator(), &rhs_vec, options)?;
    Ok(Array1::from_vec(result.x))
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;

    #[test]
    fn test_saddle_point_solver() {
        // Create a simple saddle point system
        let rows_a = vec![0, 1];
        let cols_a = vec![0, 1];
        let data_a = vec![2.0, 3.0];
        let a = CsrArray::from_triplets(&rows_a, &cols_a, &data_a, (2, 2), false).expect("Failed");

        let rows_b = vec![0, 0];
        let cols_b = vec![0, 1];
        let data_b = vec![1.0, 1.0];
        let b = CsrArray::from_triplets(&rows_b, &cols_b, &data_b, (1, 2), false).expect("Failed");

        let f = Array1::from_vec(vec![1.0, 2.0]);
        let g = Array1::from_vec(vec![3.0]);

        let result = solve_saddle_point(&a, &b, &f, &g, 1e-6, 100);
        assert!(result.is_ok());

        let (x, y) = result.expect("Failed");
        assert_eq!(x.len(), 2);
        assert_eq!(y.len(), 1);
    }

    #[test]
    fn test_block_2x2_solver() {
        // Create diagonal-dominant blocks for stability
        let rows_a11 = vec![0, 1];
        let cols_a11 = vec![0, 1];
        let data_a11 = vec![4.0, 5.0];
        let a11 = CsrArray::from_triplets(&rows_a11, &cols_a11, &data_a11, (2, 2), false)
            .expect("Failed");

        let rows_a12 = vec![0];
        let cols_a12 = vec![0];
        let data_a12 = vec![1.0];
        let a12 = CsrArray::from_triplets(&rows_a12, &cols_a12, &data_a12, (2, 1), false)
            .expect("Failed");

        let rows_a21 = vec![0];
        let cols_a21 = vec![0];
        let data_a21 = vec![1.0];
        let a21 = CsrArray::from_triplets(&rows_a21, &cols_a21, &data_a21, (1, 2), false)
            .expect("Failed");

        let rows_a22 = vec![0];
        let cols_a22 = vec![0];
        let data_a22 = vec![3.0];
        let a22 = CsrArray::from_triplets(&rows_a22, &cols_a22, &data_a22, (1, 1), false)
            .expect("Failed");

        let b1 = Array1::from_vec(vec![1.0, 2.0]);
        let b2 = Array1::from_vec(vec![3.0]);

        let result = solve_block_2x2(&a11, &a12, &a21, &a22, &b1, &b2, 1e-6, 100);
        assert!(result.is_ok());

        let (x1, x2) = result.expect("Failed");
        assert_eq!(x1.len(), 2);
        assert_eq!(x2.len(), 1);
    }

    #[test]
    fn test_arrow_matrix_solver() {
        let diag = Array1::from_vec(vec![3.0, 2.0, 4.0, 5.0]);
        let arrow_row = Array1::from_vec(vec![1.0, 0.5, 0.3]);
        let arrow_col = Array1::from_vec(vec![0.8, 0.6, 0.4]);
        let rhs = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0]);

        let result = solve_arrow_matrix(&diag, &arrow_row, &arrow_col, &rhs);
        assert!(result.is_ok());

        let solution = result.expect("Failed");
        assert_eq!(solution.len(), 4);

        // Solution should not have NaN or infinite values
        for &val in solution.iter() {
            assert!(val.is_finite());
        }
    }

    #[test]
    fn test_banded_system_solver() {
        // Create a tridiagonal (banded) matrix
        let rows = vec![0, 0, 1, 1, 1, 2, 2, 2, 3, 3];
        let cols = vec![0, 1, 0, 1, 2, 1, 2, 3, 2, 3];
        let data = vec![2.0, -1.0, -1.0, 2.0, -1.0, -1.0, 2.0, -1.0, -1.0, 2.0];

        let matrix = CsrArray::from_triplets(&rows, &cols, &data, (4, 4), false).expect("Failed");
        let rhs = Array1::from_vec(vec![1.0, 0.0, 0.0, 1.0]);

        let result = solve_banded_system(&matrix, &rhs, 1, 1e-6, 100);
        assert!(result.is_ok());

        let solution = result.expect("Failed");
        assert_eq!(solution.len(), 4);

        // Verify solution satisfies Ax = b approximately
        let ax = matrix.dot_vector(&solution.view()).expect("Failed");
        for i in 0..4 {
            assert_relative_eq!(ax[i], rhs[i], epsilon = 1e-4);
        }
    }
}