GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Cholesky.cpp
1 /*
2  GRT MIT License
3  Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6  and associated documentation files (the "Software"), to deal in the Software without restriction,
7  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all copies or substantial
12  portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 
20  This code is based on the LU Decomposition code from Numerical Recipes (3rd Edition)
21 
22 */
23 
24 #define GRT_DLL_EXPORTS
25 #include "Cholesky.h"
26 
27 GRT_BEGIN_NAMESPACE
28 
29 Cholesky::Cholesky(){
30  errorLog.setKey("[ERROR LUdcmp]");
31  warningLog.setKey("[WARNING LUdcmp]");
32  success = false;
33  N = 0;
34 }
35 
36 Cholesky::Cholesky(MatrixFloat &a) : N(a.getNumRows()), el(a) {
37 
38  errorLog.setKey("[ERROR LUdcmp]");
39  warningLog.setKey("[WARNING LUdcmp]");
40  success = false;
41 
42  int i,j,k; //k has to an int (rather than a UINT)
43  VectorFloat tmp;
44  Float sum = 0;
45  if( el.getNumCols() != N ){
46  errorLog << "The input matrix is not square!" << std::endl;
47  return;
48  }
49 
50  const int n = int(N);
51  for (i=0;i<n;i++) {
52  for (j=i;j<n;j++) {
53  for (sum=el[i][j],k=i-1;k>=0;k--) sum -= el[i][k]*el[j][k];
54  if (i == j) {
55  if (sum <= 0.0){
56  errorLog << "Sum is <=0.0" << std::endl;
57  return;
58  }
59  el[i][i]=sqrt(sum);
60  }else el[j][i]=sum/el[i][i];
61  }
62  }
63  for(i=0; i<n; i++)
64  for (j=0; j<i; j++)
65  el[j][i] = 0.;
66 
67  success = true;
68 }
69 
70 bool Cholesky::solve(VectorFloat &b,VectorFloat &x) {
71  int i,k;
72  const int n = int(N);
73  Float sum;
74 
75  if (b.size() != N || x.size() != N){
76  errorLog << ":solve(vector<Float> &b, vector<Float> &x) - The input vectors are not the same size!" << std::endl;
77  return false;
78  }
79  for(i=0; i<n; i++) {
80  for(sum=b[i],k=i-1;k>=0;k--) sum -= el[i][k]*x[k];
81  x[i]=sum/el[i][i];
82  }
83  for (i=n-1; i>=0; i--) {
84  for (sum=x[i],k=i+1;k<n;k++) sum -= el[k][i]*x[k];
85  x[i]=sum/el[i][i];
86  }
87  return true;
88 }
89 
90 bool Cholesky::elmult(VectorFloat &y,VectorFloat &b){
91  unsigned int i,j;
92  if (b.size() != N || y.size() != N){
93  errorLog << "elmult(vector<Float> &y vector<Float> &b) - The input vectors are not the same size!" << std::endl;
94  return false;
95  }
96  for (i=0;i<N;i++) {
97  b[i] = 0.;
98  for (j=0; j<=i; j++) b[i] += el[i][j]*y[j];
99  }
100  return true;
101 }
102 
103 bool Cholesky::elsolve(VectorFloat &b,VectorFloat &y){
104  UINT i,j;
105  Float sum = 0;
106 
107  if (b.size() != N || y.size() != N){
108  errorLog << "elsolve(vector<Float> &b vector<Float> &y) - The input vectors are not the same size!" << std::endl;
109  return false;
110  }
111  for (i=0; i<N; i++) {
112  for (sum=b[i],j=0; j<i; j++) sum -= el[i][j]*y[j];
113  y[i] = sum/el[i][i];
114  }
115  return true;
116 }
117 
118 bool Cholesky::inverse(MatrixFloat &ainv){
119  int i,j,k;
120  const int n = int(N);
121  Float sum = 0;
122  ainv.resize(N,N);
123 
124  for(i=0; i<n; i++) for(j=0; j<=i; j++){
125  sum = (i==j? 1. : 0.);
126  for(k=i-1; k>=j; k--) sum -= el[i][k]*ainv[j][k];
127  ainv[j][i]= sum/el[i][i];
128  }
129  for(i=n-1; i>=0; i--) for(j=0; j<=i; j++){
130  sum = (i<j? 0. : ainv[j][i]);
131  for(k=i+1; k<n; k++) sum -= el[k][i]*ainv[j][k];
132  ainv[i][j] = ainv[j][i] = sum/el[i][i];
133  }
134  return true;
135 }
136 
137 Float Cholesky::logdet(){
138  Float sum = 0.;
139  for(unsigned int i=0; i<N; i++) sum += log(el[i][i]);
140  return 2.*sum;
141 }
142 
143 GRT_END_NAMESPACE
This code is based on the LU Decomposition code from Numerical Recipes (3rd Edition) ...
virtual bool setKey(const std::string &key)
sets the key that gets written at the start of each message, this will be written in the format &#39;key ...
Definition: Log.h:166
virtual bool resize(const unsigned int r, const unsigned int c)
Definition: Matrix.h:245