def _generate_broadcasting_indices(out_shape, *shapes):
 all_shapes = ((out_shape,) + shapes)
 ret_indices = [[()] for shape in all_shapes]
 for dim in xrange(len(out_shape)):
  _ret_indices = [[] for shape in all_shapes]
  out_range = list(range(out_shape[dim]))
  ranges = [out_range]
  for shape in shapes:
   if (shape[dim] == out_shape[dim]):
    ranges.append(out_range)
   elif (shape[dim] == 1):
    ranges.append(([0] * out_shape[dim]))
   else:
    raise ValueError(('shape[%i] (%i) should be equal to out_shape[%i] (%i) or to 1' % (dim, shape[dim], dim, out_shape[dim])), shape, out_shape, shapes)
  for prev_index in zip(*ret_indices):
   for dim_index in zip(*ranges):
    for i in xrange(len(all_shapes)):
     _ret_indices[i].append((prev_index[i] + (dim_index[i],)))
  ret_indices = _ret_indices
 return ret_indices

def _generate_broadcasting_indices ( out_shape , * shapes ) : all_shapes = ( ( out_shape , ) + shapes ) ret_indices = [ [ ( ) ] for shape in all_shapes ] for dim in xrange ( len ( out_shape ) ) : _ret_indices = [ [ ] for shape in all_shapes ] out_range = list ( range ( out_shape [ dim ] ) ) ranges = [ out_range ] for shape in shapes : if ( shape [ dim ] == out_shape [ dim ] ) : ranges . append ( out_range ) elif ( shape [ dim ] == 1 ) : ranges . append ( ( [ 0 ] * out_shape [ dim ] ) ) else : raise ValueError ( ( 'shape[%i] (%i) should be equal to out_shape[%i] (%i) or to 1' % ( dim , shape [ dim ] , dim , out_shape [ dim ] ) ) , shape , out_shape , shapes ) for prev_index in zip ( * ret_indices ) : for dim_index in zip ( * ranges ) : for i in xrange ( len ( all_shapes ) ) : _ret_indices [ i ] . append ( ( prev_index [ i ] + ( dim_index [ i ] , ) ) ) ret_indices = _ret_indices return ret_indices
return indices over each shape that broadcast them to match out_shape .