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
//! Transpose-Free Quasi-Minimal Residual (TFQMR) method for sparse linear systems
//!
//! TFQMR is a Krylov subspace method that can solve non-symmetric linear systems
//! without requiring the transpose of the coefficient matrix.
//!
//! Implementation follows SciPy's tfqmr based on R.W. Freund (1993).
//!
//! Reference:
//! R. W. Freund, "A Transpose-Free Quasi-Minimal Residual Algorithm for
//! Non-Hermitian Linear Systems", SIAM J. Sci. Comput., 14(2), 470-482, 1993.

#![allow(unused_variables)]
#![allow(unused_assignments)]
#![allow(unused_mut)]

use crate::error::{SparseError, SparseResult};
use crate::sparray::SparseArray;
use scirs2_core::ndarray::{Array1, ArrayView1};
use scirs2_core::numeric::{Float, SparseElement};
use std::fmt::Debug;

/// Options for the TFQMR solver
#[derive(Debug, Clone)]
pub struct TFQMROptions {
    /// Maximum number of iterations
    pub max_iter: usize,
    /// Convergence tolerance
    pub tol: f64,
    /// Whether to use left preconditioning
    pub use_left_preconditioner: bool,
    /// Whether to use right preconditioning
    pub use_right_preconditioner: bool,
}

impl Default for TFQMROptions {
    fn default() -> Self {
        Self {
            max_iter: 1000,
            tol: 1e-6,
            use_left_preconditioner: false,
            use_right_preconditioner: false,
        }
    }
}

/// Result from TFQMR solver
#[derive(Debug, Clone)]
pub struct TFQMRResult<T> {
    /// Solution vector
    pub x: Array1<T>,
    /// Number of iterations performed
    pub iterations: usize,
    /// Final residual norm
    pub residual_norm: T,
    /// Whether the solver converged
    pub converged: bool,
    /// Residual history (if requested)
    pub residual_history: Option<Vec<T>>,
}

/// Transpose-Free Quasi-Minimal Residual method
///
/// Solves the linear system A * x = b using the TFQMR method.
/// This method is suitable for non-symmetric matrices and does not
/// require computing A^T explicitly.
///
/// # Arguments
///
/// * `matrix` - The coefficient matrix A
/// * `b` - The right-hand side vector
/// * `x0` - Initial guess (optional)
/// * `options` - Solver options
///
/// # Returns
///
/// A `TFQMRResult` containing the solution and convergence information
///
/// # Example
///
/// ```rust
/// use scirs2_sparse::csr_array::CsrArray;
/// use scirs2_sparse::linalg::{tfqmr, TFQMROptions};
/// use scirs2_core::ndarray::Array1;
///
/// // Create a simple matrix
/// let rows = vec![0, 0, 1, 1, 2, 2];
/// let cols = vec![0, 1, 0, 1, 1, 2];
/// let data = vec![2.0, -1.0, -1.0, 2.0, -1.0, 2.0];
/// let matrix = CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");
///
/// // Right-hand side
/// let b = Array1::from_vec(vec![1.0, 0.0, 1.0]);
///
/// // Solve using TFQMR
/// let result = tfqmr(&matrix, &b.view(), None, TFQMROptions::default()).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn tfqmr<T, S>(
    matrix: &S,
    b: &ArrayView1<T>,
    x0: Option<&ArrayView1<T>>,
    options: TFQMROptions,
) -> SparseResult<TFQMRResult<T>>
where
    T: Float + SparseElement + Debug + Copy + 'static,
    S: SparseArray<T>,
{
    let n = b.len();
    let (rows, cols) = matrix.shape();

    if rows != cols || rows != n {
        return Err(SparseError::DimensionMismatch {
            expected: n,
            found: rows,
        });
    }

    let one = T::sparse_one();
    let zero = T::sparse_zero();

    // Initialize solution vector
    let mut x = match x0 {
        Some(x0_val) => x0_val.to_owned(),
        None => Array1::zeros(n),
    };

    // Compute initial residual: r = b - A * x
    let ax = matrix_vector_multiply(matrix, &x.view())?;
    let r = b - &ax;

    let r0norm = l2_norm(&r.view());
    let b_norm = l2_norm(b);
    let tolerance = T::from(options.tol).expect("Operation failed")
        * b_norm.max(T::from(1e-10).expect("Operation failed"));

    if r0norm <= tolerance || r0norm == zero {
        return Ok(TFQMRResult {
            x,
            iterations: 0,
            residual_norm: r0norm,
            converged: true,
            residual_history: Some(vec![r0norm]),
        });
    }

    // Initialize vectors following SciPy's notation
    let mut u = r.clone();
    let mut w = r.clone();
    let rstar = r.clone(); // Shadow residual

    // v = A * r (no preconditioner in this version)
    let ar = matrix_vector_multiply(matrix, &r.view())?;
    let mut v = ar;
    let mut uhat = v.clone();

    let mut d: Array1<T> = Array1::zeros(n);
    let mut theta = zero;
    let mut eta = zero;

    // rho = <rstar, r>
    let mut rho = dot_product(&rstar.view(), &r.view());
    let mut rho_last = rho;
    let mut tau = r0norm;

    let mut residual_history = Vec::new();
    residual_history.push(r0norm);

    let mut converged = false;
    let mut iter = 0;

    for it in 0..options.max_iter {
        iter = it + 1;
        let even = it % 2 == 0;

        // On even iterations, compute alpha and uNext
        let mut alpha = zero;
        let mut u_next: Array1<T> = Array1::zeros(n);

        if even {
            let vtrstar = dot_product(&rstar.view(), &v.view());
            if vtrstar.abs() < T::from(1e-300).expect("Operation failed") {
                return Err(SparseError::ConvergenceError(
                    "TFQMR breakdown: v'*rstar = 0".to_string(),
                ));
            }
            alpha = rho / vtrstar;

            // uNext = u - alpha * v
            for i in 0..n {
                u_next[i] = u[i] - alpha * v[i];
            }
        }

        // w = w - alpha * uhat (every iteration)
        let alpha_used = if even {
            alpha
        } else {
            rho / dot_product(&rstar.view(), &v.view())
        };

        for i in 0..n {
            w[i] = w[i] - alpha_used * uhat[i];
        }

        // d = u + (theta^2 / alpha) * eta * d
        let theta_sq_over_alpha = if alpha_used.abs() > T::from(1e-300).expect("Operation failed") {
            theta * theta / alpha_used
        } else {
            zero
        };
        for i in 0..n {
            d[i] = u[i] + theta_sq_over_alpha * eta * d[i];
        }

        // theta = ||w|| / tau
        theta = l2_norm(&w.view()) / tau;

        // c = 1 / sqrt(1 + theta^2)
        let c = one / (one + theta * theta).sqrt();

        // tau = tau * theta * c
        tau = tau * theta * c;

        // eta = c^2 * alpha
        eta = c * c * alpha_used;

        // x = x + eta * d (no preconditioner)
        for i in 0..n {
            x[i] = x[i] + eta * d[i];
        }

        residual_history.push(tau);

        // Convergence criterion: tau * sqrt(iter+1) < tolerance
        let iter_f = T::from(iter).expect("Operation failed");
        if tau * iter_f.sqrt() < tolerance {
            converged = true;
            break;
        }

        if !even {
            // Odd iteration updates
            rho = dot_product(&rstar.view(), &w.view());

            if rho.abs() < T::from(1e-300).expect("Operation failed") {
                return Err(SparseError::ConvergenceError(
                    "TFQMR breakdown: rho = 0".to_string(),
                ));
            }

            let beta = rho / rho_last;

            // u = w + beta * u
            for i in 0..n {
                u[i] = w[i] + beta * u[i];
            }

            // v = beta * uhat + beta^2 * v
            for i in 0..n {
                v[i] = beta * uhat[i] + beta * beta * v[i];
            }

            // uhat = A * u
            let au = matrix_vector_multiply(matrix, &u.view())?;
            uhat = au;

            // v = v + uhat
            for i in 0..n {
                v[i] = v[i] + uhat[i];
            }
        } else {
            // Even iteration updates
            // uhat = A * uNext
            let au_next = matrix_vector_multiply(matrix, &u_next.view())?;
            uhat = au_next;

            // u = uNext
            u = u_next;

            // rho_last = rho
            rho_last = rho;
        }
    }

    // Compute final residual norm by explicit calculation
    let ax_final = matrix_vector_multiply(matrix, &x.view())?;
    let final_residual = b - &ax_final;
    let final_residual_norm = l2_norm(&final_residual.view());

    Ok(TFQMRResult {
        x,
        iterations: iter,
        residual_norm: final_residual_norm,
        converged,
        residual_history: Some(residual_history),
    })
}

/// Helper function for matrix-vector multiplication
#[allow(dead_code)]
fn matrix_vector_multiply<T, S>(matrix: &S, x: &ArrayView1<T>) -> SparseResult<Array1<T>>
where
    T: Float + SparseElement + Debug + Copy + 'static,
    S: SparseArray<T>,
{
    let (rows, cols) = matrix.shape();
    if x.len() != cols {
        return Err(SparseError::DimensionMismatch {
            expected: cols,
            found: x.len(),
        });
    }

    let mut result = Array1::zeros(rows);
    let (row_indices, col_indices, values) = matrix.find();

    for (k, (&i, &j)) in row_indices.iter().zip(col_indices.iter()).enumerate() {
        result[i] = result[i] + values[k] * x[j];
    }

    Ok(result)
}

/// Compute L2 norm of a vector
#[allow(dead_code)]
fn l2_norm<T>(x: &ArrayView1<T>) -> T
where
    T: Float + SparseElement + Debug + Copy,
{
    (x.iter()
        .map(|&val| val * val)
        .fold(T::sparse_zero(), |a, b| a + b))
    .sqrt()
}

/// Compute dot product of two vectors
#[allow(dead_code)]
fn dot_product<T>(x: &ArrayView1<T>, y: &ArrayView1<T>) -> T
where
    T: Float + SparseElement + Debug + Copy,
{
    x.iter()
        .zip(y.iter())
        .map(|(&xi, &yi)| xi * yi)
        .fold(T::sparse_zero(), |a, b| a + b)
}

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

    #[test]
    fn test_tfqmr_simple_system() {
        // Create a simple 3x3 system
        let rows = vec![0, 0, 1, 1, 2, 2];
        let cols = vec![0, 1, 0, 1, 1, 2];
        let data = vec![2.0, -1.0, -1.0, 2.0, -1.0, 2.0];
        let matrix =
            CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");

        let b = Array1::from_vec(vec![1.0, 0.0, 1.0]);
        let result =
            tfqmr(&matrix, &b.view(), None, TFQMROptions::default()).expect("Operation failed");

        assert!(result.converged);

        // Verify solution by computing residual
        let ax = matrix_vector_multiply(&matrix, &result.x.view()).expect("Operation failed");
        let residual = &b - &ax;
        let residual_norm = l2_norm(&residual.view());

        assert!(residual_norm < 1e-6);
    }

    #[test]
    fn test_tfqmr_diagonal_system() {
        // Create a diagonal system
        let rows = vec![0, 1, 2];
        let cols = vec![0, 1, 2];
        let data = vec![2.0, 3.0, 4.0];
        let matrix =
            CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");

        let b = Array1::from_vec(vec![4.0, 9.0, 16.0]);
        let result =
            tfqmr(&matrix, &b.view(), None, TFQMROptions::default()).expect("Operation failed");

        assert!(result.converged);

        // For diagonal system, solution should be [2, 3, 4]
        assert_relative_eq!(result.x[0], 2.0, epsilon = 1e-6);
        assert_relative_eq!(result.x[1], 3.0, epsilon = 1e-6);
        assert_relative_eq!(result.x[2], 4.0, epsilon = 1e-6);
    }

    #[test]
    fn test_tfqmr_with_initial_guess() {
        let rows = vec![0, 1, 2];
        let cols = vec![0, 1, 2];
        let data = vec![1.0, 1.0, 1.0];
        let matrix =
            CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).expect("Operation failed");

        let b = Array1::from_vec(vec![5.0, 6.0, 7.0]);
        let x0 = Array1::from_vec(vec![4.0, 5.0, 6.0]); // Close to solution

        let result = tfqmr(
            &matrix,
            &b.view(),
            Some(&x0.view()),
            TFQMROptions::default(),
        )
        .expect("Operation failed");

        assert!(result.converged);
        assert!(result.iterations <= 5); // Should converge quickly with good initial guess
    }
}