cleanup
This commit is contained in:
parent
3bb8da9209
commit
1d76c2245a
@ -1,18 +1,24 @@
|
|||||||
use exmex::prelude::*;
|
use exmex::prelude::*;
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
|
/// Function returns `f64::NaN` at every x value, which is not displayed.
|
||||||
static ref EMPTY_FUNCTION: FlatEx<f64> = exmex::parse::<f64>("0/0").unwrap();
|
static ref EMPTY_FUNCTION: FlatEx<f64> = exmex::parse::<f64>("0/0").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BackingFunction {
|
pub struct BackingFunction {
|
||||||
|
/// f(x)
|
||||||
function: FlatEx<f64>,
|
function: FlatEx<f64>,
|
||||||
|
/// f'(x)
|
||||||
derivative_1: FlatEx<f64>,
|
derivative_1: FlatEx<f64>,
|
||||||
|
/// Mathematical representation of f'(x)
|
||||||
derivative_1_str: String,
|
derivative_1_str: String,
|
||||||
|
/// f''(x)
|
||||||
derivative_2: FlatEx<f64>,
|
derivative_2: FlatEx<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BackingFunction {
|
impl BackingFunction {
|
||||||
|
/// Create new BackingFunction instance
|
||||||
pub fn new(func_str: &str) -> Self {
|
pub fn new(func_str: &str) -> Self {
|
||||||
let function = exmex::parse::<f64>(func_str).unwrap();
|
let function = exmex::parse::<f64>(func_str).unwrap();
|
||||||
let derivative_1 = function
|
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 }
|
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) }
|
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 {
|
pub fn get_derivative_1(&self, x: f64) -> f64 {
|
||||||
self.derivative_1.eval(&[x]).unwrap_or(f64::NAN)
|
self.derivative_1.eval(&[x]).unwrap_or(f64::NAN)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculate f''(x)
|
||||||
pub fn get_derivative_2(&self, x: f64) -> f64 {
|
pub fn get_derivative_2(&self, x: f64) -> f64 {
|
||||||
self.derivative_2.eval(&[x]).unwrap_or(f64::NAN)
|
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(")
|
output_string.replace("log(", "log10(")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests function to make sure it's able to be parsed. Returns the string of the
|
/// Tests function to make sure it's able to be parsed. Returns the string of
|
||||||
// Error produced, or an empty string if it runs successfully.
|
/// the Error produced, or an empty string if it runs successfully.
|
||||||
pub fn test_func(function_string: &str) -> Option<String> {
|
pub fn test_func(function_string: &str) -> Option<String> {
|
||||||
let parse_result = exmex::parse::<f64>(function_string);
|
let parse_result = exmex::parse::<f64>(function_string);
|
||||||
|
|
||||||
@ -171,13 +181,14 @@ pub fn test_func(function_string: &str) -> Option<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used for testing: passes function to `add_asterisks` before running
|
/// Used for testing: passes function to `add_asterisks` before running
|
||||||
// `test_func`
|
/// `test_func`
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn test_func_helper(function_string: &str) -> Option<String> {
|
fn test_func_helper(function_string: &str) -> Option<String> {
|
||||||
test_func(&process_func_str(function_string.to_string()))
|
test_func(&process_func_str(function_string.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tests to make sure functions are parsed correctly
|
||||||
#[test]
|
#[test]
|
||||||
fn test_func_test() {
|
fn test_func_test() {
|
||||||
// These shouldn't fail
|
// These shouldn't fail
|
||||||
@ -195,12 +206,13 @@ fn test_func_test() {
|
|||||||
assert!(test_func_helper("abcdef").is_some());
|
assert!(test_func_helper("abcdef").is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helps with tests of `process_func_str`
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn test_process_helper(input: &str, expected: &str) {
|
fn test_process_helper(input: &str, expected: &str) {
|
||||||
assert_eq!(&process_func_str(input.to_string()), expected);
|
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]
|
#[test]
|
||||||
fn func_process_test() {
|
fn func_process_test() {
|
||||||
test_process_helper("2x", "2*x");
|
test_process_helper("2x", "2*x");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user