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