<s> from __future__ import print_function <EOL> import collections <EOL> import functools <EOL> import math <EOL> import os <EOL> import random <EOL> import subprocess <EOL> import sys <EOL> import numpy as np <EOL> import numba . unittest_support as unittest <EOL> from numba import jit , _helperlib , types <EOL> from numba . compiler import compile_isolated <EOL> from numba . targets . randomimpl import random_init <EOL> from . support import TestCase , compile_function , tag <EOL> N = <NUM_LIT> <EOL> py_state_ptr = _helperlib . c_helpers [ '<STR_LIT>' ] <EOL> np_state_ptr = _helperlib . c_helpers [ '<STR_LIT>' ] <EOL> def numpy_randint1 ( a ) : <EOL> return np . random . randint ( a ) <EOL> def numpy_randint2 ( a , b ) : <EOL> return np . random . randint ( a , b ) <EOL> def random_randint ( a , b ) : <EOL> return random . randint ( a , b ) <EOL> def random_randrange1 ( a ) : <EOL> return random . randrange ( a ) <EOL> def random_randrange2 ( a , b ) : <EOL> return random . randrange ( a , b ) <EOL> def random_randrange3 ( a , b , c ) : <EOL> return random . randrange ( a , b , c ) <EOL> def numpy_choice1 ( a ) : <EOL> return np . random . choice ( a ) <EOL> def numpy_choice2 ( a , size ) : <EOL> return np . random . choice ( a , size = size ) <EOL> def numpy_choice3 ( a , size , replace ) : <EOL> return np . random . choice ( a , size = size , replace = replace ) <EOL> def numpy_multinomial2 ( n , pvals ) : <EOL> return np . random . multinomial ( n , pvals ) <EOL> def numpy_multinomial3 ( n , pvals , size ) : <EOL> return np . random . multinomial ( n , pvals = pvals , size = size ) <EOL> def numpy_check_rand ( seed , a , b ) : <EOL> np . random . seed ( seed ) <EOL> expected = np . random . random ( ( a , b ) ) <EOL> np . random . seed ( seed ) <EOL> got = np . random . rand ( a , b ) <EOL> return expected , got <EOL> def numpy_check_randn ( seed , a , b ) : <EOL> np . random . seed ( seed ) <EOL> expected = np . random . standard_normal ( ( a , b ) ) <EOL> np . random . seed ( seed ) <EOL> got = np . random . randn ( a , b ) <EOL> return expected , got <EOL> def jit_with_args ( name , argstring ) : <EOL> code = """<STR_LIT>""" % locals ( ) <EOL> pyfunc = compile_function ( "<STR_LIT>" , code , globals ( ) ) <EOL> return jit ( nopython = True ) ( pyfunc ) <EOL> def jit_nullary ( name ) : <EOL> return jit_with_args ( name , "<STR_LIT>" ) <EOL> def jit_unary ( name ) : <EOL> return jit_with_args ( name , "<STR_LIT:a>" ) <EOL> def jit_binary ( name ) : <EOL> return jit_with_args ( name , "<STR_LIT>" ) <EOL> def jit_ternary ( name ) : <EOL> return jit_with_args ( name , "<STR_LIT>" ) <EOL> random_gauss = jit_binary ( "<STR_LIT>" ) <EOL> random_random = jit_nullary ( "<STR_LIT>" ) <EOL> random_seed = jit_unary ( "<STR_LIT>" ) <EOL> numpy_normal = jit_binary ( "<STR_LIT>" ) <EOL> numpy_random = jit_nullary ( "<STR_LIT>" ) <EOL> numpy_seed = jit_unary ( "<STR_LIT>" ) <EOL> def _copy_py_state ( r , ptr ) : <EOL> """<STR_LIT>""" <EOL> mt = r . getstate ( ) [ <NUM_LIT:1> ] <EOL> ints , index = mt [ : - <NUM_LIT:1> ] , mt [ - <NUM_LIT:1> ] <EOL> _helperlib . rnd_set_state ( ptr , ( index , list ( ints ) ) ) <EOL> return ints , index <EOL> def _copy_np_state ( r , ptr ) : <EOL> """<STR_LIT>""" <EOL> ints , index = r . get_state ( ) [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> _helperlib . rnd_set_state ( ptr , ( index , [ int ( x ) for x in ints ] ) ) <EOL> return ints , index <EOL> def sync_to_numpy ( r ) : <EOL> _ver , mt_st , _gauss_next = r . getstate ( ) <EOL> mt_pos = mt_st [ - <NUM_LIT:1> ] <EOL> mt_ints = mt_st [ : - <NUM_LIT:1> ] <EOL> assert len ( mt_ints ) == <NUM_LIT> <EOL> np_st = ( '<STR_LIT>' , np . array ( mt_ints , dtype = '<STR_LIT>' ) , mt_pos ) <EOL> if _gauss_next is None : <EOL> np_st += ( <NUM_LIT:0> , <NUM_LIT:0.0> ) <EOL> else : <EOL> np_st += ( <NUM_LIT:1> , _gauss_next ) <EOL> np . random . set_state ( np_st ) <EOL> def py_chisquare ( r , df ) : <EOL> return <NUM_LIT> * r . gammavariate ( df / <NUM_LIT> , <NUM_LIT:1.0> ) <EOL> def py_f ( r , num , denom ) : <EOL> return ( ( py_chisquare ( r , num ) * denom ) / <EOL> ( py_chisquare ( r , denom ) * num ) ) <EOL> class BaseTest ( TestCase ) : <EOL> def _follow_cpython ( self , ptr , seed = <NUM_LIT:2> ) : <EOL> r = random . Random ( seed ) <EOL> _copy_py_state ( r , ptr ) <EOL> return r <EOL> def _follow_numpy ( self , ptr , seed = <NUM_LIT:2> ) : <EOL> r = np . random . RandomState ( seed ) <EOL> _copy_np_state ( r , ptr ) <EOL> return r <EOL> class TestInternals ( BaseTest ) : <EOL> """<STR_LIT>""" <EOL> def _check_get_set_state ( self , ptr ) : <EOL> state = _helperlib . rnd_get_state ( ptr ) <EOL> i , ints = state <EOL> self . assertIsInstance ( i , int ) <EOL> self . assertIsInstance ( ints , list ) <EOL> self . assertEqual ( len ( ints ) , N ) <EOL> j = ( i * <NUM_LIT> ) % N <EOL> ints = [ i * <NUM_LIT:3> for i in range ( N ) ] <EOL> _helperlib . rnd_set_state ( ptr , ( j , ints ) ) <EOL> self . assertEqual ( _helperlib . rnd_get_state ( ptr ) , ( j , ints ) ) <EOL> def _check_shuffle ( self , ptr ) : <EOL> r = random . Random ( ) <EOL> ints , index = _copy_py_state ( r , ptr ) <EOL> for i in range ( index , N + <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> r . random ( ) <EOL> _helperlib . rnd_shuffle ( ptr ) <EOL> mt = r . getstate ( ) [ <NUM_LIT:1> ] <EOL> ints , index = mt [ : - <NUM_LIT:1> ] , mt [ - <NUM_LIT:1> ] <EOL> self . assertEqual ( _helperlib . rnd_get_state ( ptr ) [ <NUM_LIT:1> ] , list ( ints ) ) <EOL> def _check_init ( self , ptr ) : <EOL> r = np . random . RandomState ( ) <EOL> for i in [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:5> ] : <EOL> r . seed ( np . uint32 ( i ) ) <EOL> st = r . get_state ( ) <EOL> ints = list ( st [ <NUM_LIT:1> ] ) <EOL> index = st [ <NUM_LIT:2> ] <EOL> assert index == N <EOL> _helperlib . rnd_seed ( ptr , i ) <EOL> self . assertEqual ( _helperlib . rnd_get_state ( ptr ) , ( index , ints ) ) <EOL> def _check_perturb ( self , ptr ) : <EOL> states = [ ] <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> _helperlib . rnd_seed ( ptr , <NUM_LIT:0> ) <EOL> _helperlib . rnd_seed ( ptr , os . urandom ( <NUM_LIT> ) ) <EOL> states . append ( tuple ( _helperlib . rnd_get_state ( ptr ) [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( len ( set ( states ) ) , len ( states ) ) <EOL> def test_get_set_state ( self ) : <EOL> self . _check_get_set_state ( py_state_ptr ) <EOL> def test_shuffle ( self ) : <EOL> self . _check_shuffle ( py_state_ptr ) <EOL> def test_init ( self ) : <EOL> self . _check_init ( py_state_ptr ) <EOL> def test_perturb ( self ) : <EOL> self . _check_perturb ( py_state_ptr ) <EOL> class TestRandom ( BaseTest ) : <EOL> def setUp ( self ) : <EOL> random_init ( ) <EOL> def _check_random_seed ( self , seedfunc , randomfunc ) : <EOL> """<STR_LIT>""" <EOL> r = np . random . RandomState ( ) <EOL> for i in [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> ] : <EOL> r . seed ( np . uint32 ( i ) ) <EOL> seedfunc ( i ) <EOL> for j in range ( N + <NUM_LIT:10> ) : <EOL> self . assertPreciseEqual ( randomfunc ( ) , r . uniform ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_random_random ( self ) : <EOL> self . _check_random_seed ( random_seed , random_random ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_random ( self ) : <EOL> self . _check_random_seed ( numpy_seed , numpy_random ) <EOL> self . _check_random_seed ( numpy_seed , jit_nullary ( "<STR_LIT>" ) ) <EOL> self . _check_random_seed ( numpy_seed , jit_nullary ( "<STR_LIT>" ) ) <EOL> self . _check_random_seed ( numpy_seed , jit_nullary ( "<STR_LIT>" ) ) <EOL> self . _check_random_seed ( numpy_seed , jit_nullary ( "<STR_LIT>" ) ) <EOL> def test_independent_generators ( self ) : <EOL> N = <NUM_LIT:10> <EOL> random_seed ( <NUM_LIT:1> ) <EOL> py_numbers = [ random_random ( ) for i in range ( N ) ] <EOL> numpy_seed ( <NUM_LIT:2> ) <EOL> np_numbers = [ numpy_random ( ) for i in range ( N ) ] <EOL> random_seed ( <NUM_LIT:1> ) <EOL> numpy_seed ( <NUM_LIT:2> ) <EOL> pairs = [ ( random_random ( ) , numpy_random ( ) ) for i in range ( N ) ] <EOL> self . assertPreciseEqual ( [ p [ <NUM_LIT:0> ] for p in pairs ] , py_numbers ) <EOL> self . assertPreciseEqual ( [ p [ <NUM_LIT:1> ] for p in pairs ] , np_numbers ) <EOL> def _check_getrandbits ( self , func , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> for nbits in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> expected = r . getrandbits ( nbits ) <EOL> got = func ( nbits ) <EOL> self . assertPreciseEqual ( expected , got ) <EOL> self . assertRaises ( OverflowError , func , <NUM_LIT> ) <EOL> self . assertRaises ( OverflowError , func , <NUM_LIT> ) <EOL> self . assertRaises ( OverflowError , func , - <NUM_LIT:1> ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_random_getrandbits ( self ) : <EOL> self . _check_getrandbits ( jit_unary ( "<STR_LIT>" ) , py_state_ptr ) <EOL> def _check_dist ( self , func , pyfunc , argslist , niters = <NUM_LIT:3> , <EOL> prec = '<STR_LIT>' , ulps = <NUM_LIT:12> ) : <EOL> assert len ( argslist ) <EOL> for args in argslist : <EOL> results = [ func ( * args ) for i in range ( niters ) ] <EOL> pyresults = [ pyfunc ( * args ) for i in range ( niters ) ] <EOL> self . assertPreciseEqual ( results , pyresults , prec = prec , ulps = ulps , <EOL> msg = "<STR_LIT>" % ( args , ) ) <EOL> def _check_gauss ( self , func2 , func1 , func0 , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_numpy ( ptr ) <EOL> if func2 is not None : <EOL> self . _check_dist ( func2 , r . normal , <EOL> [ ( <NUM_LIT:1.0> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:0.5> ) , ( - <NUM_LIT> , <NUM_LIT:0.5> ) ] , <EOL> niters = N // <NUM_LIT:2> + <NUM_LIT:10> ) <EOL> if func1 is not None : <EOL> self . _check_dist ( func1 , r . normal , [ ( <NUM_LIT:0.5> , ) ] ) <EOL> if func0 is not None : <EOL> self . _check_dist ( func0 , r . normal , [ ( ) ] ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_random_gauss ( self ) : <EOL> self . _check_gauss ( jit_binary ( "<STR_LIT>" ) , None , None , py_state_ptr ) <EOL> def test_random_normalvariate ( self ) : <EOL> self . _check_gauss ( jit_binary ( "<STR_LIT>" ) , None , None , <EOL> py_state_ptr ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_normal ( self ) : <EOL> self . _check_gauss ( jit_binary ( "<STR_LIT>" ) , <EOL> jit_unary ( "<STR_LIT>" ) , <EOL> jit_nullary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_standard_normal ( self ) : <EOL> self . _check_gauss ( None , None , jit_nullary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_randn ( self ) : <EOL> self . _check_gauss ( None , None , jit_nullary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> def _check_lognormvariate ( self , func2 , func1 , func0 , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_numpy ( ptr ) <EOL> if func2 is not None : <EOL> self . _check_dist ( func2 , r . lognormal , <EOL> [ ( <NUM_LIT:1.0> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:0.5> ) , ( - <NUM_LIT> , <NUM_LIT:0.5> ) ] , <EOL> niters = N // <NUM_LIT:2> + <NUM_LIT:10> ) <EOL> if func1 is not None : <EOL> self . _check_dist ( func1 , r . lognormal , [ ( <NUM_LIT:0.5> , ) ] ) <EOL> if func0 is not None : <EOL> self . _check_dist ( func0 , r . lognormal , [ ( ) ] ) <EOL> def test_random_lognormvariate ( self ) : <EOL> self . _check_lognormvariate ( jit_binary ( "<STR_LIT>" ) , <EOL> None , None , py_state_ptr ) <EOL> def test_numpy_lognormal ( self ) : <EOL> self . _check_lognormvariate ( jit_binary ( "<STR_LIT>" ) , <EOL> jit_unary ( "<STR_LIT>" ) , <EOL> jit_nullary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> def _check_randrange ( self , func1 , func2 , func3 , ptr , max_width ) : <EOL> """<STR_LIT>""" <EOL> ints = [ ] <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> ints . append ( func1 ( <NUM_LIT> ) ) <EOL> ints . append ( func2 ( <NUM_LIT:5> , <NUM_LIT> ) ) <EOL> if func3 is not None : <EOL> ints . append ( func3 ( <NUM_LIT:5> , <NUM_LIT> , <NUM_LIT:3> ) ) <EOL> self . assertEqual ( len ( ints ) , len ( set ( ints ) ) , ints ) <EOL> if sys . version_info >= ( <NUM_LIT:3> , ) : <EOL> r = self . _follow_cpython ( ptr ) <EOL> widths = [ w for w in [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT> , <NUM_LIT:2> ** <NUM_LIT> + <NUM_LIT:2> ** <NUM_LIT> ] if w < max_width ] <EOL> for width in widths : <EOL> self . _check_dist ( func1 , r . randrange , [ ( width , ) ] , niters = <NUM_LIT:10> ) <EOL> self . _check_dist ( func2 , r . randrange , [ ( - <NUM_LIT:2> , <NUM_LIT:2> + width ) ] , niters = <NUM_LIT:10> ) <EOL> if func3 is not None : <EOL> self . assertPreciseEqual ( func3 ( - <NUM_LIT:2> , <NUM_LIT:2> + width , <NUM_LIT:6> ) , <EOL> r . randrange ( - <NUM_LIT:2> , <NUM_LIT:2> + width , <NUM_LIT:6> ) ) <EOL> self . assertPreciseEqual ( func3 ( <NUM_LIT:2> + width , <NUM_LIT:2> , - <NUM_LIT:3> ) , <EOL> r . randrange ( <NUM_LIT:2> + width , <NUM_LIT:2> , - <NUM_LIT:3> ) ) <EOL> self . assertRaises ( ValueError , func1 , <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , func1 , - <NUM_LIT:5> ) <EOL> self . assertRaises ( ValueError , func2 , <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> self . assertRaises ( ValueError , func2 , <NUM_LIT:5> , <NUM_LIT:2> ) <EOL> if func3 is not None : <EOL> self . assertRaises ( ValueError , func3 , <NUM_LIT:5> , <NUM_LIT:7> , - <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , func3 , <NUM_LIT:7> , <NUM_LIT:5> , <NUM_LIT:1> ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_random_randrange ( self ) : <EOL> for tp , max_width in [ ( types . int64 , <NUM_LIT:2> ** <NUM_LIT> ) , ( types . int32 , <NUM_LIT:2> ** <NUM_LIT> ) ] : <EOL> cr1 = compile_isolated ( random_randrange1 , ( tp , ) ) <EOL> cr2 = compile_isolated ( random_randrange2 , ( tp , tp ) ) <EOL> cr3 = compile_isolated ( random_randrange3 , ( tp , tp , tp ) ) <EOL> self . _check_randrange ( cr1 . entry_point , cr2 . entry_point , <EOL> cr3 . entry_point , py_state_ptr , max_width ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_randint ( self ) : <EOL> for tp , max_width in [ ( types . int64 , <NUM_LIT:2> ** <NUM_LIT> ) , ( types . int32 , <NUM_LIT:2> ** <NUM_LIT> ) ] : <EOL> cr1 = compile_isolated ( numpy_randint1 , ( tp , ) ) <EOL> cr2 = compile_isolated ( numpy_randint2 , ( tp , tp ) ) <EOL> self . _check_randrange ( cr1 . entry_point , cr2 . entry_point , <EOL> None , np_state_ptr , max_width ) <EOL> def _check_randint ( self , func , ptr , max_width ) : <EOL> """<STR_LIT>""" <EOL> ints = [ ] <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> ints . append ( func ( <NUM_LIT:5> , <NUM_LIT> ) ) <EOL> self . assertEqual ( len ( ints ) , len ( set ( ints ) ) , ints ) <EOL> if sys . version_info >= ( <NUM_LIT:3> , ) : <EOL> r = self . _follow_cpython ( ptr ) <EOL> for args in [ ( <NUM_LIT:1> , <NUM_LIT:5> ) , ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT:20> , <NUM_LIT:2> ** <NUM_LIT> + <NUM_LIT:2> ** <NUM_LIT> ) ] : <EOL> if args [ <NUM_LIT:1> ] > max_width : <EOL> continue <EOL> self . _check_dist ( func , r . randint , [ args ] , niters = <NUM_LIT:10> ) <EOL> self . assertRaises ( ValueError , func , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> self . assertRaises ( ValueError , func , <NUM_LIT:5> , <NUM_LIT:2> ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_random_randint ( self ) : <EOL> for tp , max_width in [ ( types . int64 , <NUM_LIT:2> ** <NUM_LIT> ) , ( types . int32 , <NUM_LIT:2> ** <NUM_LIT> ) ] : <EOL> cr = compile_isolated ( random_randint , ( tp , tp ) ) <EOL> self . _check_randint ( cr . entry_point , py_state_ptr , max_width ) <EOL> def _check_uniform ( self , func , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> self . _check_dist ( func , r . uniform , <EOL> [ ( <NUM_LIT> , <NUM_LIT> ) , ( - <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , - <NUM_LIT> ) ] ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_random_uniform ( self ) : <EOL> self . _check_uniform ( jit_binary ( "<STR_LIT>" ) , py_state_ptr ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_uniform ( self ) : <EOL> self . _check_uniform ( jit_binary ( "<STR_LIT>" ) , np_state_ptr ) <EOL> def _check_triangular ( self , func2 , func3 , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> if func2 is not None : <EOL> self . _check_dist ( func2 , r . triangular , <EOL> [ ( <NUM_LIT> , <NUM_LIT> ) , ( - <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> self . _check_dist ( func3 , r . triangular , [ ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> def test_random_triangular ( self ) : <EOL> self . _check_triangular ( jit_binary ( "<STR_LIT>" ) , <EOL> jit_ternary ( "<STR_LIT>" ) , <EOL> py_state_ptr ) <EOL> def test_numpy_triangular ( self ) : <EOL> triangular = jit_ternary ( "<STR_LIT>" ) <EOL> fixed_triangular = lambda l , r , m : triangular ( l , m , r ) <EOL> self . _check_triangular ( None , fixed_triangular , np_state_ptr ) <EOL> def _check_gammavariate ( self , func2 , func1 , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> if func2 is not None : <EOL> self . _check_dist ( func2 , r . gammavariate , <EOL> [ ( <NUM_LIT:0.5> , <NUM_LIT> ) , ( <NUM_LIT:1.0> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> if func1 is not None : <EOL> self . assertPreciseEqual ( func1 ( <NUM_LIT> ) , r . gammavariate ( <NUM_LIT> , <NUM_LIT:1.0> ) ) <EOL> if func2 is not None : <EOL> self . assertRaises ( ValueError , func2 , <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( ValueError , func2 , <NUM_LIT:1.0> , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , func2 , - <NUM_LIT:0.5> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( ValueError , func2 , <NUM_LIT:1.0> , - <NUM_LIT:0.5> ) <EOL> if func1 is not None : <EOL> self . assertRaises ( ValueError , func1 , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , func1 , - <NUM_LIT:0.5> ) <EOL> def test_random_gammavariate ( self ) : <EOL> self . _check_gammavariate ( jit_binary ( "<STR_LIT>" ) , None , <EOL> py_state_ptr ) <EOL> def test_numpy_gamma ( self ) : <EOL> self . _check_gammavariate ( jit_binary ( "<STR_LIT>" ) , <EOL> jit_unary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> self . _check_gammavariate ( None , <EOL> jit_unary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> def _check_betavariate ( self , func , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> self . _check_dist ( func , r . betavariate , [ ( <NUM_LIT:0.5> , <NUM_LIT> ) ] ) <EOL> self . assertRaises ( ValueError , func , <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( ValueError , func , <NUM_LIT:1.0> , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , func , - <NUM_LIT:0.5> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( ValueError , func , <NUM_LIT:1.0> , - <NUM_LIT:0.5> ) <EOL> def test_random_betavariate ( self ) : <EOL> self . _check_betavariate ( jit_binary ( "<STR_LIT>" ) , py_state_ptr ) <EOL> def test_numpy_beta ( self ) : <EOL> self . _check_betavariate ( jit_binary ( "<STR_LIT>" ) , np_state_ptr ) <EOL> def _check_vonmisesvariate ( self , func , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> self . _check_dist ( func , r . vonmisesvariate , [ ( <NUM_LIT:0.5> , <NUM_LIT> ) ] ) <EOL> def test_random_vonmisesvariate ( self ) : <EOL> self . _check_vonmisesvariate ( jit_binary ( "<STR_LIT>" ) , <EOL> py_state_ptr ) <EOL> def test_numpy_vonmises ( self ) : <EOL> self . _check_vonmisesvariate ( jit_binary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> def _check_expovariate ( self , func , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_numpy ( ptr ) <EOL> for lambd in ( <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> ) : <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> self . assertPreciseEqual ( func ( lambd ) , r . exponential ( <NUM_LIT:1> / lambd ) , <EOL> prec = '<STR_LIT>' ) <EOL> def test_random_expovariate ( self ) : <EOL> self . _check_expovariate ( jit_unary ( "<STR_LIT>" ) , py_state_ptr ) <EOL> def _check_exponential ( self , func1 , func0 , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_numpy ( ptr ) <EOL> if func1 is not None : <EOL> self . _check_dist ( func1 , r . exponential , [ ( <NUM_LIT:0.5> , ) , ( <NUM_LIT:1.0> , ) , ( <NUM_LIT> , ) ] ) <EOL> if func0 is not None : <EOL> self . _check_dist ( func0 , r . exponential , [ ( ) ] ) <EOL> def test_numpy_exponential ( self ) : <EOL> self . _check_exponential ( jit_unary ( "<STR_LIT>" ) , <EOL> jit_nullary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> def test_numpy_standard_exponential ( self ) : <EOL> self . _check_exponential ( None , <EOL> jit_nullary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> def _check_paretovariate ( self , func , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> self . _check_dist ( func , r . paretovariate , [ ( <NUM_LIT:0.5> , ) , ( <NUM_LIT> , ) ] ) <EOL> def test_random_paretovariate ( self ) : <EOL> self . _check_paretovariate ( jit_unary ( "<STR_LIT>" ) , py_state_ptr ) <EOL> def test_numpy_pareto ( self ) : <EOL> pareto = jit_unary ( "<STR_LIT>" ) <EOL> fixed_pareto = lambda a : pareto ( a ) + <NUM_LIT:1.0> <EOL> self . _check_paretovariate ( fixed_pareto , np_state_ptr ) <EOL> def _check_weibullvariate ( self , func2 , func1 , ptr ) : <EOL> """<STR_LIT>""" <EOL> r = self . _follow_cpython ( ptr ) <EOL> if func2 is not None : <EOL> self . _check_dist ( func2 , r . weibullvariate , [ ( <NUM_LIT:0.5> , <NUM_LIT> ) ] ) <EOL> if func1 is not None : <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> self . assertPreciseEqual ( func1 ( <NUM_LIT> ) , <EOL> r . weibullvariate ( <NUM_LIT:1.0> , <NUM_LIT> ) ) <EOL> def test_random_weibullvariate ( self ) : <EOL> self . _check_weibullvariate ( jit_binary ( "<STR_LIT>" ) , <EOL> None , py_state_ptr ) <EOL> def test_numpy_weibull ( self ) : <EOL> self . _check_weibullvariate ( None , jit_unary ( "<STR_LIT>" ) , <EOL> np_state_ptr ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_binomial ( self ) : <EOL> binomial = jit_binary ( "<STR_LIT>" ) <EOL> r = self . _follow_numpy ( np_state_ptr , <NUM_LIT:0> ) <EOL> self . _check_dist ( binomial , r . binomial , [ ( <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> for n in ( <NUM_LIT:100> , <NUM_LIT:1000> , <NUM_LIT> ) : <EOL> self . assertEqual ( binomial ( n , <NUM_LIT:0.0> ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( binomial ( n , <NUM_LIT:1.0> ) , n ) <EOL> for p in ( <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> r = binomial ( n , p ) <EOL> if p > <NUM_LIT:0.5> : <EOL> r = n - r <EOL> p = <NUM_LIT:1> - p <EOL> self . assertGreaterEqual ( r , <NUM_LIT:0> ) <EOL> self . assertLessEqual ( r , n ) <EOL> expected = p * n <EOL> tol = <NUM_LIT:3> * n / math . sqrt ( n ) <EOL> self . assertGreaterEqual ( r , expected - tol , ( p , n , r ) ) <EOL> self . assertLessEqual ( r , expected + tol , ( p , n , r ) ) <EOL> self . assertRaises ( ValueError , binomial , - <NUM_LIT:1> , <NUM_LIT:0.5> ) <EOL> self . assertRaises ( ValueError , binomial , <NUM_LIT:10> , - <NUM_LIT:0.1> ) <EOL> self . assertRaises ( ValueError , binomial , <NUM_LIT:10> , <NUM_LIT> ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_chisquare ( self ) : <EOL> chisquare = jit_unary ( "<STR_LIT>" ) <EOL> r = self . _follow_cpython ( np_state_ptr ) <EOL> self . _check_dist ( chisquare , <EOL> functools . partial ( py_chisquare , r ) , <EOL> [ ( <NUM_LIT> , ) , ( <NUM_LIT> , ) ] ) <EOL> def test_numpy_f ( self ) : <EOL> f = jit_binary ( "<STR_LIT>" ) <EOL> r = self . _follow_cpython ( np_state_ptr ) <EOL> self . _check_dist ( f , functools . partial ( py_f , r ) , <EOL> [ ( <NUM_LIT:0.5> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> def test_numpy_geometric ( self ) : <EOL> geom = jit_unary ( "<STR_LIT>" ) <EOL> self . assertRaises ( ValueError , geom , - <NUM_LIT:1.0> ) <EOL> self . assertRaises ( ValueError , geom , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , geom , <NUM_LIT> ) <EOL> N = <NUM_LIT:200> <EOL> r = [ geom ( <NUM_LIT:1.0> ) for i in range ( N ) ] <EOL> self . assertPreciseEqual ( r , [ <NUM_LIT:1> ] * N ) <EOL> r = [ geom ( <NUM_LIT> ) for i in range ( N ) ] <EOL> n = r . count ( <NUM_LIT:1> ) <EOL> self . assertGreaterEqual ( n , N // <NUM_LIT:2> ) <EOL> self . assertLess ( n , N ) <EOL> self . assertFalse ( [ i for i in r if i > <NUM_LIT:1000> ] ) <EOL> r = [ geom ( <NUM_LIT> ) for i in range ( N ) ] <EOL> self . assertTrue ( [ i for i in r if i > <NUM_LIT:4> ] ) <EOL> r = [ geom ( <NUM_LIT> ) for i in range ( N ) ] <EOL> self . assertTrue ( [ i for i in r if i > <NUM_LIT:50> ] ) <EOL> r = [ geom ( <NUM_LIT> ) for i in range ( N ) ] <EOL> self . assertTrue ( [ i for i in r if i > <NUM_LIT:2> ** <NUM_LIT:32> ] ) <EOL> def test_numpy_gumbel ( self ) : <EOL> gumbel = jit_binary ( "<STR_LIT>" ) <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> self . _check_dist ( gumbel , r . gumbel , [ ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) , ( - <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> def test_numpy_hypergeometric ( self ) : <EOL> hg = jit_ternary ( "<STR_LIT>" ) <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> self . _check_dist ( hg , r . hypergeometric , <EOL> [ ( <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT:10> ) , ( <NUM_LIT> , <NUM_LIT:1000> , <NUM_LIT:10> ) ] , <EOL> niters = <NUM_LIT:30> ) <EOL> r = [ hg ( <NUM_LIT:1000> , <NUM_LIT:1000> , <NUM_LIT:100> ) for i in range ( <NUM_LIT:100> ) ] <EOL> self . assertTrue ( all ( x >= <NUM_LIT:0> and x <= <NUM_LIT:100> for x in r ) , r ) <EOL> self . assertGreaterEqual ( np . mean ( r ) , <NUM_LIT> ) <EOL> self . assertLessEqual ( np . mean ( r ) , <NUM_LIT> ) <EOL> r = [ hg ( <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT:100> ) for i in range ( <NUM_LIT:100> ) ] <EOL> self . assertTrue ( all ( x >= <NUM_LIT:0> and x <= <NUM_LIT:100> for x in r ) , r ) <EOL> self . assertLessEqual ( np . mean ( r ) , <NUM_LIT> ) <EOL> r = [ hg ( <NUM_LIT> , <NUM_LIT:1000> , <NUM_LIT:100> ) for i in range ( <NUM_LIT:100> ) ] <EOL> self . assertTrue ( all ( x >= <NUM_LIT:0> and x <= <NUM_LIT:100> for x in r ) , r ) <EOL> self . assertGreaterEqual ( np . mean ( r ) , <NUM_LIT> ) <EOL> def test_numpy_laplace ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> self . _check_dist ( jit_binary ( "<STR_LIT>" ) , r . laplace , <EOL> [ ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) , ( - <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> self . _check_dist ( jit_unary ( "<STR_LIT>" ) , r . laplace , <EOL> [ ( <NUM_LIT:0.0> , ) , ( - <NUM_LIT> , ) ] ) <EOL> self . _check_dist ( jit_nullary ( "<STR_LIT>" ) , r . laplace , [ ( ) ] ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_logistic ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> self . _check_dist ( jit_binary ( "<STR_LIT>" ) , r . logistic , <EOL> [ ( <NUM_LIT:0.0> , <NUM_LIT:1.0> ) , ( - <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> self . _check_dist ( jit_unary ( "<STR_LIT>" ) , r . logistic , <EOL> [ ( <NUM_LIT:0.0> , ) , ( - <NUM_LIT> , ) ] ) <EOL> self . _check_dist ( jit_nullary ( "<STR_LIT>" ) , r . logistic , [ ( ) ] ) <EOL> def test_numpy_logseries ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> logseries = jit_unary ( "<STR_LIT>" ) <EOL> self . _check_dist ( logseries , r . logseries , <EOL> [ ( <NUM_LIT:0.1> , ) , ( <NUM_LIT> , ) , ( <NUM_LIT> , ) ] , <EOL> niters = <NUM_LIT:50> ) <EOL> r = self . _follow_numpy ( np_state_ptr , seed = <NUM_LIT:1> ) <EOL> self . assertEqual ( [ logseries ( <NUM_LIT> ) for i in range ( <NUM_LIT:10> ) ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . assertRaises ( ValueError , logseries , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , logseries , - <NUM_LIT:0.1> ) <EOL> self . assertRaises ( ValueError , logseries , <NUM_LIT> ) <EOL> def test_numpy_poisson ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> poisson = jit_unary ( "<STR_LIT>" ) <EOL> self . _check_dist ( poisson , r . poisson , <EOL> [ ( <NUM_LIT:0.0> , ) , ( <NUM_LIT:0.5> , ) , ( <NUM_LIT> , ) , ( <NUM_LIT> , ) , ( <NUM_LIT> , ) ] , <EOL> niters = <NUM_LIT:50> ) <EOL> self . assertRaises ( ValueError , poisson , - <NUM_LIT:0.1> ) <EOL> def test_numpy_negative_binomial ( self ) : <EOL> self . _follow_numpy ( np_state_ptr , <NUM_LIT:0> ) <EOL> negbin = jit_binary ( "<STR_LIT>" ) <EOL> self . assertEqual ( [ negbin ( <NUM_LIT:10> , <NUM_LIT> ) for i in range ( <NUM_LIT:10> ) ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> self . assertEqual ( [ negbin ( <NUM_LIT:10> , <NUM_LIT:0.1> ) for i in range ( <NUM_LIT:10> ) ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . assertEqual ( [ negbin ( <NUM_LIT:1000> , <NUM_LIT:0.1> ) for i in range ( <NUM_LIT:10> ) ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> m = np . mean ( [ negbin ( <NUM_LIT> , <NUM_LIT:0.1> ) <EOL> for i in range ( <NUM_LIT:50> ) ] ) <EOL> self . assertGreater ( m , <NUM_LIT> * <NUM_LIT> ) <EOL> self . assertLess ( m , <NUM_LIT> * <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , negbin , <NUM_LIT:0> , <NUM_LIT:0.5> ) <EOL> self . assertRaises ( ValueError , negbin , - <NUM_LIT:1> , <NUM_LIT:0.5> ) <EOL> self . assertRaises ( ValueError , negbin , <NUM_LIT:10> , - <NUM_LIT:0.1> ) <EOL> self . assertRaises ( ValueError , negbin , <NUM_LIT:10> , <NUM_LIT> ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_power ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> power = jit_unary ( "<STR_LIT>" ) <EOL> self . _check_dist ( power , r . power , <EOL> [ ( <NUM_LIT:0.1> , ) , ( <NUM_LIT:0.5> , ) , ( <NUM_LIT> , ) , ( <NUM_LIT> , ) ] ) <EOL> self . assertRaises ( ValueError , power , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , power , - <NUM_LIT:0.1> ) <EOL> def test_numpy_rayleigh ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> rayleigh1 = jit_unary ( "<STR_LIT>" ) <EOL> rayleigh0 = jit_nullary ( "<STR_LIT>" ) <EOL> self . _check_dist ( rayleigh1 , r . rayleigh , <EOL> [ ( <NUM_LIT:0.1> , ) , ( <NUM_LIT> , ) , ( <NUM_LIT> , ) , ( <NUM_LIT> , ) ] ) <EOL> self . _check_dist ( rayleigh0 , r . rayleigh , [ ( ) ] ) <EOL> self . assertRaises ( ValueError , rayleigh1 , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , rayleigh1 , - <NUM_LIT:0.1> ) <EOL> def test_numpy_standard_cauchy ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> cauchy = jit_nullary ( "<STR_LIT>" ) <EOL> self . _check_dist ( cauchy , r . standard_cauchy , [ ( ) ] ) <EOL> def test_numpy_standard_t ( self ) : <EOL> r = self . _follow_cpython ( np_state_ptr ) <EOL> standard_t = jit_unary ( "<STR_LIT>" ) <EOL> avg = np . mean ( [ standard_t ( <NUM_LIT:5> ) for i in range ( <NUM_LIT> ) ] ) <EOL> self . assertLess ( abs ( avg ) , <NUM_LIT:0.5> ) <EOL> def test_numpy_wald ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> wald = jit_binary ( "<STR_LIT>" ) <EOL> self . _check_dist ( wald , r . wald , [ ( <NUM_LIT:1.0> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> self . assertRaises ( ValueError , wald , <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( ValueError , wald , - <NUM_LIT:0.1> , <NUM_LIT:1.0> ) <EOL> self . assertRaises ( ValueError , wald , <NUM_LIT:1.0> , <NUM_LIT:0.0> ) <EOL> self . assertRaises ( ValueError , wald , <NUM_LIT:1.0> , - <NUM_LIT:0.1> ) <EOL> def test_numpy_zipf ( self ) : <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> zipf = jit_unary ( "<STR_LIT>" ) <EOL> self . _check_dist ( zipf , r . zipf , [ ( <NUM_LIT> , ) , ( <NUM_LIT> , ) ] , niters = <NUM_LIT:100> ) <EOL> for val in ( <NUM_LIT:1.0> , <NUM_LIT:0.5> , <NUM_LIT:0.0> , - <NUM_LIT:0.1> ) : <EOL> self . assertRaises ( ValueError , zipf , val ) <EOL> def _check_shuffle ( self , func , ptr ) : <EOL> """<STR_LIT>""" <EOL> a = np . arange ( <NUM_LIT:20> ) <EOL> if sys . version_info >= ( <NUM_LIT:3> , ) : <EOL> r = self . _follow_cpython ( ptr ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> got = a . copy ( ) <EOL> expected = a . copy ( ) <EOL> func ( got ) <EOL> r . shuffle ( expected ) <EOL> self . assertTrue ( np . all ( got == expected ) , ( got , expected ) ) <EOL> else : <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> b = a . copy ( ) <EOL> func ( b ) <EOL> self . assertFalse ( np . all ( a == b ) ) <EOL> self . assertEqual ( sorted ( a ) , sorted ( b ) ) <EOL> a = b <EOL> b = a . copy ( ) <EOL> func ( memoryview ( b ) ) <EOL> self . assertFalse ( np . all ( a == b ) ) <EOL> self . assertEqual ( sorted ( a ) , sorted ( b ) ) <EOL> with self . assertTypingError ( ) : <EOL> func ( memoryview ( b"<STR_LIT>" ) ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_random_shuffle ( self ) : <EOL> self . _check_shuffle ( jit_unary ( "<STR_LIT>" ) , py_state_ptr ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_shuffle ( self ) : <EOL> self . _check_shuffle ( jit_unary ( "<STR_LIT>" ) , np_state_ptr ) <EOL> def _check_startup_randomness ( self , func_name , func_args ) : <EOL> """<STR_LIT>""" <EOL> code = """<STR_LIT>""" % ( locals ( ) ) <EOL> numbers = set ( ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> popen = subprocess . Popen ( [ sys . executable , "<STR_LIT:-c>" , code ] , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> out , err = popen . communicate ( ) <EOL> if popen . returncode != <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % ( popen . returncode , err . decode ( ) ) ) <EOL> numbers . add ( float ( out . strip ( ) ) ) <EOL> self . assertEqual ( len ( numbers ) , <NUM_LIT:3> , numbers ) <EOL> def test_random_random_startup ( self ) : <EOL> self . _check_startup_randomness ( "<STR_LIT>" , ( ) ) <EOL> def test_random_gauss_startup ( self ) : <EOL> self . _check_startup_randomness ( "<STR_LIT>" , ( <NUM_LIT:1.0> , <NUM_LIT:1.0> ) ) <EOL> def test_numpy_random_startup ( self ) : <EOL> self . _check_startup_randomness ( "<STR_LIT>" , ( ) ) <EOL> def test_numpy_gauss_startup ( self ) : <EOL> self . _check_startup_randomness ( "<STR_LIT>" , ( <NUM_LIT:1.0> , <NUM_LIT:1.0> ) ) <EOL> class TestRandomArrays ( BaseTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> random_init ( ) <EOL> def _compile_array_dist ( self , funcname , nargs ) : <EOL> qualname = "<STR_LIT>" % ( funcname , ) <EOL> argstring = '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' [ : nargs ] ) <EOL> return jit_with_args ( qualname , argstring ) <EOL> def _check_array_dist ( self , funcname , scalar_args ) : <EOL> """<STR_LIT>""" <EOL> cfunc = self . _compile_array_dist ( funcname , len ( scalar_args ) + <NUM_LIT:1> ) <EOL> r = self . _follow_numpy ( np_state_ptr ) <EOL> pyfunc = getattr ( r , funcname ) <EOL> for size in ( <NUM_LIT:8> , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) : <EOL> args = scalar_args + ( size , ) <EOL> expected = pyfunc ( * args ) <EOL> got = cfunc ( * args ) <EOL> if ( expected . dtype == np . dtype ( '<STR_LIT>' ) <EOL> and got . dtype == np . dtype ( '<STR_LIT>' ) ) : <EOL> expected = expected . astype ( got . dtype ) <EOL> self . assertPreciseEqual ( expected , got , prec = '<STR_LIT>' , ulps = <NUM_LIT:5> ) <EOL> def test_numpy_randint ( self ) : <EOL> cfunc = self . _compile_array_dist ( "<STR_LIT>" , <NUM_LIT:3> ) <EOL> low , high = <NUM_LIT:1000> , <NUM_LIT> <EOL> size = ( <NUM_LIT:30> , <NUM_LIT:30> ) <EOL> res = cfunc ( low , high , size ) <EOL> self . assertIsInstance ( res , np . ndarray ) <EOL> self . assertEqual ( res . shape , size ) <EOL> self . assertIn ( res . dtype , ( np . dtype ( '<STR_LIT>' ) , np . dtype ( '<STR_LIT>' ) ) ) <EOL> self . assertTrue ( np . all ( res >= low ) ) <EOL> self . assertTrue ( np . all ( res < high ) ) <EOL> mean = ( low + high ) / <NUM_LIT:2> <EOL> tol = ( high - low ) / <NUM_LIT:20> <EOL> self . assertGreaterEqual ( res . mean ( ) , mean - tol ) <EOL> self . assertLessEqual ( res . mean ( ) , mean + tol ) <EOL> def test_numpy_random_random ( self ) : <EOL> cfunc = self . _compile_array_dist ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> size = ( <NUM_LIT:30> , <NUM_LIT:30> ) <EOL> res = cfunc ( size ) <EOL> self . assertIsInstance ( res , np . ndarray ) <EOL> self . assertEqual ( res . shape , size ) <EOL> self . assertEqual ( res . dtype , np . dtype ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( np . all ( res >= <NUM_LIT:0.0> ) ) <EOL> self . assertTrue ( np . all ( res < <NUM_LIT:1.0> ) ) <EOL> self . assertTrue ( np . any ( res <= <NUM_LIT:0.1> ) ) <EOL> self . assertTrue ( np . any ( res >= <NUM_LIT> ) ) <EOL> mean = res . mean ( ) <EOL> self . assertGreaterEqual ( mean , <NUM_LIT> ) <EOL> self . assertLessEqual ( mean , <NUM_LIT> ) <EOL> def test_numpy_binomial ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT:20> , <NUM_LIT:0.5> ) ) <EOL> def test_numpy_exponential ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , ) ) <EOL> def test_numpy_gumbel ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , <NUM_LIT:0.5> ) ) <EOL> def test_numpy_laplace ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , <NUM_LIT:0.5> ) ) <EOL> def test_numpy_logistic ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , <NUM_LIT:0.5> ) ) <EOL> def test_numpy_lognormal ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> def test_numpy_logseries ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , ) ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_normal ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT:0.5> , <NUM_LIT> ) ) <EOL> def test_numpy_poisson ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , ) ) <EOL> def test_numpy_power ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , ) ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_rand ( self ) : <EOL> cfunc = jit ( nopython = True ) ( numpy_check_rand ) <EOL> expected , got = cfunc ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . assertEqual ( got . shape , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> self . assertPreciseEqual ( expected , got ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_numpy_randn ( self ) : <EOL> cfunc = jit ( nopython = True ) ( numpy_check_randn ) <EOL> expected , got = cfunc ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> self . assertEqual ( got . shape , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> self . assertPreciseEqual ( expected , got ) <EOL> def test_numpy_rayleigh ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , ) ) <EOL> def test_numpy_standard_cauchy ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( ) ) <EOL> def test_numpy_standard_exponential ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( ) ) <EOL> def test_numpy_standard_normal ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( ) ) <EOL> def test_numpy_uniform ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT:0.1> , <NUM_LIT> ) ) <EOL> def test_numpy_wald ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT:0.1> , <NUM_LIT> ) ) <EOL> def test_numpy_zipf ( self ) : <EOL> self . _check_array_dist ( "<STR_LIT>" , ( <NUM_LIT> , ) ) <EOL> class TestRandomChoice ( BaseTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> random_init ( ) <EOL> def _check_results ( self , pop , res , replace = True ) : <EOL> """<STR_LIT>""" <EOL> spop = set ( pop ) <EOL> sres = set ( res ) <EOL> self . assertLessEqual ( sres , spop ) <EOL> self . assertNotEqual ( sorted ( res ) , list ( res ) ) <EOL> if replace : <EOL> self . assertLess ( len ( sres ) , len ( res ) , res ) <EOL> else : <EOL> self . assertEqual ( len ( sres ) , len ( res ) , res ) <EOL> def _check_dist ( self , pop , samples ) : <EOL> """<STR_LIT>""" <EOL> self . assertGreaterEqual ( len ( samples ) , len ( pop ) * <NUM_LIT:100> ) <EOL> expected_frequency = len ( samples ) / len ( pop ) <EOL> c = collections . Counter ( samples ) <EOL> for value in pop : <EOL> n = c [ value ] <EOL> self . assertGreaterEqual ( n , expected_frequency * <NUM_LIT:0.5> ) <EOL> self . assertLessEqual ( n , expected_frequency * <NUM_LIT> ) <EOL> def _accumulate_array_results ( self , func , nresults ) : <EOL> """<STR_LIT>""" <EOL> res = [ ] <EOL> while len ( res ) < nresults : <EOL> res += list ( func ( ) . flat ) <EOL> return res [ : nresults ] <EOL> def _check_choice_1 ( self , a , pop ) : <EOL> """<STR_LIT>""" <EOL> cfunc = jit ( nopython = True ) ( numpy_choice1 ) <EOL> n = len ( pop ) <EOL> res = [ cfunc ( a ) for i in range ( n ) ] <EOL> self . _check_results ( pop , res ) <EOL> dist = [ cfunc ( a ) for i in range ( n * <NUM_LIT:100> ) ] <EOL> self . _check_dist ( pop , dist ) <EOL> def test_choice_scalar_1 ( self ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:50> <EOL> pop = list ( range ( n ) ) <EOL> self . _check_choice_1 ( n , pop ) <EOL> def test_choice_array_1 ( self ) : <EOL> """<STR_LIT>""" <EOL> pop = np . arange ( <NUM_LIT:50> ) * <NUM_LIT:2> + <NUM_LIT:100> <EOL> self . _check_choice_1 ( pop , pop ) <EOL> def _check_array_results ( self , func , pop , replace = True ) : <EOL> """<STR_LIT>""" <EOL> n = len ( pop ) <EOL> res = list ( func ( ) . flat ) <EOL> self . _check_results ( pop , res , replace ) <EOL> dist = self . _accumulate_array_results ( func , n * <NUM_LIT:100> ) <EOL> self . _check_dist ( pop , dist ) <EOL> def _check_choice_2 ( self , a , pop ) : <EOL> """<STR_LIT>""" <EOL> cfunc = jit ( nopython = True ) ( numpy_choice2 ) <EOL> n = len ( pop ) <EOL> sizes = [ n - <NUM_LIT:10> , ( <NUM_LIT:3> , ( n - <NUM_LIT:1> ) // <NUM_LIT:3> ) , n * <NUM_LIT:10> ] <EOL> for size in sizes : <EOL> res = cfunc ( a , size ) <EOL> expected_shape = size if isinstance ( size , tuple ) else ( size , ) <EOL> self . assertEqual ( res . shape , expected_shape ) <EOL> self . _check_array_results ( lambda : cfunc ( a , size ) , pop ) <EOL> def test_choice_scalar_2 ( self ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:50> <EOL> pop = np . arange ( n ) <EOL> self . _check_choice_2 ( n , pop ) <EOL> def test_choice_array_2 ( self ) : <EOL> """<STR_LIT>""" <EOL> pop = np . arange ( <NUM_LIT:50> ) * <NUM_LIT:2> + <NUM_LIT:100> <EOL> self . _check_choice_2 ( pop , pop ) <EOL> def _check_choice_3 ( self , a , pop ) : <EOL> """<STR_LIT>""" <EOL> cfunc = jit ( nopython = True ) ( numpy_choice3 ) <EOL> n = len ( pop ) <EOL> sizes = [ n - <NUM_LIT:10> , ( <NUM_LIT:3> , ( n - <NUM_LIT:1> ) // <NUM_LIT:3> ) ] <EOL> replaces = [ True , False ] <EOL> for size in sizes : <EOL> for replace in [ True , False ] : <EOL> res = cfunc ( a , size , replace ) <EOL> expected_shape = size if isinstance ( size , tuple ) else ( size , ) <EOL> self . assertEqual ( res . shape , expected_shape ) <EOL> for size in sizes : <EOL> self . _check_array_results ( lambda : cfunc ( a , size , True ) , pop ) <EOL> for size in sizes : <EOL> self . _check_array_results ( lambda : cfunc ( a , size , False ) , pop , False ) <EOL> for size in [ n + <NUM_LIT:1> , ( <NUM_LIT:3> , n // <NUM_LIT:3> + <NUM_LIT:1> ) ] : <EOL> with self . assertRaises ( ValueError ) : <EOL> cfunc ( a , size , False ) <EOL> def test_choice_scalar_3 ( self ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:50> <EOL> pop = np . arange ( n ) <EOL> self . _check_choice_3 ( n , pop ) <EOL> def test_choice_array_3 ( self ) : <EOL> """<STR_LIT>""" <EOL> pop = np . arange ( <NUM_LIT:50> ) * <NUM_LIT:2> + <NUM_LIT:100> <EOL> self . _check_choice_3 ( pop , pop ) <EOL> class TestRandomMultinomial ( BaseTest ) : <EOL> """<STR_LIT>""" <EOL> pvals = np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ] , dtype = np . float64 ) <EOL> pvals /= pvals . sum ( ) <EOL> def setUp ( self ) : <EOL> random_init ( ) <EOL> def _check_sample ( self , n , pvals , sample ) : <EOL> """<STR_LIT>""" <EOL> self . assertIsInstance ( sample , np . ndarray ) <EOL> self . assertEqual ( sample . shape , ( len ( pvals ) , ) ) <EOL> self . assertIn ( sample . dtype , ( np . dtype ( '<STR_LIT>' ) , np . dtype ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( sample . sum ( ) , n ) <EOL> for p , nexp in zip ( pvals , sample ) : <EOL> self . assertGreaterEqual ( nexp , <NUM_LIT:0> ) <EOL> self . assertLessEqual ( nexp , n ) <EOL> pexp = float ( nexp ) / n <EOL> self . assertGreaterEqual ( pexp , p * <NUM_LIT:0.5> ) <EOL> self . assertLessEqual ( pexp , p * <NUM_LIT> ) <EOL> def test_multinomial_2 ( self ) : <EOL> """<STR_LIT>""" <EOL> cfunc = jit ( nopython = True ) ( numpy_multinomial2 ) <EOL> n , pvals = <NUM_LIT:1000> , self . pvals <EOL> res = cfunc ( n , pvals ) <EOL> self . _check_sample ( n , pvals , res ) <EOL> pvals = list ( pvals ) <EOL> res = cfunc ( n , pvals ) <EOL> self . _check_sample ( n , pvals , res ) <EOL> n = <NUM_LIT> <EOL> pvals = np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , n // <NUM_LIT:100> , <NUM_LIT:1> ] , dtype = np . float64 ) <EOL> pvals /= pvals . sum ( ) <EOL> res = cfunc ( n , pvals ) <EOL> self . _check_sample ( n , pvals , res ) <EOL> def test_multinomial_3_int ( self ) : <EOL> """<STR_LIT>""" <EOL> cfunc = jit ( nopython = True ) ( numpy_multinomial3 ) <EOL> n , pvals = <NUM_LIT:1000> , self . pvals <EOL> k = <NUM_LIT:10> <EOL> res = cfunc ( n , pvals , k ) <EOL> self . assertEqual ( res . shape [ <NUM_LIT:0> ] , k ) <EOL> for sample in res : <EOL> self . _check_sample ( n , pvals , sample ) <EOL> def test_multinomial_3_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> cfunc = jit ( nopython = True ) ( numpy_multinomial3 ) <EOL> n , pvals = <NUM_LIT:1000> , self . pvals <EOL> k = ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> res = cfunc ( n , pvals , k ) <EOL> self . assertEqual ( res . shape [ : - <NUM_LIT:1> ] , k ) <EOL> for sample in res . reshape ( ( - <NUM_LIT:1> , res . shape [ - <NUM_LIT:1> ] ) ) : <EOL> self . _check_sample ( n , pvals , sample ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
