GestureRecognitionToolkit  Version: 0.2.0
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  debugLog.setProceedingText("[DEBUG LUdcmp]");
31  errorLog.setProceedingText("[ERROR LUdcmp]");
32  warningLog.setProceedingText("[WARNING LUdcmp]");
33  success = false;
34  N = 0;
35 }
36 
37 Cholesky::Cholesky(MatrixFloat &a) : N(a.getNumRows()), el(a) {
38 
39  debugLog.setProceedingText("[DEBUG LUdcmp]");
40  errorLog.setProceedingText("[ERROR LUdcmp]");
41  warningLog.setProceedingText("[WARNING LUdcmp]");
42  success = false;
43 
44  int i,j,k; //k has to an int (rather than a UINT)
45  VectorFloat tmp;
46  Float sum = 0;
47  if( el.getNumCols() != N ){
48  errorLog << "The input matrix is not square!" << std::endl;
49  return;
50  }
51 
52  const int n = int(N);
53  for (i=0;i<n;i++) {
54  for (j=i;j<n;j++) {
55  for (sum=el[i][j],k=i-1;k>=0;k--) sum -= el[i][k]*el[j][k];
56  if (i == j) {
57  if (sum <= 0.0){
58  errorLog << "Sum is <=0.0" << std::endl;
59  return;
60  }
61  el[i][i]=sqrt(sum);
62  }else el[j][i]=sum/el[i][i];
63  }
64  }
65  for(i=0; i<n; i++)
66  for (j=0; j<i; j++)
67  el[j][i] = 0.;
68 
69  success = true;
70 }
71 
72 bool Cholesky::solve(VectorFloat &b,VectorFloat &x) {
73  int i,k;
74  const int n = int(N);
75  Float sum;
76 
77  if (b.size() != N || x.size() != N){
78  errorLog << ":solve(vector<Float> &b, vector<Float> &x) - The input vectors are not the same size!" << std::endl;
79  return false;
80  }
81  for(i=0; i<n; i++) {
82  for(sum=b[i],k=i-1;k>=0;k--) sum -= el[i][k]*x[k];
83  x[i]=sum/el[i][i];
84  }
85  for (i=n-1; i>=0; i--) {
86  for (sum=x[i],k=i+1;k<n;k++) sum -= el[k][i]*x[k];
87  x[i]=sum/el[i][i];
88  }
89  return true;
90 }
91 
92 bool Cholesky::elmult(VectorFloat &y,VectorFloat &b){
93  unsigned int i,j;
94  if (b.size() != N || y.size() != N){
95  errorLog << "elmult(vector<Float> &y vector<Float> &b) - The input vectors are not the same size!" << std::endl;
96  return false;
97  }
98  for (i=0;i<N;i++) {
99  b[i] = 0.;
100  for (j=0; j<=i; j++) b[i] += el[i][j]*y[j];
101  }
102  return true;
103 }
104 
105 bool Cholesky::elsolve(VectorFloat &b,VectorFloat &y){
106  UINT i,j;
107  Float sum = 0;
108 
109  if (b.size() != N || y.size() != N){
110  errorLog << "elsolve(vector<Float> &b vector<Float> &y) - The input vectors are not the same size!" << std::endl;
111  return false;
112  }
113  for (i=0; i<N; i++) {
114  for (sum=b[i],j=0; j<i; j++) sum -= el[i][j]*y[j];
115  y[i] = sum/el[i][i];
116  }
117  return true;
118 }
119 
120 bool Cholesky::inverse(MatrixFloat &ainv){
121  int i,j,k;
122  const int n = int(N);
123  Float sum = 0;
124  ainv.resize(N,N);
125 
126  for(i=0; i<n; i++) for(j=0; j<=i; j++){
127  sum = (i==j? 1. : 0.);
128  for(k=i-1; k>=j; k--) sum -= el[i][k]*ainv[j][k];
129  ainv[j][i]= sum/el[i][i];
130  }
131  for(i=n-1; i>=0; i--) for(j=0; j<=i; j++){
132  sum = (i<j? 0. : ainv[j][i]);
133  for(k=i+1; k<n; k++) sum -= el[k][i]*ainv[j][k];
134  ainv[i][j] = ainv[j][i] = sum/el[i][i];
135  }
136  return true;
137 }
138 
139 Float Cholesky::logdet(){
140  Float sum = 0.;
141  for(unsigned int i=0; i<N; i++) sum += log(el[i][i]);
142  return 2.*sum;
143 }
144 
145 GRT_END_NAMESPACE
This code is based on the LU Decomposition code from Numerical Recipes (3rd Edition) ...
virtual bool resize(const unsigned int r, const unsigned int c)
Definition: Matrix.h:232