From e7e82a72b2447afefc10896aed5ef769ae3a9e7c Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 18 May 2022 14:52:03 -0400 Subject: [PATCH] fix build and add test for newtons_method --- Cargo.lock | 12 ++++++------ src/lib.rs | 1 + src/misc.rs | 2 +- tests/misc.rs | 13 +++++++++++++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df9aff3..80ef731 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -659,7 +659,7 @@ checksum = "453440c271cf5577fd2a40e4942540cb7d0d2f85e27c8d07dd0023c925a67541" [[package]] name = "eframe" version = "0.18.0" -source = "git+https://github.com/Titaniumtown/egui.git#537c879b1c5a40384188dcc731a6d55c04deb99f" +source = "git+https://github.com/Titaniumtown/egui.git#ea25cc1a991610099ada8fe02f1c11ba708f77fd" dependencies = [ "bytemuck", "egui", @@ -679,7 +679,7 @@ dependencies = [ [[package]] name = "egui" version = "0.18.1" -source = "git+https://github.com/Titaniumtown/egui.git#537c879b1c5a40384188dcc731a6d55c04deb99f" +source = "git+https://github.com/Titaniumtown/egui.git#ea25cc1a991610099ada8fe02f1c11ba708f77fd" dependencies = [ "ahash", "epaint", @@ -691,7 +691,7 @@ dependencies = [ [[package]] name = "egui-winit" version = "0.18.0" -source = "git+https://github.com/Titaniumtown/egui.git#537c879b1c5a40384188dcc731a6d55c04deb99f" +source = "git+https://github.com/Titaniumtown/egui.git#ea25cc1a991610099ada8fe02f1c11ba708f77fd" dependencies = [ "arboard", "egui", @@ -705,7 +705,7 @@ dependencies = [ [[package]] name = "egui_glow" version = "0.18.1" -source = "git+https://github.com/Titaniumtown/egui.git#537c879b1c5a40384188dcc731a6d55c04deb99f" +source = "git+https://github.com/Titaniumtown/egui.git#ea25cc1a991610099ada8fe02f1c11ba708f77fd" dependencies = [ "bytemuck", "egui", @@ -725,7 +725,7 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "emath" version = "0.18.0" -source = "git+https://github.com/Titaniumtown/egui.git#537c879b1c5a40384188dcc731a6d55c04deb99f" +source = "git+https://github.com/Titaniumtown/egui.git#ea25cc1a991610099ada8fe02f1c11ba708f77fd" dependencies = [ "bytemuck", "serde", @@ -734,7 +734,7 @@ dependencies = [ [[package]] name = "epaint" version = "0.18.1" -source = "git+https://github.com/Titaniumtown/egui.git#537c879b1c5a40384188dcc731a6d55c04deb99f" +source = "git+https://github.com/Titaniumtown/egui.git#ea25cc1a991610099ada8fe02f1c11ba708f77fd" dependencies = [ "ab_glyph", "ahash", diff --git a/src/lib.rs b/src/lib.rs index b7c0950..120e415 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,7 @@ pub use crate::{ // decimal_round, hashed_storage_create, hashed_storage_read, + newtons_method, option_vec_printer, step_helper, EguiHelper, diff --git a/src/misc.rs b/src/misc.rs index 82afac7..3c62b5b 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -247,7 +247,7 @@ pub fn newtons_method_helper( /// `f` is f(x) /// `f_1` is f'(x) aka the derivative of f(x) /// The function returns an `Option` of the x value at which a root occurs -fn newtons_method( +pub fn newtons_method( f: &dyn Fn(f64) -> f64, f_1: &dyn Fn(f64) -> f64, start_x: &f64, range: &std::ops::Range, threshold: &f64, ) -> Option { diff --git a/tests/misc.rs b/tests/misc.rs index 1d88214..bff9109 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -162,3 +162,16 @@ fn to_points() { assert_eq!(*data.get_series().get_values(), data_raw); } + +#[test] +fn newtons_method() { + use ytbn_graphing_software::newtons_method; + let data = newtons_method( + &|x: f64| x.powf(2.0) - 1.0, + &|x: f64| 2.0 * x, + &3.0, + &(0.0..10.0), + &f64::EPSILON, + ); + assert_eq!(data, Some(1.0)); +}