Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testMatrixPseudoInverse.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test various svd decompositions.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
44 #include <algorithm>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <vector>
48 
49 #include <visp3/core/vpColVector.h>
50 #include <visp3/core/vpMatrix.h>
51 #include <visp3/core/vpTime.h>
52 #include <visp3/io/vpParseArgv.h>
53 
54 // List of allowed command line options
55 #define GETOPTARGS "cdn:i:pf:R:C:vh"
56 
65 void usage(const char *name, const char *badparam)
66 {
67  fprintf(stdout, "\n\
68 Test matrix pseudo-inverse.\n\
69 Outputs a comparison of the results obtained by supported 3rd parties.\n\
70 \n\
71 SYNOPSIS\n\
72  %s [-n <number of matrices>] [-f <plot filename>]\n\
73  [-R <number of rows>] [-C <number of columns>]\n\
74  [-i <number of iterations>] [-p] [-h]\n", name);
75 
76  fprintf(stdout, "\n\
77 OPTIONS: Default\n\
78  -n <number of matrices> \n\
79  Number of matrices inverted during each test loop.\n\
80 \n\
81  -i <number of iterations> \n\
82  Number of iterations of the test.\n\
83 \n\
84  -f <plot filename> \n\
85  Set output path for plot output.\n\
86  The plot logs the times of \n\
87  the different inversion methods: \n\
88  QR,LU,Cholesky and Pseudo-inverse.\n\
89 \n\
90  -R <number of rows>\n\
91  Number of rows of the automatically generated matrices \n\
92  we test on.\n\
93 \n\
94  -C <number of columns>\n\
95  Number of colums of the automatically generated matrices \n\
96  we test on.\n\
97 \n\
98  -p \n\
99  Plot into filename in the gnuplot format. \n\
100  If this option is used, tests results will be logged \n\
101  into a filename specified with -f.\n\
102 \n\
103  -h\n\
104  Print the help.\n\n");
105 
106  if (badparam) {
107  fprintf(stderr, "ERROR: \n");
108  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
109  }
110 }
111 
119 bool getOptions(int argc, const char **argv, unsigned int &nb_matrices, unsigned int &nb_iterations,
120  bool &use_plot_file, std::string &plotfile, unsigned int &nbrows, unsigned int &nbcols, bool &verbose)
121 {
122  const char *optarg_;
123  int c;
124  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
125 
126  switch (c) {
127  case 'h':
128  usage(argv[0], NULL);
129  return false;
130  break;
131  case 'n':
132  nb_matrices = (unsigned int)atoi(optarg_);
133  break;
134  case 'i':
135  nb_iterations = (unsigned int)atoi(optarg_);
136  break;
137  case 'f':
138  plotfile = optarg_;
139  use_plot_file = true;
140  break;
141  case 'p':
142  use_plot_file = true;
143  break;
144  case 'R':
145  nbrows = (unsigned int)atoi(optarg_);
146  break;
147  case 'C':
148  nbcols = (unsigned int)atoi(optarg_);
149  break;
150  case 'v':
151  verbose = true;
152  break;
153  // add default options -c -d
154  case 'c':
155  break;
156  case 'd':
157  break;
158  default:
159  usage(argv[0], optarg_);
160  return false;
161  break;
162  }
163  }
164 
165  if ((c == 1) || (c == -1)) {
166  // standalone param or error
167  usage(argv[0], NULL);
168  std::cerr << "ERROR: " << std::endl;
169  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
170  return false;
171  }
172 
173  return true;
174 }
175 
176 vpMatrix make_random_matrix(unsigned int nbrows, unsigned int nbcols)
177 {
178  vpMatrix A;
179  A.resize(nbrows, nbcols);
180 
181  for (unsigned int i = 0; i < A.getRows(); i++) {
182  for (unsigned int j = 0; j < A.getCols(); j++) {
183  A[i][j] = (double)rand() / (double)RAND_MAX;
184  }
185  }
186 
187  return A;
188 }
189 
190 void create_bench_random_matrix(unsigned int nb_matrices, unsigned int nb_rows, unsigned int nb_cols, bool verbose,
191  std::vector<vpMatrix> &bench)
192 {
193  if (verbose)
194  std::cout << "Create a bench of " << nb_matrices << " " << nb_rows << " by " << nb_cols << " matrices" << std::endl;
195  bench.clear();
196  for (unsigned int i = 0; i < nb_matrices; i++) {
197  vpMatrix M = make_random_matrix(nb_rows, nb_cols);
198  bench.push_back(M);
199  }
200 }
201 
202 int test_pseudo_inverse(const std::vector<vpMatrix> &A, const std::vector<vpMatrix> &Api)
203 {
204  double allowed_error = 1e-3;
205 
206  for (unsigned int i = 0; i < A.size(); i++) {
207  double error = (A[i] * Api[i] * A[i] - A[i]).euclideanNorm();
208  if (error > allowed_error) {
209  std::cout << "Bad pseudo-inverse [" << i << "]: euclidean norm: " << error << std::endl;
210  return EXIT_FAILURE;
211  }
212  }
213  return EXIT_SUCCESS;
214 }
215 
216 int test_pseudo_inverse(const std::vector<vpMatrix> &A, const std::vector<vpMatrix> &Api,
217  const std::vector<vpColVector> &sv, const std::vector<vpMatrix> &imA,
218  const std::vector<vpMatrix> &imAt, const std::vector<vpMatrix> &kerAt)
219 {
220  double allowed_error = 1e-3;
221  // test Api
222  for (unsigned int i = 0; i < A.size(); i++) {
223  double error = (A[i] * Api[i] * A[i] - A[i]).euclideanNorm();
224  if (error > allowed_error) {
225  std::cout << "Bad pseudo-inverse [" << i << "]: euclidean norm: " << error << std::endl;
226  return EXIT_FAILURE;
227  }
228  }
229 
230  // test kerA
231  for (unsigned int i = 0; i < kerAt.size(); i++) {
232  if (kerAt[i].size()) {
233  vpMatrix nullspace = A[i] * kerAt[i].t();
234  double error = nullspace.euclideanNorm();
235 
236  if (error > allowed_error) {
237  std::cout << "Bad kernel [" << i << "]: euclidean norm: " << error << std::endl;
238  return EXIT_FAILURE;
239  }
240  }
241  }
242 
243  // test sv, imA, imAt, kerA
244  for (unsigned int i = 0; i < kerAt.size(); i++) {
245  unsigned int rank = imA[i].getCols();
246  vpMatrix U, S(rank, A[i].getCols()), Vt(A[i].getCols(), A[i].getCols());
247  U = imA[i];
248 
249  for (unsigned int j = 0; j < rank; j++)
250  S[j][j] = sv[i][j];
251 
252  Vt.insert(imAt[i].t(), 0, 0);
253  Vt.insert(kerAt[i], imAt[i].getCols(), 0);
254 
255  double error = (U * S * Vt - A[i]).euclideanNorm();
256 
257  if (error > allowed_error) {
258  std::cout << "Bad imA, imAt, sv, kerAt [" << i << "]: euclidean norm: " << error << std::endl;
259  return EXIT_FAILURE;
260  }
261  }
262 
263  return EXIT_SUCCESS;
264 }
265 
266 int test_pseudo_inverse_default(bool verbose, const std::vector<vpMatrix> &bench, std::vector<double> &time)
267 {
268  if (verbose)
269  std::cout << "Test pseudo-inverse using default 3rd party" << std::endl;
270  if (verbose)
271  std::cout << " Pseudo-inverse on a " << bench[0].getRows() << "x" << bench[0].getCols() << " matrix" << std::endl;
272 
273  size_t size = bench.size();
274  std::vector<vpMatrix> PI(size), imA(size), imAt(size), kerAt(size);
275  std::vector<vpColVector> sv(size);
276  int ret = EXIT_SUCCESS;
277 
278  // test 0
279  unsigned int test = 0;
280  double t = vpTime::measureTimeMs();
281  for (unsigned int i = 0; i < bench.size(); i++) {
282  PI[i] = bench[i].pseudoInverse();
283  }
284  time[test] = vpTime::measureTimeMs() - t;
285  for (unsigned int i = 0; i < time.size(); i++) {
286  ret += test_pseudo_inverse(bench, PI);
287  }
288 
289  // test 1
290  test++;
291  t = vpTime::measureTimeMs();
292  for (unsigned int i = 0; i < bench.size(); i++) {
293  bench[i].pseudoInverse(PI[i]);
294  }
295  time[test] = vpTime::measureTimeMs() - t;
296  for (unsigned int i = 0; i < time.size(); i++) {
297  ret += test_pseudo_inverse(bench, PI);
298  }
299 
300  // test 2
301  test++;
302  t = vpTime::measureTimeMs();
303  for (unsigned int i = 0; i < bench.size(); i++) {
304  bench[i].pseudoInverse(PI[i], sv[i]);
305  }
306  time[test] = vpTime::measureTimeMs() - t;
307  for (unsigned int i = 0; i < time.size(); i++) {
308  ret += test_pseudo_inverse(bench, PI);
309  }
310 
311  // test 3
312  test++;
313  t = vpTime::measureTimeMs();
314  for (unsigned int i = 0; i < bench.size(); i++) {
315  bench[i].pseudoInverse(PI[i], sv[i], 1e-6, imA[i], imAt[i], kerAt[i]);
316  }
317  time[test] = vpTime::measureTimeMs() - t;
318 
319  for (unsigned int i = 0; i < time.size(); i++) {
320  ret += test_pseudo_inverse(bench, PI, sv, imA, imAt, kerAt);
321  }
322 
323  return ret;
324 }
325 
326 #if defined(VISP_HAVE_EIGEN3)
327 int test_pseudo_inverse_eigen3(bool verbose, const std::vector<vpMatrix> &bench, std::vector<double> &time)
328 {
329  if (verbose)
330  std::cout << "Test pseudo-inverse using Eigen3 3rd party" << std::endl;
331  if (verbose)
332  std::cout << " Pseudo-inverse on a " << bench[0].getRows() << "x" << bench[0].getCols() << " matrix" << std::endl;
333 
334  size_t size = bench.size();
335  std::vector<vpMatrix> PI(size), imA(size), imAt(size), kerAt(size);
336  std::vector<vpColVector> sv(size);
337  int ret = EXIT_SUCCESS;
338 
339  // test 0
340  unsigned int test = 0;
341  double t = vpTime::measureTimeMs();
342  for (unsigned int i = 0; i < bench.size(); i++) {
343  PI[i] = bench[i].pseudoInverseEigen3();
344  }
345  time[test] = vpTime::measureTimeMs() - t;
346  for (unsigned int i = 0; i < time.size(); i++) {
347  ret += test_pseudo_inverse(bench, PI);
348  }
349 
350  // test 1
351  test++;
352  t = vpTime::measureTimeMs();
353  for (unsigned int i = 0; i < bench.size(); i++) {
354  bench[i].pseudoInverseEigen3(PI[i]);
355  }
356  time[test] = vpTime::measureTimeMs() - t;
357  for (unsigned int i = 0; i < time.size(); i++) {
358  ret += test_pseudo_inverse(bench, PI);
359  }
360 
361  // test 2
362  test++;
363  t = vpTime::measureTimeMs();
364  for (unsigned int i = 0; i < bench.size(); i++) {
365  bench[i].pseudoInverseEigen3(PI[i], sv[i]);
366  }
367  time[test] = vpTime::measureTimeMs() - t;
368  for (unsigned int i = 0; i < time.size(); i++) {
369  ret += test_pseudo_inverse(bench, PI);
370  }
371 
372  // test 3
373  test++;
374  t = vpTime::measureTimeMs();
375  for (unsigned int i = 0; i < bench.size(); i++) {
376  bench[i].pseudoInverseEigen3(PI[i], sv[i], 1e-6, imA[i], imAt[i], kerAt[i]);
377  }
378  time[test] = vpTime::measureTimeMs() - t;
379 
380  for (unsigned int i = 0; i < time.size(); i++) {
381  ret += test_pseudo_inverse(bench, PI, sv, imA, imAt, kerAt);
382  }
383 
384  return ret;
385 }
386 #endif
387 
388 #if defined(VISP_HAVE_LAPACK)
389 int test_pseudo_inverse_lapack(bool verbose, const std::vector<vpMatrix> &bench, std::vector<double> &time)
390 {
391  if (verbose)
392  std::cout << "Test pseudo-inverse using Lapack 3rd party" << std::endl;
393  if (verbose)
394  std::cout << " Pseudo-inverse on a " << bench[0].getRows() << "x" << bench[0].getCols() << " matrix" << std::endl;
395 
396  size_t size = bench.size();
397  std::vector<vpMatrix> PI(size), imA(size), imAt(size), kerAt(size);
398  std::vector<vpColVector> sv(size);
399  int ret = EXIT_SUCCESS;
400 
401  // test 0
402  unsigned int test = 0;
403  double t = vpTime::measureTimeMs();
404  for (unsigned int i = 0; i < bench.size(); i++) {
405  PI[i] = bench[i].pseudoInverseLapack();
406  }
407  time[test] = vpTime::measureTimeMs() - t;
408  for (unsigned int i = 0; i < time.size(); i++) {
409  ret += test_pseudo_inverse(bench, PI);
410  }
411 
412  // test 1
413  test++;
414  t = vpTime::measureTimeMs();
415  for (unsigned int i = 0; i < bench.size(); i++) {
416  bench[i].pseudoInverseLapack(PI[i]);
417  }
418  time[test] = vpTime::measureTimeMs() - t;
419  for (unsigned int i = 0; i < time.size(); i++) {
420  ret += test_pseudo_inverse(bench, PI);
421  }
422 
423  // test 2
424  test++;
425  t = vpTime::measureTimeMs();
426  for (unsigned int i = 0; i < bench.size(); i++) {
427  bench[i].pseudoInverseLapack(PI[i], sv[i]);
428  }
429  time[test] = vpTime::measureTimeMs() - t;
430  for (unsigned int i = 0; i < time.size(); i++) {
431  ret += test_pseudo_inverse(bench, PI);
432  }
433 
434  // test 3
435  test++;
436  t = vpTime::measureTimeMs();
437  for (unsigned int i = 0; i < bench.size(); i++) {
438  bench[i].pseudoInverseLapack(PI[i], sv[i], 1e-6, imA[i], imAt[i], kerAt[i]);
439  }
440  time[test] = vpTime::measureTimeMs() - t;
441 
442  for (unsigned int i = 0; i < time.size(); i++) {
443  ret += test_pseudo_inverse(bench, PI, sv, imA, imAt, kerAt);
444  }
445 
446  return ret;
447 }
448 #endif
449 
450 #if defined(VISP_HAVE_GSL)
451 int test_pseudo_inverse_gsl(bool verbose, const std::vector<vpMatrix> &bench, std::vector<double> &time)
452 {
453  if (verbose)
454  std::cout << "Test pseudo-inverse using Gsl 3rd party" << std::endl;
455  if (verbose)
456  std::cout << " Pseudo-inverse on a " << bench[0].getRows() << "x" << bench[0].getCols() << " matrix" << std::endl;
457 
458  size_t size = bench.size();
459  std::vector<vpMatrix> PI(size), imA(size), imAt(size), kerAt(size);
460  std::vector<vpColVector> sv(size);
461  int ret = EXIT_SUCCESS;
462 
463  // test 0
464  unsigned int test = 0;
465  double t = vpTime::measureTimeMs();
466  for (unsigned int i = 0; i < bench.size(); i++) {
467  PI[i] = bench[i].pseudoInverseGsl();
468  }
469  time[test] = vpTime::measureTimeMs() - t;
470  for (unsigned int i = 0; i < time.size(); i++) {
471  ret += test_pseudo_inverse(bench, PI);
472  }
473 
474  // test 1
475  test++;
476  t = vpTime::measureTimeMs();
477  for (unsigned int i = 0; i < bench.size(); i++) {
478  bench[i].pseudoInverseGsl(PI[i]);
479  }
480  time[test] = vpTime::measureTimeMs() - t;
481  for (unsigned int i = 0; i < time.size(); i++) {
482  ret += test_pseudo_inverse(bench, PI);
483  }
484 
485  // test 2
486  test++;
487  t = vpTime::measureTimeMs();
488  for (unsigned int i = 0; i < bench.size(); i++) {
489  bench[i].pseudoInverseGsl(PI[i], sv[i]);
490  }
491  time[test] = vpTime::measureTimeMs() - t;
492  for (unsigned int i = 0; i < time.size(); i++) {
493  ret += test_pseudo_inverse(bench, PI);
494  }
495 
496  // test 3
497  test++;
498  t = vpTime::measureTimeMs();
499  for (unsigned int i = 0; i < bench.size(); i++) {
500  bench[i].pseudoInverseGsl(PI[i], sv[i], 1e-6, imA[i], imAt[i], kerAt[i]);
501  }
502  time[test] = vpTime::measureTimeMs() - t;
503 
504  for (unsigned int i = 0; i < time.size(); i++) {
505  ret += test_pseudo_inverse(bench, PI, sv, imA, imAt, kerAt);
506  }
507 
508  return ret;
509 }
510 #endif
511 
512 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
513 int test_pseudo_inverse_opencv(bool verbose, const std::vector<vpMatrix> &bench, std::vector<double> &time)
514 {
515  if (verbose)
516  std::cout << "Test pseudo-inverse using OpenCV 3rd party" << std::endl;
517  if (verbose)
518  std::cout << " Pseudo-inverse on a " << bench[0].getRows() << "x" << bench[0].getCols() << " matrix" << std::endl;
519 
520  size_t size = bench.size();
521  std::vector<vpMatrix> PI(size), imA(size), imAt(size), kerAt(size);
522  std::vector<vpColVector> sv(size);
523  int ret = EXIT_SUCCESS;
524 
525  // test 0
526  unsigned int test = 0;
527  double t = vpTime::measureTimeMs();
528  for (unsigned int i = 0; i < bench.size(); i++) {
529  PI[i] = bench[i].pseudoInverseOpenCV();
530  }
531  time[test] = vpTime::measureTimeMs() - t;
532  for (unsigned int i = 0; i < time.size(); i++) {
533  ret += test_pseudo_inverse(bench, PI);
534  }
535 
536  // test 1
537  test++;
538  t = vpTime::measureTimeMs();
539  for (unsigned int i = 0; i < bench.size(); i++) {
540  bench[i].pseudoInverseOpenCV(PI[i]);
541  }
542  time[test] = vpTime::measureTimeMs() - t;
543  for (unsigned int i = 0; i < time.size(); i++) {
544  ret += test_pseudo_inverse(bench, PI);
545  }
546 
547  // test 2
548  test++;
549  t = vpTime::measureTimeMs();
550  for (unsigned int i = 0; i < bench.size(); i++) {
551  bench[i].pseudoInverseOpenCV(PI[i], sv[i]);
552  }
553  time[test] = vpTime::measureTimeMs() - t;
554  for (unsigned int i = 0; i < time.size(); i++) {
555  ret += test_pseudo_inverse(bench, PI);
556  }
557 
558  // test 3
559  test++;
560  t = vpTime::measureTimeMs();
561  for (unsigned int i = 0; i < bench.size(); i++) {
562  bench[i].pseudoInverseOpenCV(PI[i], sv[i], 1e-6, imA[i], imAt[i], kerAt[i]);
563  }
564  time[test] = vpTime::measureTimeMs() - t;
565 
566  for (unsigned int i = 0; i < time.size(); i++) {
567  ret += test_pseudo_inverse(bench, PI, sv, imA, imAt, kerAt);
568  }
569 
570  return ret;
571 }
572 #endif
573 
574 void save_time(const std::string &method, unsigned int nrows, unsigned int ncols, bool verbose, bool use_plot_file,
575  std::ofstream &of, const std::vector<double> &time)
576 {
577  for (size_t i = 0; i < time.size(); i++) {
578  if (use_plot_file)
579  of << time[i] << "\t";
580  if (verbose) {
581  std::cout << " " << method << " svd(" << nrows << "x" << ncols << ")"
582  << " test " << i << ": " << time[i] << std::endl;
583  }
584  }
585 }
586 
587 int main(int argc, const char *argv[])
588 {
589  try {
590 #if defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_LAPACK) || (VISP_HAVE_OPENCV_VERSION >= 0x020101) || \
591  defined(VISP_HAVE_GSL)
592  unsigned int nb_matrices = 10;
593  unsigned int nb_iterations = 10;
594  unsigned int nb_rows = 12;
595  unsigned int nb_cols = 6;
596  bool verbose = false;
597  std::string plotfile("plot-pseudo-inv.csv");
598  bool use_plot_file = false;
599  std::ofstream of;
600 
601  unsigned int nb_svd_functions = 4; // 4 tests for each existing vpMatrix::pseudoInverse(...) functions
602  unsigned int nb_test_matrix_size = 3; // 3 tests: m > n, m = n, m < n
603  std::vector<double> time(nb_svd_functions);
604  std::vector<unsigned int> nrows(nb_test_matrix_size), ncols(nb_test_matrix_size);
605 
606  // Read the command line options
607  if (getOptions(argc, argv, nb_matrices, nb_iterations, use_plot_file, plotfile, nb_rows, nb_cols, verbose) ==
608  false) {
609  exit(-1);
610  }
611 
612  for (unsigned int s = 0; s < nb_test_matrix_size; s++) {
613  // consider m > n, m = n, m < n
614  if (s == 0) {
615  nrows[s] = nb_rows;
616  ncols[s] = nb_cols;
617  } else if (s == 1) {
618  nrows[s] = nb_cols;
619  ncols[s] = nb_cols;
620  } else {
621  nrows[s] = nb_cols;
622  ncols[s] = nb_rows;
623  }
624  }
625 
626  if (use_plot_file) {
627  of.open(plotfile.c_str());
628  of << "iter"
629  << "\t";
630 
631  for (unsigned int s = 0; s < nb_test_matrix_size; s++) {
632  for (unsigned int i = 0; i < nb_svd_functions; i++)
633  of << "\"default " << nrows[s] << "x" << ncols[s] << " test " << i << "\""
634  << "\t";
635 
636 #if defined(VISP_HAVE_LAPACK)
637  for (unsigned int i = 0; i < nb_svd_functions; i++)
638  of << "\"Lapack " << nrows[s] << "x" << ncols[s] << " test " << i << "\""
639  << "\t";
640 #endif
641 #if defined(VISP_HAVE_EIGEN3)
642  for (unsigned int i = 0; i < nb_svd_functions; i++)
643  of << "\"Eigen3 " << nrows[s] << "x" << ncols[s] << " test " << i << "\""
644  << "\t";
645 #endif
646 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
647  for (unsigned int i = 0; i < nb_svd_functions; i++)
648  of << "\"OpenCV " << nrows[s] << "x" << ncols[s] << " test " << i << "\""
649  << "\t";
650 #endif
651 #if defined(VISP_HAVE_GSL)
652  for (unsigned int i = 0; i < nb_svd_functions; i++)
653  of << "\"GSL " << nrows[s] << "x" << ncols[s] << " test " << i << "\""
654  << "\t";
655 #endif
656  }
657  of << std::endl;
658  }
659 
660  int ret = EXIT_SUCCESS;
661  for (unsigned int iter = 0; iter < nb_iterations; iter++) {
662 
663  if (use_plot_file)
664  of << iter << "\t";
665 
666  for (unsigned int s = 0; s < nb_test_matrix_size; s++) {
667  std::vector<vpMatrix> bench_random_matrices;
668  create_bench_random_matrix(nb_matrices, nrows[s], ncols[s], verbose, bench_random_matrices);
669 
670  ret += test_pseudo_inverse_default(verbose, bench_random_matrices, time);
671  save_time("default -", nrows[s], ncols[s], verbose, use_plot_file, of, time);
672 
673 #if defined(VISP_HAVE_LAPACK)
674  ret += test_pseudo_inverse_lapack(verbose, bench_random_matrices, time);
675  save_time("Lapack -", nrows[s], ncols[s], verbose, use_plot_file, of, time);
676 #endif
677 
678 #if defined(VISP_HAVE_EIGEN3)
679  ret += test_pseudo_inverse_eigen3(verbose, bench_random_matrices, time);
680  save_time("Eigen3 -", nrows[s], ncols[s], verbose, use_plot_file, of, time);
681 #endif
682 
683 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
684  ret += test_pseudo_inverse_opencv(verbose, bench_random_matrices, time);
685  save_time("OpenCV -", nrows[s], ncols[s], verbose, use_plot_file, of, time);
686 #endif
687 
688 #if defined(VISP_HAVE_GSL)
689  ret += test_pseudo_inverse_gsl(verbose, bench_random_matrices, time);
690  save_time("GSL -", nrows[s], ncols[s], verbose, use_plot_file, of, time);
691 #endif
692  }
693  if (use_plot_file)
694  of << std::endl;
695  }
696  if (use_plot_file) {
697  of.close();
698  std::cout << "Result saved in " << plotfile << std::endl;
699  }
700 
701  if (ret == EXIT_SUCCESS) {
702  std::cout << "Test succeed" << std::endl;
703  } else {
704  std::cout << "Test failed" << std::endl;
705  }
706 
707  return ret;
708 #else
709  (void)argc;
710  (void)argv;
711  std::cout << "Test does nothing since you dont't have Eigen3, Lapack, "
712  "OpenCV or GSL 3rd party"
713  << std::endl;
714  return EXIT_SUCCESS;
715 #endif
716  } catch (const vpException &e) {
717  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
718  return EXIT_FAILURE;
719  }
720 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
Definition: vpArray2D.h:171
error that can be emited by ViSP classes.
Definition: vpException.h:71
unsigned int getCols() const
Definition: vpArray2D.h:146
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void insert(const vpMatrix &A, const unsigned int r, const unsigned int c)
Definition: vpMatrix.cpp:4570
unsigned int getRows() const
Definition: vpArray2D.h:156
vpMatrix t() const
Definition: vpMatrix.cpp:375
double euclideanNorm() const
Definition: vpMatrix.cpp:5096
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92