@@ -106,6 +106,30 @@ def cormat_to_covmat(err_list, cormat_list):
106106 covmat_list .append (cormat_list [i ] * err_list [a ] * err_list [b ])
107107 return covmat_list
108108
109+ def sort_eigenvalues (evals , evecs ):
110+ r"""Defines ordering of the eigenvalues and eigenvectors such
111+ that eigenvalues are given in decreasing order and the first
112+ non-zero entry of each eigenvector is positive.
113+ Parameters
114+ ----------
115+ evals, evecs : output of np.linalg.eig
116+
117+ Returns
118+ -------
119+ evacs_sorted, evecs_sorted : ordered eigen- values and vectors,
120+ as defined above
121+
122+ """
123+ idx = evals .argsort ()[::- 1 ]
124+ evals_sorted = evals [idx ]
125+ evecs_sorted = evecs [:,idx ]
126+ for i in range (len (evecs_sorted )):
127+ j = 0
128+ while evecs_sorted [j ,i ] == 0 :
129+ j += 1
130+ if evecs_sorted [j ,i ] < 0 :
131+ evecs_sorted [:, i ] *= - 1
132+ return evals_sorted , evecs_sorted
109133
110134def covmat_to_artunc (ndata , covmat_list , no_of_norm_mat = 0 ):
111135 r"""Convert the covariance matrix to a matrix of
@@ -152,6 +176,7 @@ def covmat_to_artunc(ndata, covmat_list, no_of_norm_mat=0):
152176 b = i % ndata
153177 covmat [a ][b ] = covmat_list [i ]
154178 eigval , eigvec = eig (covmat )
179+ eigval , eigvec = sort_eigenvalues (eigval , eigvec )
155180 for j in range (len (eigval )):
156181 if eigval [j ] < epsilon :
157182 psd_check = False
@@ -397,7 +422,8 @@ def decompose_covmat(covmat):
397422 giving ndat correlated systematics for each of the ndat point.
398423 The original covmat is obtained by doing sys@sys.T"""
399424
400- lamb , mat = np .linalg .eig (covmat )
425+ lamb , mat = eig (covmat )
426+ lamb , mat = sort_eigenvalues (lamb , mat )
401427 sys = np .multiply (np .sqrt (lamb ), mat )
402428 return sys
403429
0 commit comments