don't unparse derivative_1 every time

This commit is contained in:
Simon Gardling 2022-03-21 09:01:11 -04:00
parent 3bd76a87d7
commit 12ba62b322
2 changed files with 5 additions and 4 deletions

View File

@ -359,7 +359,7 @@ impl FunctionEntry {
self.output.display( self.output.display(
plot_ui, plot_ui,
self.get_func_str(), self.get_func_str(),
&self.function.get_derivative_str(), self.function.get_derivative_str(),
(self.integral_min_x - self.integral_max_x).abs() / (self.integral_num as f64), (self.integral_min_x - self.integral_max_x).abs() / (self.integral_num as f64),
self.derivative, self.derivative,
extrema, extrema,

View File

@ -8,6 +8,7 @@ lazy_static::lazy_static! {
pub struct BackingFunction { pub struct BackingFunction {
function: FlatEx<f64>, function: FlatEx<f64>,
derivative_1: FlatEx<f64>, derivative_1: FlatEx<f64>,
derivative_1_str: String,
derivative_2: FlatEx<f64>, derivative_2: FlatEx<f64>,
} }
@ -17,6 +18,7 @@ impl BackingFunction {
let derivative_1 = function let derivative_1 = function
.partial(0) .partial(0)
.unwrap_or_else(|_| EMPTY_FUNCTION.clone()); .unwrap_or_else(|_| EMPTY_FUNCTION.clone());
let derivative_1_str = derivative_1.unparse().to_owned().replace("{x}", "x");
let derivative_2 = function let derivative_2 = function
.partial_iter([0, 0].iter()) .partial_iter([0, 0].iter())
.unwrap_or_else(|_| EMPTY_FUNCTION.clone()); .unwrap_or_else(|_| EMPTY_FUNCTION.clone());
@ -24,13 +26,12 @@ impl BackingFunction {
Self { Self {
function, function,
derivative_1, derivative_1,
derivative_1_str,
derivative_2, derivative_2,
} }
} }
pub fn get_derivative_str(&self) -> String { pub fn get_derivative_str(&self) -> &str { &self.derivative_1_str }
String::from(self.derivative_1.unparse()).replace("{x}", "x")
}
pub fn get(&self, x: f64) -> f64 { self.function.eval(&[x]).unwrap_or(f64::NAN) } pub fn get(&self, x: f64) -> f64 { self.function.eval(&[x]).unwrap_or(f64::NAN) }