Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMatrix_lu.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Matrix LU decomposition.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 
42 #include <visp3/core/vpMatrix.h>
43 #include <visp3/core/vpMath.h>
44 #include <visp3/core/vpColVector.h>
45 
46 // Exception
47 #include <visp3/core/vpException.h>
48 #include <visp3/core/vpMatrixException.h>
49 
50 // Debug trace
51 #include <visp3/core/vpDebug.h>
52 
53 #include <cmath> // std::fabs
54 #include <limits> // numeric_limits
55 
56 #define TINY 1.0e-20;
57 
58 
59 /*--------------------------------------------------------------------
60  LU Decomposition related functions
61 -------------------------------------------------------------------- */
62 
81 void
82 vpMatrix::LUDcmp(unsigned int *perm, int& d)
83 {
84  unsigned int n = rowNum;
85 
86  unsigned int i,imax=0,j,k;
87  double big,dum,sum_,temp;
88  vpColVector vv(n);
89 
90  d=1;
91  for (i=0;i<n;i++) {
92  big=0.0;
93  for (j=0;j<n;j++)
94  if ((temp=fabs(rowPtrs[i][j])) > big) big=temp;
95  //if (big == 0.0)
96  if (std::fabs(big) <= std::numeric_limits<double>::epsilon())
97  {
98  //vpERROR_TRACE("Singular vpMatrix in LUDcmp") ;
100  "Singular vpMatrix in LUDcmp")) ;
101  }
102  vv[i]=1.0/big;
103  }
104  for (j=0;j<n;j++) {
105  for (i=0;i<j;i++) {
106  sum_=rowPtrs[i][j];
107  for (k=0;k<i;k++) sum_ -= rowPtrs[i][k]*rowPtrs[k][j];
108  rowPtrs[i][j]=sum_;
109  }
110  big=0.0;
111  for (i=j;i<n;i++) {
112  sum_=rowPtrs[i][j];
113  for (k=0;k<j;k++)
114  sum_ -= rowPtrs[i][k]*rowPtrs[k][j];
115  rowPtrs[i][j]=sum_;
116  if ( (dum=vv[i]*fabs(sum_)) >= big) {
117  big=dum;
118  imax=i;
119  }
120  }
121  if (j != imax) {
122  for (k=0;k<n;k++) {
123  dum=rowPtrs[imax][k];
124  rowPtrs[imax][k]=rowPtrs[j][k];
125  rowPtrs[j][k]=dum;
126  }
127  d *= -1;
128  vv[imax]=vv[j];
129  }
130  perm[j]=imax;
131  //if (rowPtrs[j][j] == 0.0)
132  if (std::fabs(rowPtrs[j][j]) <= std::numeric_limits<double>::epsilon())
133  rowPtrs[j][j]=TINY;
134  if (j != n) {
135  dum=1.0/(rowPtrs[j][j]);
136  for (i=j+1;i<n;i++) rowPtrs[i][j] *= dum;
137  }
138  }
139 }
140 
141 #undef TINY
142 
162 void vpMatrix::LUBksb(unsigned int *perm, vpColVector& b)
163 {
164  unsigned int n = rowNum;
165 
166  unsigned int ii=0;
167  double sum_;
168  bool flag = false;
169  unsigned int i;
170 
171  for (i=0;i<n;i++) {
172  unsigned int ip=perm[i];
173  sum_=b[ip];
174  b[ip]=b[i];
175  if (flag) {
176  for (unsigned int j=ii;j<=i-1;j++) sum_ -= rowPtrs[i][j]*b[j];
177  }
178  //else if (sum_) {
179  else if (std::fabs(sum_) > std::numeric_limits<double>::epsilon()) {
180  ii=i;
181  flag = true;
182  }
183  b[i]=sum_;
184  }
185  // for (int i=n-1;i>=0;i--) {
186  // sum_=b[i];
187  // for (int j=i+1;j<n;j++) sum_ -= rowPtrs[i][j]*b[j];
188  // b[i]=sum_/rowPtrs[i][i];
189  // }
190  i=n;
191  do {
192  i --;
193 
194  sum_=b[i];
195  for (unsigned int j=i+1;j<n;j++) sum_ -= rowPtrs[i][j]*b[j];
196  b[i]=sum_/rowPtrs[i][i];
197  } while(i != 0);
198 }
199 #endif // doxygen should skip this
200 
230 vpMatrix
232 {
233  unsigned int i,j;
234 
235  if ( rowNum != colNum)
236  {
237  vpERROR_TRACE("\n\t\tCannot invert a non-square vpMatrix") ;
239  "Cannot invert a non-square vpMatrix")) ;
240  }
241 
243  vpMatrix V(rowNum, rowNum);
244  vpColVector W(rowNum);
245 
246  for (i=0; i<rowNum; i++) {
247  for (j=0; j<rowNum; j++) {
248  B[i][j] = (i == j) ? 1 : 0;
249  }
250  }
251 
252  vpMatrix A(rowNum, rowNum);
253  A = *this;
254 
255  unsigned int *perm = new unsigned int[rowNum];
256 
257  try {
258  int p;
259  A.LUDcmp(perm, p);
260  }
261  catch(vpException &e) {
262  delete [] perm;
263  throw(e);
264  }
265 
266  vpColVector c_tmp(rowNum) ;
267  for (j=1; j<=rowNum; j++)
268  {
269  c_tmp =0 ; c_tmp[j-1] = 1 ;
270  A.LUBksb(perm, c_tmp);
271  for (unsigned int k=0 ; k < c_tmp.getRows() ; k++)
272  B[k][j-1] = c_tmp[k] ;
273  }
274  delete [] perm;
275  return B;
276 }
277 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
#define vpERROR_TRACE
Definition: vpDebug.h:391
error that can be emited by ViSP classes.
Definition: vpException.h:73
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:76
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpMatrix inverseByLU() const
error that can be emited by the vpMatrix class and its derivates
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78