diff -r 9738b51d90d7 -r 4f468d35fa29 src/rand_distr.rs --- a/src/rand_distr.rs Sun Apr 27 15:03:51 2025 -0500 +++ b/src/rand_distr.rs Thu Feb 26 11:38:43 2026 -0500 @@ -1,35 +1,42 @@ //! Random distribution wrappers and implementations +use alg_tools::types::*; use numeric_literals::replace_float_literals; use rand::Rng; -use rand_distr::{Distribution, Normal, StandardNormal, NormalError}; -use serde::{Serialize, Deserialize}; -use serde::ser::{Serializer, SerializeStruct}; -use alg_tools::types::*; +use rand_distr::{Distribution, Normal, NormalError, StandardNormal}; +use serde::ser::{SerializeStruct, Serializer}; +use serde::{Deserialize, Serialize}; /// Wrapper for [`Normal`] that can be serialized by serde. #[derive(Debug)] -pub struct SerializableNormal(Normal) -where StandardNormal : Distribution; +pub struct SerializableNormal(Normal) +where + StandardNormal: Distribution; -impl Distribution for SerializableNormal -where StandardNormal : Distribution { +impl Distribution for SerializableNormal +where + StandardNormal: Distribution, +{ fn sample(&self, rng: &mut R) -> T where - R : Rng + ?Sized - { self.0.sample(rng) } + R: Rng + ?Sized, + { + self.0.sample(rng) + } } -impl SerializableNormal -where StandardNormal : Distribution { - pub fn new(mean : T, std_dev : T) -> Result, NormalError> { +impl SerializableNormal +where + StandardNormal: Distribution, +{ + pub fn new(mean: T, std_dev: T) -> Result, NormalError> { Ok(SerializableNormal(Normal::new(mean, std_dev)?)) } } impl Serialize for SerializableNormal where - StandardNormal : Distribution, + StandardNormal: Distribution, F: Float + Serialize, { fn serialize(&self, serializer: S) -> Result @@ -48,11 +55,11 @@ /// This is the distribution that outputs each $\\{-m,0,m\\}$ with the corresponding /// probabilities $\\{1-p, p/2, p/2\\}$. #[derive(Copy, Clone, Debug, Serialize, Deserialize)] -pub struct SaltAndPepper{ +pub struct SaltAndPepper { /// The magnitude parameter $m$ - magnitude : T, + magnitude: T, /// The probability parameter $p$ - probability : T + probability: T, } /// Error for [`SaltAndPepper`]. @@ -66,15 +73,16 @@ impl std::fmt::Display for SaltAndPepperError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { - SaltAndPepperError::InvalidProbability => - " The probability parameter is not in the range [0, 1].", + SaltAndPepperError::InvalidProbability => { + " The probability parameter is not in the range [0, 1]." + } }) } } #[replace_float_literals(T::cast_from(literal))] -impl SaltAndPepper { - pub fn new(magnitude : T, probability : T) -> Result, SaltAndPepperError> { +impl SaltAndPepper { + pub fn new(magnitude: T, probability: T) -> Result, SaltAndPepperError> { if probability > 1.0 || probability < 0.0 { Err(SaltAndPepperError::InvalidProbability) } else { @@ -84,16 +92,16 @@ } #[replace_float_literals(T::cast_from(literal))] -impl Distribution for SaltAndPepper { +impl Distribution for SaltAndPepper { fn sample(&self, rng: &mut R) -> T where - R : Rng + ?Sized + R: Rng + ?Sized, { - let (p, sign) : (float, bool) = rng.gen(); + let (p, sign): (float, bool) = rng.random(); match (p < self.probability.as_(), sign) { - (false, _) => 0.0, - (true, true) => self.magnitude, - (true, false) => -self.magnitude, + (false, _) => 0.0, + (true, true) => self.magnitude, + (true, false) => -self.magnitude, } } }