2017-08-03 01:38:28 +08:00
|
|
|
use num::Zero;
|
|
|
|
use num_complex::Complex;
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
use crate::ComplexHelper;
|
2018-05-25 05:51:57 +08:00
|
|
|
use na::allocator::Allocator;
|
2021-04-12 16:34:44 +08:00
|
|
|
use na::dimension::{Const, DimDiff, DimSub, U1};
|
2021-04-11 17:00:38 +08:00
|
|
|
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
use lapack;
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
/// The Hessenberg decomposition of a general matrix.
|
2017-08-14 01:53:04 +08:00
|
|
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
2018-05-25 05:51:57 +08:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(serialize = "DefaultAllocator: Allocator<T, D, D> +
|
|
|
|
Allocator<T, DimDiff<D, U1>>,
|
|
|
|
OMatrix<T, D, D>: Serialize,
|
|
|
|
OVector<T, DimDiff<D, U1>>: Serialize"))
|
2018-05-25 05:51:57 +08:00
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(deserialize = "DefaultAllocator: Allocator<T, D, D> +
|
|
|
|
Allocator<T, DimDiff<D, U1>>,
|
|
|
|
OMatrix<T, D, D>: Deserialize<'de>,
|
|
|
|
OVector<T, DimDiff<D, U1>>: Deserialize<'de>"))
|
2018-05-25 05:51:57 +08:00
|
|
|
)]
|
2017-08-14 01:53:04 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub struct Hessenberg<T: Scalar, D: DimSub<U1>>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, DimDiff<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
h: OMatrix<T, D, D>,
|
|
|
|
tau: OVector<T, DimDiff<D, U1>>,
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Copy, D: DimSub<U1>> Copy for Hessenberg<T, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, DimDiff<D, U1>>,
|
|
|
|
OMatrix<T, D, D>: Copy,
|
|
|
|
OVector<T, DimDiff<D, U1>>: Copy,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
|
|
|
}
|
2017-08-14 01:53:04 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: HessenbergScalar + Zero, D: DimSub<U1>> Hessenberg<T, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, DimDiff<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:38:28 +08:00
|
|
|
/// Computes the hessenberg decomposition of the matrix `m`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(mut m: OMatrix<T, D, D>) -> Self {
|
2021-08-03 00:41:46 +08:00
|
|
|
let nrows = m.shape_generic().0;
|
2018-02-02 19:26:35 +08:00
|
|
|
let n = nrows.value() as i32;
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
m.is_square(),
|
|
|
|
"Unable to compute the hessenberg decomposition of a non-square matrix."
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
!m.is_empty(),
|
|
|
|
"Unable to compute the hessenberg decomposition of an empty matrix."
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-08-03 23:39:45 +08:00
|
|
|
let mut tau = Matrix::zeros_generic(nrows.sub(Const::<1>), Const::<1>);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
let mut info = 0;
|
|
|
|
let lwork =
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xgehrd_work_size(n, 1, n, m.as_mut_slice(), n, tau.as_mut_slice(), &mut info);
|
2021-08-03 23:39:45 +08:00
|
|
|
let mut work = vec![T::zero(); lwork as usize];
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
lapack_panic!(info);
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xgehrd(
|
2018-02-02 19:26:35 +08:00
|
|
|
n,
|
|
|
|
1,
|
|
|
|
n,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
n,
|
|
|
|
tau.as_mut_slice(),
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_panic!(info);
|
|
|
|
|
2021-07-28 07:18:29 +08:00
|
|
|
Self { h: m, tau }
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the hessenberg matrix of this decomposition.
|
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn h(&self) -> OMatrix<T, D, D> {
|
2017-08-03 01:38:28 +08:00
|
|
|
let mut h = self.h.clone_owned();
|
2021-04-11 17:00:38 +08:00
|
|
|
h.fill_lower_triangle(T::zero(), 2);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: HessenbergReal + Zero, D: DimSub<U1>> Hessenberg<T, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, DimDiff<D, U1>>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:38:28 +08:00
|
|
|
/// Computes the matrices `(Q, H)` of this decomposition.
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unpack(self) -> (OMatrix<T, D, D>, OMatrix<T, D, D>) {
|
2017-08-03 01:38:28 +08:00
|
|
|
(self.q(), self.h())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the unitary matrix `Q` of this decomposition.
|
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn q(&self) -> OMatrix<T, D, D> {
|
2017-08-03 01:38:28 +08:00
|
|
|
let n = self.h.nrows() as i32;
|
|
|
|
let mut q = self.h.clone_owned();
|
2018-02-02 19:26:35 +08:00
|
|
|
let mut info = 0;
|
|
|
|
|
|
|
|
let lwork =
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xorghr_work_size(n, 1, n, q.as_mut_slice(), n, self.tau.as_slice(), &mut info);
|
|
|
|
let mut work = vec![T::zero(); lwork as usize];
|
2018-02-02 19:26:35 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xorghr(
|
2018-02-02 19:26:35 +08:00
|
|
|
n,
|
|
|
|
1,
|
|
|
|
n,
|
|
|
|
q.as_mut_slice(),
|
|
|
|
n,
|
|
|
|
self.tau.as_slice(),
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
q
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Lapack functions dispatch.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 06:15:18 +08:00
|
|
|
pub trait HessenbergScalar: Scalar + Copy {
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgehrd(
|
|
|
|
n: i32,
|
|
|
|
ilo: i32,
|
|
|
|
ihi: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
tau: &mut [Self],
|
|
|
|
work: &mut [Self],
|
|
|
|
lwork: i32,
|
|
|
|
info: &mut i32,
|
|
|
|
);
|
|
|
|
fn xgehrd_work_size(
|
|
|
|
n: i32,
|
|
|
|
ilo: i32,
|
|
|
|
ihi: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
tau: &mut [Self],
|
|
|
|
info: &mut i32,
|
|
|
|
) -> i32;
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
/// Trait implemented by scalars for which Lapack implements the hessenberg decomposition.
|
2017-08-03 01:38:28 +08:00
|
|
|
pub trait HessenbergReal: HessenbergScalar {
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xorghr(
|
|
|
|
n: i32,
|
|
|
|
ilo: i32,
|
|
|
|
ihi: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
tau: &[Self],
|
|
|
|
work: &mut [Self],
|
|
|
|
lwork: i32,
|
|
|
|
info: &mut i32,
|
|
|
|
);
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xorghr_work_size(
|
|
|
|
n: i32,
|
|
|
|
ilo: i32,
|
|
|
|
ihi: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
tau: &[Self],
|
|
|
|
info: &mut i32,
|
|
|
|
) -> i32;
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! hessenberg_scalar_impl(
|
|
|
|
($N: ty, $xgehrd: path) => (
|
|
|
|
impl HessenbergScalar for $N {
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgehrd(n: i32, ilo: i32, ihi: i32, a: &mut [Self], lda: i32,
|
2017-08-03 01:38:28 +08:00
|
|
|
tau: &mut [Self], work: &mut [Self], lwork: i32, info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xgehrd(n, ilo, ihi, a, lda, tau, work, lwork, info) }
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgehrd_work_size(n: i32, ilo: i32, ihi: i32, a: &mut [Self], lda: i32,
|
2017-08-03 01:38:28 +08:00
|
|
|
tau: &mut [Self], info: &mut i32) -> i32 {
|
|
|
|
let mut work = [ Zero::zero() ];
|
|
|
|
let lwork = -1 as i32;
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xgehrd(n, ilo, ihi, a, lda, tau, &mut work, lwork, info) };
|
2017-08-03 01:38:28 +08:00
|
|
|
ComplexHelper::real_part(work[0]) as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
macro_rules! hessenberg_real_impl(
|
|
|
|
($N: ty, $xorghr: path) => (
|
|
|
|
impl HessenbergReal for $N {
|
|
|
|
#[inline]
|
|
|
|
fn xorghr(n: i32, ilo: i32, ihi: i32, a: &mut [Self], lda: i32, tau: &[Self],
|
|
|
|
work: &mut [Self], lwork: i32, info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xorghr(n, ilo, ihi, a, lda, tau, work, lwork, info) }
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn xorghr_work_size(n: i32, ilo: i32, ihi: i32, a: &mut [Self], lda: i32,
|
|
|
|
tau: &[Self], info: &mut i32) -> i32 {
|
|
|
|
let mut work = [ Zero::zero() ];
|
|
|
|
let lwork = -1 as i32;
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xorghr(n, ilo, ihi, a, lda, tau, &mut work, lwork, info) };
|
2017-08-03 01:38:28 +08:00
|
|
|
ComplexHelper::real_part(work[0]) as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
hessenberg_scalar_impl!(f32, lapack::sgehrd);
|
|
|
|
hessenberg_scalar_impl!(f64, lapack::dgehrd);
|
|
|
|
hessenberg_scalar_impl!(Complex<f32>, lapack::cgehrd);
|
|
|
|
hessenberg_scalar_impl!(Complex<f64>, lapack::zgehrd);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
hessenberg_real_impl!(f32, lapack::sorghr);
|
|
|
|
hessenberg_real_impl!(f64, lapack::dorghr);
|