fix SteppedVector
This commit is contained in:
parent
d20058fb92
commit
8191852f17
18
src/misc.rs
18
src/misc.rs
@ -101,10 +101,8 @@ impl SteppedVector {
|
|||||||
|
|
||||||
// Convert `Vec<f64>` into `SteppedVector`
|
// Convert `Vec<f64>` into `SteppedVector`
|
||||||
impl From<Vec<f64>> for SteppedVector {
|
impl From<Vec<f64>> for SteppedVector {
|
||||||
/// Note: input `data` is assumed to be sorted properly
|
fn from(input_data: Vec<f64>) -> SteppedVector {
|
||||||
/// `data` is a Vector of 64 bit floating point numbers ordered from max ->
|
let mut data = input_data;
|
||||||
/// min
|
|
||||||
fn from(data: Vec<f64>) -> SteppedVector {
|
|
||||||
// length of data
|
// length of data
|
||||||
let data_length = data.len();
|
let data_length = data.len();
|
||||||
// length of data subtracted by 1 (represents the maximum index value)
|
// length of data subtracted by 1 (represents the maximum index value)
|
||||||
@ -112,15 +110,17 @@ impl From<Vec<f64>> for SteppedVector {
|
|||||||
|
|
||||||
// Ensure data is of correct length
|
// Ensure data is of correct length
|
||||||
if data_length < 2 {
|
if data_length < 2 {
|
||||||
panic!("SteppedVector: data should have a length longer than 2")
|
panic!("SteppedVector: data should have a length longer than 2");
|
||||||
}
|
}
|
||||||
|
|
||||||
let max = data[0]; // The max value should be the first element
|
let mut max: f64 = data[0]; // The max value should be the first element
|
||||||
let min = data[data_i_length]; // The minimum value should be the last element
|
let mut min: f64 = data[data_i_length]; // The minimum value should be the last element
|
||||||
|
|
||||||
// Just some checks here
|
// if min is bigger than max, sort the input data
|
||||||
if min > max {
|
if min > max {
|
||||||
panic!("SteppedVector: first element is smaller than the last element")
|
data.sort_unstable_by(|a, b| b.partial_cmp(a).unwrap());
|
||||||
|
max = data[0];
|
||||||
|
min = data[data_i_length];
|
||||||
}
|
}
|
||||||
|
|
||||||
let step = (max - min).abs() / (data_i_length as f64); // Calculate the step between elements
|
let step = (max - min).abs() / (data_i_length as f64); // Calculate the step between elements
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user