ndarray_linalg/krylov/
arnoldi.rsuse super::*;
use crate::{norm::Norm, operator::LinearOperator};
use num_traits::One;
use std::iter::*;
pub struct Arnoldi<A, S, F, Ortho>
where
A: Scalar,
S: DataMut<Elem = A>,
F: LinearOperator<Elem = A>,
Ortho: Orthogonalizer<Elem = A>,
{
a: F,
v: ArrayBase<S, Ix1>,
ortho: Ortho,
h: Vec<Array1<A>>,
}
impl<A, S, F, Ortho> Arnoldi<A, S, F, Ortho>
where
A: Scalar + Lapack,
S: DataMut<Elem = A>,
F: LinearOperator<Elem = A>,
Ortho: Orthogonalizer<Elem = A>,
{
pub fn new(a: F, mut v: ArrayBase<S, Ix1>, mut ortho: Ortho) -> Self {
assert_eq!(ortho.len(), 0);
assert!(ortho.tolerance() < One::one());
let norm = v.norm_l2();
azip!((v in &mut v) *v = v.div_real(norm));
ortho.append(v.view());
Arnoldi {
a,
v,
ortho,
h: Vec::new(),
}
}
pub fn dim(&self) -> usize {
self.ortho.len()
}
pub fn complete(mut self) -> (Q<A>, H<A>) {
for _ in &mut self {} let q = self.ortho.get_q();
let n = self.h.len();
let mut h = Array2::zeros((n, n).f());
for (i, hc) in self.h.iter().enumerate() {
let m = std::cmp::min(n, i + 2);
for j in 0..m {
h[(j, i)] = hc[j];
}
}
(q, h)
}
}
impl<A, S, F, Ortho> Iterator for Arnoldi<A, S, F, Ortho>
where
A: Scalar + Lapack,
S: DataMut<Elem = A>,
F: LinearOperator<Elem = A>,
Ortho: Orthogonalizer<Elem = A>,
{
type Item = Array1<A>;
fn next(&mut self) -> Option<Self::Item> {
self.a.apply_mut(&mut self.v);
let result = self.ortho.div_append(&mut self.v);
let norm = self.v.norm_l2();
azip!((v in &mut self.v) *v = v.div_real(norm));
match result {
AppendResult::Added(coef) => {
self.h.push(coef.clone());
Some(coef)
}
AppendResult::Dependent(coef) => {
self.h.push(coef);
None
}
}
}
}
pub fn arnoldi_householder<A, S>(
a: impl LinearOperator<Elem = A>,
v: ArrayBase<S, Ix1>,
tol: A::Real,
) -> (Q<A>, H<A>)
where
A: Scalar + Lapack,
S: DataMut<Elem = A>,
{
let householder = Householder::new(v.len(), tol);
Arnoldi::new(a, v, householder).complete()
}
pub fn arnoldi_mgs<A, S>(
a: impl LinearOperator<Elem = A>,
v: ArrayBase<S, Ix1>,
tol: A::Real,
) -> (Q<A>, H<A>)
where
A: Scalar + Lapack,
S: DataMut<Elem = A>,
{
let mgs = MGS::new(v.len(), tol);
Arnoldi::new(a, v, mgs).complete()
}