fixes
This commit is contained in:
parent
10dbc33746
commit
f18d7b41a2
@ -269,6 +269,10 @@ impl MathApp {
|
|||||||
let proc_func_str = add_asterisks(self.func_strs[i].clone());
|
let proc_func_str = add_asterisks(self.func_strs[i].clone());
|
||||||
if (proc_func_str != function.get_func_str())
|
if (proc_func_str != function.get_func_str())
|
||||||
| self.last_error.iter().any(|ele| ele.0 == i)
|
| 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 proc_func_str = self.func_strs[i].clone();
|
||||||
let func_test_output = test_func(&proc_func_str);
|
let func_test_output = test_func(&proc_func_str);
|
||||||
@ -299,8 +303,6 @@ impl MathApp {
|
|||||||
.map(|(a, b)| (*a, b.clone()))
|
.map(|(a, b)| (*a, b.clone()))
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
function.empty_func_str();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -35,7 +35,6 @@ pub struct FunctionEntry {
|
|||||||
|
|
||||||
pub(crate) integral: bool,
|
pub(crate) integral: bool,
|
||||||
pub(crate) derivative: bool,
|
pub(crate) derivative: bool,
|
||||||
pub(crate) nth_derivative: u64,
|
|
||||||
integral_min_x: f64,
|
integral_min_x: f64,
|
||||||
integral_max_x: f64,
|
integral_max_x: f64,
|
||||||
integral_num: usize,
|
integral_num: usize,
|
||||||
@ -56,7 +55,6 @@ impl FunctionEntry {
|
|||||||
derivative_cache: None,
|
derivative_cache: None,
|
||||||
integral: false,
|
integral: false,
|
||||||
derivative: false,
|
derivative: false,
|
||||||
nth_derivative: 1,
|
|
||||||
integral_min_x: f64::NAN,
|
integral_min_x: f64::NAN,
|
||||||
integral_max_x: f64::NAN,
|
integral_max_x: f64::NAN,
|
||||||
integral_num: 0,
|
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(
|
pub fn update(
|
||||||
&mut self, func_str: String, integral: bool, derivative: bool, integral_min_x: Option<f64>,
|
&mut self, func_str: String, integral: bool, derivative: bool, integral_min_x: Option<f64>,
|
||||||
integral_max_x: Option<f64>, integral_num: Option<usize>, sum: Option<RiemannSum>,
|
integral_max_x: Option<f64>, integral_num: Option<usize>, sum: Option<RiemannSum>,
|
||||||
@ -122,7 +117,7 @@ impl FunctionEntry {
|
|||||||
if let Some(i) = x_data.get_index(x) {
|
if let Some(i) = x_data.get_index(x) {
|
||||||
back_cache[i]
|
back_cache[i]
|
||||||
} else {
|
} else {
|
||||||
Value::new(x, self.run_func(x))
|
Value::new(x, self.function.get(x))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
@ -138,7 +133,7 @@ impl FunctionEntry {
|
|||||||
if let Some(i) = x_data.get_index(x) {
|
if let Some(i) = x_data.get_index(x) {
|
||||||
derivative_cache[i]
|
derivative_cache[i]
|
||||||
} else {
|
} else {
|
||||||
Value::new(x, self.function.derivative(x, self.nth_derivative))
|
Value::new(x, self.function.derivative(x))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
@ -166,7 +161,7 @@ impl FunctionEntry {
|
|||||||
self.back_cache = Some(
|
self.back_cache = Some(
|
||||||
(0..self.pixel_width)
|
(0..self.pixel_width)
|
||||||
.map(|x| (x as f64 / resolution as f64) + self.min_x)
|
.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(),
|
.collect(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -180,9 +175,7 @@ impl FunctionEntry {
|
|||||||
self.derivative_cache = Some(
|
self.derivative_cache = Some(
|
||||||
(0..self.pixel_width)
|
(0..self.pixel_width)
|
||||||
.map(|x| (x as f64 / resolution as f64) + self.min_x)
|
.map(|x| (x as f64 / resolution as f64) + self.min_x)
|
||||||
.map(|x| {
|
.map(|x| Value::new(x, self.function.derivative(x)))
|
||||||
Value::new(x, self.function.derivative(x, self.nth_derivative))
|
|
||||||
})
|
|
||||||
.collect(),
|
.collect(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -253,9 +246,11 @@ impl FunctionEntry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let y = match self.sum {
|
let y = match self.sum {
|
||||||
RiemannSum::Left => self.run_func(left_x),
|
RiemannSum::Left => self.function.get(left_x),
|
||||||
RiemannSum::Right => self.run_func(right_x),
|
RiemannSum::Right => self.function.get(right_x),
|
||||||
RiemannSum::Middle => (self.run_func(left_x) + self.run_func(right_x)) / 2.0,
|
RiemannSum::Middle => {
|
||||||
|
(self.function.get(left_x) + self.function.get(right_x)) / 2.0
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if last_positive.is_none() {
|
if last_positive.is_none() {
|
||||||
@ -276,7 +271,7 @@ impl FunctionEntry {
|
|||||||
// Set func_str to an empty string
|
// Set func_str to an empty string
|
||||||
pub fn empty_func_str(&mut self) { self.func_str = String::new(); }
|
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
|
// Updates riemann value and invalidates front_cache if needed
|
||||||
pub fn update_riemann(mut self, riemann: RiemannSum) -> Self {
|
pub fn update_riemann(mut self, riemann: RiemannSum) -> Self {
|
||||||
@ -315,10 +310,6 @@ impl FunctionEntry {
|
|||||||
self.integral_max_x = max_x;
|
self.integral_max_x = max_x;
|
||||||
self
|
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]
|
#[test]
|
||||||
|
|||||||
@ -8,23 +8,18 @@ pub struct BackingFunction {
|
|||||||
impl BackingFunction {
|
impl BackingFunction {
|
||||||
pub fn new(func_str: &str) -> Self {
|
pub fn new(func_str: &str) -> Self {
|
||||||
let function = exmex::parse::<f64>(func_str).unwrap();
|
let function = exmex::parse::<f64>(func_str).unwrap();
|
||||||
|
let derivative = function.partial(0).unwrap_or(function.clone());
|
||||||
|
println!("{}\n{}", function.unparse(), derivative.unparse());
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
function: function.clone(),
|
function,
|
||||||
derivative: function.partial(0).unwrap(),
|
derivative,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) }
|
||||||
|
|
||||||
pub fn derivative(&self, x: f64, n: u64) -> f64 {
|
pub fn derivative(&self, x: f64) -> f64 { self.derivative.eval(&[x]).unwrap_or(f64::NAN) }
|
||||||
if n == 0 {
|
|
||||||
self.get(x)
|
|
||||||
} else if n == 1 {
|
|
||||||
self.derivative.eval(&[x]).unwrap_or(f64::NAN)
|
|
||||||
} else {
|
|
||||||
panic!("n > 1");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -95,7 +90,7 @@ pub fn add_asterisks(function_in: String) -> String {
|
|||||||
output_string += &c.to_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.
|
// 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.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user