diff options
author | dhuard <dhuard@localhost> | 2009-03-09 16:12:57 +0000 |
---|---|---|
committer | dhuard <dhuard@localhost> | 2009-03-09 16:12:57 +0000 |
commit | 46b375c98bf489699d1de5f4d8a6910e0e1c8ad5 (patch) | |
tree | 4e5b8477db3f039b2eb4503a8d45e44d8c6dc72f /numpy | |
parent | dd8299b40e9128b18abd7df1adb462f53bb2493a (diff) | |
download | numpy-46b375c98bf489699d1de5f4d8a6910e0e1c8ad5.tar.gz |
Applied Anand's patch fixing Ticket #986 regarding the domain of the Von Mises random variable generator. Regression test added.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/random/mtrand/distributions.c | 14 | ||||
-rw-r--r-- | numpy/random/tests/test_random.py | 11 |
2 files changed, 22 insertions, 3 deletions
diff --git a/numpy/random/mtrand/distributions.c b/numpy/random/mtrand/distributions.c index 8cd508f7a..08a0d49f1 100644 --- a/numpy/random/mtrand/distributions.c +++ b/numpy/random/mtrand/distributions.c @@ -561,6 +561,7 @@ double rk_vonmises(rk_state *state, double mu, double kappa) double r, rho, s; double U, V, W, Y, Z; double result, mod; + int neg; if (kappa < 1e-8) { @@ -585,15 +586,22 @@ double rk_vonmises(rk_state *state, double mu, double kappa) } } - U = rk_double(state); + U = rk_double(state); - result = acos(W); + result = acos(W); if (U < 0.5) { result = -result; } result += mu; - mod = fmod(result, 2*M_PI); + neg = (result < 0); + mod = fabs(result); + mod = (fmod(mod+M_PI, 2*M_PI)-M_PI); + if (neg) + { + mod *= -1; + } + return mod; } } diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 8dae16fcb..9d3de27e2 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -15,6 +15,17 @@ class TestMultinomial(TestCase): assert np.all(-5 <= x) assert np.all(x < -1) +class TestVonMises(TestCase): + def test_output_domain(self): + """Make sure generated random variables are in [-pi, pi]. + + Regression test for ticket #986. + """ + for mu in np.linspace(-7., 7., 5): + r = random.mtrand.vonmises(mu,1,50) + assert np.all(r>-np.pi) and np.all(r<=np.pi) + + class TestSetState(TestCase): def setUp(self): |