def fill_diagonal(a, val, wrap=False):
 if (a.ndim < 2):
  raise ValueError('array must be at least 2-d')
 end = None
 if (a.ndim == 2):
  step = (a.shape[1] + 1)
  if (not wrap):
   end = (a.shape[1] * a.shape[1])
 else:
  if (not numpy.alltrue((numpy.diff(a.shape) == 0))):
   raise ValueError('All dimensions of input must be of equal length')
  step = (1 + numpy.cumprod(a.shape[:(-1)]).sum())
 a.ravel()[:end:step] = val

def fill_diagonal ( a , val , wrap = False ) : if ( a . ndim < 2 ) : raise ValueError ( 'array must be at least 2-d' ) end = None if ( a . ndim == 2 ) : step = ( a . shape [ 1 ] + 1 ) if ( not wrap ) : end = ( a . shape [ 1 ] * a . shape [ 1 ] ) else : if ( not numpy . alltrue ( ( numpy . diff ( a . shape ) == 0 ) ) ) : raise ValueError ( 'All dimensions of input must be of equal length' ) step = ( 1 + numpy . cumprod ( a . shape [ : ( - 1 ) ] ) . sum ( ) ) a . ravel ( ) [ : end : step ] = val
fill the main diagonal of the given array of any dimensionality .