#NEWLINE# def structured_rand_arr( size , sample_func = np . random . random , ltfac = None , utfac = None , fill_diag = None ) : #NEWLINE# #INDENT# 'Make a structured random 2-d array of shape (size,size).\n\n    If no optional arguments are given, a symmetric array is returned.\n\n    Parameters\n    ----------\n    size : int\n      Determines the shape of the output array: (size,size).\n\n    sample_func : function, optional.\n      Must be a function which when called with a 2-tuple of ints, returns a\n      2-d array of that shape.  By default, np.random.random is used, but any\n      other sampling function can be used as long as matches this API.\n\n    utfac : float, optional\n      Multiplicative factor for the upper triangular part of the matrix.\n\n    ltfac : float, optional\n      Multiplicative factor for the lower triangular part of the matrix.\n\n    fill_diag : float, optional\n      If given, use this value to fill in the diagonal.  Otherwise the diagonal\n      will contain random elements.\n\n    Examples\n    --------\n    >>> np.random.seed(0)  # for doctesting\n    >>> np.set_printoptions(precision=4)  # for doctesting\n    >>> structured_rand_arr(4)\n    array([[ 0.5488,  0.7152,  0.6028,  0.5449],\n           [ 0.7152,  0.6459,  0.4376,  0.8918],\n           [ 0.6028,  0.4376,  0.7917,  0.5289],\n           [ 0.5449,  0.8918,  0.5289,  0.0871]])\n    >>> structured_rand_arr(4,ltfac=-10,utfac=10,fill_diag=0.5)\n    array([[ 0.5   ,  8.3262,  7.7816,  8.7001],\n           [-8.3262,  0.5   ,  4.6148,  7.8053],\n           [-7.7816, -4.6148,  0.5   ,  9.4467],\n           [-8.7001, -7.8053, -9.4467,  0.5   ]])\n    ' #NEWLINE# rmat = sample_func ( ( size , size ) ) #NEWLINE# out = np . empty_like ( rmat ) #NEWLINE# uidx = triu_indices ( size , 1 ) #NEWLINE# lidx = tril_indices ( size , ( - 1 ) ) #NEWLINE# didx = diag_indices ( size ) #NEWLINE# if utfac : #NEWLINE# #INDENT# out [ uidx ] = ( utfac * rmat [ uidx ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# out [ uidx ] = rmat [ fill_diag ] #NEWLINE# #UNINDENT# if ltfac : #NEWLINE# #INDENT# out [ lidx ] = ( ltfac * rmat . T [ lidx ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# out [ lidx ] = rmat . T [ lidx ] #NEWLINE# #UNINDENT# if ( fill_diag is not None ) : #NEWLINE# #INDENT# out [ didx ] = fill_diag #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# out [ didx ] = rmat [ didx ] #NEWLINE# #UNINDENT# return out