Skip to content

Commit

Permalink
feat: add accessor methods to various distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
Qazalbash committed Jan 15, 2025
1 parent a8fe65c commit 7c028b5
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/distribution/dirac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ impl Dirac {
Ok(Dirac(v))
}
}

/// Returns the value `v` of the dirac distribution
///
/// # Examples
///
/// ```
/// use statrs::distribution::Dirac;
///
/// let n = Dirac::new(3.0).unwrap();
/// assert_eq!(n.v(), 3.0);
/// ```
pub fn v(&self) -> f64 {
self.0
}
}

impl std::fmt::Display for Dirac {
Expand Down
38 changes: 38 additions & 0 deletions src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,44 @@ impl DiscreteUniform {
Ok(DiscreteUniform { min, max })
}
}

/// Returns the minimum value in the domain of the discrete uniform
/// distribution
///
/// # Remarks
///
/// This is the same value as the minimum passed into the constructor
///
/// # Examples
///
/// ```
/// use statrs::distribution::DiscreteUniform;
///
/// let n = DiscreteUniform::new(0, 5).unwrap();
/// assert_eq!(n.min(), 0);
/// ```
pub fn min(&self) -> i64 {
self.min
}

/// Returns the maximum value in the domain of the discrete uniform
/// distribution
///
/// # Remarks
///
/// This is the same value as the maximum passed into the constructor
///
/// # Examples
///
/// ```
/// use statrs::distribution::DiscreteUniform;
///
/// let n = DiscreteUniform::new(0, 5).unwrap();
/// assert_eq!(n.max(), 5);
/// ```
pub fn max(&self) -> i64 {
self.max
}
}

impl std::fmt::Display for DiscreteUniform {
Expand Down
28 changes: 28 additions & 0 deletions src/distribution/log_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ impl LogNormal {

Ok(LogNormal { location, scale })
}

/// Returns the location of the log-normal distribution
///
/// # Examples
///
/// ```
/// use statrs::distribution::LogNormal;
///
/// let n = LogNormal::new(0.0, 1.0).unwrap();
/// assert_eq!(n.location(), 0.0);
/// ```
pub fn location(&self) -> f64 {
self.location
}

/// Returns the scale of the log-normal distribution
///
/// # Examples
///
/// ```
/// use statrs::distribution::LogNormal;
///
/// let n = LogNormal::new(0.0, 1.0).unwrap();
/// assert_eq!(n.scale(), 1.0);
/// ```
pub fn scale(&self) -> f64 {
self.scale
}
}

impl std::fmt::Display for LogNormal {
Expand Down
38 changes: 38 additions & 0 deletions src/distribution/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,44 @@ impl Triangular {

Ok(Triangular { min, max, mode })
}

/// Returns the minimum value in the domain of the
/// triangular distribution
///
/// # Remarks
///
/// The return value is the same min used to construct the distribution
///
/// # Examples
///
/// ```
/// use statrs::distribution::Triangular;
///
/// let n = Triangular::new(0.0, 5.0, 2.5).unwrap();
/// assert_eq!(n.min(), 0.0);
/// ```
pub fn min(&self) -> f64 {
self.min
}

/// Returns the maximum value in the domain of the
/// triangular distribution
///
/// # Remarks
///
/// The return value is the same max used to construct the distribution
///
/// # Examples
///
/// ```
/// use statrs::distribution::Triangular;
///
/// let n = Triangular::new(0.0, 5.0, 2.5).unwrap();
/// assert_eq!(n.max(), 5.0);
/// ```
pub fn max(&self) -> f64 {
self.max
}
}

impl std::fmt::Display for Triangular {
Expand Down
30 changes: 30 additions & 0 deletions src/distribution/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,36 @@ impl Uniform {
pub fn standard() -> Self {
Self { min: 0.0, max: 1.0 }
}

/// Returns the lower bound of the uniform distribution
/// as a `f64`
///
/// # Examples
///
/// ```
/// use statrs::distribution::Uniform;
///
/// let uniform = Uniform::new(0.0, 1.0).unwrap();
/// assert_eq!(uniform.min(), 0.0);
/// ```
pub fn min(&self) -> f64 {
self.min
}

/// Returns the upper bound of the uniform distribution
/// as a `f64`
///
/// # Examples
///
/// ```
/// use statrs::distribution::Uniform;
///
/// let uniform = Uniform::new(0.0, 1.0).unwrap();
/// assert_eq!(uniform.max(), 1.0);
/// ```
pub fn max(&self) -> f64 {
self.max
}
}

impl Default for Uniform {
Expand Down

0 comments on commit 7c028b5

Please sign in to comment.