def flat2triu(a, dim):
 res = zeros((dim, dim))
 index = 0
 for row in range(dim):
  res[row, row:] = a[index:((index + dim) - row)]
  index += (dim - row)
 return res

def flat2triu ( a , dim ) : res = zeros ( ( dim , dim ) ) index = 0 for row in range ( dim ) : res [ row , row : ] = a [ index : ( ( index + dim ) - row ) ] index += ( dim - row ) return res
produces an upper triangular matrix of dimension dim from the elements of the given vector .