44 #include <visp3/core/vpMatrix.h> 45 #include <visp3/core/vpTime.h> 46 #include <visp3/io/vpParseArgv.h> 49 #define GETOPTARGS "cdn:i:pf:R:C:vh" 59 void usage(
const char *name,
const char *badparam)
62 Test matrix inversions\n\ 63 using LU, QR and Cholesky methods as well as Pseudo-inverse.\n\ 64 Outputs a comparison of these methods.\n\ 67 %s [-n <number of matrices>] [-f <plot filename>]\n\ 68 [-R <number of rows>] [-C <number of columns>]\n\ 69 [-i <number of iterations>] [-p] [-h]\n", name);
73 -n <number of matrices> \n\ 74 Number of matrices inverted during each test loop.\n\ 76 -i <number of iterations> \n\ 77 Number of iterations of the test.\n\ 79 -f <plot filename> \n\ 80 Set output path for plot output.\n\ 81 The plot logs the times of \n\ 82 the different inversion methods: \n\ 83 QR,LU,Cholesky and Pseudo-inverse.\n\ 85 -R <number of rows>\n\ 86 Number of rows of the automatically generated matrices \n\ 89 -C <number of columns>\n\ 90 Number of colums of the automatically generated matrices \n\ 94 Plot into filename in the gnuplot format. \n\ 95 If this option is used, tests results will be logged \n\ 96 into a filename specified with -f.\n\ 99 Print the help.\n\n");
102 fprintf(stderr,
"ERROR: \n");
103 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
114 bool getOptions(
int argc,
const char **argv,
unsigned int &nb_matrices,
unsigned int &nb_iterations,
115 bool &use_plot_file, std::string &plotfile,
unsigned int &nbrows,
unsigned int &nbcols,
bool &verbose)
123 usage(argv[0], NULL);
127 nb_matrices = (
unsigned int)atoi(optarg_);
130 nb_iterations = (
unsigned int)atoi(optarg_);
134 use_plot_file =
true;
137 use_plot_file =
true;
140 nbrows = (
unsigned int)atoi(optarg_);
143 nbcols = (
unsigned int)atoi(optarg_);
154 usage(argv[0], optarg_);
160 if ((c == 1) || (c == -1)) {
162 usage(argv[0], NULL);
163 std::cerr <<
"ERROR: " << std::endl;
164 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
171 vpMatrix make_random_matrix(
unsigned int nbrows,
unsigned int nbcols)
176 for (
unsigned int i = 0; i < A.
getRows(); i++)
177 for (
unsigned int j = 0; j < A.
getCols(); j++)
178 A[i][j] = (
double)rand() / (double)RAND_MAX;
182 void create_bench(
unsigned int nb_matrices,
unsigned int nb_rows,
unsigned int nb_cols,
bool verbose,
183 std::vector<vpMatrix> &bench)
186 std::cout <<
"Create a bench of " << nb_matrices <<
" " << nb_rows <<
" by " << nb_cols <<
" matrices" << std::endl;
188 for (
unsigned int i = 0; i < nb_matrices; i++) {
189 vpMatrix M = make_random_matrix(nb_rows, nb_cols);
194 void test_det_default(
bool verbose,
const std::vector<vpMatrix> &bench,
double &time, std::vector<double> &result)
197 std::cout <<
"Test determinant using default method" << std::endl;
200 std::cout <<
" Matrix size: " << bench[0].AtA().getRows() <<
"x" << bench[0].AtA().getCols() << std::endl;
202 result.resize(bench.size());
204 for (
unsigned int i = 0; i < bench.size(); i++) {
205 result[i] = bench[i].AtA().det();
210 #if defined(VISP_HAVE_EIGEN3) 211 void test_det_eigen3(
bool verbose,
const std::vector<vpMatrix> &bench,
double &time, std::vector<double> &result)
214 std::cout <<
"Test determinant using Eigen3 3rd party" << std::endl;
217 std::cout <<
" Matrix size: " << bench[0].AtA().getRows() <<
"x" << bench[0].AtA().getCols() << std::endl;
219 result.resize(bench.size());
221 for (
unsigned int i = 0; i < bench.size(); i++) {
222 result[i] = bench[i].AtA().detByLUEigen3();
228 #if defined(VISP_HAVE_GSL) 229 void test_det_gsl(
bool verbose,
const std::vector<vpMatrix> &bench,
double &time, std::vector<double> &result)
232 std::cout <<
"Test determinant using GSL 3rd party" << std::endl;
235 std::cout <<
" Matrix size: " << bench[0].AtA().getRows() <<
"x" << bench[0].AtA().getCols() << std::endl;
237 result.resize(bench.size());
239 for (
unsigned int i = 0; i < bench.size(); i++) {
240 result[i] = bench[i].AtA().detByLUGsl();
246 #if defined(VISP_HAVE_LAPACK) 247 void test_det_lapack(
bool verbose,
const std::vector<vpMatrix> &bench,
double &time, std::vector<double> &result)
250 std::cout <<
"Test determinant using Lapack 3rd party" << std::endl;
253 std::cout <<
" Matrix size: " << bench[0].AtA().getRows() <<
"x" << bench[0].AtA().getCols() << std::endl;
255 result.resize(bench.size());
257 for (
unsigned int i = 0; i < bench.size(); i++) {
258 result[i] = bench[i].AtA().detByLULapack();
264 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) 265 void test_det_opencv(
bool verbose,
const std::vector<vpMatrix> &bench,
double &time, std::vector<double> &result)
268 std::cout <<
"Test determinant using OpenCV 3rd party" << std::endl;
271 std::cout <<
" Matrix size: " << bench[0].AtA().getRows() <<
"x" << bench[0].AtA().getCols() << std::endl;
273 result.resize(bench.size());
275 for (
unsigned int i = 0; i < bench.size(); i++) {
276 result[i] = bench[i].AtA().detByLUOpenCV();
282 void save_time(
const std::string &method,
bool verbose,
bool use_plot_file, std::ofstream &of,
double time)
286 if (verbose || !use_plot_file) {
287 std::cout << method << time << std::endl;
291 int main(
int argc,
const char *argv[])
294 #if defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_GSL) || defined(VISP_HAVE_LAPACK) || \ 295 (VISP_HAVE_OPENCV_VERSION >= 0x020101) 296 unsigned int nb_matrices = 1000;
297 unsigned int nb_iterations = 10;
298 unsigned int nb_rows = 6;
299 unsigned int nb_cols = 6;
300 bool verbose =
false;
301 std::string plotfile(
"plot-det.csv");
302 bool use_plot_file =
false;
306 if (getOptions(argc, argv, nb_matrices, nb_iterations, use_plot_file, plotfile, nb_rows, nb_cols, verbose) ==
312 of.open(plotfile.c_str());
316 of <<
"\"Determinant default\"" 319 #if defined(VISP_HAVE_LAPACK) 320 of <<
"\"Determinant Lapack\"" 323 #if defined(VISP_HAVE_EIGEN3) 324 of <<
"\"Determinant Eigen3\"" 327 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) 328 of <<
"\"Determinant OpenCV\"" 331 #if defined(VISP_HAVE_GSL) 332 of <<
"\"Determinant GSL\"" 338 int ret = EXIT_SUCCESS;
339 for (
unsigned int iter = 0; iter < nb_iterations; iter++) {
340 std::vector<vpMatrix> bench;
341 create_bench(nb_matrices, nb_rows, nb_cols, verbose, bench);
348 std::vector<double> result_default;
349 test_det_default(verbose, bench, time, result_default);
350 save_time(
"Determinant default: ", verbose, use_plot_file, of, time);
352 #if defined(VISP_HAVE_LAPACK) 353 std::vector<double> result_lapack;
354 test_det_lapack(verbose, bench, time, result_lapack);
355 save_time(
"Determinant by Lapack: ", verbose, use_plot_file, of, time);
358 #if defined(VISP_HAVE_EIGEN3) 359 std::vector<double> result_eigen3;
360 test_det_eigen3(verbose, bench, time, result_eigen3);
361 save_time(
"Determinant by Eigen3: ", verbose, use_plot_file, of, time);
364 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) 365 std::vector<double> result_opencv;
366 test_det_opencv(verbose, bench, time, result_opencv);
367 save_time(
"Determinant by OpenCV: ", verbose, use_plot_file, of, time);
370 #if defined(VISP_HAVE_GSL) 371 std::vector<double> result_gsl;
372 test_det_gsl(verbose, bench, time, result_gsl);
373 save_time(
"Determinant by GSL: ", verbose, use_plot_file, of, time);
379 #if defined(VISP_HAVE_GSL) && (VISP_HAVE_OPENCV_VERSION >= 0x020101) 381 for (
unsigned int i = 0; i < bench.size(); i++) {
382 if (std::fabs(result_gsl[i] - result_opencv[i]) > 1e-6) {
383 std::cout <<
"Determinant differ between GSL and OpenCV: " << result_gsl[i] <<
" " << result_opencv[i]
389 #if defined(VISP_HAVE_GSL) && defined(VISP_HAVE_LAPACK) 391 for (
unsigned int i = 0; i < bench.size(); i++) {
392 if (std::fabs(result_gsl[i] - result_lapack[i]) > 1e-6) {
393 std::cout <<
"Determinant differ between GSL and Lapack: " << result_gsl[i] <<
" " << result_lapack[i]
399 #if defined(VISP_HAVE_GSL) && defined(VISP_HAVE_EIGEN3) 401 for (
unsigned int i = 0; i < bench.size(); i++) {
402 if (std::fabs(result_gsl[i] - result_eigen3[i]) > 1e-6) {
403 std::cout <<
"Determinant differ between GSL and Eigen3: " << result_gsl[i] <<
" " << result_eigen3[i]
409 #if defined(VISP_HAVE_LAPACK) && (VISP_HAVE_OPENCV_VERSION >= 0x020101) 411 for (
unsigned int i = 0; i < bench.size(); i++) {
412 if (std::fabs(result_lapack[i] - result_opencv[i]) > 1e-6) {
413 std::cout <<
"Determinant differ between Lapack and OpenCV: " << result_lapack[i] <<
" " << result_opencv[i]
419 #if defined(VISP_HAVE_EIGEN3) && (VISP_HAVE_OPENCV_VERSION >= 0x020101) 421 for (
unsigned int i = 0; i < bench.size(); i++) {
422 if (std::fabs(result_eigen3[i] - result_opencv[i]) > 1e-6) {
423 std::cout <<
"Determinant differ between Eigen3 and OpenCV: " << result_eigen3[i] <<
" " << result_opencv[i]
429 #if defined(VISP_HAVE_EIGEN3) && defined(VISP_HAVE_LAPACK) 431 for (
unsigned int i = 0; i < bench.size(); i++) {
432 if (std::fabs(result_eigen3[i] - result_lapack[i]) > 1e-6) {
433 std::cout <<
"Determinant differ between Eigen3 and Lapack: " << result_eigen3[i] <<
" " << result_lapack[i]
442 std::cout <<
"Result saved in " << plotfile << std::endl;
445 if (ret == EXIT_SUCCESS) {
446 std::cout <<
"Test succeed" << std::endl;
448 std::cout <<
"Test failed" << std::endl;
455 std::cout <<
"Test does nothing since you dont't have Eigen3, Lapack, " 456 "OpenCV or GSL 3rd party" Implementation of a matrix and operations on matrices.
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true, const bool recopy_=true)
error that can be emited by ViSP classes.
unsigned int getCols() const
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
unsigned int getRows() const
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).