Correct typos in comments (#639)

* Correct typos in comments

Correct comments in code on type of generated distribution. Improve Gaussian RNG to take advantage of Box Muller method

* Inline Box Muller

Added inline function for the Box Muller algorithm and updated code comments to be more concise

* Update tensor_fill.h

* Update tensor_fill.h

* small changes to pass tests

Co-authored-by: Haicheng Wu <haichengw@nvidia.com>
This commit is contained in:
Alexander Freudenberg 2022-10-01 04:51:30 +02:00 committed by GitHub
parent dadc881a96
commit cb539dab78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -92,6 +92,25 @@ struct TensorFillFunc {
}
};
/// Returns a pair of values of the Gaussian distribution generated by the Box Muller method
struct BoxMullerFunc {
BoxMullerFunc() {}
void operator()(
double* rnd, ///< Size-2 vector to be filled with random values
double mean = 0, ///< Mean of the Gaussian distribution
double stddev = 1, ///< Standard deviation of the Gaussian distribution
double pi = std::acos(-1)) const {
double u1 = double(std::rand()) / double(RAND_MAX);
double u2 = double(std::rand()) / double(RAND_MAX);
rnd[0] = std::sqrt(-2 * std::log(u1)) * std::cos(2 * pi * u2);
rnd[1] = std::sqrt(-2 * std::log(u1)) * std::sin(2 * pi * u2);
rnd[0] = mean + stddev * rnd[0];
rnd[1] = mean + stddev * rnd[1];
}
};
} // namespace detail
///////////////////////////////////////////////////////////////////////////////////////////////////
@ -205,22 +224,18 @@ struct RandomGaussianFunc<complex<Element> > {
Element reals[2];
for (int i = 0; i < 2; ++i) {
// Box-Muller transform to generate random numbers with Normal distribution
double u1 = double(std::rand()) / double(RAND_MAX);
double u2 = double(std::rand()) / double(RAND_MAX);
double rnd[2];
detail::BoxMullerFunc func;
func(rnd, mean, stddev, pi);
// Compute Gaussian random value
double rnd = std::sqrt(-2 * std::log(u1)) * std::cos(2 * pi * u2);
rnd = mean + stddev * rnd;
if (int_scale >= 0) {
rnd = double(int(rnd * double(1 << int_scale)));
reals[i] = from_real<Element>(rnd / double(1 << int_scale));
}
else {
reals[i] = from_real<Element>(rnd);
}
if (int_scale >= 0) {
rnd[0] = double(int(rnd[0] * double(1 << int_scale)));
rnd[1] = double(int(rnd[1] * double(1 << int_scale)));
reals[0] = from_real<Element>(rnd[0] / double(1 << int_scale));
reals[1] = from_real<Element>(rnd[1] / double(1 << int_scale));
} else {
reals[0] = from_real<Element>(rnd[0]);
reals[1] = from_real<Element>(rnd[1]);
}
return complex<Element>(reals[0], reals[1]);
@ -255,22 +270,27 @@ struct RandomGaussianFunc<Quaternion<Element> > {
Element reals[4];
for (int i = 0; i < 4; ++i) {
// Box-Muller transform to generate random numbers with Normal distribution
double u1 = double(std::rand()) / double(RAND_MAX);
double u2 = double(std::rand()) / double(RAND_MAX);
double rnd1[2];
double rnd2[2];
detail::BoxMullerFunc func;
func(rnd1, mean, stddev, pi);
func(rnd2, mean, stddev, pi);
// Compute Gaussian random value
double rnd = std::sqrt(-2 * std::log(u1)) * std::cos(2 * pi * u2);
rnd = mean + stddev * rnd;
if (int_scale >= 0) {
rnd1[0] = double(int(rnd1[0] * double(1 << int_scale)));
rnd1[1] = double(int(rnd1[1] * double(1 << int_scale)));
rnd2[0] = double(int(rnd2[0] * double(1 << int_scale)));
rnd2[1] = double(int(rnd2[1] * double(1 << int_scale)));
if (int_scale >= 0) {
rnd = double(int(rnd * double(1 << int_scale)));
reals[i] = from_real<Element>(rnd / double(1 << int_scale));
}
else {
reals[i] = from_real<Element>(rnd);
}
reals[0] = from_real<Element>(rnd1[0] / double(1 << int_scale));
reals[1] = from_real<Element>(rnd1[1] / double(1 << int_scale));
reals[2] = from_real<Element>(rnd2[0] / double(1 << int_scale));
reals[3] = from_real<Element>(rnd2[1] / double(1 << int_scale));
} else {
reals[0] = from_real<Element>(rnd1[0]);
reals[1] = from_real<Element>(rnd1[1]);
reals[2] = from_real<Element>(rnd2[0]);
reals[3] = from_real<Element>(rnd2[1]);
}
return Quaternion<Element>(reals[0], reals[1], reals[2], reals[3]);
@ -311,7 +331,7 @@ struct TensorFillGaussianFunc {
}
};
/// Computes a random Gaussian distribution
/// Computes a random Gaussian distribution for a rank-2 tensor
template <
typename Element, ///< Element type
typename Layout> ///< Layout function
@ -404,7 +424,7 @@ void TensorFillRandomGaussian(
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Fills a tensor with random values with a Gaussian distribution.
/// Fills the upper or lower part of a symmetric rank-2 tensor with random values of a Gaussian distribution.
template <
typename Element, ///< Element type
typename Layout> ///< Layout function
@ -434,7 +454,7 @@ void TensorFillSymmetricRandomGaussian(
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Fills a tensor with random values with a Gaussian distribution.
/// Fills a tensor with random values of a Gaussian distribution.
template <
typename Element ///< Element type
>
@ -614,7 +634,7 @@ struct RandomUniformFunc<Quaternion<Element> > {
}
};
/// Computes a random Gaussian distribution
/// Computes a random uniform distribution
template <
typename Element, ///< Element type
typename Layout> ///< Layout function
@ -633,7 +653,7 @@ struct TensorFillRandomUniformFunc {
// Methods
//
/// Construction of Gaussian RNG functor.
/// Construction of uniform RNG functor.
TensorFillRandomUniformFunc(
TensorView view_ = TensorView(),
RandomUniformFunc<Element> func_ = RandomUniformFunc<Element>()
@ -649,7 +669,7 @@ struct TensorFillRandomUniformFunc {
}
};
/// Computes a random Gaussian distribution
/// Fills the upper or lower part of a symmetric rank-2 tensor with random values of a uniform distribution.
template <
typename Element, ///< Element type
typename Layout> ///< Layout function
@ -669,7 +689,7 @@ struct TensorFillSymmetricRandomUniformFunc {
// Methods
//
/// Construction of Gaussian RNG functor.
/// Construction of uniform RNG functor.
TensorFillSymmetricRandomUniformFunc(
TensorView view_ = TensorView(),
RandomUniformFunc<Element> func_ = RandomUniformFunc<Element>(),
@ -715,7 +735,7 @@ struct TensorFillPadDiagonalRandomUniformFunc {
// Methods
//
/// Construction of Gaussian RNG functor.
/// Construction of uniform RNG functor.
TensorFillPadDiagonalRandomUniformFunc(
TensorView view_ = TensorView(),
RandomUniformFunc<Element> func_ = RandomUniformFunc<Element>(),
@ -747,7 +767,7 @@ struct TensorFillPadDiagonalRandomUniformFunc {
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Fills a tensor with random values with a uniform random distribution.
/// Fills a tensor with random values of a uniform random distribution.
template <
typename Element, ///< Element type
typename Layout> ///< Layout function
@ -772,7 +792,7 @@ void TensorFillRandomUniform(
);
}
/// Fills a tensor with random values with a uniform random distribution.
/// Fills a tensor with random values of a uniform random distribution.
template <
typename Element, ///< Element type
typename Layout> ///< Layout function