#[repr(transparent)]pub struct Reduction<T>(pub T);
Expand description
A newtype representing a reduction factor of (1 - T)
.
See the module-level documentation for more information.
Note that this type does not implement the num_traits::One
trait.
This is intentional as the trait requires one()
to return the multiplicative identity, which for Reduction
is Reduction::none()
.
However, having Reduction::one()
return a Reduction(T::zero())
would be highly confusing.
Tuple Fields§
§0: T
Implementations§
Source§impl<T> Reduction<T>
impl<T> Reduction<T>
Sourcepub fn none() -> Selfwhere
T: Zero,
pub fn none() -> Selfwhere
T: Zero,
Creates a Reduction
representing no reduction (0%).
This is the multiplicative identity for composition: any reduction composed
with none
remains unchanged.
Its multiplier is 1.
Source§impl<T: One + Sub<Output = T>> Reduction<T>
impl<T: One + Sub<Output = T>> Reduction<T>
Sourcepub fn multiplier(self) -> T
pub fn multiplier(self) -> T
Calculates the multiplicative factor, 1 - self.0
.
This is the value that is multiplied with a base value when the reduction is applied.
§Example
use reduction_factor::Reduction;
let r = Reduction(0.25f32);
assert_eq!(r.multiplier(), 0.75);
Sourcepub fn reduce(self, value: T) -> T
pub fn reduce(self, value: T) -> T
Applies the reduction to a given value.
This is equivalent to value * self.multiplier()
.
This operation is also available through multiplication: reduction * value
.
§Example
use reduction_factor::Reduction;
let r = Reduction(0.25f32);
assert_eq!(r.reduce(100.0), 75.0);
assert_eq!(r * 100.0, 75.0);
Sourcepub fn complement(self) -> Self
pub fn complement(self) -> Self
Returns the complement of the reduction.
The complement of Reduction(x)
is Reduction(1 - x)
.
For example, the complement of a 25% reduction is an 75% reduction.
§Example
use reduction_factor::Reduction;
let r = Reduction(0.25f32);
let complement = r.complement();
assert_eq!(*complement, 0.75);
Source§impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone> Reduction<T>
impl<T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone> Reduction<T>
Sourcepub fn compose(self, other: Self) -> Self
pub fn compose(self, other: Self) -> Self
Composes this reduction with another, returning a new Reduction
.
This is equivalent to applying one reduction and then the other.
The formula for the new inner value is x + y - xy
where
x
and y
are the inner values of the two reductions.
This operation is also available through multiplication: r1 * r2
.
§Example
use reduction_factor::Reduction;
let r1 = Reduction(0.20f32);
let r2 = Reduction(0.10f32);
let stacked = r1.compose(r2);
assert_eq!(*stacked, 0.28);
assert_eq!(r1 * r2, stacked); // Equivalent
Sourcepub fn compose_inplace(&mut self, other: Self)where
T: Default,
pub fn compose_inplace(&mut self, other: Self)where
T: Default,
In-place version of compose
.
Sourcepub fn pow(&self, exponent: usize) -> Selfwhere
T: Zero,
pub fn pow(&self, exponent: usize) -> Selfwhere
T: Zero,
Composes the reduction with itself exponent
times.
pow(0)
returns Reduction(0)
the identity reduction, Reduction::none()
.
§Example
use reduction_factor::Reduction;
let r = Reduction(0.5f32);
// Applying a 50% reduction twice results in a 75% reduction.
let r2 = r.pow(2);
assert_eq!(*r2, 0.75);
assert_eq!(r * r, r2);
Zero exponent returns the identity reduction.
use reduction_factor::Reduction;
let r = Reduction(0.5f32);
let r0 = r.pow(0);
assert_eq!(*r0, 0.0);
assert_eq!(r0, Reduction::none());
Sourcepub fn pow_nonzero(&self, exponent: NonZeroUsize) -> Self
pub fn pow_nonzero(&self, exponent: NonZeroUsize) -> Self
Composes the reduction with itself exponent
times for a non-zero exponent.
The main difference from Reduction::pow()
is that T: Zero
isn’t required.
§Example
use reduction_factor::Reduction;
use core::num::NonZeroUsize;
let r = Reduction(0.5f32);
// Applying a 50% reduction twice results in a 75% reduction.
let r2 = r.pow_nonzero(NonZeroUsize::new(2).unwrap());
assert_eq!(*r2, 0.75);
assert_eq!(r * r, r2);
Trait Implementations§
Source§impl<T: Default + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone> MulAssign for Reduction<T>
impl<T: Default + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Clone> MulAssign for Reduction<T>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
The *=
operator is overloaded for two Reduction
s to perform compose_inplace
.