From f18d7b41a29d117ad28e6f687175d71f777eb986 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 7 Mar 2022 15:36:40 -0500 Subject: [PATCH] fixes --- src/egui_app.rs | 6 ++++-- src/function.rs | 29 ++++++++++------------------- src/parsing.rs | 19 +++++++------------ 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/egui_app.rs b/src/egui_app.rs index d67489b..7eb900e 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -269,6 +269,10 @@ impl MathApp { let proc_func_str = add_asterisks(self.func_strs[i].clone()); if (proc_func_str != function.get_func_str()) | self.last_error.iter().any(|ele| ele.0 == i) + | integral_toggle + | derivative_toggle + | max_x_changed + | min_x_changed { // let proc_func_str = self.func_strs[i].clone(); let func_test_output = test_func(&proc_func_str); @@ -299,8 +303,6 @@ impl MathApp { .map(|(a, b)| (*a, b.clone())) .collect(); } - } else { - function.empty_func_str(); } } diff --git a/src/function.rs b/src/function.rs index 01efc45..c2e261a 100644 --- a/src/function.rs +++ b/src/function.rs @@ -35,7 +35,6 @@ pub struct FunctionEntry { pub(crate) integral: bool, pub(crate) derivative: bool, - pub(crate) nth_derivative: u64, integral_min_x: f64, integral_max_x: f64, integral_num: usize, @@ -56,7 +55,6 @@ impl FunctionEntry { derivative_cache: None, integral: false, derivative: false, - nth_derivative: 1, integral_min_x: f64::NAN, integral_max_x: f64::NAN, integral_num: 0, @@ -64,9 +62,6 @@ impl FunctionEntry { } } - // Runs the internal function to get values - fn run_func(&self, x: f64) -> f64 { self.function.get(x) } - pub fn update( &mut self, func_str: String, integral: bool, derivative: bool, integral_min_x: Option, integral_max_x: Option, integral_num: Option, sum: Option, @@ -122,7 +117,7 @@ impl FunctionEntry { if let Some(i) = x_data.get_index(x) { back_cache[i] } else { - Value::new(x, self.run_func(x)) + Value::new(x, self.function.get(x)) } }) .collect(), @@ -138,7 +133,7 @@ impl FunctionEntry { if let Some(i) = x_data.get_index(x) { derivative_cache[i] } else { - Value::new(x, self.function.derivative(x, self.nth_derivative)) + Value::new(x, self.function.derivative(x)) } }) .collect(), @@ -166,7 +161,7 @@ impl FunctionEntry { self.back_cache = Some( (0..self.pixel_width) .map(|x| (x as f64 / resolution as f64) + self.min_x) - .map(|x| Value::new(x, self.run_func(x))) + .map(|x| Value::new(x, self.function.get(x))) .collect(), ); } @@ -180,9 +175,7 @@ impl FunctionEntry { self.derivative_cache = Some( (0..self.pixel_width) .map(|x| (x as f64 / resolution as f64) + self.min_x) - .map(|x| { - Value::new(x, self.function.derivative(x, self.nth_derivative)) - }) + .map(|x| Value::new(x, self.function.derivative(x))) .collect(), ); } @@ -253,9 +246,11 @@ impl FunctionEntry { }; let y = match self.sum { - RiemannSum::Left => self.run_func(left_x), - RiemannSum::Right => self.run_func(right_x), - RiemannSum::Middle => (self.run_func(left_x) + self.run_func(right_x)) / 2.0, + RiemannSum::Left => self.function.get(left_x), + RiemannSum::Right => self.function.get(right_x), + RiemannSum::Middle => { + (self.function.get(left_x) + self.function.get(right_x)) / 2.0 + } }; if last_positive.is_none() { @@ -276,7 +271,7 @@ impl FunctionEntry { // Set func_str to an empty string pub fn empty_func_str(&mut self) { self.func_str = String::new(); } - pub fn get_func_str(&self) -> String { self.func_str.clone() } + pub fn get_func_str(&self) -> &str { &self.func_str } // Updates riemann value and invalidates front_cache if needed pub fn update_riemann(mut self, riemann: RiemannSum) -> Self { @@ -315,10 +310,6 @@ impl FunctionEntry { self.integral_max_x = max_x; self } - - // Invalidates the derivative cache. This would be used in the case of a change in the nth_derivative - #[allow(dead_code)] - pub fn invalidate_derivative_cache(&mut self) { self.derivative_cache = None; } } #[test] diff --git a/src/parsing.rs b/src/parsing.rs index 0ac0632..3154157 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -8,23 +8,18 @@ pub struct BackingFunction { impl BackingFunction { pub fn new(func_str: &str) -> Self { let function = exmex::parse::(func_str).unwrap(); + let derivative = function.partial(0).unwrap_or(function.clone()); + println!("{}\n{}", function.unparse(), derivative.unparse()); + Self { - function: function.clone(), - derivative: function.partial(0).unwrap(), + function, + derivative, } } pub fn get(&self, x: f64) -> f64 { self.function.eval(&[x]).unwrap_or(f64::NAN) } - pub fn derivative(&self, x: f64, n: u64) -> f64 { - if n == 0 { - self.get(x) - } else if n == 1 { - self.derivative.eval(&[x]).unwrap_or(f64::NAN) - } else { - panic!("n > 1"); - } - } + pub fn derivative(&self, x: f64) -> f64 { self.derivative.eval(&[x]).unwrap_or(f64::NAN) } } /* @@ -95,7 +90,7 @@ pub fn add_asterisks(function_in: String) -> String { output_string += &c.to_string(); } - output_string.replace('π', "pi") // π -> pi + output_string } // Tests function to make sure it's able to be parsed. Returns the string of the Error produced, or an empty string if it runs successfully.