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
use num_complex::{Complex};
use num_traits::{Float};

use crate::{MMul, Diag, QRHouseholder, Matrix};

const ITERATIONS: usize = 1000;

pub trait Eig: Matrix
{
    type Output;

    /// Returns an approximation of the eigenvalues of a matrix
    /// 
    /// eig(A)
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// let a = [
    ///     [1.0, 2.0],
    ///     [3.0, 4.0]
    /// ];
    /// let eig_a = eig(a);
    /// ```
    fn eig(&self) -> Self::Output;
}


impl<F: Float, const L: usize, const H: usize> Eig for [[Complex<F>; L]; H]
where
    [[Complex<F>; L]; H]: QRHouseholder,
    <[[Complex<F>; L]; H] as QRHouseholder>::OutputR: Diag
        + MMul<<[[Complex<F>; L]; H] as QRHouseholder>::OutputQ, Output = [[Complex<F>; L]; H]>
{
    type Output = <<[[Complex<F>; L]; H] as QRHouseholder>::OutputR as Diag>::Output;
    fn eig(&self) -> Self::Output
    {
        let mut a = self.clone();
        for _ in 0..ITERATIONS
        {
            let (q, r) = a.qr_householder();
            a = r.mul(q)
        }
        let r = a.qr_householder().1;
        r.diag()
    }
}

impl<const L: usize, const H: usize> Eig for [[f32; L]; H]
where
    Self: Matrix,
    [[Complex<f32>; L]; H]: Eig
{
    type Output = <[[Complex<f32>; L]; H] as Eig>::Output;
    fn eig(&self) -> Self::Output
    {
        self.map(|ar| ar.map(|arc| Complex::from(arc))).eig()
    }
}

impl<const L: usize, const H: usize> Eig for [[f64; L]; H]
where
    Self: Matrix,
    [[Complex<f64>; L]; H]: Eig
{
    type Output = <[[Complex<f64>; L]; H] as Eig>::Output;
    fn eig(&self) -> Self::Output
    {
        self.map(|ar| ar.map(|arc| Complex::from(arc))).eig()
    }
}