refactoring
This commit is contained in:
@@ -210,13 +210,7 @@ impl FunctionEntry {
|
|||||||
fn integral_rectangles(
|
fn integral_rectangles(
|
||||||
&self, integral_min_x: &f64, integral_max_x: &f64, sum: &Riemann, integral_num: &usize,
|
&self, integral_min_x: &f64, integral_max_x: &f64, sum: &Riemann, integral_num: &usize,
|
||||||
) -> (Vec<(f64, f64)>, f64) {
|
) -> (Vec<(f64, f64)>, f64) {
|
||||||
if integral_min_x.is_nan() {
|
let step = (integral_max_x - integral_min_x) / (*integral_num as f64);
|
||||||
panic!("integral_min_x is NaN")
|
|
||||||
} else if integral_max_x.is_nan() {
|
|
||||||
panic!("integral_max_x is NaN")
|
|
||||||
}
|
|
||||||
|
|
||||||
let step = (integral_min_x - integral_max_x).abs() / (*integral_num as f64);
|
|
||||||
|
|
||||||
let sum_func = self.get_sum_func(*sum);
|
let sum_func = self.get_sum_func(*sum);
|
||||||
|
|
||||||
@@ -234,7 +228,7 @@ impl FunctionEntry {
|
|||||||
|
|
||||||
(x + (step_offset / 2.0), y)
|
(x + (step_offset / 2.0), y)
|
||||||
})
|
})
|
||||||
.filter(|(_, y)| !y.is_nan())
|
.filter(|(_, y)| y.is_finite())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let area = data2.iter().map(|(_, y)| y * step).sum();
|
let area = data2.iter().map(|(_, y)| y * step).sum();
|
||||||
@@ -271,15 +265,15 @@ impl FunctionEntry {
|
|||||||
|
|
||||||
/// Does the calculations and stores results in `self`
|
/// Does the calculations and stores results in `self`
|
||||||
pub fn calculate(
|
pub fn calculate(
|
||||||
&mut self, min_x: &f64, max_x: &f64, width_changed: bool, min_max_changed: bool,
|
&mut self, width_changed: bool, min_max_changed: bool, settings: &AppSettings,
|
||||||
settings: &AppSettings,
|
|
||||||
) {
|
) {
|
||||||
if self.test_result.is_some() {
|
if self.test_result.is_some() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let resolution: f64 = settings.plot_width as f64 / (max_x.abs() + min_x.abs());
|
let step = (settings.plot_width as f64) / (settings.max_x - settings.min_x);
|
||||||
let resolution_iter = resolution_helper(&settings.plot_width + 1, min_x, &resolution);
|
debug_assert!(step > 0.0);
|
||||||
|
let resolution_iter = resolution_helper(&settings.plot_width + 1, &settings.min_x, &step);
|
||||||
|
|
||||||
unsafe { assume(!resolution_iter.is_empty()) }
|
unsafe { assume(!resolution_iter.is_empty()) }
|
||||||
|
|
||||||
@@ -416,7 +410,7 @@ impl FunctionEntry {
|
|||||||
self.invalidate_integral();
|
self.invalidate_integral();
|
||||||
}
|
}
|
||||||
|
|
||||||
let threshold: f64 = resolution / 2.0;
|
let threshold: f64 = step / 2.0;
|
||||||
let x_range = settings.min_x..settings.max_x;
|
let x_range = settings.min_x..settings.max_x;
|
||||||
|
|
||||||
// Calculates extrema
|
// Calculates extrema
|
||||||
@@ -440,10 +434,11 @@ impl FunctionEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let derivative_str = self.function.get_derivative_str();
|
let derivative_str = self.function.get_derivative_str();
|
||||||
let step = (settings.integral_min_x - settings.integral_max_x).abs()
|
let step =
|
||||||
/ (settings.integral_num as f64);
|
(settings.integral_max_x - settings.integral_min_x) / (settings.integral_num as f64);
|
||||||
|
|
||||||
let resolution = (settings.min_x - settings.max_x).abs() / (settings.plot_width as f64);
|
let resolution = (settings.max_x - settings.min_x) / (settings.plot_width as f64);
|
||||||
|
debug_assert!(resolution > 0.0);
|
||||||
|
|
||||||
// Plot back data
|
// Plot back data
|
||||||
if !self.back_data.is_empty() {
|
if !self.back_data.is_empty() {
|
||||||
@@ -557,10 +552,11 @@ impl FunctionEntry {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn tests(
|
pub fn tests(
|
||||||
&mut self, settings: AppSettings, back_target: Vec<(f64, f64)>,
|
&mut self, settings: AppSettings, back_target: Vec<(f64, f64)>,
|
||||||
derivative_target: Vec<(f64, f64)>, area_target: f64, min_x: f64, max_x: f64,
|
derivative_target: Vec<(f64, f64)>, area_target: f64,
|
||||||
) {
|
) {
|
||||||
|
let mut settings = settings;
|
||||||
{
|
{
|
||||||
self.calculate(&min_x, &max_x, true, true, &settings);
|
self.calculate(true, true, &settings);
|
||||||
assert!(!self.back_data.is_empty());
|
assert!(!self.back_data.is_empty());
|
||||||
assert_eq!(self.back_data.len(), settings.plot_width + 1);
|
assert_eq!(self.back_data.len(), settings.plot_width + 1);
|
||||||
let back_vec_tuple = self.back_data.to_tuple();
|
let back_vec_tuple = self.back_data.to_tuple();
|
||||||
@@ -580,7 +576,9 @@ impl FunctionEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
self.calculate(&(min_x + 1.0), &(max_x + 1.0), true, true, &settings);
|
settings.min_x += 1.0;
|
||||||
|
settings.max_x += 1.0;
|
||||||
|
self.calculate(true, true, &settings);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
self.derivative_data
|
self.derivative_data
|
||||||
@@ -620,7 +618,9 @@ impl FunctionEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
self.calculate(&(min_x - 1.0), &(max_x - 1.0), true, true, &settings);
|
settings.min_x -= 2.0;
|
||||||
|
settings.max_x -= 2.0;
|
||||||
|
self.calculate(true, true, &settings);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
self.derivative_data
|
self.derivative_data
|
||||||
@@ -676,7 +676,10 @@ impl FunctionEntry {
|
|||||||
assert!(self.extrema_data.is_empty());
|
assert!(self.extrema_data.is_empty());
|
||||||
assert!(self.derivative_data.is_empty());
|
assert!(self.derivative_data.is_empty());
|
||||||
|
|
||||||
self.calculate(&min_x, &max_x, true, true, &settings);
|
settings.min_x -= 1.0;
|
||||||
|
settings.max_x -= 1.0;
|
||||||
|
|
||||||
|
self.calculate(true, true, &settings);
|
||||||
|
|
||||||
assert!(!self.back_data.is_empty());
|
assert!(!self.back_data.is_empty());
|
||||||
assert!(self.integral_data.is_none());
|
assert!(self.integral_data.is_none());
|
||||||
|
|||||||
@@ -591,13 +591,7 @@ impl App for MathApp {
|
|||||||
self.settings.max_x = max_x;
|
self.settings.max_x = max_x;
|
||||||
|
|
||||||
dyn_mut_iter(self.functions.get_entries_mut()).for_each(|(_, function)| {
|
dyn_mut_iter(self.functions.get_entries_mut()).for_each(|(_, function)| {
|
||||||
function.calculate(
|
function.calculate(width_changed, min_max_changed, &self.settings)
|
||||||
&min_x,
|
|
||||||
&max_x,
|
|
||||||
width_changed,
|
|
||||||
min_max_changed,
|
|
||||||
&self.settings,
|
|
||||||
)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.last_info.0 = self
|
self.last_info.0 = self
|
||||||
|
|||||||
@@ -59,8 +59,6 @@ fn do_test(sum: Riemann, area_target: f64) {
|
|||||||
BACK_TARGET.to_vec(),
|
BACK_TARGET.to_vec(),
|
||||||
DERIVATIVE_TARGET.to_vec(),
|
DERIVATIVE_TARGET.to_vec(),
|
||||||
area_target,
|
area_target,
|
||||||
-1.0,
|
|
||||||
1.0,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user