<s> import __builtin__ <EOL> import numpy as np <EOL> from . . frontend . decorators import jit , macro , typed_macro <EOL> from . . ndtypes import ( make_array_type , combine_type_list , repeat_tuple , <EOL> ArrayT , TupleT , Int64 , ScalarT ) <EOL> from . . syntax import ( Range , Cast , AllocArray , TupleProj , Array , <EOL> ConstArray , ConstArrayLike , Index , Shape ) <EOL> from . . syntax . helpers import get_types , one_i64 , zero_i64 , make_tuple <EOL> from adverbs import imap <EOL> from numpy_types import float64 <EOL> from lib_helpers import _get_type , _get_shape , _get_tuple_elts <EOL> @ macro <EOL> def arange ( n , * xs , ** kwds ) : <EOL> if '<STR_LIT>' in kwds : <EOL> elt_t = _get_type ( kwds [ '<STR_LIT>' ] ) <EOL> del kwds [ '<STR_LIT>' ] <EOL> else : <EOL> elt_t = Int64 <EOL> assert len ( kwds ) == <NUM_LIT:0> , "<STR_LIT>" % kwds <EOL> array_t = make_array_type ( elt_t , <NUM_LIT:1> ) <EOL> count = __builtin__ . len ( xs ) <EOL> assert <NUM_LIT:0> <= count <= <NUM_LIT:2> , "<STR_LIT>" % ( ( n , ) + tuple ( xs ) ) <EOL> if count == <NUM_LIT:0> : <EOL> start = zero_i64 <EOL> stop = n <EOL> step = one_i64 <EOL> elif count == <NUM_LIT:1> : <EOL> start = n <EOL> stop = xs [ <NUM_LIT:0> ] <EOL> step = one_i64 <EOL> else : <EOL> start = n <EOL> stop = xs [ <NUM_LIT:0> ] <EOL> step = xs [ <NUM_LIT:1> ] <EOL> if elt_t != Int64 : <EOL> start = Cast ( start , type = elt_t ) <EOL> stop = Cast ( stop , type = elt_t ) <EOL> step = Cast ( step , type = elt_t ) <EOL> return Range ( start , stop , step , type = array_t ) <EOL> @ typed_macro <EOL> def empty ( shape , dtype = float64 ) : <EOL> elt_t = _get_type ( dtype ) <EOL> assert isinstance ( elt_t , ScalarT ) , "<STR_LIT>" % ( elt_t , ) <EOL> shape = _get_shape ( shape ) <EOL> rank = len ( shape . type . elt_types ) <EOL> arr_t = make_array_type ( elt_t , rank ) <EOL> return AllocArray ( shape = shape , elt_type = elt_t , type = arr_t ) <EOL> @ jit <EOL> def empty_like ( x , dtype = None ) : <EOL> if dtype is None : <EOL> return empty ( x . shape , x . dtype ) <EOL> else : <EOL> return empty ( x . shape , dtype ) <EOL> @ typed_macro <EOL> def zeros ( shape , dtype = float64 ) : <EOL> shape = _get_shape ( shape ) <EOL> elt_type = _get_type ( dtype ) <EOL> zero = Cast ( zero_i64 , type = elt_type ) <EOL> ndims = len ( shape . type . elt_types ) <EOL> if ndims == <NUM_LIT:0> : <EOL> return zero <EOL> else : <EOL> t = make_array_type ( elt_type , ndims ) <EOL> return ConstArray ( shape = shape , value = zero , type = t ) <EOL> @ jit <EOL> def zeros_like ( x , dtype = None ) : <EOL> if dtype is None : <EOL> dtype = x . dtype <EOL> return zeros ( x . shape , dtype ) <EOL> @ typed_macro <EOL> def ones ( shape , dtype = float64 ) : <EOL> shape = _get_shape ( shape ) <EOL> elt_type = _get_type ( dtype ) <EOL> one = Cast ( one_i64 , type = elt_type ) <EOL> ndims = len ( shape . type . elt_types ) <EOL> if ndims == <NUM_LIT:0> : <EOL> return one <EOL> else : <EOL> t = make_array_type ( elt_type , ndims ) <EOL> return ConstArray ( shape = shape , value = one , type = t ) <EOL> @ jit <EOL> def ones_like ( x , dtype = None ) : <EOL> if dtype is None : <EOL> dtype = x . dtype <EOL> return ones ( x . shape , dtype ) <EOL> @ jit <EOL> def copy ( x ) : <EOL> return [ xi for xi in x ] <EOL> @ jit <EOL> def linspace ( start , stop , num = <NUM_LIT:50> , endpoint = True ) : <EOL> """<STR_LIT>""" <EOL> num = int ( num ) <EOL> if num <= <NUM_LIT:0> : <EOL> return np . array ( [ ] ) <EOL> elif endpoint : <EOL> if num == <NUM_LIT:1> : <EOL> return np . array ( [ float ( start ) ] ) <EOL> step = ( stop - start ) / float ( ( num - <NUM_LIT:1> ) ) <EOL> y = np . arange ( <NUM_LIT:0> , num ) * step + start <EOL> y [ - <NUM_LIT:1> ] = stop <EOL> return y <EOL> else : <EOL> step = ( stop - start ) / float ( num ) <EOL> return np . arange ( <NUM_LIT:0> , num ) * step + start <EOL> @ typed_macro <EOL> def array ( value , dtype = None ) : <EOL> if dtype is not None : <EOL> expected_elt_type = _get_type ( dtype ) <EOL> print "<STR_LIT>" % expected_elt_type <EOL> if isinstance ( value . type , ArrayT ) : <EOL> return value <EOL> else : <EOL> assert isinstance ( value . type , TupleT ) , "<STR_LIT>" % ( value , value . type ) <EOL> elt_types = value . type . elt_types <EOL> assert all ( isinstance ( t , ScalarT ) for t in elt_types ) , "<STR_LIT>" % ( value , value . type ) <EOL> elt_t = combine_type_list ( value . type . elt_types ) <EOL> array_elts = _get_tuple_elts ( value , cast_type = elt_t ) <EOL> array_t = make_array_type ( elt_t , <NUM_LIT:1> ) <EOL> return Array ( elts = array_elts , type = array_t ) <EOL> @ typed_macro <EOL> def tile ( A , reps ) : <EOL> reps = _get_shape ( reps ) <EOL> reps_dims = len ( reps . type . elt_types ) <EOL> if reps_dims == <NUM_LIT:0> : <EOL> return A <EOL> A_rank = A . type . rank if isinstance ( A . type , ArrayT ) else <NUM_LIT:0> <EOL> if A_rank == <NUM_LIT:0> : <EOL> if isinstance ( A . type , ArrayT ) : <EOL> A = Index ( A , make_tuple ( ( ) ) , type = A . elt_type ) <EOL> assert isinstance ( A . type , ScalarT ) , "<STR_LIT>" <EOL> array_t = make_array_type ( A . type , reps_dims ) <EOL> return ConstArray ( value = A , shape = reps , type = array_t ) <EOL> else : <EOL> A_shape = Shape ( A , type = repeat_tuple ( Int64 , A_rank ) ) <EOL> A_shape_elts = _get_tuple_elts ( A_shape ) <EOL> reps_elts = _get_tuple_elts ( reps ) <EOL> result_shape_elts = [ ] <EOL> assert False , "<STR_LIT>" </s>
