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
//! Eigenvalue problem for general matricies
//!
//! LAPACK correspondance
//! ----------------------
//!
//! | f32 | f64 | c32 | c64 |
//! |:------|:------|:------|:------|
//! | sgeev | dgeev | cgeev | zgeev |
//!
use crate::{error::*, layout::MatrixLayout, *};
use cauchy::*;
use num_traits::{ToPrimitive, Zero};
#[cfg_attr(doc, katexit::katexit)]
/// Eigenvalue problem for general matrix
///
/// To manage memory more strictly, use [EigWork].
///
/// Right and Left eigenvalue problem
/// ----------------------------------
/// LAPACK can solve both right eigenvalue problem
/// $$
/// AV_R = V_R \Lambda
/// $$
/// where $V_R = \left( v_R^1, \cdots, v_R^n \right)$ are right eigenvectors
/// and left eigenvalue problem
/// $$
/// V_L^\dagger A = V_L^\dagger \Lambda
/// $$
/// where $V_L = \left( v_L^1, \cdots, v_L^n \right)$ are left eigenvectors
/// and eigenvalues
/// $$
/// \Lambda = \begin{pmatrix}
/// \lambda_1 & & 0 \\\\
/// & \ddots & \\\\
/// 0 & & \lambda_n
/// \end{pmatrix}
/// $$
/// which satisfies $A v_R^i = \lambda_i v_R^i$ and
/// $\left(v_L^i\right)^\dagger A = \lambda_i \left(v_L^i\right)^\dagger$
/// for column-major matrices, although row-major matrices are not supported.
/// Since a row-major matrix can be interpreted
/// as a transpose of a column-major matrix,
/// this transforms right eigenvalue problem to left one:
///
/// $$
/// A^\dagger V = V Λ ⟺ V^\dagger A = Λ V^\dagger
/// $$
///
#[non_exhaustive]
pub struct EigWork<T: Scalar> {
/// Problem size
pub n: i32,
/// Compute right eigenvectors or not
pub jobvr: JobEv,
/// Compute left eigenvectors or not
pub jobvl: JobEv,
/// Eigenvalues
pub eigs: Vec<MaybeUninit<T::Complex>>,
/// Real part of eigenvalues used in real routines
pub eigs_re: Option<Vec<MaybeUninit<T::Real>>>,
/// Imaginary part of eigenvalues used in real routines
pub eigs_im: Option<Vec<MaybeUninit<T::Real>>>,
/// Left eigenvectors
pub vc_l: Option<Vec<MaybeUninit<T::Complex>>>,
/// Left eigenvectors used in real routines
pub vr_l: Option<Vec<MaybeUninit<T::Real>>>,
/// Right eigenvectors
pub vc_r: Option<Vec<MaybeUninit<T::Complex>>>,
/// Right eigenvectors used in real routines
pub vr_r: Option<Vec<MaybeUninit<T::Real>>>,
/// Working memory
pub work: Vec<MaybeUninit<T>>,
/// Working memory with `T::Real`
pub rwork: Option<Vec<MaybeUninit<T::Real>>>,
}
impl<T> EigWork<T>
where
T: Scalar,
EigWork<T>: EigWorkImpl<Elem = T>,
{
/// Create new working memory for eigenvalues compution.
pub fn new(calc_v: bool, l: MatrixLayout) -> Result<Self> {
EigWorkImpl::new(calc_v, l)
}
/// Compute eigenvalues and vectors on this working memory.
pub fn calc(&mut self, a: &mut [T]) -> Result<EigRef<T>> {
EigWorkImpl::calc(self, a)
}
/// Compute eigenvalues and vectors by consuming this working memory.
pub fn eval(self, a: &mut [T]) -> Result<EigOwned<T>> {
EigWorkImpl::eval(self, a)
}
}
/// Owned result of eigenvalue problem by [EigWork::eval]
#[derive(Debug, Clone, PartialEq)]
pub struct EigOwned<T: Scalar> {
/// Eigenvalues
pub eigs: Vec<T::Complex>,
/// Right eigenvectors
pub vr: Option<Vec<T::Complex>>,
/// Left eigenvectors
pub vl: Option<Vec<T::Complex>>,
}
/// Reference result of eigenvalue problem by [EigWork::calc]
#[derive(Debug, Clone, PartialEq)]
pub struct EigRef<'work, T: Scalar> {
/// Eigenvalues
pub eigs: &'work [T::Complex],
/// Right eigenvectors
pub vr: Option<&'work [T::Complex]>,
/// Left eigenvectors
pub vl: Option<&'work [T::Complex]>,
}
/// Helper trait for implementing [EigWork] methods
pub trait EigWorkImpl: Sized {
type Elem: Scalar;
fn new(calc_v: bool, l: MatrixLayout) -> Result<Self>;
fn calc<'work>(&'work mut self, a: &mut [Self::Elem]) -> Result<EigRef<'work, Self::Elem>>;
fn eval(self, a: &mut [Self::Elem]) -> Result<EigOwned<Self::Elem>>;
}
macro_rules! impl_eig_work_c {
($c:ty, $ev:path) => {
impl EigWorkImpl for EigWork<$c> {
type Elem = $c;
fn new(calc_v: bool, l: MatrixLayout) -> Result<Self> {
let (n, _) = l.size();
let (jobvl, jobvr) = if calc_v {
match l {
MatrixLayout::C { .. } => (JobEv::All, JobEv::None),
MatrixLayout::F { .. } => (JobEv::None, JobEv::All),
}
} else {
(JobEv::None, JobEv::None)
};
let mut eigs = vec_uninit(n as usize);
let mut rwork = vec_uninit(2 * n as usize);
let mut vc_l = jobvl.then(|| vec_uninit((n * n) as usize));
let mut vc_r = jobvr.then(|| vec_uninit((n * n) as usize));
// calc work size
let mut info = 0;
let mut work_size = [<$c>::zero()];
unsafe {
$ev(
jobvl.as_ptr(),
jobvr.as_ptr(),
&n,
std::ptr::null_mut(),
&n,
AsPtr::as_mut_ptr(&mut eigs),
AsPtr::as_mut_ptr(vc_l.as_deref_mut().unwrap_or(&mut [])),
&n,
AsPtr::as_mut_ptr(vc_r.as_deref_mut().unwrap_or(&mut [])),
&n,
AsPtr::as_mut_ptr(&mut work_size),
&(-1),
AsPtr::as_mut_ptr(&mut rwork),
&mut info,
)
};
info.as_lapack_result()?;
let lwork = work_size[0].to_usize().unwrap();
let work: Vec<MaybeUninit<$c>> = vec_uninit(lwork);
Ok(Self {
n,
jobvl,
jobvr,
eigs,
eigs_re: None,
eigs_im: None,
rwork: Some(rwork),
vc_l,
vc_r,
vr_l: None,
vr_r: None,
work,
})
}
fn calc<'work>(
&'work mut self,
a: &mut [Self::Elem],
) -> Result<EigRef<'work, Self::Elem>> {
let lwork = self.work.len().to_i32().unwrap();
let mut info = 0;
unsafe {
$ev(
self.jobvl.as_ptr(),
self.jobvr.as_ptr(),
&self.n,
AsPtr::as_mut_ptr(a),
&self.n,
AsPtr::as_mut_ptr(&mut self.eigs),
AsPtr::as_mut_ptr(self.vc_l.as_deref_mut().unwrap_or(&mut [])),
&self.n,
AsPtr::as_mut_ptr(self.vc_r.as_deref_mut().unwrap_or(&mut [])),
&self.n,
AsPtr::as_mut_ptr(&mut self.work),
&lwork,
AsPtr::as_mut_ptr(self.rwork.as_mut().unwrap()),
&mut info,
)
};
info.as_lapack_result()?;
// Hermite conjugate
if let Some(vl) = self.vc_l.as_mut() {
for value in vl {
let value = unsafe { value.assume_init_mut() };
value.im = -value.im;
}
}
Ok(EigRef {
eigs: unsafe { self.eigs.slice_assume_init_ref() },
vl: self
.vc_l
.as_ref()
.map(|v| unsafe { v.slice_assume_init_ref() }),
vr: self
.vc_r
.as_ref()
.map(|v| unsafe { v.slice_assume_init_ref() }),
})
}
fn eval(mut self, a: &mut [Self::Elem]) -> Result<EigOwned<Self::Elem>> {
let _eig_ref = self.calc(a)?;
Ok(EigOwned {
eigs: unsafe { self.eigs.assume_init() },
vl: self.vc_l.map(|v| unsafe { v.assume_init() }),
vr: self.vc_r.map(|v| unsafe { v.assume_init() }),
})
}
}
};
}
impl_eig_work_c!(c32, lapack_sys::cgeev_);
impl_eig_work_c!(c64, lapack_sys::zgeev_);
macro_rules! impl_eig_work_r {
($f:ty, $ev:path) => {
impl EigWorkImpl for EigWork<$f> {
type Elem = $f;
fn new(calc_v: bool, l: MatrixLayout) -> Result<Self> {
let (n, _) = l.size();
let (jobvl, jobvr) = if calc_v {
match l {
MatrixLayout::C { .. } => (JobEv::All, JobEv::None),
MatrixLayout::F { .. } => (JobEv::None, JobEv::All),
}
} else {
(JobEv::None, JobEv::None)
};
let mut eigs_re = vec_uninit(n as usize);
let mut eigs_im = vec_uninit(n as usize);
let mut vr_l = jobvl.then(|| vec_uninit((n * n) as usize));
let mut vr_r = jobvr.then(|| vec_uninit((n * n) as usize));
let vc_l = jobvl.then(|| vec_uninit((n * n) as usize));
let vc_r = jobvr.then(|| vec_uninit((n * n) as usize));
// calc work size
let mut info = 0;
let mut work_size: [$f; 1] = [0.0];
unsafe {
$ev(
jobvl.as_ptr(),
jobvr.as_ptr(),
&n,
std::ptr::null_mut(),
&n,
AsPtr::as_mut_ptr(&mut eigs_re),
AsPtr::as_mut_ptr(&mut eigs_im),
AsPtr::as_mut_ptr(vr_l.as_deref_mut().unwrap_or(&mut [])),
&n,
AsPtr::as_mut_ptr(vr_r.as_deref_mut().unwrap_or(&mut [])),
&n,
AsPtr::as_mut_ptr(&mut work_size),
&(-1),
&mut info,
)
};
info.as_lapack_result()?;
// actual ev
let lwork = work_size[0].to_usize().unwrap();
let work = vec_uninit(lwork);
Ok(Self {
n,
jobvr,
jobvl,
eigs: vec_uninit(n as usize),
eigs_re: Some(eigs_re),
eigs_im: Some(eigs_im),
rwork: None,
vr_l,
vr_r,
vc_l,
vc_r,
work,
})
}
fn calc<'work>(
&'work mut self,
a: &mut [Self::Elem],
) -> Result<EigRef<'work, Self::Elem>> {
let lwork = self.work.len().to_i32().unwrap();
let mut info = 0;
unsafe {
$ev(
self.jobvl.as_ptr(),
self.jobvr.as_ptr(),
&self.n,
AsPtr::as_mut_ptr(a),
&self.n,
AsPtr::as_mut_ptr(self.eigs_re.as_mut().unwrap()),
AsPtr::as_mut_ptr(self.eigs_im.as_mut().unwrap()),
AsPtr::as_mut_ptr(self.vr_l.as_deref_mut().unwrap_or(&mut [])),
&self.n,
AsPtr::as_mut_ptr(self.vr_r.as_deref_mut().unwrap_or(&mut [])),
&self.n,
AsPtr::as_mut_ptr(&mut self.work),
&lwork,
&mut info,
)
};
info.as_lapack_result()?;
let eigs_re = self
.eigs_re
.as_ref()
.map(|e| unsafe { e.slice_assume_init_ref() })
.unwrap();
let eigs_im = self
.eigs_im
.as_ref()
.map(|e| unsafe { e.slice_assume_init_ref() })
.unwrap();
reconstruct_eigs(eigs_re, eigs_im, &mut self.eigs);
if let Some(v) = self.vr_l.as_ref() {
let v = unsafe { v.slice_assume_init_ref() };
reconstruct_eigenvectors(true, eigs_im, v, self.vc_l.as_mut().unwrap());
}
if let Some(v) = self.vr_r.as_ref() {
let v = unsafe { v.slice_assume_init_ref() };
reconstruct_eigenvectors(false, eigs_im, v, self.vc_r.as_mut().unwrap());
}
Ok(EigRef {
eigs: unsafe { self.eigs.slice_assume_init_ref() },
vl: self
.vc_l
.as_ref()
.map(|v| unsafe { v.slice_assume_init_ref() }),
vr: self
.vc_r
.as_ref()
.map(|v| unsafe { v.slice_assume_init_ref() }),
})
}
fn eval(mut self, a: &mut [Self::Elem]) -> Result<EigOwned<Self::Elem>> {
let _eig_ref = self.calc(a)?;
Ok(EigOwned {
eigs: unsafe { self.eigs.assume_init() },
vl: self.vc_l.map(|v| unsafe { v.assume_init() }),
vr: self.vc_r.map(|v| unsafe { v.assume_init() }),
})
}
}
};
}
impl_eig_work_r!(f32, lapack_sys::sgeev_);
impl_eig_work_r!(f64, lapack_sys::dgeev_);
/// Reconstruct eigenvectors into complex-array
///
/// From LAPACK API https://software.intel.com/en-us/node/469230
///
/// - If the j-th eigenvalue is real,
/// - v(j) = VR(:,j), the j-th column of VR.
///
/// - If the j-th and (j+1)-st eigenvalues form a complex conjugate pair,
/// - v(j) = VR(:,j) + i*VR(:,j+1)
/// - v(j+1) = VR(:,j) - i*VR(:,j+1).
///
/// In the C-layout case, we need the conjugates of the left
/// eigenvectors, so the signs should be reversed.
fn reconstruct_eigenvectors<T: Scalar>(
take_hermite_conjugate: bool,
eig_im: &[T],
vr: &[T],
vc: &mut [MaybeUninit<T::Complex>],
) {
let n = eig_im.len();
assert_eq!(vr.len(), n * n);
assert_eq!(vc.len(), n * n);
let mut col = 0;
while col < n {
if eig_im[col].is_zero() {
// The corresponding eigenvalue is real.
for row in 0..n {
let re = vr[row + col * n];
vc[row + col * n].write(T::complex(re, T::zero()));
}
col += 1;
} else {
// This is a complex conjugate pair.
assert!(col + 1 < n);
for row in 0..n {
let re = vr[row + col * n];
let mut im = vr[row + (col + 1) * n];
if take_hermite_conjugate {
im = -im;
}
vc[row + col * n].write(T::complex(re, im));
vc[row + (col + 1) * n].write(T::complex(re, -im));
}
col += 2;
}
}
}
/// Create complex eigenvalues from real and imaginary parts.
fn reconstruct_eigs<T: Scalar>(re: &[T], im: &[T], eigs: &mut [MaybeUninit<T::Complex>]) {
let n = eigs.len();
assert_eq!(re.len(), n);
assert_eq!(im.len(), n);
for i in 0..n {
eigs[i].write(T::complex(re[i], im[i]));
}
}