Test various svd decompositions.
#include <visp3/core/vpTime.h>
#include <visp3/core/vpMatrix.h>
#include <visp3/core/vpColVector.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
bool testSvdOpenCvGSLCoherence(double epsilon);
#ifdef VISP_HAVE_GSL
bool testRandom(double epsilon);
#endif
#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()
{
try {
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;
}
}