Minor cleanup
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 488f18a · Browse files · 1 file·+30 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+30 added−28 removed
- other:+30 added−28 removed
Provenance
- Commit
0bf7d50- Tree
b4e403d- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
488f18a
1 file changed+30 added−28 removed
modified:src/quantity/statistics.rs+30 added-28 removed
6 unmodified lines7 use uom::si::ratio::ratio;8 9 use super::{byte, second, Information, Time};10 11-pub trait UnsafeRawValue {12- fn unsafe_raw_value(&self) -> f64;13- fn unsafe_from_raw_value(value: f64) -> Self;14-}15-16-impl UnsafeRawValue for Time {17- fn unsafe_raw_value(&self) -> f64 {18- self.get::<second>()19- }20-21- fn unsafe_from_raw_value(value: f64) -> Self {22- Time::new::<second>(value)23- }24-}25-26-impl UnsafeRawValue for Information {27- fn unsafe_raw_value(&self) -> f64 {28- self.get::<byte>()29- }30-31- fn unsafe_from_raw_value(value: f64) -> Self {32- Information::new::<byte>(value)33- }34-}35-11 /// A min function that assumes no NaNs and at least one element12 pub fn min<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {13 values14 .into_iter()44 unmodified lines59 values[len / 2]60 }61 }62 88-fn standard_deviation_f64(values: impl IntoIterator<Item = f64> + Clone) -> f64 {89- let mean_value = mean(values.clone());63+/// This is just sad, but after several hours trying to figure out how to write64+/// a generic `standard_deviation` function using `uom` quantities, I gave up.65+pub trait UnsafeRawValue {66+ fn unsafe_raw_value(&self) -> f64;67+ fn unsafe_from_raw_value(value: f64) -> Self;68+}69 70+impl UnsafeRawValue for Time {71+ fn unsafe_raw_value(&self) -> f64 {72+ self.get::<second>()73+ }74+75+ fn unsafe_from_raw_value(value: f64) -> Self {76+ Time::new::<second>(value)77+ }78+}79+80+impl UnsafeRawValue for Information {81+ fn unsafe_raw_value(&self) -> f64 {82+ self.get::<byte>()83+ }84+85+ fn unsafe_from_raw_value(value: f64) -> Self {86+ Information::new::<byte>(value)87+ }88+}89+90+fn standard_deviation_f64(values: &[f64]) -> f64 {91+ let mean_value = mean(values.iter().copied());92+93 let mut squared_deviations = 0.;94 let mut n = 0;95 for value in values {96 let deviation = value - mean_value;5 unmodified lines102 }103 104 pub fn standard_deviation<Q: UnsafeRawValue>(values: impl IntoIterator<Item = Q> + Clone) -> Q {105 let values: Vec<_> = values.into_iter().map(|q| q.unsafe_raw_value()).collect();104- let result = standard_deviation_f64(values);106+ let result = standard_deviation_f64(&values);107 Q::unsafe_from_raw_value(result)108 }109 110 /// Compute modifized Z-scores for a given sample. A (unmodified) Z-score is defined by116 unmodified lines