russell_lab 1.11.0

Scientific laboratory for linear algebra and numerical mathematics
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
use super::ComplexMatrix;
use crate::{cpx, to_i32, CcBool, Complex64, ComplexVector, StrError, C_FALSE, C_TRUE};

extern "C" {
    // Computes the eigenvalues and, optionally, the left and/or right eigenvectors for GE matrices
    // <https://www.netlib.org/lapack/explore-html/d3/d47/zggev_8f.html>
    fn c_zggev(
        calc_vl: CcBool,
        calc_vr: CcBool,
        n: *const i32,
        a: *mut Complex64,
        lda: *const i32,
        b: *mut Complex64,
        ldb: *const i32,
        alpha: *mut Complex64,
        beta: *mut Complex64,
        vl: *mut Complex64,
        ldvl: *const i32,
        vr: *mut Complex64,
        ldvr: *const i32,
        work: *mut Complex64,
        lwork: *const i32,
        rwork: *mut f64,
        info: *mut i32,
    );
}

/// (zggev) Computes the generalized eigenvalues and right eigenvectors
///
/// A generalized eigenvalue for a pair of matrices (A,B) is a scalar lambda
/// or a ratio alpha/beta = lambda, such that A - lambda*B is singular.
/// It is usually represented as the pair (alpha,beta), as there is a
/// reasonable interpretation for beta=0, and even for both being zero.
///
/// Computes the eigenvalues `l` and right eigenvectors `v`, such that:
///
/// ```text
/// a ⋅ vj = lj ⋅ b ⋅ vj
/// ```
///
/// where `lj` is the component j of `l` and `vj` is the column j of `v`.
///
/// The eigenvalues are returned in two parts, α (alpha_real, alpha_imag) and β (beta).
///
/// - If alpha_imag(j) = 0, then the j-th eigenvalue is real.
/// - If alpha_imag(j) > 0, then the j-th and (j+1)-th eigenvalues are a complex conjugate pair.
///
/// See also: <https://www.netlib.org/lapack/explore-html/d3/d47/zggev_8f.html>
///
/// # Output
///
/// * `alpha_real` -- (m) real part to compose the eigenvalues
/// * `alpha_imag` -- (m) imaginary part to compose the eigenvalues
/// * `beta` -- (m) coefficients to compose the eigenvalues
/// * `v` -- (m,m) **right** eigenvectors (as columns)
///
/// # Input
///
/// * `a` -- (m,m) general matrix (will be modified)
/// * `b` -- (m,m) general matrix (will be modified)
///
/// # Examples
///
/// ```
/// use russell_lab::*;
///
/// fn main() -> Result<(), StrError> {
///     // a and b matrices
///     let mut a = ComplexMatrix::from(&[
///         [cpx!(1.0, 2.0), cpx!(3.0, 4.0), cpx!(21.0, 22.0)],
///         [cpx!(43.0, 44.0), cpx!(13.0, 14.0), cpx!(15.0, 16.0)],
///         [cpx!(5.0, 6.0), cpx!(7.0, 8.0), cpx!(25.0, 26.0)],
///     ]);
///     let mut b = ComplexMatrix::from(&[
///         [cpx!(2.0, 0.0), cpx!(0.0, -1.0), cpx!(0.0, 0.0)],
///         [cpx!(0.0, 1.0), cpx!(2.0, 0.0), cpx!(0.0, 0.0)],
///         [cpx!(0.0, 0.0), cpx!(0.0, 0.0), cpx!(3.0, 0.0)],
///     ]);
///     let m = a.nrow();
///     let mut alpha = ComplexVector::new(m);
///     let mut beta = ComplexVector::new(m);
///     let mut v = ComplexMatrix::new(m, m);
///     complex_mat_gen_eigen(&mut alpha, &mut beta, &mut v, &mut a, &mut b).unwrap();
///     println!("α =\n{:.6}", alpha);
///     println!("β =\n{:.6}", beta);
///     println!("v =\n{:.6}", v);
///     // compare with reference
///     // (https://www.ibm.com/docs/en/essl/6.2)
///     let alpha_ref = &[
///         cpx!( 15.863783, 41.115283),
///         cpx!(-12.917205, 19.973815),
///         cpx!(  3.215518, -4.912439),
///     ];
///     let beta_ref = &[
///         cpx!(1.668461, 0.0),
///         cpx!(2.024212, 0.0),
///         cpx!(2.664836, 0.0),
///     ];
///     complex_vec_approx_eq(&alpha, alpha_ref, 1e-6);
///     complex_vec_approx_eq(&beta, beta_ref, 1e-6);
///     Ok(())
/// }
/// ```
pub fn complex_mat_gen_eigen(
    alpha: &mut ComplexVector,
    beta: &mut ComplexVector,
    v: &mut ComplexMatrix,
    a: &mut ComplexMatrix,
    b: &mut ComplexMatrix,
) -> Result<(), StrError> {
    let (m, n) = a.dims();
    if m != n {
        return Err("matrix must be square");
    }
    if alpha.dim() != m || beta.dim() != m {
        return Err("vectors are incompatible");
    }
    if v.nrow() != m || v.ncol() != m || b.nrow() != m || b.ncol() != m {
        return Err("matrices are incompatible");
    }
    let m_i32 = to_i32(m);
    let lda = m_i32;
    let ldb = m_i32;
    let ldu = 1;
    let ldv = m_i32;
    const EXTRA: i32 = 1;
    let lwork = 2 * m_i32 + EXTRA;
    let mut u = vec![cpx!(0.0, 0.0); ldu as usize];
    let mut work = vec![cpx!(0.0, 0.0); lwork as usize];
    let mut rwork = vec![0.0; 8 * m];
    let mut info = 0;
    unsafe {
        c_zggev(
            C_FALSE,
            C_TRUE,
            &m_i32,
            a.as_mut_data().as_mut_ptr(),
            &lda,
            b.as_mut_data().as_mut_ptr(),
            &ldb,
            alpha.as_mut_data().as_mut_ptr(),
            beta.as_mut_data().as_mut_ptr(),
            u.as_mut_ptr(),
            &ldu,
            v.as_mut_data().as_mut_ptr(),
            &ldv,
            work.as_mut_ptr(),
            &lwork,
            rwork.as_mut_ptr(),
            &mut info,
        );
    }
    if info < 0 {
        println!("LAPACK ERROR (zggev): Argument #{} had an illegal value", -info);
        return Err("LAPACK ERROR (zggev): An argument had an illegal value");
    } else if info > 0 {
        return Err("LAPACK ERROR (zggev): Iterations failed ");
    }
    Ok(())
}

/// Computes the generalized eigenvalues and eigenvectors (left and right)
///
/// A generalized eigenvalue for a pair of matrices (A,B) is a scalar lambda
/// or a ratio alpha/beta = lambda, such that A - lambda*B is singular.
/// It is usually represented as the pair (alpha,beta), as there is a
/// reasonable interpretation for beta=0, and even for both being zero.
/// Performs the eigen-decomposition of a square matrix (left and right)
///
/// Computes the eigenvalues `l` and left eigenvectors `u`, such that:
///
/// ```text
/// ujᴴ ⋅ a = lj ⋅ ujᴴ ⋅ b
/// ```
///
/// where `lj` is the component j of `l` and `ujᴴ` is the column j of `uᴴ`,
/// with `uᴴ` being the conjugate-transpose of `u`.
///
/// Also, computes the right eigenvectors `v`, such that:
///
/// ```text
/// a ⋅ vj = lj ⋅ b ⋅ vj
/// ```
///
/// where `vj` is the column j of `v`.
///
/// The eigenvalues are returned in two parts, α (alpha_real, alpha_imag) and β (beta).
///
/// - If alpha_imag(j) = 0, then the j-th eigenvalue is real.
/// - If alpha_imag(j) > 0, then the j-th and (j+1)-th eigenvalues are a complex conjugate pair.
///
/// See also: <https://www.netlib.org/lapack/explore-html/d3/d47/zggev_8f.html>
///
/// # Output
///
/// * `alpha_real` -- (m) real part to compose the eigenvalues
/// * `alpha_imag` -- (m) imaginary part to compose the eigenvalues
/// * `beta` -- (m) coefficients to compose the eigenvalues
/// * `u` -- (m,m) **left** eigenvectors (as columns)
/// * `v` -- (m,m) **right** eigenvectors (as columns)
///
/// # Input
///
/// * `a` -- (m,m) general matrix (will be modified)
/// * `b` -- (m,m) general matrix (will be modified)
///
/// # Note
///
/// * The matrix `a` will be modified
///
/// # Examples
///
/// ```
/// use russell_lab::*;
///
/// fn main() -> Result<(), StrError> {
///     // a and b matrices
///     let mut a = ComplexMatrix::from(&[
///         [cpx!(1.0, 2.0), cpx!(3.0, 4.0), cpx!(21.0, 22.0)],
///         [cpx!(43.0, 44.0), cpx!(13.0, 14.0), cpx!(15.0, 16.0)],
///         [cpx!(5.0, 6.0), cpx!(7.0, 8.0), cpx!(25.0, 26.0)],
///     ]);
///     let mut b = ComplexMatrix::from(&[
///         [cpx!(2.0, 0.0), cpx!(0.0, -1.0), cpx!(0.0, 0.0)],
///         [cpx!(0.0, 1.0), cpx!(2.0, 0.0), cpx!(0.0, 0.0)],
///         [cpx!(0.0, 0.0), cpx!(0.0, 0.0), cpx!(3.0, 0.0)],
///     ]);
///     let m = a.nrow();
///     let mut alpha = ComplexVector::new(m);
///     let mut beta = ComplexVector::new(m);
///     let mut u = ComplexMatrix::new(m, m);
///     let mut v = ComplexMatrix::new(m, m);
///     complex_mat_gen_eigen_lr(&mut alpha, &mut beta, &mut u, &mut v, &mut a, &mut b).unwrap();
///     println!("α =\n{:.6}", alpha);
///     println!("β =\n{:.6}", beta);
///     println!("u =\n{:.6}", u);
///     println!("v =\n{:.6}", v);
///     // compare with reference
///     // (https://www.ibm.com/docs/en/essl/6.2)
///     #[rustfmt::skip]
///     let alpha_ref = &[
///         cpx!( 15.863783, 41.115283),
///         cpx!(-12.917205, 19.973815),
///         cpx!(  3.215518, -4.912439),
///     ];
///     #[rustfmt::skip]
///     let beta_ref = &[
///         cpx!(1.668461, 0.0),
///         cpx!(2.024212, 0.0),
///         cpx!(2.664836, 0.0),
///     ];
///     complex_vec_approx_eq(&alpha, alpha_ref, 1e-6);
///     complex_vec_approx_eq(&beta, beta_ref, 1e-6);
///     Ok(())
/// }
/// ```
pub fn complex_mat_gen_eigen_lr(
    alpha: &mut ComplexVector,
    beta: &mut ComplexVector,
    u: &mut ComplexMatrix,
    v: &mut ComplexMatrix,
    a: &mut ComplexMatrix,
    b: &mut ComplexMatrix,
) -> Result<(), StrError> {
    let (m, n) = a.dims();
    if m != n {
        return Err("matrix must be square");
    }
    if alpha.dim() != m || beta.dim() != m {
        return Err("vectors are incompatible");
    }
    if u.nrow() != m || u.ncol() != m || v.nrow() != m || v.ncol() != m || b.nrow() != m || b.ncol() != m {
        return Err("matrices are incompatible");
    }
    let m_i32 = to_i32(m);
    let lda = m_i32;
    let ldb = m_i32;
    let ldu = m_i32;
    let ldv = m_i32;
    const EXTRA: i32 = 1;
    let lwork = 2 * m_i32 + EXTRA;
    let mut work = vec![cpx!(0.0, 0.0); lwork as usize];
    let mut rwork = vec![0.0; 8 * m];
    let mut info = 0;
    unsafe {
        c_zggev(
            C_FALSE,
            C_TRUE,
            &m_i32,
            a.as_mut_data().as_mut_ptr(),
            &lda,
            b.as_mut_data().as_mut_ptr(),
            &ldb,
            alpha.as_mut_data().as_mut_ptr(),
            beta.as_mut_data().as_mut_ptr(),
            u.as_mut_data().as_mut_ptr(),
            &ldu,
            v.as_mut_data().as_mut_ptr(),
            &ldv,
            work.as_mut_ptr(),
            &lwork,
            rwork.as_mut_ptr(),
            &mut info,
        );
    }
    if info < 0 {
        println!("LAPACK ERROR (zggev): Argument #{} had an illegal value", -info);
        return Err("LAPACK ERROR (zggev): An argument had an illegal value");
    } else if info > 0 {
        return Err("LAPACK ERROR (zggev): Iterations failed ");
    }
    Ok(())
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::{complex_mat_gen_eigen, complex_mat_gen_eigen_lr};
    use crate::matrix::testing::complex_check_gen_eigen;
    use crate::{complex_vec_approx_eq, cpx, Complex64, ComplexMatrix, ComplexVector};

    #[test]
    fn complex_mat_gen_eigen_captures_errors() {
        let mut a = ComplexMatrix::new(2, 1);
        let m = a.nrow();
        let mut b = ComplexMatrix::new(m, m);
        let mut alpha = ComplexVector::new(m);
        let mut beta = ComplexVector::new(m);
        let mut v = ComplexMatrix::new(m, m);
        assert_eq!(
            complex_mat_gen_eigen(&mut alpha, &mut beta, &mut v, &mut a, &mut b),
            Err("matrix must be square")
        );
        let mut a = ComplexMatrix::new(2, 2);
        let mut b_wrong1 = ComplexMatrix::new(m + 1, m);
        let mut b_wrong2 = ComplexMatrix::new(m, m + 1);
        let mut alpha_wrong = ComplexVector::new(m + 1);
        let mut beta_wrong = ComplexVector::new(m + 1);
        let mut v_wrong = ComplexMatrix::new(m + 1, m);
        assert_eq!(
            complex_mat_gen_eigen(&mut alpha_wrong, &mut beta, &mut v, &mut a, &mut b),
            Err("vectors are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen(&mut alpha, &mut beta_wrong, &mut v, &mut a, &mut b),
            Err("vectors are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen(&mut alpha, &mut beta, &mut v_wrong, &mut a, &mut b),
            Err("matrices are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen(&mut alpha, &mut beta, &mut v, &mut a, &mut b_wrong1,),
            Err("matrices are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen(&mut alpha, &mut beta, &mut v, &mut a, &mut b_wrong2,),
            Err("matrices are incompatible")
        );
    }

    #[test]
    fn complex_mat_gen_eigen_lr_captures_errors() {
        let mut a = ComplexMatrix::new(2, 1);
        let m = a.nrow();
        let mut b = ComplexMatrix::new(m, m);
        let mut alpha = ComplexVector::new(m);
        let mut beta = ComplexVector::new(m);
        let mut u = ComplexMatrix::new(m, m);
        let mut v = ComplexMatrix::new(m, m);
        assert_eq!(
            complex_mat_gen_eigen_lr(&mut alpha, &mut beta, &mut u, &mut v, &mut a, &mut b),
            Err("matrix must be square")
        );
        let mut a = ComplexMatrix::new(2, 2);
        let mut b_wrong1 = ComplexMatrix::new(m + 1, m);
        let mut b_wrong2 = ComplexMatrix::new(m, m + 1);
        let mut alpha_wrong = ComplexVector::new(m + 1);
        let mut beta_wrong = ComplexVector::new(m + 1);
        let mut u_wrong = ComplexMatrix::new(m + 1, m);
        let mut v_wrong = ComplexMatrix::new(m + 1, m);
        assert_eq!(
            complex_mat_gen_eigen_lr(&mut alpha_wrong, &mut beta, &mut u, &mut v, &mut a, &mut b),
            Err("vectors are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen_lr(&mut alpha, &mut beta_wrong, &mut u, &mut v, &mut a, &mut b),
            Err("vectors are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen_lr(&mut alpha, &mut beta, &mut u_wrong, &mut v, &mut a, &mut b),
            Err("matrices are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen_lr(&mut alpha, &mut beta, &mut u, &mut v_wrong, &mut a, &mut b),
            Err("matrices are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen_lr(&mut alpha, &mut beta, &mut u, &mut v, &mut a, &mut b_wrong1,),
            Err("matrices are incompatible")
        );
        assert_eq!(
            complex_mat_gen_eigen_lr(&mut alpha, &mut beta, &mut u, &mut v, &mut a, &mut b_wrong2,),
            Err("matrices are incompatible")
        );
    }

    #[test]
    fn complex_mat_gen_eigen_works() {
        #[rustfmt::skip]
        let a_data = &[
            [cpx!(2.0, 4.0), cpx!(1.0, 6.0), cpx!(2.0, 8.0), cpx!(4.0, 4.0)],
            [cpx!(3.0, 3.0), cpx!(6.0, 1.0), cpx!(5.0, 3.0), cpx!(0.0, 0.0)],
            [cpx!(5.0, 1.0), cpx!(8.0, 5.0), cpx!(3.0, 2.0), cpx!(8.0, 5.0)],
            [cpx!(7.0, 6.0), cpx!(3.0, 7.0), cpx!(2.0, 1.0), cpx!(5.0, 4.0)],
        ];
        #[rustfmt::skip]
        let b_data = &[
            [cpx!(0.0, 0.0), cpx!(1.0, 0.0), cpx!(0.0, 0.0), cpx!(0.0, 0.0)],
            [cpx!(0.0, 0.0), cpx!(0.0, 0.0), cpx!(1.0, 0.0), cpx!(0.0, 0.0)],
            [cpx!(0.0, 0.0), cpx!(0.0, 0.0), cpx!(0.0, 0.0), cpx!(1.0, 0.0)],
            [cpx!(1.0, 0.0), cpx!(0.0, 0.0), cpx!(0.0, 0.0), cpx!(0.0, 0.0)],
        ];
        let mut a = ComplexMatrix::from(a_data);
        let mut b = ComplexMatrix::from(b_data);
        let m = a.nrow();
        let mut alpha = ComplexVector::new(m);
        let mut beta = ComplexVector::new(m);
        let mut v = ComplexMatrix::new(m, m);
        complex_mat_gen_eigen(&mut alpha, &mut beta, &mut v, &mut a, &mut b).unwrap();
        // println!("α =\n{}", alpha);
        // println!("β =\n{}", beta);
        // println!("v =\n{}", v);
        // compare with reference (scipy)
        #[rustfmt::skip]
        let alpha_ref = &[
            cpx!(15.8864, 15.0474),
            cpx!( 7.0401,  2.0585),
            cpx!( 1.7083,  4.1133),
            cpx!(-3.6348, -1.2193),
        ];
        #[rustfmt::skip]
        let beta_ref = &[
            cpx!(1.0, 0.0),
            cpx!(1.0, 0.0),
            cpx!(1.0, 0.0),
            cpx!(1.0, 0.0),
        ];
        complex_vec_approx_eq(&alpha, alpha_ref, 1e-4);
        complex_vec_approx_eq(&beta, beta_ref, 1e-14);
        complex_check_gen_eigen(a_data, b_data, &v, &alpha, &beta, 1e-13);
    }

    #[test]
    fn complex_mat_gen_eigen_lr_works() {
        // https://www.ibm.com/docs/en/essl/6.2?topic=eas-sggev-dggev-cggev-zggev-sggevx-dggevx-cggevx-zggevx-eigenvalues-optionally-right-eigenvectors-left-eigenvectors-reciprocal-condition-numbers-eigenvalues-reciprocal-condition-numbers-right-eigenvectors-general-matrix-generalized-eigenproblem
        #[rustfmt::skip]
        let a_data = &[
            [cpx!( 1.0, 2.0), cpx!( 3.0, 4.0), cpx!(21.0,22.0)],
            [cpx!(43.0,44.0), cpx!(13.0,14.0), cpx!(15.0,16.0)],
            [cpx!( 5.0, 6.0), cpx!( 7.0, 8.0), cpx!(25.0,26.0)],
        ];
        #[rustfmt::skip]
        let b_data = &[
            [cpx!( 2.0, 0.0), cpx!( 0.0,-1.0), cpx!( 0.0, 0.0)],
            [cpx!( 0.0, 1.0), cpx!( 2.0, 0.0), cpx!( 0.0, 0.0)],
            [cpx!( 0.0, 0.0), cpx!( 0.0, 0.0), cpx!( 3.0, 0.0)],
        ];
        let mut a = ComplexMatrix::from(a_data);
        let mut b = ComplexMatrix::from(b_data);
        let m = a.nrow();
        let mut alpha = ComplexVector::new(m);
        let mut beta = ComplexVector::new(m);
        let mut u = ComplexMatrix::new(m, m);
        let mut v = ComplexMatrix::new(m, m);
        complex_mat_gen_eigen_lr(&mut alpha, &mut beta, &mut u, &mut v, &mut a, &mut b).unwrap();
        println!("α =\n{}", alpha);
        println!("β =\n{}", beta);
        println!("u =\n{}", u);
        println!("v =\n{}", v);
        // compare with reference
        #[rustfmt::skip]
        let alpha_ref = &[
            cpx!( 15.863783, 41.115283),
            cpx!(-12.917205, 19.973815),
            cpx!(  3.215518, -4.912439),
        ];
        #[rustfmt::skip]
        let beta_ref = &[
            cpx!(1.668461, 0.0),
            cpx!(2.024212, 0.0),
            cpx!(2.664836, 0.0),
        ];
        complex_vec_approx_eq(&alpha, alpha_ref, 1e-6);
        complex_vec_approx_eq(&beta, beta_ref, 1e-6);
        // check the definition: a ⋅ vj = lj ⋅ b ⋅ vj
        complex_check_gen_eigen(a_data, b_data, &v, &alpha, &beta, 1e-13);
    }
}