refactor newtons_method

This commit is contained in:
Simon Gardling 2022-03-24 00:07:59 -04:00
parent 8240c041c6
commit 7dc7167a57

View File

@ -186,12 +186,11 @@ pub fn newtons_method(
) -> Vec<f64> { ) -> Vec<f64> {
data.iter() data.iter()
.tuple_windows() .tuple_windows()
.filter(|(prev, curr)| !(prev.y.is_nan() | curr.y.is_nan())) .filter(|(prev, curr)| !prev.y.is_nan() && !curr.y.is_nan())
.map(|(prev, curr)| { .filter(|(prev, curr)| prev.y.signum() != curr.y.signum())
if prev.y.signum() != curr.y.signum() { .map(|(prev, _)| prev.x)
// actual start of newton's method .map(|start_x| {
let x = { let mut x1: f64 = start_x;
let mut x1: f64 = prev.x;
let mut x2: f64; let mut x2: f64;
let mut fail: bool = false; let mut fail: bool = false;
loop { loop {
@ -214,11 +213,6 @@ pub fn newtons_method(
true => f64::NAN, true => f64::NAN,
false => x1, false => x1,
} }
};
return x;
}
f64::NAN
}) })
.filter(|x| !x.is_nan()) .filter(|x| !x.is_nan())
.collect() .collect()