summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorpanpiort8 <dociebieaniuszlem@gmail.com>2020-04-19 03:51:20 +0200
committerGitHub <noreply@github.com>2020-04-18 20:51:20 -0500
commit1d598997543637b9882d1663117b573210a20583 (patch)
treeaba59e702d7c5d0e7a1e849da94921b73f21eb70 /numpy
parent11c1e161be588da14a501f54d8a3974bee7a03f3 (diff)
downloadnumpy-1d598997543637b9882d1663117b573210a20583.tar.gz
BUG: Alpha parameter must be 1D in `generator.dirichlet` (#15951)
Only one dimensional alpha paramter is currently supported, but higher dimensions were silently allowed and gave an incorrect results. This fixes the regression. In the future, the API could be extended to allow higher dimensional arrays for alpha. Fixes gh-15915
Diffstat (limited to 'numpy')
-rw-r--r--numpy/random/_generator.pyx19
-rw-r--r--numpy/random/mtrand.pyx19
-rw-r--r--numpy/random/tests/test_generator_mt19937.py6
-rw-r--r--numpy/random/tests/test_random.py6
4 files changed, 32 insertions, 18 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx
index dc0ec83f5..b976d51c6 100644
--- a/numpy/random/_generator.pyx
+++ b/numpy/random/_generator.pyx
@@ -3923,23 +3923,23 @@ cdef class Generator:
Parameters
----------
- alpha : array
- Parameter of the distribution (k dimension for sample of
- dimension k).
+ alpha : sequence of floats, length k
+ Parameter of the distribution (length ``k`` for sample of
+ length ``k``).
size : int or tuple of ints, optional
- Output shape. If the given shape is, e.g., ``(m, n, k)``, then
+ Output shape. If the given shape is, e.g., ``(m, n)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
- single value is returned.
+ vector of length ``k`` is returned.
Returns
-------
samples : ndarray,
- The drawn samples, of shape (size, alpha.ndim).
+ The drawn samples, of shape ``(size, k)``.
Raises
-------
ValueError
- If any value in alpha is less than or equal to zero
+ If any value in ``alpha`` is less than or equal to zero
Notes
-----
@@ -4013,8 +4013,9 @@ cdef class Generator:
cdef double acc, invacc
k = len(alpha)
- alpha_arr = <np.ndarray>np.PyArray_FROM_OTF(
- alpha, np.NPY_DOUBLE, np.NPY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS)
+ alpha_arr = <np.ndarray>np.PyArray_FROMANY(
+ alpha, np.NPY_DOUBLE, 1, 1,
+ np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS)
if np.any(np.less_equal(alpha_arr, 0)):
raise ValueError('alpha <= 0')
alpha_data = <double*>np.PyArray_DATA(alpha_arr)
diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx
index ec1fa352b..540e19ec6 100644
--- a/numpy/random/mtrand.pyx
+++ b/numpy/random/mtrand.pyx
@@ -4246,23 +4246,23 @@ cdef class RandomState:
Parameters
----------
- alpha : array
- Parameter of the distribution (k dimension for sample of
- dimension k).
+ alpha : sequence of floats, length k
+ Parameter of the distribution (length ``k`` for sample of
+ length ``k``).
size : int or tuple of ints, optional
- Output shape. If the given shape is, e.g., ``(m, n, k)``, then
+ Output shape. If the given shape is, e.g., ``(m, n)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
- single value is returned.
+ vector of length ``k`` is returned.
Returns
-------
samples : ndarray,
- The drawn samples, of shape (size, alpha.ndim).
+ The drawn samples, of shape ``(size, k)``.
Raises
-------
ValueError
- If any value in alpha is less than or equal to zero
+ If any value in ``alpha`` is less than or equal to zero
See Also
--------
@@ -4340,8 +4340,9 @@ cdef class RandomState:
cdef double acc, invacc
k = len(alpha)
- alpha_arr = <np.ndarray>np.PyArray_FROM_OTF(
- alpha, np.NPY_DOUBLE, np.NPY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS)
+ alpha_arr = <np.ndarray>np.PyArray_FROMANY(
+ alpha, np.NPY_DOUBLE, 1, 1,
+ np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS)
if np.any(np.less_equal(alpha_arr, 0)):
raise ValueError('alpha <= 0')
alpha_data = <double*>np.PyArray_DATA(alpha_arr)
diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py
index ce90ccdc5..3e820d9d7 100644
--- a/numpy/random/tests/test_generator_mt19937.py
+++ b/numpy/random/tests/test_generator_mt19937.py
@@ -1049,6 +1049,12 @@ class TestRandomDist:
alpha = np.array([5.4e-01, -1.0e-16])
assert_raises(ValueError, random.dirichlet, alpha)
+ # gh-15876
+ assert_raises(ValueError, random.dirichlet, [[5, 1]])
+ assert_raises(ValueError, random.dirichlet, [[5], [1]])
+ assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]])
+ assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]]))
+
def test_dirichlet_alpha_non_contiguous(self):
a = np.array([51.72840233779265162, -1.0, 39.74494232180943953])
alpha = a[::2]
diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py
index c5f79d2c1..276b5bc81 100644
--- a/numpy/random/tests/test_random.py
+++ b/numpy/random/tests/test_random.py
@@ -564,6 +564,12 @@ class TestRandomDist:
alpha = np.array([5.4e-01, -1.0e-16])
assert_raises(ValueError, np.random.mtrand.dirichlet, alpha)
+ # gh-15876
+ assert_raises(ValueError, random.dirichlet, [[5, 1]])
+ assert_raises(ValueError, random.dirichlet, [[5], [1]])
+ assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]])
+ assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]]))
+
def test_exponential(self):
np.random.seed(self.seed)
actual = np.random.exponential(1.1234, size=(3, 2))