From 1d76c2245a8e9d3e49178e9a47b0215219f9e032 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 21 Mar 2022 13:45:17 -0400 Subject: [PATCH] cleanup --- src/parsing.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/parsing.rs b/src/parsing.rs index 6c8cd4a..c5effef 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -1,18 +1,24 @@ use exmex::prelude::*; lazy_static::lazy_static! { + /// Function returns `f64::NaN` at every x value, which is not displayed. static ref EMPTY_FUNCTION: FlatEx = exmex::parse::("0/0").unwrap(); } #[derive(Clone)] pub struct BackingFunction { + /// f(x) function: FlatEx, + /// f'(x) derivative_1: FlatEx, + /// Mathematical representation of f'(x) derivative_1_str: String, + /// f''(x) derivative_2: FlatEx, } impl BackingFunction { + /// Create new BackingFunction instance pub fn new(func_str: &str) -> Self { let function = exmex::parse::(func_str).unwrap(); let derivative_1 = function @@ -31,14 +37,18 @@ impl BackingFunction { } } + /// Returns Mathematical representation of the function's derivative pub fn get_derivative_str(&self) -> &str { &self.derivative_1_str } + /// Calculate f(x) pub fn get(&self, x: f64) -> f64 { self.function.eval(&[x]).unwrap_or(f64::NAN) } + /// Calculate f'(x) pub fn get_derivative_1(&self, x: f64) -> f64 { self.derivative_1.eval(&[x]).unwrap_or(f64::NAN) } + /// Calculate f''(x) pub fn get_derivative_2(&self, x: f64) -> f64 { self.derivative_2.eval(&[x]).unwrap_or(f64::NAN) } @@ -134,8 +144,8 @@ pub fn process_func_str(function_in: String) -> String { output_string.replace("log(", "log10(") } -// Tests function to make sure it's able to be parsed. Returns the string of the -// Error produced, or an empty string if it runs successfully. +/// Tests function to make sure it's able to be parsed. Returns the string of +/// the Error produced, or an empty string if it runs successfully. pub fn test_func(function_string: &str) -> Option { let parse_result = exmex::parse::(function_string); @@ -171,13 +181,14 @@ pub fn test_func(function_string: &str) -> Option { } } -// Used for testing: passes function to `add_asterisks` before running -// `test_func` +/// Used for testing: passes function to `add_asterisks` before running +/// `test_func` #[cfg(test)] fn test_func_helper(function_string: &str) -> Option { test_func(&process_func_str(function_string.to_string())) } +/// Tests to make sure functions are parsed correctly #[test] fn test_func_test() { // These shouldn't fail @@ -195,12 +206,13 @@ fn test_func_test() { assert!(test_func_helper("abcdef").is_some()); } +/// Helps with tests of `process_func_str` #[cfg(test)] fn test_process_helper(input: &str, expected: &str) { assert_eq!(&process_func_str(input.to_string()), expected); } -// Tests to make sure my cursed function works as intended +/// Tests to make sure my cursed function works as intended #[test] fn func_process_test() { test_process_helper("2x", "2*x");