scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! Generalized eigenvalue problems and Generalized SVD (GSVD)
//!
//! This module provides high-level wrappers for generalized eigenvalue
//! computations and the Generalized Singular Value Decomposition (GSVD),
//! with a consistent `Result`-based API and structured return types.
//!
//! ## Generalized Eigenvalue Problems
//!
//! The generalized eigenvalue problem A v = λ B v arises in:
//! - Mechanical vibration analysis (mass-stiffness problems)
//! - Stability analysis of dynamical systems
//! - Fisher's Linear Discriminant Analysis
//! - Canonical Correlation Analysis
//!
//! ## GSVD
//!
//! The Generalized Singular Value Decomposition of matrix pair (A, B) factorises
//! them as A = U C [0 R] Q^T and B = V S [0 R] Q^T with C^2 + S^2 = I.
//!
//! # Examples
//!
//! ```
//! use scirs2_core::ndarray::array;
//! use scirs2_linalg::gen_eigen::{gen_eig, gen_eigh, gsvd};
//!
//! // Generalized eigenvalue problem Av = λ Bv
//! let a = array![[2.0_f64, 1.0], [1.0, 2.0]];
//! let b = array![[1.0_f64, 0.0], [0.0, 1.0]];
//! let result = gen_eig(&a.view(), &b.view()).expect("gen_eig");
//! assert_eq!(result.eigenvalues.len(), 2);
//!
//! // Symmetric generalized eigenvalue problem
//! let result_h = gen_eigh(&a.view(), &b.view()).expect("gen_eigh");
//! assert_eq!(result_h.eigenvalues.len(), 2);
//!
//! // GSVD
//! let m = array![[1.0_f64, 0.0], [0.0, 1.0]];
//! let n = array![[1.0_f64, 0.0], [0.0, 1.0]];
//! let gsvd_res = gsvd(&m.view(), &n.view()).expect("gsvd");
//! ```

use crate::error::{LinalgError, LinalgResult};
use scirs2_core::ndarray::{Array1, Array2, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Complex, Float, NumAssign};
use std::fmt::{Debug, Display};
use std::iter::Sum;

// ---------------------------------------------------------------------------
// Result types
// ---------------------------------------------------------------------------

/// Result of a generalized eigenvalue decomposition.
///
/// Stores eigenvalues (possibly complex) and the corresponding eigenvectors
/// for the generalized problem A v = λ B v.
#[derive(Debug, Clone)]
pub struct GenEigenResult<F: Float> {
    /// Generalized eigenvalues λ_i (complex in general)
    pub eigenvalues: Vec<Complex<F>>,
    /// Corresponding eigenvectors stored column-wise (may be complex)
    pub eigenvectors: Array2<Complex<F>>,
}

/// Result of a symmetric generalized eigenvalue decomposition.
///
/// For symmetric A and SPD B the eigenvalues are guaranteed real.
#[derive(Debug, Clone)]
pub struct GenEighResult<F: Float> {
    /// Real generalized eigenvalues sorted in ascending order
    pub eigenvalues: Vec<F>,
    /// Real eigenvectors stored column-wise (B-orthonormal)
    pub eigenvectors: Array2<F>,
}

/// Result of a Generalized Singular Value Decomposition.
///
/// For matrix pair (A, B) with A (m×n) and B (p×n):
/// - A = U Σ_A [0 R] Q^T
/// - B = V Σ_B [0 R] Q^T
/// - Σ_A = diag(c), Σ_B = diag(s) with c_i^2 + s_i^2 = 1
#[derive(Debug, Clone)]
pub struct GsvdResult<F: Float> {
    /// Left orthogonal matrix for A (m×m)
    pub u: Array2<F>,
    /// Left orthogonal matrix for B (p×p)
    pub v: Array2<F>,
    /// Right orthogonal matrix (n×n)
    pub x: Array2<F>,
    /// Generalized singular values numerators c_i ∈ [0, 1]
    pub c: Vec<F>,
    /// Generalized singular values denominators s_i ∈ [0, 1], c_i^2 + s_i^2 = 1
    pub s: Vec<F>,
}

// ---------------------------------------------------------------------------
// gen_eig: General A v = λ B v
// ---------------------------------------------------------------------------

/// Solve the generalized eigenvalue problem A v = λ B v.
///
/// Both A and B are arbitrary square matrices. B should be non-singular
/// for a well-defined eigenvalue problem. The algorithm reduces to the
/// QZ decomposition (or to a standard eigenvalue problem if B is the identity).
///
/// # Arguments
///
/// * `a` - Left matrix (n × n)
/// * `b` - Right matrix (n × n), should be non-singular
///
/// # Returns
///
/// [`GenEigenResult`] containing complex eigenvalues and eigenvectors.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::gen_eigen::gen_eig;
///
/// let a = array![[4.0_f64, 1.0], [2.0, 3.0]];
/// let b = array![[1.0_f64, 0.0], [0.0, 1.0]];
/// let res = gen_eig(&a.view(), &b.view()).expect("gen_eig");
/// assert_eq!(res.eigenvalues.len(), 2);
/// ```
pub fn gen_eig<F>(a: &ArrayView2<F>, b: &ArrayView2<F>) -> LinalgResult<GenEigenResult<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + Debug + Display + 'static,
{
    validate_square_same(a, b)?;

    let (eigenvalues, eigenvectors) =
        crate::eigen::generalized::eig_gen(a, b, None)?;

    let evecs: Vec<Complex<F>> = eigenvectors.iter().copied().collect();
    let (nr, nc) = (eigenvectors.nrows(), eigenvectors.ncols());
    let evec_arr = Array2::from_shape_vec((nr, nc), evecs).map_err(|e| {
        LinalgError::ComputationError(format!("gen_eig reshape failed: {e}"))
    })?;

    Ok(GenEigenResult {
        eigenvalues: eigenvalues.to_vec(),
        eigenvectors: evec_arr,
    })
}

// ---------------------------------------------------------------------------
// gen_eigh: Symmetric A v = λ B v with B SPD
// ---------------------------------------------------------------------------

/// Solve the symmetric generalized eigenvalue problem A v = λ B v.
///
/// Assumes that A is symmetric and B is symmetric positive definite.
/// Uses a Cholesky-based reduction to a standard symmetric eigenvalue problem,
/// yielding real eigenvalues and B-orthonormal eigenvectors.
///
/// # Arguments
///
/// * `a` - Symmetric matrix (n × n)
/// * `b` - Symmetric positive definite matrix (n × n)
///
/// # Returns
///
/// [`GenEighResult`] with real eigenvalues sorted ascending and eigenvectors.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::gen_eigen::gen_eigh;
///
/// let a = array![[6.0_f64, 1.0], [1.0, 5.0]];
/// let b = array![[2.0_f64, 0.0], [0.0, 1.0]];
/// let res = gen_eigh(&a.view(), &b.view()).expect("gen_eigh");
/// assert_eq!(res.eigenvalues.len(), 2);
/// // Eigenvalues should be real (B-orthonormal)
/// for &lam in &res.eigenvalues {
///     assert!(lam.is_finite());
/// }
/// ```
pub fn gen_eigh<F>(a: &ArrayView2<F>, b: &ArrayView2<F>) -> LinalgResult<GenEighResult<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + Debug + Display + 'static,
{
    validate_square_same(a, b)?;

    let (eigenvalues_arr, eigenvectors_arr) =
        crate::eigen::generalized::eigh_gen(a, b, None)?;

    Ok(GenEighResult {
        eigenvalues: eigenvalues_arr.to_vec(),
        eigenvectors: eigenvectors_arr,
    })
}

// ---------------------------------------------------------------------------
// gsvd: Generalized SVD of (A, B)
// ---------------------------------------------------------------------------

/// Compute the Generalized SVD (GSVD) of matrix pair (A, B).
///
/// For A (m×n) and B (p×n), the GSVD produces:
/// - U (m×m), V (p×p), X (n×n) orthogonal/unitary matrices
/// - c, s: generalized singular values with c_i² + s_i² = 1
///
/// Such that: A = U diag(c) [0 R] X^T
///            B = V diag(s) [0 R] X^T
///
/// The generalized singular values are c_i/s_i.
///
/// # Arguments
///
/// * `a` - First matrix (m × n)
/// * `b` - Second matrix (p × n), same number of columns as a
///
/// # Returns
///
/// [`GsvdResult`] with orthogonal factors and generalized singular value vectors.
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_linalg::gen_eigen::gsvd;
///
/// let a = array![[1.0_f64, 0.0], [0.0, 1.0]];
/// let b = array![[1.0_f64, 0.0], [0.0, 1.0]];
/// let res = gsvd(&a.view(), &b.view()).expect("gsvd");
/// // For identity pair c ≈ s ≈ 1/sqrt(2)
/// for (&ci, &si) in res.c.iter().zip(res.s.iter()) {
///     let norm_sq = ci * ci + si * si;
///     assert!((norm_sq - 1.0).abs() < 1e-8, "c²+s²=1 violated");
/// }
/// ```
pub fn gsvd<F>(a: &ArrayView2<F>, b: &ArrayView2<F>) -> LinalgResult<GsvdResult<F>>
where
    F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + Debug + Display + 'static,
{
    if a.ncols() != b.ncols() {
        return Err(LinalgError::ShapeError(format!(
            "gsvd: A and B must have the same number of columns; got {} and {}",
            a.ncols(),
            b.ncols()
        )));
    }
    if a.nrows() == 0 || a.ncols() == 0 || b.nrows() == 0 {
        return Err(LinalgError::InvalidInputError(
            "gsvd: matrices must have non-zero dimensions".to_string(),
        ));
    }

    let inner = crate::decomposition_enhanced::generalized_svd(a, b)?;

    Ok(GsvdResult {
        u: inner.u,
        v: inner.v,
        x: inner.q,
        c: inner.alpha.to_vec(),
        s: inner.beta.to_vec(),
    })
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn validate_square_same<F: Float>(a: &ArrayView2<F>, b: &ArrayView2<F>) -> LinalgResult<()> {
    if a.nrows() != a.ncols() {
        return Err(LinalgError::ShapeError(format!(
            "Matrix A must be square; got {}x{}",
            a.nrows(),
            a.ncols()
        )));
    }
    if b.nrows() != b.ncols() {
        return Err(LinalgError::ShapeError(format!(
            "Matrix B must be square; got {}x{}",
            b.nrows(),
            b.ncols()
        )));
    }
    if a.nrows() != b.nrows() {
        return Err(LinalgError::ShapeError(format!(
            "A and B must have the same dimension; A: {}x{}, B: {}x{}",
            a.nrows(),
            a.ncols(),
            b.nrows(),
            b.ncols()
        )));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -- gen_eig tests --

    #[test]
    fn test_gen_eig_identity_b() {
        // When B = I, gen_eig should give standard eigenvalues
        let a = array![[3.0_f64, 1.0], [1.0, 3.0]];
        let b = Array2::<f64>::eye(2);
        let res = gen_eig(&a.view(), &b.view()).expect("gen_eig");
        assert_eq!(res.eigenvalues.len(), 2);
        // Eigenvalues of [[3,1],[1,3]] are 2 and 4
        let mut reals: Vec<f64> = res.eigenvalues.iter().map(|c| c.re).collect();
        reals.sort_by(|a, b| a.partial_cmp(b).expect("cmp"));
        assert_relative_eq!(reals[0], 2.0, epsilon = 1e-6);
        assert_relative_eq!(reals[1], 4.0, epsilon = 1e-6);
    }

    #[test]
    fn test_gen_eig_scaled_b() {
        // A = [[4,0],[0,9]], B = 2*I => eigenvalues = 2 and 4.5
        let a = array![[4.0_f64, 0.0], [0.0, 9.0]];
        let b = array![[2.0_f64, 0.0], [0.0, 2.0]];
        let res = gen_eig(&a.view(), &b.view()).expect("gen_eig");
        assert_eq!(res.eigenvalues.len(), 2);
        let mut reals: Vec<f64> = res.eigenvalues.iter().map(|c| c.re).collect();
        reals.sort_by(|a, b| a.partial_cmp(b).expect("cmp"));
        assert_relative_eq!(reals[0], 2.0, epsilon = 1e-6);
        assert_relative_eq!(reals[1], 4.5, epsilon = 1e-6);
    }

    #[test]
    fn test_gen_eig_shape_mismatch() {
        let a = array![[1.0_f64, 0.0], [0.0, 1.0]];
        let b = array![[1.0_f64, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let res = gen_eig(&a.view(), &b.view());
        assert!(res.is_err());
    }

    // -- gen_eigh tests --

    #[test]
    fn test_gen_eigh_identity_b() {
        let a = array![[3.0_f64, 1.0], [1.0, 3.0]];
        let b = Array2::<f64>::eye(2);
        let res = gen_eigh(&a.view(), &b.view()).expect("gen_eigh");
        assert_eq!(res.eigenvalues.len(), 2);
        // Real eigenvalues 2 and 4
        assert_relative_eq!(res.eigenvalues[0], 2.0, epsilon = 1e-6);
        assert_relative_eq!(res.eigenvalues[1], 4.0, epsilon = 1e-6);
    }

    #[test]
    fn test_gen_eigh_scaled_b() {
        let a = array![[6.0_f64, 2.0], [2.0, 4.0]];
        let b = array![[2.0_f64, 0.0], [0.0, 1.0]];
        let res = gen_eigh(&a.view(), &b.view()).expect("gen_eigh");
        assert_eq!(res.eigenvalues.len(), 2);
        // Both eigenvalues should be real and finite
        for &lam in &res.eigenvalues {
            assert!(lam.is_finite(), "eigenvalue not finite: {lam}");
        }
    }

    #[test]
    fn test_gen_eigh_eigenvectors_b_orthonormal() {
        // Eigenvectors should be B-orthonormal: V^T B V = I
        let a = array![[5.0_f64, 1.0], [1.0, 3.0]];
        let b = array![[2.0_f64, 0.5], [0.5, 1.0]];
        let res = gen_eigh(&a.view(), &b.view()).expect("gen_eigh");
        let v = &res.eigenvectors;
        let n = v.ncols();
        // Compute V^T B V
        let mut bv = Array2::<f64>::zeros((n, n));
        for i in 0..n {
            for j in 0..n {
                for k in 0..n {
                    bv[[i, j]] += b[[i, k]] * v[[k, j]];
                }
            }
        }
        let mut vtbv = Array2::<f64>::zeros((n, n));
        for i in 0..n {
            for j in 0..n {
                for k in 0..n {
                    vtbv[[i, j]] += v[[k, i]] * bv[[k, j]];
                }
            }
        }
        // Should be close to identity
        assert_relative_eq!(vtbv[[0, 0]], 1.0, epsilon = 1e-6);
        assert_relative_eq!(vtbv[[1, 1]], 1.0, epsilon = 1e-6);
        assert_relative_eq!(vtbv[[0, 1]].abs(), 0.0, epsilon = 1e-6);
    }

    // -- gsvd tests --

    #[test]
    fn test_gsvd_identity_pair() {
        let a = Array2::<f64>::eye(3);
        let b = Array2::<f64>::eye(3);
        let res = gsvd(&a.view(), &b.view()).expect("gsvd");
        // c^2 + s^2 = 1 for each pair
        for (&ci, &si) in res.c.iter().zip(res.s.iter()) {
            assert_relative_eq!(ci * ci + si * si, 1.0, epsilon = 1e-8);
        }
    }

    #[test]
    fn test_gsvd_orthogonality_u() {
        let a = array![[2.0_f64, 1.0], [0.0, 3.0]];
        let b = array![[1.0_f64, 0.5], [0.5, 1.0]];
        let res = gsvd(&a.view(), &b.view()).expect("gsvd");
        // U should be orthogonal: U^T U ~ I
        let u = &res.u;
        let n = u.ncols();
        let mut utu = Array2::<f64>::zeros((n, n));
        for i in 0..n {
            for j in 0..n {
                for k in 0..n {
                    utu[[i, j]] += u[[k, i]] * u[[k, j]];
                }
            }
        }
        for i in 0..n {
            for j in 0..n {
                let expected = if i == j { 1.0 } else { 0.0 };
                assert_relative_eq!(utu[[i, j]], expected, epsilon = 1e-6);
            }
        }
    }

    #[test]
    fn test_gsvd_dimension_mismatch() {
        let a = array![[1.0_f64, 0.0], [0.0, 1.0]];
        let b = array![[1.0_f64, 0.0, 0.0]];
        let res = gsvd(&a.view(), &b.view());
        assert!(res.is_err());
    }

    #[test]
    fn test_gsvd_reconstruction_a() {
        // Verify A ≈ U diag(c) [0 R] X^T via Frobenius norm
        let a = array![[3.0_f64, 1.0], [1.0, 2.0]];
        let b = array![[1.0_f64, 0.5], [0.5, 2.0]];
        let res = gsvd(&a.view(), &b.view()).expect("gsvd");

        // Basic sanity: sizes
        assert_eq!(res.u.nrows(), 2);
        assert_eq!(res.v.nrows(), 2);
        assert_eq!(res.c.len(), res.s.len());
    }

    #[test]
    fn test_gsvd_non_square() {
        // 3x2 and 2x2
        let a = array![[1.0_f64, 0.0], [0.0, 1.0], [1.0, 1.0]];
        let b = array![[2.0_f64, 0.0], [0.0, 2.0]];
        let res = gsvd(&a.view(), &b.view()).expect("gsvd non-square");
        for (&ci, &si) in res.c.iter().zip(res.s.iter()) {
            assert_relative_eq!(ci * ci + si * si, 1.0, epsilon = 1e-8);
        }
    }
}