-
-
Notifications
You must be signed in to change notification settings - Fork 7
MultiDistribution #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4d7387d
0048240
5c96617
6a5bd80
c9df79c
e61da69
4a78dbc
cb4c824
7572ce3
b1e663d
b53aeda
2435d3f
c4acb6d
860897c
3219cd6
e74b4b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! Contains Multi-dimensional distributions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drop the word 'contains' and remove the full-stop since this isn't a sentence. |
||
//! | ||
//! We provide a trait `MultiDistribution` which allows to sample from a multi-dimensional distribution without extra allocations. | ||
//! All multi-dimensional distributions implement `MultiDistribution` instead of the `Distribution` trait. | ||
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally wrap comments at 80 chars width (sometimes up to 100 if the line already has a large indent). The wording could be a little better, e.g.
|
||
|
||
use alloc::vec::Vec; | ||
use rand::Rng; | ||
|
||
/// This trait allows to sample from a multi-dimensional distribution without extra allocations. | ||
/// For convenience it also provides a `sample` method which returns the result as a `Vec`. | ||
pub trait MultiDistribution<T> { | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Items have a short one-line description, with additional details in new paragraphs. |
||
/// returns the length of one sample (dimension of the distribution) | ||
fn sample_len(&self) -> usize; | ||
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Capitalise the first letter of 'returns' |
||
/// samples from the distribution and writes the result to `buf` | ||
fn sample_to_buf<R: Rng + ?Sized>(&self, rng: &mut R, buf: &mut [T]); | ||
/// samples from the distribution and returns the result as a `Vec`, to avoid extra allocations use `sample_to_buf` | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec<T> | ||
where | ||
T: Default, | ||
{ | ||
let mut buf = Vec::new(); | ||
buf.resize_with(self.sample_len(), || T::default()); | ||
self.sample_to_buf(rng, &mut buf); | ||
buf | ||
} | ||
} | ||
|
||
pub use dirichlet::Dirichlet; | ||
|
||
mod dirichlet; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be a
const fn
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, this is not supported by traits.