nodes: 0, module; 1, function_definition; 2, function_name:eigendecomp; 3, parameters; 4, block; 5, identifier:G; 6, identifier:d; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, comment:# test decomposition of G.; 11, comment:#G_hat = np.dot(np.dot(u, np.diag(lamda)), u.T); 12, comment:#assert np.linalg.norm(G_hat - G) < 1e-10, 'decomposition not exact: err {}'.format(np.linalg.norm(G_hat - G)); 13, comment:# sort the eigenvalues in decreasing order; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, assert_statement; 18, expression_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, return_statement; 23, comment:"""
      Computes sorted eigendecomposition of G.
  
      :param G:  Matrix 
      :param d:  rank
  
      :return factor: vector of square root d of eigenvalues (biggest to smallest). 
      :return u: matrix with colums equal to the normalized eigenvectors corresponding to sorted eigenvalues.
      """; 24, assignment; 25, assignment; 26, assignment; 27, assignment; 28, assignment; 29, call; 30, call; 31, assignment; 32, assignment; 33, call; 34, assignment; 35, expression_list; 36, identifier:N; 37, subscript; 38, pattern_list; 39, call; 40, identifier:lamda; 41, call; 42, identifier:indices; 43, subscript; 44, identifier:lamda_sorted; 45, subscript; 46, attribute; 47, argument_list; 48, attribute; 49, argument_list; 50, identifier:u; 51, subscript; 52, identifier:factor; 53, call; 54, attribute; 55, argument_list; 56, subscript; 57, float:0.0; 58, identifier:factor; 59, call; 60, attribute; 61, integer:0; 62, identifier:lamda; 63, identifier:u; 64, attribute; 65, argument_list; 66, attribute; 67, argument_list; 68, call; 69, slice; 70, identifier:lamda; 71, identifier:indices; 72, parenthesized_expression; 73, identifier:all; 74, string:"{} not all positive!"; 75, identifier:format; 76, subscript; 77, identifier:u; 78, slice; 79, identifier:indices; 80, attribute; 81, argument_list; 82, identifier:np; 83, identifier:sqrt; 84, subscript; 85, keyword_argument; 86, identifier:factor; 87, slice; 88, attribute; 89, argument_list; 90, identifier:G; 91, identifier:shape; 92, attribute; 93, identifier:eig; 94, identifier:G; 95, identifier:np; 96, identifier:real; 97, identifier:lamda; 98, attribute; 99, argument_list; 100, unary_operator; 101, comparison_operator:lamda_sorted[
              :d] > -1e-10; 102, identifier:lamda_sorted; 103, slice; 104, identifier:np; 105, identifier:empty; 106, tuple; 107, keyword_argument; 108, identifier:lamda_sorted; 109, slice; 110, identifier:out; 111, subscript; 112, identifier:d; 113, identifier:np; 114, identifier:real; 115, identifier:u; 116, identifier:np; 117, identifier:linalg; 118, identifier:np; 119, identifier:argsort; 120, identifier:lamda; 121, integer:1; 122, subscript; 123, unary_operator; 124, identifier:d; 125, identifier:N; 126, identifier:dtype; 127, attribute; 128, identifier:d; 129, identifier:factor; 130, slice; 131, identifier:lamda_sorted; 132, slice; 133, float:1e-10; 134, identifier:lamda; 135, identifier:dtype; 136, integer:0; 137, identifier:d; 138, identifier:d
code: def eigendecomp(G, d):
      """
      Computes sorted eigendecomposition of G.
  
      :param G:  Matrix 
      :param d:  rank
  
      :return factor: vector of square root d of eigenvalues (biggest to smallest). 
      :return u: matrix with colums equal to the normalized eigenvectors corresponding to sorted eigenvalues.
      """
      N = G.shape[0]
      lamda, u = np.linalg.eig(G)
      # test decomposition of G.
      #G_hat = np.dot(np.dot(u, np.diag(lamda)), u.T)
      #assert np.linalg.norm(G_hat - G) < 1e-10, 'decomposition not exact: err {}'.format(np.linalg.norm(G_hat - G))
  
      # sort the eigenvalues in decreasing order
      lamda = np.real(lamda)
      indices = np.argsort(lamda)[::-1]
      lamda_sorted = lamda[indices]
      assert (lamda_sorted[
              :d] > -1e-10).all(), "{} not all positive!".format(lamda_sorted[:d])
  
      u = u[:, indices]
      factor = np.empty((N,), dtype=lamda.dtype)
      np.sqrt(lamda_sorted[:d], out=factor[0:d])
      factor[d:] = 0.0
      return factor, np.real(u)