eigensort.inl
Go to the documentation of this file.
1 /**
2 *
3 * \brief Inline implementation of eigenvalue/-vector sorting
4 *
5 * \ingroup shark_globals
6 *
7 * \par Copyright (c) 1998-2011:
8 * Institut f&uuml;r Neuroinformatik<BR>
9 * Ruhr-Universit&auml;t Bochum<BR>
10 * D-44780 Bochum, Germany<BR>
11 * Phone: +49-234-32-25558<BR>
12 * Fax: +49-234-32-14209<BR>
13 * eMail: shark-admin@neuroinformatik.ruhr-uni-bochum.de<BR>
14 * www: http://www.neuroinformatik.ruhr-uni-bochum.de<BR>
15 * <BR>
16 *
17 *
18 * <BR><HR>
19 * This file is part of Shark. This library is free software;
20 * you can redistribute it and/or modify it under the terms of the
21 * GNU General Public License as published by the Free Software
22 * Foundation; either version 3, or (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this library; if not, see <http://www.gnu.org/licenses/>.
31 *
32 */
33 
34 
35 #ifndef SHARK_LINALG_EIGENSORT_INL
36 #define SHARK_LINALG_EIGENSORT_INL
37 
38 
39 //===========================================================================
40 /*!
41  * \brief Sorts the eigenvalues in vector "dvecA" and the corresponding
42  * eigenvectors in matrix "vmatA".
43  *
44  * Given the matrix \em vmatA of eigenvectors and the vector
45  * \em dvecA of corresponding eigenvalues, the values in \em dvecA will
46  * be sorted by descending order and the eigenvectors in
47  * \em vmatA will change their places in a way, that at the
48  * end of the function an eigenvalue at position \em j of
49  * vector \em dvecA will belong to the eigenvector
50  * at column \em j of matrix \em vmatA.
51  * If we've got for example the following result after calling the function:
52  *
53  * \f$
54  * \begin{array}{*{3}{r}}
55  * v_{11} & v_{21} & v_{31}\\
56  * v_{12} & v_{22} & v_{32}\\
57  * v_{13} & v_{23} & v_{33}\\
58  * & & \\
59  * v_1 & v_2 & v_3\\
60  * \end{array}
61  * \f$
62  *
63  * then eigenvalue \f$ v_1 \f$ has the corresponding eigenvector
64  * \f$ ( v_{11}\ v_{12}\ v_{13} ) \f$ and \f$ v_1 > v_2 > v_3 \f$.
65  *
66  *
67  * \param vmatA \f$ n \times n \f$ matrix with eigenvectors (each column
68  * contains an eigenvector, corresponding to
69  * one eigenvalue).
70  * \param dvecA n-dimensional vector with eigenvalues, will
71  * contain the eigenvalues in descending order
72  * when returning from the function.
73  * \return none.
74  * \throw SharkException the type of the exception will be
75  * "size mismatch" and indicates that \em dvecA
76  * is not one-dimensional or that the number of
77  * rows or the number of columns in \em vmatA
78  * is different from the number of values
79  * in \em dvecA
80  *
81  *
82  * Please follow the link to view the source code of the example.
83  * The example can be executed in the example directory
84  * of package LinAlg.
85  *
86  * \author M. Kreutz
87  * \date 1998
88  *
89  */
90 template<class MatrixT,class VectorT>
92 (
93  MatrixT& vmatA,
94  VectorT& dvecA
95 )
96 {
98  (
99  dvecA.size() == vmatA.size1() &&
100  dvecA.size() == vmatA.size2()
101  );
102 
103  unsigned n = dvecA.size();
104  unsigned i, j, l;//l: position of largest remainig eigenvalue
105  double t;//largest remaining eigenvalue
106 
107  //
108  // sort eigen values
109  //
110  for (i = 0; i < n - 1; i++)
111  {
112  t = dvecA( l = i );
113  //find largest remaining eigenvalue
114  for (j = i + 1; j < n; j++) {
115  if (dvecA( j ) >= t) {
116  t = dvecA( l = j );
117  }
118  }
119 
120  if (l != i) {
121  //switch position of i's eigenvalue and the largest remaining eigenvalue
122  dvecA( l ) = dvecA( i );
123  dvecA( i ) = t;
124  //switch postions of corresponding eigenvectors
125  for (j = 0; j < n; j++) {
126  t = vmatA( j , i );
127  vmatA( j , i ) = vmatA( j , l );
128  vmatA( j , l ) = t;
129  }
130  }
131  }
132 }
133 
134 #endif