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
//! Eigenvalue decomposition for non-symmetric square matrices
use crate::error::*;
use crate::layout::*;
use crate::types::*;
pub use lax::GeneralizedEigenvalue;
use ndarray::*;
#[cfg_attr(doc, katexit::katexit)]
/// Eigenvalue decomposition of general matrix reference
pub trait Eig {
/// EigVec is the right eivenvector
type EigVal;
type EigVec;
/// Calculate eigenvalues with the right eigenvector
///
/// $$ A u_i = \lambda_i u_i $$
///
/// ```
/// use ndarray::*;
/// use ndarray_linalg::*;
///
/// let a: Array2<f64> = array![
/// [-1.01, 0.86, -4.60, 3.31, -4.81],
/// [ 3.98, 0.53, -7.04, 5.29, 3.55],
/// [ 3.30, 8.26, -3.89, 8.20, -1.51],
/// [ 4.43, 4.96, -7.66, -7.33, 6.18],
/// [ 7.31, -6.43, -6.16, 2.47, 5.58],
/// ];
/// let (eigs, vecs) = a.eig().unwrap();
///
/// let a = a.map(|v| v.as_c());
/// for (&e, vec) in eigs.iter().zip(vecs.axis_iter(Axis(1))) {
/// let ev = vec.map(|v| v * e);
/// let av = a.dot(&vec);
/// assert_close_l2!(&av, &ev, 1e-5);
/// }
/// ```
fn eig(&self) -> Result<(Self::EigVal, Self::EigVec)>;
}
impl<A> Eig for ArrayRef<A, Ix2>
where
A: Scalar + Lapack,
{
type EigVal = Array1<A::Complex>;
type EigVec = Array2<A::Complex>;
fn eig(&self) -> Result<(Self::EigVal, Self::EigVec)> {
let mut a = self.to_owned();
let layout = a.square_layout()?;
let (s, t) = A::eig(true, layout, a.as_allocated_mut()?)?;
let n = layout.len() as usize;
Ok((
ArrayBase::from(s),
Array2::from_shape_vec((n, n).f(), t).unwrap(),
))
}
}
/// Calculate eigenvalues without eigenvectors
pub trait EigVals {
type EigVal;
fn eigvals(&self) -> Result<Self::EigVal>;
}
impl<A> EigVals for ArrayRef<A, Ix2>
where
A: Scalar + Lapack,
{
type EigVal = Array1<A::Complex>;
fn eigvals(&self) -> Result<Self::EigVal> {
let mut a = self.to_owned();
let (s, _) = A::eig(false, a.square_layout()?, a.as_allocated_mut()?)?;
Ok(ArrayBase::from(s))
}
}
#[cfg_attr(doc, katexit::katexit)]
/// Eigenvalue decomposition of general matrix reference
pub trait EigGeneralized {
/// EigVec is the right eivenvector
type EigVal;
type EigVec;
type Real;
/// Calculate eigenvalues with the right eigenvector
///
/// $$ A u_i = \lambda_i B u_i $$
///
/// ```
/// use ndarray::*;
/// use ndarray_linalg::*;
///
/// let a: Array2<f64> = array![
/// [-1.01, 0.86, -4.60, 3.31, -4.81],
/// [ 3.98, 0.53, -7.04, 5.29, 3.55],
/// [ 3.30, 8.26, -3.89, 8.20, -1.51],
/// [ 4.43, 4.96, -7.66, -7.33, 6.18],
/// [ 7.31, -6.43, -6.16, 2.47, 5.58],
/// ];
/// let b: Array2<f64> = array![
/// [ 1.23, -4.56, 7.89, 0.12, -3.45],
/// [ 6.78, -9.01, 2.34, -5.67, 8.90],
/// [-1.11, 3.33, -6.66, 9.99, -2.22],
/// [ 4.44, -7.77, 0.00, 1.11, 5.55],
/// [-8.88, 6.66, -3.33, 2.22, -9.99],
/// ];
/// let (geneigs, vecs) = (a.clone(), b.clone()).eig_generalized(None).unwrap();
///
/// let a = a.map(|v| v.as_c());
/// let b = b.map(|v| v.as_c());
/// for (ge, vec) in geneigs.iter().zip(vecs.axis_iter(Axis(1))) {
/// if let GeneralizedEigenvalue::Finite(e, _) = ge {
/// let ebv = b.dot(&vec).map(|v| v * e);
/// let av = a.dot(&vec);
/// assert_close_l2!(&av, &ebv, 1e-5);
/// }
/// }
/// ```
///
/// # Arguments
///
/// * `thresh_opt` - An optional threshold for determining approximate zero |β| values when
/// computing the eigenvalues as α/β. If `None`, no approximate comparisons to zero will be
/// made.
fn eig_generalized(
self,
thresh_opt: Option<Self::Real>,
) -> Result<(Self::EigVal, Self::EigVec)>;
}
/// Turn arrays, references to arrays, and [`ArrayRef`]s into owned arrays
pub trait MaybeOwnedMatrix {
type Elem;
/// Convert into an owned array, cloning only when necessary.
fn into_owned(self) -> Array2<Self::Elem>;
}
impl<S> MaybeOwnedMatrix for ArrayBase<S, Ix2>
where
S: Data,
S::Elem: Clone,
{
type Elem = S::Elem;
fn into_owned(self) -> Array2<S::Elem> {
ArrayBase::into_owned(self)
}
}
impl<S> MaybeOwnedMatrix for &ArrayBase<S, Ix2>
where
S: Data,
S::Elem: Clone,
{
type Elem = S::Elem;
fn into_owned(self) -> Array2<S::Elem> {
self.to_owned()
}
}
impl<A> MaybeOwnedMatrix for &ArrayRef2<A>
where
A: Clone,
{
type Elem = A;
fn into_owned(self) -> Array2<A> {
self.to_owned()
}
}
impl<T1, T2> EigGeneralized for (T1, T2)
where
T1: MaybeOwnedMatrix,
T1::Elem: Lapack + Scalar,
T2: MaybeOwnedMatrix<Elem = T1::Elem>,
{
type EigVal = Array1<GeneralizedEigenvalue<<T1::Elem as Scalar>::Complex>>;
type EigVec = Array2<<T1::Elem as Scalar>::Complex>;
type Real = <T1::Elem as Scalar>::Real;
fn eig_generalized(
self,
thresh_opt: Option<Self::Real>,
) -> Result<(Self::EigVal, Self::EigVec)> {
let (mut a, mut b) = (self.0.into_owned(), self.1.into_owned());
let layout = a.square_layout()?;
let (s, t) = T1::Elem::eig_generalized(
true,
layout,
a.as_allocated_mut()?,
b.as_allocated_mut()?,
thresh_opt,
)?;
let n = layout.len() as usize;
Ok((
ArrayBase::from(s),
Array2::from_shape_vec((n, n).f(), t).unwrap(),
))
}
}
#[cfg(test)]
mod tests {
use crate::MaybeOwnedMatrix;
#[test]
fn test_maybe_owned_matrix() {
let a = array![[1.0, 2.0], [3.0, 4.0]];
let a_ptr = a.as_ptr();
let a1 = MaybeOwnedMatrix::into_owned(a);
assert_eq!(a_ptr, a1.as_ptr());
let b = a1.clone();
let b1 = MaybeOwnedMatrix::into_owned(&b);
assert_eq!(b, b1);
assert_ne!(b.as_ptr(), b1.as_ptr());
let b2 = MaybeOwnedMatrix::into_owned(&*b);
assert_eq!(b, b2);
assert_ne!(b.as_ptr(), b2.as_ptr());
}
}