| 1 |
#[cfg(feature = "serde-serialize-no-std")] |
| 2 |
use serde::{Deserialize, Serialize}; |
| 3 |
|
| 4 |
use num_complex::Complex; |
| 5 |
use simba::scalar::ComplexField; |
| 6 |
use std::cmp; |
| 7 |
use std::fmt::Display; |
| 8 |
use std::ops::MulAssign; |
| 9 |
|
| 10 |
use crate::allocator::Allocator; |
| 11 |
use crate::base::dimension::{Dim, DimDiff, DimSub, Dyn, U1, U2, U3}; |
| 12 |
use crate::base::storage::Storage; |
| 13 |
use crate::base::{ |
| 14 |
DefaultAllocator, Hessenberg, OMatrix, OVector, SquareMatrix, Unit, Vector2, Vector3, |
| 15 |
}; |
| 16 |
use crate::constraint::{DimEq, ShapeConstraint}; |
| 17 |
|
| 18 |
use crate::geometry::{Reflection, UnitComplex}; |
| 19 |
use crate::linalg::householder; |
| 20 |
use crate::linalg::Schur; |
| 21 |
|
| 22 |
/// Eigendecomposition of a real matrix with real eigenvalues (or complex eigen values for complex matrices). |
| 23 |
#[cfg_attr(feature = "serde-serialize-no-std", derive(Serialize, Deserialize))] |
| 24 |
#[cfg_attr( |
| 25 |
feature = "serde-serialize-no-std", |
| 26 |
serde(bound(serialize = "DefaultAllocator: Allocator<D>, |
| 27 |
OVector<T, D>: Serialize, |
| 28 |
OMatrix<T, D, D>: Serialize")) |
| 29 |
)] |
| 30 |
#[cfg_attr( |
| 31 |
feature = "serde-serialize-no-std", |
| 32 |
serde(bound(deserialize = "DefaultAllocator: Allocator<D>, |
| 33 |
OVector<T, D>: Serialize, |
| 34 |
OMatrix<T, D, D>: Deserialize<'de>")) |
| 35 |
)] |
| 36 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 37 |
#[derive(Clone, Debug)] |
| 38 |
pub struct Eigen<T: ComplexField, D: Dim> |
| 39 |
where |
| 40 |
DefaultAllocator: Allocator<D, D> + Allocator<D>, |
| 41 |
{ |
| 42 |
pub eigenvectors: OMatrix<T, D, D>, |
| 43 |
pub eigenvalues: OVector<T, D>, |
| 44 |
} |
| 45 |
|
| 46 |
impl<T: ComplexField, D: Dim> Copy for Eigen<T, D> |
| 47 |
where |
| 48 |
DefaultAllocator: Allocator<D, D> + Allocator<D>, |
| 49 |
OMatrix<T, D, D>: Copy, |
| 50 |
OVector<T, D>: Copy, |
| 51 |
{ |
| 52 |
} |
| 53 |
|
| 54 |
impl<T: ComplexField, D: Dim> Eigen<T, D> |
| 55 |
where |
| 56 |
D: DimSub<U1>, // For Hessenberg. |
| 57 |
ShapeConstraint: DimEq<Dyn, DimDiff<D, U1>>, // For Hessenberg. |
| 58 |
DefaultAllocator: |
| 59 |
Allocator<D, DimDiff<D, U1>> + Allocator<DimDiff<D, U1>> + Allocator<D, D> + Allocator<D>, |
| 60 |
// XXX: for debug |
| 61 |
DefaultAllocator: Allocator<D, D>, |
| 62 |
OMatrix<T, D, D>: Display, |
| 63 |
{ |
| 64 |
/// Computes the eigendecomposition of a diagonalizable matrix with Complex eigenvalues. |
| 65 |
pub fn new(m: OMatrix<T, D, D>) -> Option<Eigen<T, D>> { |
| 66 |
assert!( |
| 67 |
m.is_square(), |
| 68 |
"Unable to compute the eigendecomposition of a non-square matrix." |
| 69 |
); |
| 70 |
|
| 71 |
let dim = m.nrows(); |
| 72 |
let (mut eigenvectors, mut eigenvalues) = Schur::new(m, 0).unwrap().unpack(); |
| 73 |
|
| 74 |
println!("Schur eigenvalues: {}", eigenvalues); |
| 75 |
|
| 76 |
// Check that the eigenvalues are all Complex. |
| 77 |
for i in 0..dim - 1 { |
| 78 |
if !eigenvalues[(i + 1, i)].is_zero() { |
| 79 |
return None; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
for j in 1..dim { |
| 84 |
for i in 0..j { |
| 85 |
let diff = eigenvalues[(i, i)] - eigenvalues[(j, j)]; |
| 86 |
|
| 87 |
if diff.is_zero() && !eigenvalues[(i, j)].is_zero() { |
| 88 |
return None; |
| 89 |
} |
| 90 |
|
| 91 |
let z = -eigenvalues[(i, j)] / diff; |
| 92 |
|
| 93 |
for k in j + 1..dim { |
| 94 |
eigenvalues[(i, k)] -= z * eigenvalues[(j, k)]; |
| 95 |
} |
| 96 |
|
| 97 |
for k in 0..dim { |
| 98 |
eigenvectors[(k, j)] += z * eigenvectors[(k, i)]; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Normalize the eigenvector basis. |
| 104 |
for i in 0..dim { |
| 105 |
let _ = eigenvectors.column_mut(i).normalize_mut(); |
| 106 |
} |
| 107 |
|
| 108 |
Some(Eigen { |
| 109 |
eigenvectors, |
| 110 |
eigenvalues: eigenvalues.diagonal(), |
| 111 |
}) |
| 112 |
} |
| 113 |
} |