Gaia
kullbackleiblerdistance.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_KULLBACKLEIBLERDISTANCE_H
21 #define GAIA_KULLBACKLEIBLERDISTANCE_H
22 
23 #include "distancefunction.h"
24 
25 namespace gaia2 {
26 
38  public:
39  KullbackLeiblerDistance(const PointLayout& layout, const ParameterMap& params);
40  Real operator()(const Point& p1, const Point& p2, int seg1, int seg2) const;
41 
42  protected:
43  int _meanIdx, _covIdx, _icovIdx;
44  int _meanIdxEnd, _covIdxEnd, _icovIdxEnd;
45 };
46 
47 
52 inline Real traceMatrixProduct(const Real* m1, const Real* m2, int size) {
53  Real result = 0.0;
54  for (int i=0; i<size; i++) {
55  for (int j=0; j<size; j++) {
56  result += m1[i*size + j] * m2[j*size + i];
57  }
58  }
59  return result;
60 }
61 
62 
70 inline Real traceMatrixProductMean(const Real* m1, const Real* m2,
71  const Real* v1, const Real* v2,
72  int size) {
73  Real result = 0.0;
74 
75  // we want this to be on the stack to avoid mem allocation, this function
76  // needs to be real fast
77  Q_ASSERT(size < 64);
78  Real v[64];
79 
80  for (int i=0; i<size; i++) v[i] = v1[i] - v2[i];
81 
82  for (int i=0; i<size; i++) {
83  for (int j=0; j<size; j++) {
84  result += (m1[i*size + j] + m2[i*size + j]) * v[j] * v[i];
85  }
86  }
87 
88  return result;
89 }
90 
91 
92 
93 } // namespace gaia2
94 
95 #endif // GAIA_KULLBACKLEIBLERDISTANCE_H
This class describes the layout of a point.
Definition: pointlayout.h:60
Real traceMatrixProduct(const Real *m1, const Real *m2, int size)
Compute the trace of the matrix product of m1 and m2.
Definition: kullbackleiblerdistance.h:52
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Definition: distancefunction.h:37
Definition: parameter.h:34
Real operator()(const Point &p1, const Point &p2, int seg1, int seg2) const
This function computes the distance between the two given points.
Definition: kullbackleiblerdistance.cpp:65
This class computes the symmetric Kullback-Leibler distance of a given Region.
Definition: kullbackleiblerdistance.h:37
Real traceMatrixProductMean(const Real *m1, const Real *m2, const Real *v1, const Real *v2, int size)
Compute the trace of the product of 2 matrices and vectors using the formula: trace((m1+m2)*(v1-v2)*(...
Definition: kullbackleiblerdistance.h:70
Definition: point.h:106