Code improvements and better handling of empty integrals

This commit is contained in:
Simon Gardling 2022-03-24 09:08:15 -04:00
parent 50892ed4b5
commit 577162a0b1
3 changed files with 42 additions and 11 deletions

View File

@ -1,5 +1,5 @@
use crate::function::{FunctionEntry, RiemannSum, DEFAULT_FUNCTION_ENTRY};
use crate::misc::{JsonFileOutput, SerdeValueHelper};
use crate::misc::{option_vec_printer, JsonFileOutput, SerdeValueHelper};
use crate::parsing::{process_func_str, test_func};
use const_format::formatc;
@ -329,7 +329,7 @@ pub struct MathApp {
/// Contains the list of Areas calculated (the vector of f64) and time it
/// took for the last frame (the Duration). Stored in a Tuple.
last_info: (Vec<f64>, Duration),
last_info: (Vec<Option<f64>>, Duration),
/// Stores settings (pretty self-explanatory)
settings: AppSettings,
@ -341,7 +341,7 @@ impl Default for MathApp {
functions: vec![DEFAULT_FUNCTION_ENTRY.clone()],
func_strs: vec![String::new()],
last_error: Vec::new(),
last_info: (vec![0.0], Duration::ZERO),
last_info: (vec![None], Duration::ZERO),
settings: AppSettings::default(),
}
}
@ -617,8 +617,9 @@ impl epi::App for MathApp {
// Display Area and time of last frame
ui.label(format!(
"Area: {:?} Took: {:?}",
self.last_info.0, self.last_info.1
"Area: {} Took: {:?}",
option_vec_printer(self.last_info.0.clone()),
self.last_info.1
));
});
});
@ -668,8 +669,9 @@ impl epi::App for MathApp {
self.side_panel(ctx);
}
let mut area_list: Vec<f64> = Vec::new(); // Referenced in plotting code, but needs to be here so it can be later
// Referenced in plotting code, but needs to be here so it can be later
// referenced when storing `last_info`
let mut area_list: Vec<Option<f64>> = Vec::new();
// Central panel which contains the central plot (or an error created when
// parsing)
@ -708,7 +710,7 @@ impl epi::App for MathApp {
.enumerate()
.map(|(i, function)| {
if self.func_strs[i].is_empty() {
return f64::NAN;
return None;
}
function.display(

View File

@ -166,7 +166,7 @@ impl FunctionEntry {
pub fn display(
&mut self, plot_ui: &mut PlotUi, min_x: f64, max_x: f64, pixel_width: usize,
width_changed: bool, settings: AppSettings,
) -> f64 {
) -> Option<f64> {
let resolution: f64 = pixel_width as f64 / (max_x.abs() + min_x.abs());
let resolution_iter = resolution_helper(pixel_width + 1, min_x, resolution);
@ -342,9 +342,9 @@ impl FunctionEntry {
);
// return value rounded to 8 decimal places
crate::misc::decimal_round(integral_data.1, 8)
Some(crate::misc::decimal_round(integral_data.1, 8))
} else {
f64::NAN // return NaN if integrals are disabled
None
}
}
}

View File

@ -229,6 +229,35 @@ fn newtons_method(
}
}
/// Inputs `Vec<Option<T>>` and outputs a `String` containing a pretty
/// representation of the Vector
pub fn option_vec_printer<T: ToString>(data: Vec<Option<T>>) -> String {
let none_representation = "None";
let unwrapped: Vec<String> = data
.iter()
.map(|x| {
if let Some(inner) = x {
inner.to_string()
} else {
none_representation.to_owned()
}
})
.collect();
let mut output: String = String::from("[");
let mut curr_len: usize = 0;
let total_len: usize = unwrapped.len();
for ele in unwrapped.iter() {
curr_len += 1;
output += ele;
if total_len > curr_len {
output += ", ";
}
}
output += "]";
output
}
// Returns a vector of length `max_i` starting at value `min_x` with resolution
// of `resolution`
pub fn resolution_helper(max_i: usize, min_x: f64, resolution: f64) -> Vec<f64> {