#NEWLINE# def _infer_ndim_bcast( ndim , shape , * args ) : #NEWLINE# #INDENT# '\n    Infer the number of dimensions from the shape or the other arguments.\n\n    Returns\n    -------\n    (int, variable, tuple) triple, where the variable is an integer vector,\n    and the tuple contains Booleans\n        The first element returned is the inferred number of dimensions.\n        The second element is the shape inferred (combining symbolic and\n        constant informations from shape and args).\n        The third element is a broadcasting pattern corresponding to that shape.\n\n    ' #NEWLINE# if args : #NEWLINE# #INDENT# args_ndim = max ( ( arg . ndim for arg in args ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# args_ndim = 0 #NEWLINE# #UNINDENT# if isinstance ( shape , ( tuple , list ) ) : #NEWLINE# #INDENT# shape_ndim = len ( shape ) #NEWLINE# if ( pre_v_shape is None ) : #NEWLINE# #INDENT# ndim = shape_ndim #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( shape_ndim != ndim ) : #NEWLINE# #INDENT# raise ValueError ( 'ndim should be equal to len(shape), but\n' , ( 'ndim = %s, len(shape) = %s, shape = %s' % ( ndim , shape_ndim , shape ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# bcast = [ ] #NEWLINE# pre_v_shape = [ ] #NEWLINE# for ( i , s ) in enumerate ( shape ) : #NEWLINE# #INDENT# if hasattr ( s , 'type' ) : #NEWLINE# #INDENT# bcast . append ( False ) #NEWLINE# pre_v_shape . append ( s ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( s >= 0 ) : #NEWLINE# #INDENT# pre_v_shape . append ( tensor . as_tensor_variable ( s ) ) #NEWLINE# bcast . append ( ( s == 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( s == ( - 1 ) ) : #NEWLINE# #INDENT# n_a_i = 0 #NEWLINE# for a in args : #NEWLINE# #INDENT# if ( i >= ( ndim - a . ndim ) ) : #NEWLINE# #INDENT# n_a_i += 1 #NEWLINE# a_i = ( ( i + a . ndim ) - ndim ) #NEWLINE# if ( not a . broadcastable [ a_i ] ) : #NEWLINE# #INDENT# pre_v_shape . append ( a . shape [ a_i ] ) #NEWLINE# bcast . append ( False ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( n_a_i == 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Auto-shape of -1 must overlapwith the shape of one of the broadcastableinputs' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pre_v_shape . append ( tensor . as_tensor_variable ( 1 ) ) #NEWLINE# bcast . append ( True ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# ValueError ( 'negative shape' , s ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( len ( pre_v_shape ) == 0 ) : #NEWLINE# #INDENT# v_shape = tensor . constant ( [ ] , dtype = 'int64' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# v_shape = tensor . stack ( pre_v_shape ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( shape is None ) : #NEWLINE# #INDENT# if ( not args ) : #NEWLINE# #INDENT# raise TypeError ( '_infer_ndim_bcast cannot infer shape without either shape or args' ) #NEWLINE# #UNINDENT# template = reduce ( ( lambda a , b : ( a + b ) ) , args ) #NEWLINE# v_shape = template . shape #NEWLINE# bcast = template . broadcastable #NEWLINE# ndim = template . ndim #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# v_shape = tensor . as_tensor_variable ( shape ) #NEWLINE# if ( v_shape . ndim != 1 ) : #NEWLINE# #INDENT# raise TypeError ( ( "shape must be a vector or list of scalar, got '%s'" % v_shape ) ) #NEWLINE# #UNINDENT# if ( ndim is None ) : #NEWLINE# #INDENT# ndim = tensor . get_vector_length ( v_shape ) #NEWLINE# #UNINDENT# bcast = ( [ False ] * ndim ) #NEWLINE# #UNINDENT# #UNINDENT# if ( v_shape . ndim != 1 ) : #NEWLINE# #INDENT# raise TypeError ( ( "shape must be a vector or list of scalar, got '%s'" % v_shape ) ) #NEWLINE# #UNINDENT# if ( not ( v_shape . dtype . startswith ( 'int' ) or v_shape . dtype . startswith ( 'uint' ) ) ) : #NEWLINE# #INDENT# raise TypeError ( 'shape must be an integer vector or list' , v_shape . dtype ) #NEWLINE# #UNINDENT# if ( args_ndim > ndim ) : #NEWLINE# #INDENT# raise ValueError ( 'ndim should be at least as big as required by args value' , ( ndim , args_ndim ) , args ) #NEWLINE# #UNINDENT# assert ( ndim == len ( bcast ) ) #NEWLINE# return ( ndim , tensor . cast ( v_shape , 'int64' ) , tuple ( bcast ) )