|
| 1 | +//! Krylov subspace |
| 2 | +
|
| 3 | +use crate::types::*; |
| 4 | +use ndarray::*; |
| 5 | + |
| 6 | +mod mgs; |
| 7 | + |
| 8 | +pub use mgs::{mgs, MGS}; |
| 9 | + |
| 10 | +/// Q-matrix |
| 11 | +/// |
| 12 | +/// - Maybe **NOT** square |
| 13 | +/// - Unitary for existing columns |
| 14 | +/// |
| 15 | +pub type Q<A> = Array2<A>; |
| 16 | + |
| 17 | +/// R-matrix |
| 18 | +/// |
| 19 | +/// - Maybe **NOT** square |
| 20 | +/// - Upper triangle |
| 21 | +/// |
| 22 | +pub type R<A> = Array2<A>; |
| 23 | + |
| 24 | +/// Trait for creating orthogonal basis from iterator of arrays |
| 25 | +pub trait Orthogonalizer { |
| 26 | + type Elem: Scalar; |
| 27 | + |
| 28 | + /// Dimension of input array |
| 29 | + fn dim(&self) -> usize; |
| 30 | + |
| 31 | + /// Number of cached basis |
| 32 | + fn len(&self) -> usize; |
| 33 | + |
| 34 | + /// check if the basis spans entire space |
| 35 | + fn is_full(&self) -> bool { |
| 36 | + self.len() == self.dim() |
| 37 | + } |
| 38 | + |
| 39 | + fn is_empty(&self) -> bool { |
| 40 | + self.len() == 0 |
| 41 | + } |
| 42 | + |
| 43 | + /// Orthogonalize given vector using current basis |
| 44 | + /// |
| 45 | + /// Panic |
| 46 | + /// ------- |
| 47 | + /// - if the size of the input array mismatches to the dimension |
| 48 | + /// |
| 49 | + fn orthogonalize<S>(&self, a: &mut ArrayBase<S, Ix1>) -> Array1<Self::Elem> |
| 50 | + where |
| 51 | + S: DataMut<Elem = Self::Elem>; |
| 52 | + |
| 53 | + /// Add new vector if the residual is larger than relative tolerance |
| 54 | + /// |
| 55 | + /// Returns |
| 56 | + /// -------- |
| 57 | + /// Coefficients to the `i`-th Q-vector |
| 58 | + /// |
| 59 | + /// - The size of array must be `self.len() + 1` |
| 60 | + /// - The last element is the residual norm of input vector |
| 61 | + /// |
| 62 | + /// Panic |
| 63 | + /// ------- |
| 64 | + /// - if the size of the input array mismatches to the dimension |
| 65 | + /// |
| 66 | + fn append<S>( |
| 67 | + &mut self, |
| 68 | + a: ArrayBase<S, Ix1>, |
| 69 | + rtol: <Self::Elem as Scalar>::Real, |
| 70 | + ) -> Result<Array1<Self::Elem>, Array1<Self::Elem>> |
| 71 | + where |
| 72 | + S: DataMut<Elem = Self::Elem>; |
| 73 | + |
| 74 | + /// Get Q-matrix of generated basis |
| 75 | + fn get_q(&self) -> Q<Self::Elem>; |
| 76 | +} |
| 77 | + |
| 78 | +/// Strategy for linearly dependent vectors appearing in iterative QR decomposition |
| 79 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 80 | +pub enum Strategy { |
| 81 | + /// Terminate iteration if dependent vector comes |
| 82 | + Terminate, |
| 83 | + |
| 84 | + /// Skip dependent vector |
| 85 | + Skip, |
| 86 | + |
| 87 | + /// Orthogonalize dependent vector without adding to Q, |
| 88 | + /// i.e. R must be non-square like following: |
| 89 | + /// |
| 90 | + /// ```text |
| 91 | + /// x x x x x |
| 92 | + /// 0 x x x x |
| 93 | + /// 0 0 0 x x |
| 94 | + /// 0 0 0 0 x |
| 95 | + /// ``` |
| 96 | + Full, |
| 97 | +} |
| 98 | + |
| 99 | +/// Online QR decomposition using arbitrary orthogonalizer |
| 100 | +pub fn qr<A, S>( |
| 101 | + iter: impl Iterator<Item = ArrayBase<S, Ix1>>, |
| 102 | + mut ortho: impl Orthogonalizer<Elem = A>, |
| 103 | + rtol: A::Real, |
| 104 | + strategy: Strategy, |
| 105 | +) -> (Q<A>, R<A>) |
| 106 | +where |
| 107 | + A: Scalar + Lapack, |
| 108 | + S: Data<Elem = A>, |
| 109 | +{ |
| 110 | + assert_eq!(ortho.len(), 0); |
| 111 | + |
| 112 | + let mut coefs = Vec::new(); |
| 113 | + for a in iter { |
| 114 | + match ortho.append(a.into_owned(), rtol) { |
| 115 | + Ok(coef) => coefs.push(coef), |
| 116 | + Err(coef) => match strategy { |
| 117 | + Strategy::Terminate => break, |
| 118 | + Strategy::Skip => continue, |
| 119 | + Strategy::Full => coefs.push(coef), |
| 120 | + }, |
| 121 | + } |
| 122 | + } |
| 123 | + let n = ortho.len(); |
| 124 | + let m = coefs.len(); |
| 125 | + let mut r = Array2::zeros((n, m).f()); |
| 126 | + for j in 0..m { |
| 127 | + for i in 0..n { |
| 128 | + if i < coefs[j].len() { |
| 129 | + r[(i, j)] = coefs[j][i]; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + (ortho.get_q(), r) |
| 134 | +} |
0 commit comments