Gaia
linalg.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_LINALG_H
21 #define GAIA_LINALG_H
22 
23 #include "dataset.h"
24 #include "Eigen/Eigen"
25 
26 
27 namespace gaia2 {
28 
35 Eigen::MatrixXd covarianceMatrix(const PointArray& v,
36  const Region& region);
37 
38 Eigen::MatrixXf covarianceMatrixSinglePrecision(const PointArray& v,
39  const Region& region);
40 
41 
42 
47 template <typename T, typename Compare>
48 void sortEigenVectors(Eigen::Matrix<T, Eigen::Dynamic, 1>& values,
49  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& vectors,
50  Compare cmp) {
51  const int n = values.size();
52 
53  // bubble sort, stable sort implementation, should be enough for our purposes
54  for (int i=0; i<n-1; i++) {
55  for (int j=n-1; j>i; j--) {
56 
57  if (cmp(std::abs(values[j]), std::abs(values[j-1]))) {
58 
59  // swap values in eigenvalues vector
60  std::swap(values[j], values[j-1]);
61 
62  // swap columns in eigenvectors matrix
63  vectors.col(j).swap(vectors.col(j-1));
64  }
65  }
66  }
67 }
68 
72 template <typename T>
73 void sortEigenVectors(Eigen::Matrix<T, Eigen::Dynamic, 1>& values,
74  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& vectors) {
75  return sortEigenVectors(values, vectors, std::less<T>());
76 }
77 
78 
79 } // namespace gaia2
80 
81 #endif // GAIA_LINALG_H
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Eigen::MatrixXd covarianceMatrix(const PointArray &v, const Region &region)
Computes the covariance matrix given a set of points.
Definition: linalg.cpp:99
void sortEigenVectors(Eigen::Matrix< T, Eigen::Dynamic, 1 > &values, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &vectors, Compare cmp)
Sort the eigen vectors and their corresponding values according to the given compare function...
Definition: linalg.h:48