Test various svd decompositions.
#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>
#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;
switch (c) {
case 'h': usage(argv[0], NULL); return false; break;
default:
usage(argv[0], optarg_);
return false; break;
}
}
if ((c == 1) || (c == -1)) {
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)
{
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;
vpMatrix V0(L0.getCols(), L0.getCols()) ;
vpMatrix V1(L1.getCols(), L1.getCols()) ;
L0.svdNr(W0,V0);
L1.svdGsl(W1,V1);
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
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 {
if (getOptions(argc, argv) == false) {
exit (-1);
}
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)) ;
Ls = L ;
std::cout << "--------------------------------------"<<std::endl ;
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;
}
std::cout << "Catch an exception: " << e << std::endl;
return 1;
}
}