This commit is contained in:
Simon Gardling 2022-03-28 08:51:35 -04:00
parent 583fb0e9c9
commit 2bd1f641f9
5 changed files with 25 additions and 20 deletions

View File

@ -229,7 +229,7 @@ cfg_if::cfg_if! {
}
}
/// Stores current settings/state of `MathApp`
/// Stores current settings/state of [`MathApp`]
// TODO: find a better name for this
#[derive(Copy, Clone)]
pub struct AppSettings {
@ -251,6 +251,7 @@ pub struct AppSettings {
/// Max value for calculating an
pub integral_max_x: f64,
/// Stores whether or not integral settings have changed
pub integral_changed: bool,
/// Number of rectangles used to calculate integral
@ -325,7 +326,7 @@ impl Default for MathApp {
impl MathApp {
#[allow(dead_code)] // This is used lol
/// Create new instance of `MathApp` and return it
/// Create new instance of [`MathApp`] and return it
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// Remove loading indicator on wasm
#[cfg(target_arch = "wasm32")]

View File

@ -128,21 +128,23 @@ impl FunctionEntry {
(data2, area)
}
/// Returns `func_str`
/// Returns `self.func_str`
pub fn get_func_str(&self) -> &str { &self.func_str }
/// Helps with processing newton's method depending on level of derivative
fn newtons_method_helper(&self, threshold: f64, derivative_level: usize) -> Option<Vec<Value>> {
let range = self.min_x..self.max_x;
let newtons_method_output: Vec<f64> = match derivative_level {
0 => newtons_method_helper(
threshold,
self.min_x..self.max_x,
range,
self.output.back.to_owned().unwrap(),
&|x: f64| self.function.get(x),
&|x: f64| self.function.get_derivative_1(x),
),
1 => newtons_method_helper(
threshold,
self.min_x..self.max_x,
range,
self.output.derivative.to_owned().unwrap(),
&|x: f64| self.function.get_derivative_1(x),
&|x: f64| self.function.get_derivative_2(x),
@ -287,10 +289,9 @@ impl FunctionEntry {
}
}
/// Calculates and displays the function on PlotUI `plot_ui`
/// Displays the function's output on PlotUI `plot_ui` with settings
/// `settings`. Returns an `Option<f64>` of the calculated integral
pub fn display(&self, plot_ui: &mut PlotUi, settings: AppSettings) -> Option<f64> {
// self.calculate(min_x, max_x, width_changed, settings);
let func_str = self.get_func_str();
let derivative_str = self.function.get_derivative_str();
let step = (settings.integral_min_x - settings.integral_max_x).abs()
@ -352,6 +353,7 @@ impl FunctionEntry {
}
}
/// Runs asserts to make sure everything is the expected value
#[cfg(test)]
pub fn tests(
&mut self, settings: AppSettings, back_target: Vec<(f64, f64)>,

View File

@ -10,7 +10,7 @@ pub struct FunctionOutput {
}
impl FunctionOutput {
/// Creates empty instance of `FunctionOutput`
/// Creates empty instance of [`FunctionOutput`]
pub fn new_empty() -> Self {
Self {
back: None,
@ -40,6 +40,8 @@ impl FunctionOutput {
pub fn invalidate_derivative(&mut self) { self.derivative = None; }
}
/// Tests to make sure invalidation and the default empty state works as
/// expected
#[test]
fn function_output_test() {
let mut function_output = FunctionOutput::new_empty();

View File

@ -48,7 +48,7 @@ where
fn to_tuple(&self) -> Vec<(f64, f64)> { self.iter().map(|ele| (ele.x, ele.y)).collect() }
}
/// `SteppedVector` is used in order to efficiently sort through an ordered
/// [`SteppedVector`] is used in order to efficiently sort through an ordered
/// `Vec<f64>` Used in order to speedup the processing of cached data when
/// moving horizontally without zoom in `FunctionEntry`. Before this struct, the
/// index was calculated with `.iter().position(....` which was horribly
@ -110,7 +110,7 @@ impl SteppedVector {
pub fn get_data(&self) -> Vec<f64> { self.data.clone() }
}
// Convert `Vec<f64>` into `SteppedVector`
// Convert `Vec<f64>` into [`SteppedVector`]
impl From<Vec<f64>> for SteppedVector {
fn from(input_data: Vec<f64>) -> SteppedVector {
let mut data = input_data;
@ -318,8 +318,8 @@ mod tests {
use super::*;
use std::collections::HashMap;
/// Tests SteppedVector to ensure everything works properly (helped me find
/// a bunch of issues)
/// Tests [`SteppedVector`] to ensure everything works properly (helped me
/// find a bunch of issues)
#[test]
fn stepped_vector_test() {
let min: i32 = -10;
@ -345,7 +345,7 @@ mod tests {
assert_eq!(stepped_vector.get_index((max + 1) as f64), None);
}
/// Ensures decimal_round returns correct values
/// Ensures [`decimal_round`] returns correct values
#[test]
fn decimal_round_test() {
assert_eq!(decimal_round(0.00001, 1), 0.0);
@ -364,7 +364,7 @@ mod tests {
assert_eq!(decimal_round(1.9, 1), 1.9);
}
/// Tests `resolution_helper` to make sure it returns expected output
/// Tests [`resolution_helper`] to make sure it returns expected output
#[test]
fn resolution_helper_test() {
assert_eq!(

View File

@ -18,7 +18,7 @@ pub struct BackingFunction {
}
impl BackingFunction {
/// Create new BackingFunction instance
/// Create new [`BackingFunction`] instance
pub fn new(func_str: &str) -> Self {
let function = match func_str {
"" => EMPTY_FUNCTION.clone(),
@ -213,11 +213,11 @@ mod tests {
use std::collections::HashMap;
/// returns if function with string `func_str` is valid after processing
/// through `process_func_str`
/// through [`process_func_str`]
fn func_is_valid(func_str: &str) -> bool { test_func(&process_func_str(func_str)).is_none() }
/// Used for testing: passes function to `add_asterisks` before running
/// `test_func`. if `expect_valid` == `true`, it expects no errors to be
/// Used for testing: passes function to [`process_func_str`] before running
/// [`test_func`]. if `expect_valid` == `true`, it expects no errors to be
/// created.
fn test_func_helper(func_str: &str, expect_valid: bool) {
let is_valid = func_is_valid(func_str);
@ -273,7 +273,7 @@ mod tests {
test_func_helper(func_str, false);
}
}
/// Helps with tests of `process_func_str`
/// Helps with tests of [`process_func_str`]
#[cfg(test)]
fn test_process_helper(input: &str, expected: &str) {
assert_eq!(&process_func_str(input), expected);