@@ -17,7 +17,6 @@ from libc.stdint cimport (uint8_t, uint16_t, uint32_t, uint64_t,
17
17
from ._bounded_integers cimport (_rand_bool , _rand_int32 , _rand_int64 ,
18
18
_rand_int16 , _rand_int8 , _rand_uint64 , _rand_uint32 , _rand_uint16 ,
19
19
_rand_uint8 , _gen_mask )
20
- from ._bounded_integers import _integers_types
21
20
from ._pcg64 import PCG64
22
21
from numpy .random cimport bitgen_t
23
22
from ._common cimport (POISSON_LAM_MAX , CONS_POSITIVE , CONS_NONE ,
@@ -312,13 +311,13 @@ cdef class Generator:
312
311
313
312
"""
314
313
cdef double temp
315
- key = np .dtype (dtype ). name
316
- if key == ' float64' :
314
+ _dtype = np .dtype (dtype )
315
+ if _dtype == np . float64 :
317
316
return double_fill (& random_standard_uniform_fill , & self ._bitgen , size , self .lock , out )
318
- elif key == ' float32' :
317
+ elif _dtype == np . float32 :
319
318
return float_fill (& random_standard_uniform_fill_f , & self ._bitgen , size , self .lock , out )
320
319
else :
321
- raise TypeError ('Unsupported dtype "%s" for random' % key )
320
+ raise TypeError ('Unsupported dtype %r for random' % _dtype )
322
321
323
322
def beta (self , a , b , size = None ):
324
323
"""
@@ -454,20 +453,20 @@ cdef class Generator:
454
453
>>> n = np.random.default_rng().standard_exponential((3, 8000))
455
454
456
455
"""
457
- key = np .dtype (dtype ). name
458
- if key == ' float64' :
456
+ _dtype = np .dtype (dtype )
457
+ if _dtype == np . float64 :
459
458
if method == u'zig' :
460
459
return double_fill (& random_standard_exponential_fill , & self ._bitgen , size , self .lock , out )
461
460
else :
462
461
return double_fill (& random_standard_exponential_inv_fill , & self ._bitgen , size , self .lock , out )
463
- elif key == ' float32' :
462
+ elif _dtype == np . float32 :
464
463
if method == u'zig' :
465
464
return float_fill (& random_standard_exponential_fill_f , & self ._bitgen , size , self .lock , out )
466
465
else :
467
466
return float_fill (& random_standard_exponential_inv_fill_f , & self ._bitgen , size , self .lock , out )
468
467
else :
469
- raise TypeError ('Unsupported dtype "%s" for standard_exponential'
470
- % key )
468
+ raise TypeError ('Unsupported dtype %r for standard_exponential'
469
+ % _dtype )
471
470
472
471
def integers (self , low , high = None , size = None , dtype = np .int64 , endpoint = False ):
473
472
"""
@@ -559,39 +558,39 @@ cdef class Generator:
559
558
high = low
560
559
low = 0
561
560
562
- dt = np .dtype (dtype )
563
- key = dt .name
564
- if key not in _integers_types :
565
- raise TypeError ('Unsupported dtype "%s" for integers' % key )
566
- if not dt .isnative :
567
- raise ValueError ('Providing a dtype with a non-native byteorder '
568
- 'is not supported. If you require '
569
- 'platform-independent byteorder, call byteswap '
570
- 'when required.' )
561
+ _dtype = np .dtype (dtype )
571
562
572
563
# Implementation detail: the old API used a masked method to generate
573
564
# bounded uniform integers. Lemire's method is preferable since it is
574
565
# faster. randomgen allows a choice, we will always use the faster one.
575
566
cdef bint _masked = False
576
567
577
- if key == ' int32' :
568
+ if _dtype == np . int32 :
578
569
ret = _rand_int32 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
579
- elif key == ' int64' :
570
+ elif _dtype == np . int64 :
580
571
ret = _rand_int64 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
581
- elif key == ' int16' :
572
+ elif _dtype == np . int16 :
582
573
ret = _rand_int16 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
583
- elif key == ' int8' :
574
+ elif _dtype == np . int8 :
584
575
ret = _rand_int8 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
585
- elif key == ' uint64' :
576
+ elif _dtype == np . uint64 :
586
577
ret = _rand_uint64 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
587
- elif key == ' uint32' :
578
+ elif _dtype == np . uint32 :
588
579
ret = _rand_uint32 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
589
- elif key == ' uint16' :
580
+ elif _dtype == np . uint16 :
590
581
ret = _rand_uint16 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
591
- elif key == ' uint8' :
582
+ elif _dtype == np . uint8 :
592
583
ret = _rand_uint8 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
593
- elif key == 'bool' :
584
+ elif _dtype == np . bool_ :
594
585
ret = _rand_bool (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
586
+ elif not _dtype .isnative :
587
+ raise ValueError ('Providing a dtype with a non-native byteorder '
588
+ 'is not supported. If you require '
589
+ 'platform-independent byteorder, call byteswap '
590
+ 'when required.' )
591
+ else :
592
+ raise TypeError ('Unsupported dtype %r for integers' % _dtype )
593
+
595
594
596
595
if size is None and dtype in (bool , int , np .compat .long ):
597
596
if np .array (ret ).shape == ():
@@ -1038,14 +1037,14 @@ cdef class Generator:
1038
1037
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random
1039
1038
1040
1039
"""
1041
- key = np .dtype (dtype ). name
1042
- if key == ' float64' :
1040
+ _dtype = np .dtype (dtype )
1041
+ if _dtype == np . float64 :
1043
1042
return double_fill (& random_standard_normal_fill , & self ._bitgen , size , self .lock , out )
1044
- elif key == ' float32' :
1043
+ elif _dtype == np . float32 :
1045
1044
return float_fill (& random_standard_normal_fill_f , & self ._bitgen , size , self .lock , out )
1046
1045
1047
1046
else :
1048
- raise TypeError ('Unsupported dtype "%s" for standard_normal' % key )
1047
+ raise TypeError ('Unsupported dtype %r for standard_normal' % _dtype )
1049
1048
1050
1049
def normal (self , loc = 0.0 , scale = 1.0 , size = None ):
1051
1050
"""
@@ -1227,19 +1226,19 @@ cdef class Generator:
1227
1226
1228
1227
"""
1229
1228
cdef void * func
1230
- key = np .dtype (dtype ). name
1231
- if key == ' float64' :
1229
+ _dtype = np .dtype (dtype )
1230
+ if _dtype == np . float64 :
1232
1231
return cont (& random_standard_gamma , & self ._bitgen , size , self .lock , 1 ,
1233
1232
shape , 'shape' , CONS_NON_NEGATIVE ,
1234
1233
0.0 , '' , CONS_NONE ,
1235
1234
0.0 , '' , CONS_NONE ,
1236
1235
out )
1237
- if key == ' float32' :
1236
+ if _dtype == np . float32 :
1238
1237
return cont_f (& random_standard_gamma_f , & self ._bitgen , size , self .lock ,
1239
1238
shape , 'shape' , CONS_NON_NEGATIVE ,
1240
1239
out )
1241
1240
else :
1242
- raise TypeError ('Unsupported dtype "%s" for standard_gamma' % key )
1241
+ raise TypeError ('Unsupported dtype %r for standard_gamma' % _dtype )
1243
1242
1244
1243
def gamma (self , shape , scale = 1.0 , size = None ):
1245
1244
"""
0 commit comments