GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #24338 from charris/backport-24220 · numpy/numpy@baf6250

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • numpy/random
    • _generator.pyx
    • tests
      • test_generator_mt19937.py
      • test_randomstate.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎numpy/random/_generator.pyx‎

Copy file name to clipboardExpand all lines: numpy/random/_generator.pyx
+18-11Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4408,6 +4408,7 @@ cdef class Generator:
44084408
np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS)
44094409
if np.any(np.less(alpha_arr, 0)):
44104410
raise ValueError('alpha < 0')
4411+
44114412
alpha_data = <double*>np.PyArray_DATA(alpha_arr)
44124413

44134414
if size is None:
@@ -4467,17 +4468,23 @@ cdef class Generator:
44674468
csum += alpha_data[j]
44684469
alpha_csum_data[j] = csum
44694470

4470-
with self.lock, nogil:
4471-
while i < totsize:
4472-
acc = 1.
4473-
for j in range(k - 1):
4474-
v = random_beta(&self._bitgen, alpha_data[j],
4475-
alpha_csum_data[j + 1])
4476-
val_data[i + j] = acc * v
4477-
acc *= (1. - v)
4478-
val_data[i + k - 1] = acc
4479-
i = i + k
4480-
4471+
# If csum == 0, then all the values in alpha are 0, and there is
4472+
# nothing to do, because diric was created with np.zeros().
4473+
if csum > 0:
4474+
with self.lock, nogil:
4475+
while i < totsize:
4476+
acc = 1.
4477+
for j in range(k - 1):
4478+
v = random_beta(&self._bitgen, alpha_data[j],
4479+
alpha_csum_data[j + 1])
4480+
val_data[i + j] = acc * v
4481+
acc *= (1. - v)
4482+
if alpha_csum_data[j + 1] == 0:
4483+
# v must be 1, so acc is now 0. All
4484+
# remaining elements will be left at 0.
4485+
break
4486+
val_data[i + k - 1] = acc
4487+
i = i + k
44814488
else:
44824489
# Standard case: Unit normalisation of a vector of gamma random
44834490
# variates
Collapse file

‎numpy/random/tests/test_generator_mt19937.py‎

Copy file name to clipboardExpand all lines: numpy/random/tests/test_generator_mt19937.py
+22-2Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
]
3737

38+
3839
@pytest.fixture(scope='module', params=[True, False])
3940
def endpoint(request):
4041
return request.param
@@ -145,6 +146,7 @@ def test_multinomial_pvals_float32(self):
145146
with pytest.raises(ValueError, match=match):
146147
random.multinomial(1, pvals)
147148

149+
148150
class TestMultivariateHypergeometric:
149151

150152
def setup_method(self):
@@ -1238,6 +1240,25 @@ def test_dirichlet_moderately_small_alpha(self):
12381240
sample_mean = sample.mean(axis=0)
12391241
assert_allclose(sample_mean, exact_mean, rtol=1e-3)
12401242

1243+
# This set of parameters includes inputs with alpha.max() >= 0.1 and
1244+
# alpha.max() < 0.1 to exercise both generation methods within the
1245+
# dirichlet code.
1246+
@pytest.mark.parametrize(
1247+
'alpha',
1248+
[[5, 9, 0, 8],
1249+
[0.5, 0, 0, 0],
1250+
[1, 5, 0, 0, 1.5, 0, 0, 0],
1251+
[0.01, 0.03, 0, 0.005],
1252+
[1e-5, 0, 0, 0],
1253+
[0.002, 0.015, 0, 0, 0.04, 0, 0, 0],
1254+
[0.0],
1255+
[0, 0, 0]],
1256+
)
1257+
def test_dirichlet_multiple_zeros_in_alpha(self, alpha):
1258+
alpha = np.array(alpha)
1259+
y = random.dirichlet(alpha)
1260+
assert_equal(y[alpha == 0], 0.0)
1261+
12411262
def test_exponential(self):
12421263
random = Generator(MT19937(self.seed))
12431264
actual = random.exponential(1.1234, size=(3, 2))
@@ -1467,7 +1488,7 @@ def test_multivariate_normal(self, method):
14671488
mu, np.empty((3, 2)))
14681489
assert_raises(ValueError, random.multivariate_normal,
14691490
mu, np.eye(3))
1470-
1491+
14711492
@pytest.mark.parametrize('mean, cov', [([0], [[1+1j]]), ([0j], [[1]])])
14721493
def test_multivariate_normal_disallow_complex(self, mean, cov):
14731494
random = Generator(MT19937(self.seed))
@@ -1847,7 +1868,6 @@ class TestBroadcast:
18471868
def setup_method(self):
18481869
self.seed = 123456789
18491870

1850-
18511871
def test_uniform(self):
18521872
random = Generator(MT19937(self.seed))
18531873
low = [0]
Collapse file

‎numpy/random/tests/test_randomstate.py‎

Copy file name to clipboardExpand all lines: numpy/random/tests/test_randomstate.py
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,10 +812,6 @@ def test_dirichlet_bad_alpha(self):
812812
alpha = np.array([5.4e-01, -1.0e-16])
813813
assert_raises(ValueError, random.dirichlet, alpha)
814814

815-
def test_dirichlet_zero_alpha(self):
816-
y = random.default_rng().dirichlet([5, 9, 0, 8])
817-
assert_equal(y[2], 0)
818-
819815
def test_dirichlet_alpha_non_contiguous(self):
820816
a = np.array([51.72840233779265162, -1.0, 39.74494232180943953])
821817
alpha = a[::2]
@@ -2061,6 +2057,7 @@ def test_randomstate_ctor_old_style_pickle():
20612057
assert_equal(state_a['has_gauss'], state_b['has_gauss'])
20622058
assert_equal(state_a['gauss'], state_b['gauss'])
20632059

2060+
20642061
def test_hot_swap(restore_singleton_bitgen):
20652062
# GH 21808
20662063
def_bg = np.random.default_rng(0)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.