diff --git a/src/misc.rs b/src/misc.rs index 3e54f11..8aa1886 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -105,9 +105,25 @@ impl From> for SteppedVector { /// `data` is a Vector of 64 bit floating point numbers ordered from max -> /// min fn from(data: Vec) -> SteppedVector { + // length of data + let data_length = data.len(); + // length of data subtracted by 1 (represents the maximum index value) + let data_i_length = data.len() - 1; + + // Ensure data is of correct length + if data_length < 2 { + panic!("SteppedVector: data should have a length longer than 2") + } + 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 - let step = (max - min).abs() / ((data.len() - 1) as f64); // Calculate the step between elements + let min = data[data_i_length]; // The minimum value should be the last element + + // Just some checks here + if min > max { + panic!("SteppedVector: first element is smaller than the last element") + } + + let step = (max - min).abs() / (data_i_length as f64); // Calculate the step between elements // Create and return the struct SteppedVector { diff --git a/src/parsing.rs b/src/parsing.rs index c5effef..5a26cd6 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -25,6 +25,7 @@ impl BackingFunction { .partial(0) .unwrap_or_else(|_| EMPTY_FUNCTION.clone()); let derivative_1_str = derivative_1.unparse().to_owned().replace("{x}", "x"); + let derivative_2 = function .partial_iter([0, 0].iter()) .unwrap_or_else(|_| EMPTY_FUNCTION.clone());