ViSP  2.9.0
testSvd.cpp

Test various svd decompositions.

/****************************************************************************
*
* $Id: testSvd.cpp 4658 2014-02-09 09:50:14Z fspindle $
*
* This file is part of the ViSP software.
* Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact INRIA about acquiring a ViSP Professional
* Edition License.
*
* See http://www.irisa.fr/lagadic/visp/visp.html for more information.
*
* This software was developed at:
* INRIA Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
* http://www.irisa.fr/lagadic
*
* If you have questions regarding the use of this file, please contact
* INRIA at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*
* Description:
* Test various svd decompositions.
*
* Authors:
* Eric Marchand
* Fabien Spindler
*
*****************************************************************************/
#include <visp/vpTime.h>
#include <visp/vpMatrix.h>
#include <visp/vpColVector.h>
#include <visp/vpParseArgv.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
// List of allowed command line options
#define GETOPTARGS "h"
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv);
bool testSvdOpenCvGSLCoherence(double epsilon);
#ifdef VISP_HAVE_GSL
bool testRandom(double epsilon);
#endif
void usage(const char *name, const char *badparam)
{
fprintf(stdout, "\n\
Test various svd decompositions.\n\
\n\
SYNOPSIS\n\
%s [-h]\n", name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-h\n\
Print the help.\n");
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, const char **argv)
{
const char *optarg_;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
switch (c) {
case 'h': usage(argv[0], NULL); return false; break;
default:
usage(argv[0], optarg_);
return false; break;
}
}
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], NULL);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
#define abs(x) ((x) < 0 ? - (x) : (x))
#ifdef VISP_HAVE_GSL
bool testRandom(double epsilon)
{
vpMatrix L0(6,6);
vpMatrix L1(6,6);
for (unsigned int i=0 ; i < L0.getRows() ; i++)
for (unsigned int j=0 ; j < L0.getCols() ; j++)
L1[i][j] = L0[i][j] = (double)rand()/(double)RAND_MAX;
vpColVector W0(L0.getCols()) ;
vpMatrix V0(L0.getCols(), L0.getCols()) ;
vpColVector W1(L1.getCols()) ;
vpMatrix V1(L1.getCols(), L1.getCols()) ;
L0.svdNr(W0,V0);
L1.svdGsl(W1,V1);
vpColVector diff = _W0-_W1;
double error=-1.0;
for(unsigned int i=0;i<6;i++)
error=std::max(abs(diff[i]),error);
return error<epsilon;
}
#endif
bool testSvdOpenCvGSLCoherence(double epsilon)
{
#if (VISP_HAVE_OPENCV_VERSION >= 0x020101) && defined (VISP_HAVE_GSL) // Require opencv >= 2.1.1
A.resize(6,6);
B.resize(6,6);
vA.resize(6,6);
vB.resize(6,6);
wA.resize(6);
wB.resize(6);
for (unsigned int i=0 ; i < A.getRows() ; i++)
for (unsigned int j=0 ; j < A.getCols() ; j++)
B[i][j] = A[i][j] = (double)rand()/(double)RAND_MAX;
A.svdOpenCV(wA,vA);
B.svdGsl(wB,vB);
bool error=false;
for (unsigned int i=0 ; i < A.getRows() ; i++){
error = error | (abs(wA[i]-wB[i])>epsilon);
}
return !error;
#else
(void)epsilon;
return true;
#endif
}
int
main(int argc, const char ** argv)
{
try {
// Read the command line options
if (getOptions(argc, argv) == false) {
exit (-1);
}
vpMatrix L(60000,6), Ls ;
for (unsigned int i=0 ; i < L.getRows() ; i++)
for (unsigned int j=0 ; j < L.getCols() ; j++)
L[i][j] = 2*i+j + cos((double)(i+j))+((double)(i)) ;
// std::cout << L << std::endl ;
Ls = L ;
std::cout << "--------------------------------------"<<std::endl ;
vpColVector W(L.getCols()) ;
vpMatrix V(L.getCols(), L.getCols()) ;
double t = vpTime::measureTimeMs() ;
L.svdNr(W,V) ;
std::cout <<"svdNr Numerical recipes \n time " <<t << std::endl;
std::cout << W.t() ;
std::cout << "--------------------------------------"<<std::endl ;
#ifdef VISP_HAVE_GSL
L = Ls ;
L.svdGsl(W,V) ;
std::cout <<"svdGsl_mod \n time " <<t << std::endl;
std::cout << W.t() ;
std::cout << "--------------------------------------"<<std::endl ;
std::cout << "TESTING RANDOM MATRICES:" ;
bool ret = true;
for(unsigned int i=0;i<2000;i++)
ret = ret & testRandom(0.00001);
if(ret)
std:: cout << "Success"<< std:: endl;
else
std:: cout << "Fail"<< std:: endl;
std::cout << "--------------------------------------"<<std::endl ;
#endif
std::cout << "--------------------------------------"<<std::endl ;
std::cout << "TESTING OPENCV-GSL coherence:" ;
bool ret2 = true;
for(unsigned int i=0;i<1;i++)
ret2 = ret2 & testSvdOpenCvGSLCoherence(0.00001);
if(ret2)
std:: cout << "Success"<< std:: endl;
else
std:: cout << "Fail"<< std:: endl;
std::cout << "--------------------------------------"<<std::endl ;
L = Ls ;
L.svdFlake(W,V) ;
std::cout <<"svdFlake\n time " <<t << std::endl;
std::cout << W.t() ;
return 0;
}
catch(vpException e) {
std::cout << "Catch an exception: " << e << std::endl;
return 1;
}
}