Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testSvd.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  * Test various svd decompositions.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
45 #include <visp3/core/vpTime.h>
46 
47 #include <visp3/core/vpMatrix.h>
48 #include <visp3/core/vpColVector.h>
49 #include <vector>
50 #include <algorithm>
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 bool testSvdOpenCvGSLCoherence(double epsilon);
55 #ifdef VISP_HAVE_GSL
56 bool testRandom(double epsilon);
57 #endif
58 
59 
60 #define abs(x) ((x) < 0 ? - (x) : (x))
61 #ifdef VISP_HAVE_GSL
62 
63 bool testRandom(double epsilon)
64 {
65  vpMatrix L0(6,6);
66  vpMatrix L1(6,6);
67 
68  for (unsigned int i=0 ; i < L0.getRows() ; i++)
69  for (unsigned int j=0 ; j < L0.getCols() ; j++)
70  L1[i][j] = L0[i][j] = (double)rand()/(double)RAND_MAX;
71 
72  vpColVector W0(L0.getCols()) ;
73  vpMatrix V0(L0.getCols(), L0.getCols()) ;
74  vpColVector W1(L1.getCols()) ;
75  vpMatrix V1(L1.getCols(), L1.getCols()) ;
76 
77  L0.svdNr(W0,V0);
78  L1.svdGsl(W1,V1);
79 
82 
83  vpColVector diff = _W0-_W1;
84  double error=-1.0;
85 
86  for(unsigned int i=0;i<6;i++)
87  error=std::max(abs(diff[i]),error);
88 
89  return error<epsilon;
90 
91 }
92 
93 #endif
94 
95 bool testSvdOpenCvGSLCoherence(double epsilon)
96 {
97 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) && defined (VISP_HAVE_GSL) // Require opencv >= 2.1.1
98  vpMatrix A;
99  vpMatrix vA;
100  vpColVector wA;
101  vpMatrix B;
102  vpMatrix vB;
103  vpColVector wB;
104  A.resize(6,6);
105  B.resize(6,6);
106  vA.resize(6,6);
107  vB.resize(6,6);
108  wA.resize(6);
109  wB.resize(6);
110 
111  for (unsigned int i=0 ; i < A.getRows() ; i++)
112  for (unsigned int j=0 ; j < A.getCols() ; j++)
113  B[i][j] = A[i][j] = (double)rand()/(double)RAND_MAX;
114 
115  A.svdOpenCV(wA,vA);
116  B.svdGsl(wB,vB);
117 
118  bool error=false;
119  for (unsigned int i=0 ; i < A.getRows() ; i++){
120  error = error | (abs(wA[i]-wB[i])>epsilon);
121  }
122 
123  return !error;
124 #else
125  (void)epsilon;
126  return true;
127 #endif
128 }
129 
130 int
131 main()
132 {
133  try {
134  vpMatrix L(60000,6), Ls ;
135  for (unsigned int i=0 ; i < L.getRows() ; i++)
136  for (unsigned int j=0 ; j < L.getCols() ; j++)
137  L[i][j] = 2*i+j + cos((double)(i+j))+((double)(i)) ;
138  // std::cout << L << std::endl ;
139  Ls = L ;
140  std::cout << "--------------------------------------"<<std::endl ;
141 
142  vpColVector W(L.getCols()) ;
143  vpMatrix V(L.getCols(), L.getCols()) ;
144 
145  double t = vpTime::measureTimeMs() ;
146  L.svdNr(W,V) ;
147  t = vpTime::measureTimeMs() -t ;
148 
149  std::cout <<"svdNr Numerical recipes \n time " <<t << std::endl;
150  std::cout << W.t() ;
151  std::cout << "--------------------------------------"<<std::endl ;
152 
153 
154 #ifdef VISP_HAVE_GSL
155  L = Ls ;
156  t = vpTime::measureTimeMs() ;
157  L.svdGsl(W,V) ;
158  t = vpTime::measureTimeMs() -t ;
159  std::cout <<"svdGsl_mod \n time " <<t << std::endl;
160  std::cout << W.t() ;
161 
162  std::cout << "--------------------------------------"<<std::endl ;
163  std::cout << "TESTING RANDOM MATRICES:" ;
164 
165  bool ret = true;
166  for(unsigned int i=0;i<2000;i++)
167  ret = ret & testRandom(0.00001);
168  if(ret)
169  std:: cout << "Success"<< std:: endl;
170  else
171  std:: cout << "Fail"<< std:: endl;
172 
173  std::cout << "--------------------------------------"<<std::endl ;
174 #endif
175 
176  std::cout << "--------------------------------------"<<std::endl ;
177  std::cout << "TESTING OPENCV-GSL coherence:" ;
178 
179  bool ret2 = true;
180  for(unsigned int i=0;i<1;i++)
181  ret2 = ret2 & testSvdOpenCvGSLCoherence(0.00001);
182  if(ret2)
183  std:: cout << "Success"<< std:: endl;
184  else
185  std:: cout << "Fail"<< std:: endl;
186 
187  std::cout << "--------------------------------------"<<std::endl ;
188 
189  L = Ls ;
190  t = vpTime::measureTimeMs() ;
191  L.svdFlake(W,V) ;
192  t = vpTime::measureTimeMs() -t ;
193  std::cout <<"svdFlake\n time " <<t << std::endl;
194  std::cout << W.t() ;
195  return 0;
196  }
197  catch(vpException &e) {
198  std::cout << "Catch an exception: " << e << std::endl;
199  return 1;
200  }
201 }
202 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
static vpColVector sort(const vpColVector &v)
error that can be emited by ViSP classes.
Definition: vpException.h:73
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225