def triu2flat(m):
 dim = m.shape[0]
 res = zeros(((dim * (dim + 1)) / 2))
 index = 0
 for row in range(dim):
  res[index:((index + dim) - row)] = m[row, row:]
  index += (dim - row)
 return res

def triu2flat ( m ) : dim = m . shape [ 0 ] res = zeros ( ( ( dim * ( dim + 1 ) ) / 2 ) ) index = 0 for row in range ( dim ) : res [ index : ( ( index + dim ) - row ) ] = m [ row , row : ] index += ( dim - row ) return res
flattens an upper triangular matrix .