mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-11 01:17:39 +00:00
Auto merge of #28941 - jcaudle:rustfmt/librand-distributions-normal, r=nrc
This is my first PR to rust and first collaboration on a rust project, but I think the formatting looked ok. If this works out, I hope to go through more of librand.
This commit is contained in:
commit
9d3e79ad37
@ -32,13 +32,13 @@ use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
|
|||||||
pub struct StandardNormal(pub f64);
|
pub struct StandardNormal(pub f64);
|
||||||
|
|
||||||
impl Rand for StandardNormal {
|
impl Rand for StandardNormal {
|
||||||
fn rand<R:Rng>(rng: &mut R) -> StandardNormal {
|
fn rand<R: Rng>(rng: &mut R) -> StandardNormal {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn pdf(x: f64) -> f64 {
|
fn pdf(x: f64) -> f64 {
|
||||||
(-x*x/2.0).exp()
|
(-x * x / 2.0).exp()
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn zero_case<R:Rng>(rng: &mut R, u: f64) -> f64 {
|
fn zero_case<R: Rng>(rng: &mut R, u: f64) -> f64 {
|
||||||
// compute a random number in the tail by hand
|
// compute a random number in the tail by hand
|
||||||
|
|
||||||
// strange initial conditions, because the loop is not
|
// strange initial conditions, because the loop is not
|
||||||
@ -56,15 +56,19 @@ impl Rand for StandardNormal {
|
|||||||
y = y_.ln();
|
y = y_.ln();
|
||||||
}
|
}
|
||||||
|
|
||||||
if u < 0.0 { x - ziggurat_tables::ZIG_NORM_R } else { ziggurat_tables::ZIG_NORM_R - x }
|
if u < 0.0 {
|
||||||
|
x - ziggurat_tables::ZIG_NORM_R
|
||||||
|
} else {
|
||||||
|
ziggurat_tables::ZIG_NORM_R - x
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StandardNormal(ziggurat(
|
StandardNormal(ziggurat(rng,
|
||||||
rng,
|
true, // this is symmetric
|
||||||
true, // this is symmetric
|
&ziggurat_tables::ZIG_NORM_X,
|
||||||
&ziggurat_tables::ZIG_NORM_X,
|
&ziggurat_tables::ZIG_NORM_F,
|
||||||
&ziggurat_tables::ZIG_NORM_F,
|
pdf,
|
||||||
pdf, zero_case))
|
zero_case))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,12 +93,14 @@ impl Normal {
|
|||||||
assert!(std_dev >= 0.0, "Normal::new called with `std_dev` < 0");
|
assert!(std_dev >= 0.0, "Normal::new called with `std_dev` < 0");
|
||||||
Normal {
|
Normal {
|
||||||
mean: mean,
|
mean: mean,
|
||||||
std_dev: std_dev
|
std_dev: std_dev,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Sample<f64> for Normal {
|
impl Sample<f64> for Normal {
|
||||||
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
|
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
|
||||||
|
self.ind_sample(rng)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl IndependentSample<f64> for Normal {
|
impl IndependentSample<f64> for Normal {
|
||||||
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
|
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
|
||||||
@ -110,7 +116,7 @@ impl IndependentSample<f64> for Normal {
|
|||||||
/// std_dev**2)` distributed.
|
/// std_dev**2)` distributed.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct LogNormal {
|
pub struct LogNormal {
|
||||||
norm: Normal
|
norm: Normal,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LogNormal {
|
impl LogNormal {
|
||||||
@ -126,7 +132,9 @@ impl LogNormal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Sample<f64> for LogNormal {
|
impl Sample<f64> for LogNormal {
|
||||||
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
|
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
|
||||||
|
self.ind_sample(rng)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl IndependentSample<f64> for LogNormal {
|
impl IndependentSample<f64> for LogNormal {
|
||||||
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
|
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
|
||||||
@ -179,7 +187,7 @@ mod bench {
|
|||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
use self::test::Bencher;
|
use self::test::Bencher;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
use distributions::{Sample};
|
use distributions::Sample;
|
||||||
use super::Normal;
|
use super::Normal;
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
|
Loading…
Reference in New Issue
Block a user