1+ //! Modified Gram-Schmit orthogonalizer
2+
13use crate :: { generate:: * , inner:: * , norm:: Norm , types:: * } ;
24use ndarray:: * ;
35
@@ -10,32 +12,47 @@ pub struct MGS<A> {
1012 q : Vec < Array1 < A > > ,
1113}
1214
13- /// Q-matrix (unitary)
15+ /// Q-matrix
16+ ///
17+ /// - Maybe **NOT** square
18+ /// - Unitary for existing columns
19+ ///
1420pub type Q < A > = Array2 < A > ;
15- /// R-matrix (upper triangle)
21+
22+ /// R-matrix
23+ ///
24+ /// - Maybe **NOT** square
25+ /// - Upper triangle
26+ ///
1627pub type R < A > = Array2 < A > ;
1728
1829impl < A : Scalar > MGS < A > {
19- /// Create empty linear space
20- ///
21- /// ```rust
22- /// # use ndarray_linalg::{mgs::*, *};
23- /// const N: usize = 5;
24- /// let mgs = MGS::<f32>::new(N);
25- /// assert_eq!(mgs.dim(), N);
26- /// assert_eq!(mgs.len(), 0);
27- /// ```
30+ /// Create an empty orthogonalizer
2831 pub fn new ( dimension : usize ) -> Self {
2932 Self {
3033 dimension,
3134 q : Vec :: new ( ) ,
3235 }
3336 }
3437
38+ /// Dimension of input array
3539 pub fn dim ( & self ) -> usize {
3640 self . dimension
3741 }
3842
43+ /// Number of cached basis
44+ ///
45+ /// ```rust
46+ /// # use ndarray::*;
47+ /// # use ndarray_linalg::{mgs::*, *};
48+ /// const N: usize = 3;
49+ /// let mut mgs = MGS::<f32>::new(N);
50+ /// assert_eq!(mgs.dim(), N);
51+ /// assert_eq!(mgs.len(), 0);
52+ ///
53+ /// mgs.append(array![0.0, 1.0, 0.0], 1e-9).unwrap();
54+ /// assert_eq!(mgs.len(), 1);
55+ /// ```
3956 pub fn len ( & self ) -> usize {
4057 self . q . len ( )
4158 }
@@ -66,10 +83,6 @@ impl<A: Scalar> MGS<A> {
6683
6784 /// Add new vector if the residual is larger than relative tolerance
6885 ///
69- /// Panic
70- /// -------
71- /// - if the size of the input array mismatches to the dimension
72- ///
7386 /// ```rust
7487 /// # use ndarray::*;
7588 /// # use ndarray_linalg::{mgs::*, *};
@@ -80,12 +93,19 @@ impl<A: Scalar> MGS<A> {
8093 /// let coef = mgs.append(array![1.0, 1.0, 0.0], 1e-9).unwrap();
8194 /// close_l2(&coef, &array![1.0, 1.0], 1e-9);
8295 ///
83- /// assert!(mgs.append(array![1.0, 2.0, 0.0], 1e-9).is_err()); // Fail if the vector is linearly dependent
96+ /// // Fail if the vector is linearly dependent
97+ /// assert!(mgs.append(array![1.0, 2.0, 0.0], 1e-9).is_err());
8498 ///
99+ /// // You can get coefficients of dependent vector
85100 /// if let Err(coef) = mgs.append(array![1.0, 2.0, 0.0], 1e-9) {
86- /// close_l2(&coef, &array![2.0, 1.0, 0.0], 1e-9); // You can get coefficients of dependent vector
101+ /// close_l2(&coef, &array![2.0, 1.0, 0.0], 1e-9);
87102 /// }
88103 /// ```
104+ ///
105+ /// Panic
106+ /// -------
107+ /// - if the size of the input array mismatches to the dimension
108+ ///
89109 pub fn append < S > ( & mut self , a : ArrayBase < S , Ix1 > , rtol : A :: Real ) -> Result < Array1 < A > , Array1 < A > >
90110 where
91111 A : Lapack ,
0 commit comments