diff options
| author | Raúl Montón Pinillos <raul_m_p@me.com> | 2020-09-26 23:27:23 +0200 |
|---|---|---|
| committer | Kevin Sheppard <kevin.k.sheppard@gmail.com> | 2021-02-26 11:35:53 +0000 |
| commit | f26a7d202a7136c0750356f7ec6c148d67a5927e (patch) | |
| tree | 9d8d3dd9b4bfa333ff51f1dcc27c22ac875f0d31 /numpy/random/src | |
| parent | b16dc4ad63ca02e5a3d88689fddc48159d297abd (diff) | |
| download | numpy-f26a7d202a7136c0750356f7ec6c148d67a5927e.tar.gz | |
Fixed style and added check for bounds in [-pi,pi] interval.
Diffstat (limited to 'numpy/random/src')
| -rw-r--r-- | numpy/random/src/distributions/distributions.c | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/numpy/random/src/distributions/distributions.c b/numpy/random/src/distributions/distributions.c index 6c60ad882..59e11ca47 100644 --- a/numpy/random/src/distributions/distributions.c +++ b/numpy/random/src/distributions/distributions.c @@ -844,7 +844,8 @@ double random_vonmises(bitgen_t *bitgen_state, double mu, double kappa) { } if (kappa < 1e-8) { return M_PI * (2 * next_double(bitgen_state) - 1); - } else { + } + else { /* with double precision rho is zero until 1.4e-8 */ if (kappa < 1e-5) { /* @@ -852,11 +853,21 @@ double random_vonmises(bitgen_t *bitgen_state, double mu, double kappa) { * precise until relatively large kappas as second order is 0 */ s = (1. / kappa + kappa); - } else { - /* Fallback to normal distribution for big values of kappa*/ - if (kappa > 1e6){ - return mu + sqrt(1/kappa) * random_standard_normal(bitgen_state); - }else{ + } + else { + /* Fallback to normal distribution for big values of kappa */ + if (kappa > 1e6) { + result = mu + sqrt(1. / kappa) * random_standard_normal(bitgen_state); + /* Check if result is within bounds */ + if (result < -M_PI) { + return result + 2*M_PI; + } + if (result > M_PI) { + return result - 2*M_PI; + } + return result; + } + else { double r = 1 + sqrt(1 + 4 * kappa * kappa); double rho = (r - sqrt(2 * r)) / (2 * kappa); s = (1 + rho * rho) / (2 * rho); |
