Skip to content

Commit cc94837

Browse files
committed
Refactoring
1 parent 4c1275a commit cc94837

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/krylov/householder.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use super::*;
22
use crate::{inner::*, norm::*};
3-
use num_traits::{One, Zero};
3+
use num_traits::One;
44

55
/// Calc a reflactor `w` from a vector `x`
6-
pub fn calc_reflector<A, S>(x: &mut ArrayBase<S, Ix1>)
6+
pub fn calc_reflector<A, S>(x: &mut ArrayBase<S, Ix1>) -> A
77
where
88
A: Scalar + Lapack,
99
S: DataMut<Elem = A>,
@@ -13,6 +13,7 @@ where
1313
x[0] -= alpha;
1414
let inv_rev_norm = A::Real::one() / x.norm_l2();
1515
azip!(mut a(x) in { *a = a.mul_real(inv_rev_norm)});
16+
alpha
1617
}
1718

1819
/// Take a reflection using `w`
@@ -121,35 +122,24 @@ impl<A: Scalar + Lapack> Orthogonalizer for Householder<A> {
121122
S: DataMut<Elem = A>,
122123
{
123124
assert_eq!(a.len(), self.dim);
124-
125-
self.forward_reflection(&mut a);
126-
127125
let k = self.len();
128-
let alpha = self.eval_residual(&a);
129-
let alpha = if k < a.len() && a[k].abs() > Zero::zero() {
130-
-a[k].mul_real(alpha / a[k].abs())
131-
} else {
132-
A::from_real(alpha)
133-
};
134126

127+
self.forward_reflection(&mut a);
135128
let mut coef = Array::zeros(k + 1);
136129
for i in 0..k {
137130
coef[i] = a[i];
138131
}
132+
if self.is_full() {
133+
return Err(coef); // coef[k] must be zero in this case
134+
}
135+
136+
let alpha = calc_reflector(&mut a.slice_mut(s![k..]));
139137
coef[k] = alpha;
140138

141139
if alpha.abs() < rtol {
142140
// linearly dependent
143141
return Err(coef);
144142
}
145-
146-
assert!(k < a.len()); // this must hold because `alpha == 0` if k >= a.len()
147-
148-
// Add reflector
149-
a[k] -= alpha;
150-
let norm = a.slice(s![k..]).norm_l2();
151-
azip!(mut a (a.slice_mut(s![..k])) in { *a = A::zero() });
152-
azip!(mut a (a.slice_mut(s![k..])) in { *a = a.div_real(norm) });
153143
self.v.push(a.into_owned());
154144
Ok(coef)
155145
}
@@ -184,6 +174,7 @@ where
184174
mod tests {
185175
use super::*;
186176
use crate::assert::*;
177+
use num_traits::Zero;
187178

188179
#[test]
189180
fn check_reflector() {

0 commit comments

Comments
 (0)