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
//! Implementation of the Schur and eigenvalue decomposition for general matrices.
use crate::UnsafeRandom1DAccessByValue;
use crate::base_types::RlstResult;
use crate::dense::array::{Array, DynArray};
use crate::dense::linalg::lapack::interface::geev::{JobVl, JobVr};
use crate::traits::base_operations::Shape;
use crate::traits::linalg::decompositions::EigenvalueDecomposition;
use crate::traits::linalg::lapack::Lapack;
use crate::traits::rlst_num::RlstScalar;
use super::interface::gees::JobVs;
/// Eigenvalue decomposition mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EigMode {
/// Compute eigenvalues only.
EigenvaluesOnly,
/// Compute right eigenvectors.
RightEigenvectors,
/// Compute left eigenvectors.
LeftEigenvectors,
/// Compute both right and left eigenvectors.
BothEigenvectors,
}
impl<Item, ArrayImpl> EigenvalueDecomposition for Array<ArrayImpl, 2>
where
ArrayImpl: UnsafeRandom1DAccessByValue<Item = Item> + Shape<2>,
Item: Lapack,
{
type Item = Item;
/// Return the eigenvalues of the matrix.
///
/// The eigenvalues are computed using the LAPACK `?gees` routine.
///
/// # Errors
/// Returns an error if the matrix is not square or if the LAPACK routine fails.
fn eigenvalues(&self) -> RlstResult<DynArray<<Self::Item as RlstScalar>::Complex, 1>> {
let mut a = DynArray::new_from(self);
let [m, n] = a.shape();
assert_eq!(
m, n,
"Matrix must be square for eigenvalue decomposition. Got shape: [{m}, {n}]"
);
let mut w = DynArray::from_shape([n]);
Item::gees(
JobVs::None,
n,
a.data_mut().unwrap(),
n,
w.data_mut().unwrap(),
None,
1,
)?;
Ok(w)
}
/// Compute the Schur decomposition of the matrix.
///
/// The Schur decomposition of a square matrix `A` is given by `A = Z * T * Z^H`, where:
/// - `T` is a block-upper triangular matrix. Its diagonal blocks are 1x1 or 2x2 and
/// encode the eigenvalues of the matrix.
/// - `Z` is a unitary matrix.
///
/// Returns a tuple `(T, Z)`.
///
/// # Errors
/// Returns an error if the matrix is not square or if the LAPACK routine fails.
fn schur(&self) -> RlstResult<(DynArray<Self::Item, 2>, DynArray<Self::Item, 2>)> {
let mut a = DynArray::new_from(self);
let [m, n] = a.shape();
assert_eq!(
m, n,
"Matrix must be square for Schur decomposition. Got shape: [{m}, {n}]"
);
let mut w = DynArray::from_shape([n]);
let mut vs = DynArray::from_shape([n, n]);
Item::gees(
JobVs::Compute,
n,
a.data_mut().unwrap(),
n,
w.data_mut().unwrap(),
vs.data_mut(),
n,
)?;
Ok((a, vs))
}
/// Compute the eigenvalues and eigenvectors of the matrix.
///
/// The eigenvalues and eigenvectors are computed using the LAPACK `?geev` routine.
///
/// # Arguments
/// - `mode`: Specifies which eigenvectors to compute.
///
/// # Returns
/// A tuple `(lam, vr, vl)` containing:
/// - `lam`: A vector of eigenvalues.
/// - `vr`: An optional matrix of right eigenvectors.
/// - `vl`: An optional matrix of left eigenvectors.
///
/// # Errors
/// Returns an error if the matrix is not square or if the LAPACK routine fails.
fn eig(
&self,
mode: EigMode,
) -> RlstResult<(
DynArray<<Self::Item as RlstScalar>::Complex, 1>,
Option<DynArray<<Self::Item as RlstScalar>::Complex, 2>>,
Option<DynArray<<Self::Item as RlstScalar>::Complex, 2>>,
)> {
let mut a = DynArray::new_from(self);
let [m, n] = a.shape();
assert_eq!(
m, n,
"Matrix must be square for eigenvalue decomposition. Got shape: [{m}, {n}]"
);
let mut w = DynArray::<<Item as RlstScalar>::Complex, 1>::from_shape([n]);
let (jobvl, jobvr, mut vl, mut vr) = match mode {
EigMode::EigenvaluesOnly => (JobVl::None, JobVr::None, None, None),
EigMode::RightEigenvectors => (
JobVl::None,
JobVr::Compute,
None,
Some(DynArray::<Item::Complex, 2>::from_shape([n, n])),
),
EigMode::LeftEigenvectors => (
JobVl::Compute,
JobVr::None,
Some(DynArray::<Item::Complex, 2>::from_shape([n, n])),
None,
),
EigMode::BothEigenvectors => (
JobVl::Compute,
JobVr::Compute,
Some(DynArray::<Item::Complex, 2>::from_shape([n, n])),
Some(DynArray::<Item::Complex, 2>::from_shape([n, n])),
),
};
Item::geev(
jobvl,
jobvr,
n,
a.data_mut().unwrap(),
n,
w.data_mut().unwrap(),
vl.as_mut().map(|v| v.data_mut().unwrap()),
n,
vr.as_mut().map(|v| v.data_mut().unwrap()),
n,
)?;
Ok((w, vr, vl))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::base_types::{c32, c64};
use crate::dense::array::DynArray;
use crate::dot_blas;
use crate::traits::base_operations::*;
use crate::traits::linalg::Inverse;
use itertools::izip;
use paste::paste;
macro_rules! implement_eigendecomposition_tests {
($scalar:ty, $tol:expr) => {
paste! {
#[test]
fn [<test_eigendecomposition_$scalar>]() {
let n = 11;
let mut a = DynArray::<$scalar, 2>::from_shape([n, n]);
a.fill_from_seed_equally_distributed(0);
let lam = a.eigenvalues().unwrap();
let (t, z) = a.schur().unwrap();
// Test the Schur decomposition
let actual = dot_blas!(z.r(), t.r(), z.r().conj().transpose().eval());
crate::assert_array_relative_eq!(actual, a, $tol);
let (lam2, vr, vl) = a.eig(EigMode::BothEigenvectors).unwrap();
crate::assert_array_relative_eq!(lam, lam2, $tol);
// Test the left eigenvectors
// First convert a to a complex matrix
let a_complex = a.into_type::<<$scalar as RlstScalar>::Complex>().eval();
// Now create a diagonal matrix from the eigenvalues
let mut diag = DynArray::from_shape([n, n]);
izip!(diag.diag_iter_mut(), lam2.iter_value()).for_each(|(v_elem, w_elem)| {
*v_elem = w_elem;
});
// Now check the left eigenvectors
let vlh = vl.unwrap().conj().transpose().eval();
let actual = dot_blas!(vlh.inverse().unwrap(), dot_blas!(diag.r(), vlh));
// We test the absolute distance since some imaginary parts are zero
// making relative tests fail.
crate::assert_array_abs_diff_eq!(actual, a_complex, $tol);
// Now check the right eigenvectors
let vr = vr.unwrap();
let actual = dot_blas!(vr.r(), dot_blas!(diag, vr.r().inverse().unwrap()));
crate::assert_array_abs_diff_eq!(actual, a_complex, $tol);
}
}
};
}
implement_eigendecomposition_tests!(f32, 1E-3);
implement_eigendecomposition_tests!(f64, 1E-10);
implement_eigendecomposition_tests!(c32, 5E-3);
implement_eigendecomposition_tests!(c64, 1E-10);
}