This commit is contained in:
Simon Gardling
2022-03-15 00:14:07 -04:00
parent 32e4699c5f
commit 5e9dc18469
6 changed files with 69 additions and 34 deletions

View File

@@ -2,7 +2,8 @@ use std::ops::Range;
use eframe::egui::plot::Value;
// Handles logging based on if the target is wasm (or not) and if `debug_assertions` is enabled or not
// Handles logging based on if the target is wasm (or not) and if
// `debug_assertions` is enabled or not
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::prelude::*;
@@ -43,8 +44,11 @@ cfg_if::cfg_if! {
}
}
/// `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 inefficient
/// `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
/// inefficient
pub struct SteppedVector {
// Actual data being referenced. HAS to be sorted from maximum value to minumum
data: Vec<f64>,
@@ -55,14 +59,17 @@ pub struct SteppedVector {
// Maximum value
max: f64,
// Since all entries in `data` are evenly spaced, this field stores the step between 2 adjacent elements
// Since all entries in `data` are evenly spaced, this field stores the step between 2 adjacent
// elements
step: f64,
}
impl SteppedVector {
/// Returns `Option<usize>` with index of element with value `x`. and `None` if `x` does not exist in `data`
/// Returns `Option<usize>` with index of element with value `x`. and `None`
/// if `x` does not exist in `data`
pub fn get_index(&self, x: f64) -> Option<usize> {
// if `x` is outside range, just go ahead and return `None` as it *shouldn't* be in `data`
// if `x` is outside range, just go ahead and return `None` as it *shouldn't* be
// in `data`
if (x > self.max) | (self.min > x) {
return None;
}
@@ -70,7 +77,8 @@ impl SteppedVector {
// Do some math in order to calculate the expected index value
let possible_i = ((x + self.min) / self.step) as usize;
// Make sure that the index is valid by checking the data returned vs the actual data (just in case)
// Make sure that the index is valid by checking the data returned vs the actual
// data (just in case)
if self.data[possible_i] == x {
// It is valid!
Some(possible_i)
@@ -96,7 +104,8 @@ impl SteppedVector {
// Convert `Vec<f64>` into `SteppedVector`
impl From<Vec<f64>> for SteppedVector {
/// Note: input `data` is assumed to be sorted properly
/// `data` is a Vector of 64 bit floating point numbers ordered from max -> min
/// `data` is a Vector of 64 bit floating point numbers ordered from max ->
/// min
fn from(data: Vec<f64>) -> SteppedVector {
let max = data[0]; // The max value should be the first element
let min = data[data.len() - 1]; // The minimum value should be the last element
@@ -115,14 +124,15 @@ impl From<Vec<f64>> for SteppedVector {
// Rounds f64 to specific number of decimal places
pub fn decimal_round(x: f64, n: usize) -> f64 {
let large_number: f64 = 10.0_f64.powf(n as f64); // 10^n
(x * large_number).round() / large_number // round and devide in order to cut off after the `n`th decimal place
(x * large_number).round() / large_number // round and devide in order to cut
// off after the `n`th decimal place
}
/// Implements newton's method of finding roots.
/// `threshold` is the target accuracy threshold
/// `range` is the range of valid x values (used to stop calculation when the point won't display anyways)
/// `data` is the data to iterate over (a Vector of egui's `Value` struct)
/// `f` is f(x)
/// `range` is the range of valid x values (used to stop calculation when the
/// point won't display anyways) `data` is the data to iterate over (a Vector of
/// egui's `Value` struct) `f` is f(x)
/// `f_1` is f'(x)
/// The function returns a Vector of `x` values where roots occur
pub fn newtons_method(