Skip to content
Myr

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

1 file+30 added−28 removed

modified:src/quantity/statistics.rs+30 added-28 removed
6 unmodified lines
7use uom::si::ratio::ratio;89use super::{byte, second, Information, Time};1011pub trait UnsafeRawValue {12 fn unsafe_raw_value(&self) -> f64;13 fn unsafe_from_raw_value(value: f64) -> Self;14}1516impl UnsafeRawValue for Time {17 fn unsafe_raw_value(&self) -> f64 {18 self.get::<second>()19 }2021 fn unsafe_from_raw_value(value: f64) -> Self {22 Time::new::<second>(value)23 }24}2526impl UnsafeRawValue for Information {27 fn unsafe_raw_value(&self) -> f64 {28 self.get::<byte>()29 }3031 fn unsafe_from_raw_value(value: f64) -> Self {32 Information::new::<byte>(value)33 }34}3511/// A min function that assumes no NaNs and at least one element12pub fn min<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {13 values14 .into_iter()
44 unmodified lines
59 values[len / 2]60 }61}6288fn 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.65pub trait UnsafeRawValue {66 fn unsafe_raw_value(&self) -> f64;67 fn unsafe_from_raw_value(value: f64) -> Self;68}6970impl UnsafeRawValue for Time {71 fn unsafe_raw_value(&self) -> f64 {72 self.get::<second>()73 }7475 fn unsafe_from_raw_value(value: f64) -> Self {76 Time::new::<second>(value)77 }78}7980impl UnsafeRawValue for Information {81 fn unsafe_raw_value(&self) -> f64 {82 self.get::<byte>()83 }8485 fn unsafe_from_raw_value(value: f64) -> Self {86 Information::new::<byte>(value)87 }88}8990fn standard_deviation_f64(values: &[f64]) -> f64 {91 let mean_value = mean(values.iter().copied());9293 let mut squared_deviations = 0.;94 let mut n = 0;95 for value in values {96 let deviation = value - mean_value;
5 unmodified lines
102}103104pub 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}109110/// Compute modifized Z-scores for a given sample. A (unmodified) Z-score is defined by
116 unmodified lines