Struct LogDomain

Source
pub struct LogDomain<F: Float>(/* private fields */);
Expand description

A struct that represents positive floats by their natural logarithm.

§Examples

use log_domain::LogDomain;
use num_traits::{One, Zero};

match (LogDomain::new(0.5), LogDomain::new(0.25), LogDomain::new(0.75)) {
    (Ok(x), Ok(y), Ok(z)) => {
        assert_eq!(x.value(), 0.5);            // 0.5         = 0.5
        assert_eq!(x.ln(), f64::ln(0.5));      // ln(0.5)     = ln(0.5)

        // Operations `+`, `-`, `*`, `/`, and `pow`
        assert_eq!(x + y, z);                  // 0.5  + 0.25 = 0.75
        assert_eq!(z - x, y);                  // 0.75 - 0.5  = 0.25
        assert_eq!(x * x, y);                  // 0.5  ⋅ 0.5  = 0.25
        assert_eq!(y / x, x);                  // 0.25 / 0.5  = 0.5
        assert_eq!(x.pow(2.0), y);             // 0.5²        = 0.25
        assert_eq!(y.pow(1.0 / 2.0), x);       // √0.25       = 0.5

        // Assignment operators `+=`, `-=`, `*=`, and `/=`
        let mut a = x;
        a += y;                                // a = 0.5  + 0.25 = 0.75
        assert_eq!(a, z);                      // a = 0.75 = z
        a = z;
        a -= x;                                // a = 0.75 - 0.5  = 0.25
        assert_eq!(a, y);                      // a = 0.25 = y
        a = x;
        a *= a;                                // a = 0.5  * 0.5  = 0.25
        assert_eq!(a, y);                      // a = 0.25 = y
        a = y;
        a /= x;                                // a = 0.25 / 0.5  = 0.5
        assert_eq!(a, x);                      // a = 0.5 = x

        // Neutral elements `LogDomain::zero()` and `LogDomain::one()`
        assert_eq!(z + LogDomain::zero(), z);  // 0.75 + 0    = 0.75
        assert_eq!(z - z, LogDomain::zero());  // 0.75 - 0.75 = 0
        assert_eq!(z * LogDomain::one(), z);   // 0.75 * 1    = 0.75
        assert_eq!(z / z, LogDomain::one());   // 0.75 / 0.75 = 1
        assert_eq!(z * LogDomain::zero(), LogDomain::zero());
                                               // 0.75 * 0    = 0
        assert_eq!(z.pow(0.0), LogDomain::one());
                                               // 0.75⁰       = 1

        // Comparison
        assert!(z > y);                        // 0.75 > 0.25
        assert!(y < z);                        // 0.25 < 0.75
    },
    _ => panic!(),
}

Implementations§

Source§

impl<F: Float + Debug> LogDomain<F>

Source

pub fn new(value: F) -> Result<Self, String>

Creates a new LogDomain from a given value in the interval [0,∞).

Source§

impl<F: Float> LogDomain<F>

Source

pub fn ln(&self) -> F

Logarithm of the value that is represented by the given LogDomain.

Source

pub fn value(&self) -> F

Value that is represented by the given LogDomain.

Source

pub fn pow(&self, power: F) -> Self

Raise the represented probability to the power of a Float value.

Trait Implementations§

Source§

impl<F: Float> Add for LogDomain<F>

An impl of Add that uses only two applications of transcendental functions (exp and ln_1p) to increase precision.

Source§

type Output = LogDomain<F>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl<F: Float> AddAssign for LogDomain<F>

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<F: Clone + Float> Clone for LogDomain<F>

Source§

fn clone(&self) -> LogDomain<F>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug + Float> Debug for LogDomain<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, F: Float + Deserialize<'de>> Deserialize<'de> for LogDomain<F>

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<F: Float + Display> Display for LogDomain<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Float> Div for LogDomain<F>

Source§

type Output = LogDomain<F>

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self

Performs the / operation. Read more
Source§

impl<F: Float> DivAssign for LogDomain<F>

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl<F: Debug + Float + FromStr<Err = E>, E: ToString> FromStr for LogDomain<F>

Source§

type Err = String

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<F: Float> Mul for LogDomain<F>

Source§

type Output = LogDomain<F>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl<F: Float> MulAssign for LogDomain<F>

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl<F: Float> One for LogDomain<F>

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
Source§

impl<F: Float> Ord for LogDomain<F>

An impl of Ord that defines Exp(NaN) = Exp(NaN), Exp(NaN) < Exp(y), and Exp(x) < Exp(y) for x < y.

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<F: Float> PartialEq for LogDomain<F>

An impl of PartialEq that defines Exp(NaN) = Exp(NaN) and Exp(x) = Exp(y) for x - y < F::EPSILON.

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F: PartialOrd + Float> PartialOrd for LogDomain<F>

Source§

fn partial_cmp(&self, other: &LogDomain<F>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<F: Float + Serialize> Serialize for LogDomain<F>

Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<F: Float + Debug> Sub for LogDomain<F>

An impl of Sub that uses only two applications of transcendental functions (exp_m1 and ln) to increase precision.

Source§

type Output = LogDomain<F>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl<F: Float + Debug> SubAssign for LogDomain<F>

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<F: Float> Zero for LogDomain<F>

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl<F: Copy + Float> Copy for LogDomain<F>

Source§

impl<F: Float> Eq for LogDomain<F>

Auto Trait Implementations§

§

impl<F> Freeze for LogDomain<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for LogDomain<F>
where F: RefUnwindSafe,

§

impl<F> Send for LogDomain<F>
where F: Send,

§

impl<F> Sync for LogDomain<F>
where F: Sync,

§

impl<F> Unpin for LogDomain<F>
where F: Unpin,

§

impl<F> UnwindSafe for LogDomain<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,