Struct Mix

Source
pub struct Mix<T, U, X>{ /* private fields */ }
Expand description

Mixture distributions.

§Examples

use rand_distr::{Distribution, Normal, Uniform};
use mix_distribution::Mix;

let mut rng = rand::thread_rng();

// Mixture of two distributions
let mix = {
    let dists = vec![
        Normal::new(0.0, 1.0).unwrap(),
        Normal::new(1.0, 2.0).unwrap(),
    ];
    let weights = &[2, 1];
    Mix::new(dists, weights).unwrap()
};
mix.sample(&mut rng);

// Mixture of three distributions
let mix = {
    let dists = vec![
        Uniform::new_inclusive(0.0, 2.0),
        Uniform::new_inclusive(1.0, 3.0),
        Uniform::new_inclusive(2.0, 4.0),
    ];
    let weights = &[2, 1, 3];
    Mix::new(dists, weights).unwrap()
};
mix.sample(&mut rng);

// From iterator over (distribution, weight) pairs
let mix = Mix::with_zip(vec![
    (Uniform::new_inclusive(0, 2), 2),
    (Uniform::new_inclusive(1, 3), 1),
])
.unwrap();
mix.sample(&mut rng);

Implementations§

Source§

impl<T, U, X> Mix<T, U, X>

Source

pub fn new<I, J>(dists: I, weights: J) -> Result<Self, WeightedError>
where I: IntoIterator<Item = T>, J: IntoIterator, J::Item: SampleBorrow<X>, X: for<'a> AddAssign<&'a X> + Clone + Default,

Creates a new Mix. dists and weights must have the same length.

Propagates errors from rand_dist::weighted::WeightedIndex::new().

Source

pub fn with_zip<W>( dists_weights: impl IntoIterator<Item = (T, W)>, ) -> Result<Self, WeightedError>
where W: SampleBorrow<X>, X: for<'a> AddAssign<&'a X> + Clone + Default,

Creats a new Mix with the given iterator over (distribution, weight) pairs.

Propagates errors from rand_dist::weighted::WeightedIndex::new().

Trait Implementations§

Source§

impl<T, U, X> Clone for Mix<T, U, X>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<T, U, X> Debug for Mix<T, U, X>

Source§

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

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

impl<T, U, X> Distribution<U> for Mix<T, U, X>

Source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> U

Generate a random value of T, using rng as the source of randomness.
Source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more

Auto Trait Implementations§

§

impl<T, U, X> Freeze for Mix<T, U, X>
where X: Freeze, <X as SampleUniform>::Sampler: Freeze,

§

impl<T, U, X> RefUnwindSafe for Mix<T, U, X>

§

impl<T, U, X> Send for Mix<T, U, X>
where X: Send, <X as SampleUniform>::Sampler: Send, U: Send, T: Send,

§

impl<T, U, X> Sync for Mix<T, U, X>
where X: Sync, <X as SampleUniform>::Sampler: Sync, U: Sync, T: Sync,

§

impl<T, U, X> Unpin for Mix<T, U, X>
where X: Unpin, <X as SampleUniform>::Sampler: Unpin, U: Unpin, T: Unpin,

§

impl<T, U, X> UnwindSafe for Mix<T, U, X>

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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V