Visual Servoing Platform  version 3.0.0
vpMatrix_lu.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  unsigned int ip;
168  double sum_;
169  bool flag = false;
170  unsigned int i;
171 
172  for (i=0;i<n;i++) {
173  ip=perm[i];
174  sum_=b[ip];
175  b[ip]=b[i];
176  if (flag) {
177  for (unsigned int j=ii;j<=i-1;j++) sum_ -= rowPtrs[i][j]*b[j];
178  }
179  //else if (sum_) {
180  else if (std::fabs(sum_) > std::numeric_limits<double>::epsilon()) {
181  ii=i;
182  flag = true;
183  }
184  b[i]=sum_;
185  }
186  // for (int i=n-1;i>=0;i--) {
187  // sum_=b[i];
188  // for (int j=i+1;j<n;j++) sum_ -= rowPtrs[i][j]*b[j];
189  // b[i]=sum_/rowPtrs[i][i];
190  // }
191  i=n;
192  do {
193  i --;
194 
195  sum_=b[i];
196  for (unsigned int j=i+1;j<n;j++) sum_ -= rowPtrs[i][j]*b[j];
197  b[i]=sum_/rowPtrs[i][i];
198  } while(i != 0);
199 }
200 #endif // doxygen should skip this
201 
231 vpMatrix
233 {
234  unsigned int i,j;
235 
236  if ( rowNum != colNum)
237  {
238  vpERROR_TRACE("\n\t\tCannot invert a non-square vpMatrix") ;
240  "Cannot invert a non-square vpMatrix")) ;
241  }
242 
244  vpMatrix V(rowNum, rowNum);
245  vpColVector W(rowNum);
246 
247  for (i=0; i<rowNum; i++) {
248  for (j=0; j<rowNum; j++) {
249  B[i][j] = (i == j) ? 1 : 0;
250  }
251  }
252 
253  vpMatrix A(rowNum, rowNum);
254  A = *this;
255 
256  unsigned int *perm = new unsigned int[rowNum];
257  int p;
258 
259  try {
260  A.LUDcmp(perm, p);
261  }
262  catch(vpException e) {
263  delete [] perm;
264  throw(e);
265  }
266 
267  vpColVector c_tmp(rowNum) ;
268  for (j=1; j<=rowNum; j++)
269  {
270  c_tmp =0 ; c_tmp[j-1] = 1 ;
271  A.LUBksb(perm, c_tmp);
272  for (unsigned int k=0 ; k < c_tmp.getRows() ; k++)
273  B[k][j-1] = c_tmp[k] ;
274  }
275  delete [] perm;
276  return B;
277 }
278 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
#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