Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
testMatrix.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 some vpMatrix functionalities.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpDebug.h>
47 #include <visp3/core/vpGEMM.h>
48 #include <visp3/core/vpHomogeneousMatrix.h>
49 #include <visp3/core/vpMath.h>
50 #include <visp3/core/vpVelocityTwistMatrix.h>
51 
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 namespace
56 {
57 bool test(const std::string &s, const vpMatrix &M, const std::vector<double> &bench)
58 {
59  static unsigned int cpt = 0;
60  std::cout << "** Test " << ++cpt << std::endl;
61  std::cout << s << "(" << M.getRows() << "," << M.getCols() << ") = \n" << M << std::endl;
62  if (bench.size() != M.size()) {
63  std::cout << "Test fails: bad size wrt bench" << std::endl;
64  return false;
65  }
66  for (unsigned int i = 0; i < M.size(); i++) {
67  if (std::fabs(M.data[i] - bench[i]) > std::fabs(M.data[i]) * std::numeric_limits<double>::epsilon()) {
68  std::cout << "Test fails: bad content" << std::endl;
69  return false;
70  }
71  }
72 
73  return true;
74 }
75 
76 double getRandomValues(double min, double max)
77 {
78  return (max - min) * ((double)rand() / (double)RAND_MAX) + min;
79 }
80 
81 bool equalMatrix(const vpMatrix &A, const vpMatrix &B, double tol = std::numeric_limits<double>::epsilon())
82 {
83  if (A.getRows() != B.getRows() || A.getCols() != B.getCols()) {
84  return false;
85  }
86 
87  for (unsigned int i = 0; i < A.getRows(); i++) {
88  for (unsigned int j = 0; j < A.getCols(); j++) {
89  if (!vpMath::equal(A[i][j], B[i][j], tol)) {
90  return false;
91  }
92  }
93  }
94 
95  return true;
96 }
97 
98 vpMatrix generateRandomMatrix(unsigned int rows, unsigned int cols, double min, double max)
99 {
100  vpMatrix M(rows, cols);
101 
102  for (unsigned int i = 0; i < M.getRows(); i++) {
103  for (unsigned int j = 0; j < M.getCols(); j++) {
104  M[i][j] = getRandomValues(min, max);
105  }
106  }
107 
108  return M;
109 }
110 }
111 
112 int main(int argc, char *argv[])
113 {
114  try {
115  bool ctest = true;
116  for (int i = 1; i < argc; i++) {
117  if (std::string(argv[i]) == "--benchmark") {
118  ctest = false;
119  }
120  }
121 
122  {
123  const double val = 10.0;
124  vpMatrix M, M2(5, 5, val);
125  M.resize(5, 5, false, false);
126  M = val;
127  for (unsigned int i = 0; i < M.getRows(); i++) {
128  for (unsigned int j = 0; j < M.getCols(); j++) {
129  if (!vpMath::equal(M[i][j], val, std::numeric_limits<double>::epsilon())) {
130  std::cerr << "Issue with matrix assignment with value." << std::endl;
131  return EXIT_FAILURE;
132  }
133 
134  if (!vpMath::equal(M2[i][j], val, std::numeric_limits<double>::epsilon())) {
135  std::cerr << "Issue with matrix constructor initialized with value." << std::endl;
136  return EXIT_FAILURE;
137  }
138  }
139  }
140  }
141  {
142  // Test vpRotationMatrix construction
143  std::vector<double> bench(9, 0);
144  bench[2] = bench[4] = bench[6] = 1.;
145  vpMatrix M(3, 3);
146  M[2][0] = M[1][1] = M[0][2] = 1.;
147  vpRotationMatrix R1(M);
148  if (test("R1", R1, bench) == false)
149  return EXIT_FAILURE;
150  vpRotationMatrix R2;
151  R2 = M;
152  if (test("R2", R2, bench) == false)
153  return EXIT_FAILURE;
154  }
155  {
156  vpColVector c(6, 1);
157  vpRowVector r(6, 1);
158  std::vector<double> bench(6, 1);
159  vpMatrix M1(c);
160  if (test("M1", M1, bench) == false)
161  return EXIT_FAILURE;
162  vpMatrix M2(r);
163  if (test("M2", M2, bench) == false)
164  return EXIT_FAILURE;
165  }
166  {
167  vpMatrix M(4, 5);
168  int val = 0;
169  for (unsigned int i = 0; i < M.getRows(); i++) {
170  for (unsigned int j = 0; j < M.getCols(); j++) {
171  M[i][j] = val++;
172  }
173  }
174  std::cout << "M ";
175  M.print(std::cout, 4);
176 
177  vpMatrix N;
178  N.init(M, 0, 1, 2, 3);
179  std::cout << "N ";
180  N.print(std::cout, 4);
181  std::string header("My 4-by-5 matrix\nwith a second line");
182 
183  // Save matrix in text format
184  if (vpMatrix::saveMatrix("matrix.mat", M, false, header.c_str()))
185  std::cout << "Matrix saved in matrix.mat file" << std::endl;
186  else
187  return EXIT_FAILURE;
188 
189  // Load matrix in text format
190  vpMatrix M1;
191  char header_[100];
192  if (vpMatrix::loadMatrix("matrix.mat", M1, false, header_))
193  std::cout << "Matrix loaded from matrix.mat file with header \"" << header_ << "\": \n" << M1 << std::endl;
194  else
195  return EXIT_FAILURE;
196  if (header != std::string(header_)) {
197  std::cout << "Bad header in matrix.mat" << std::endl;
198  return EXIT_FAILURE;
199  }
200 
201  // Save matrix in binary format
202  if (vpMatrix::saveMatrix("matrix.bin", M, true, header.c_str()))
203  std::cout << "Matrix saved in matrix.bin file" << std::endl;
204  else
205  return EXIT_FAILURE;
206 
207  // Load matrix in binary format
208  if (vpMatrix::loadMatrix("matrix.bin", M1, true, header_))
209  std::cout << "Matrix loaded from matrix.bin file with header \"" << header_ << "\": \n" << M1 << std::endl;
210  else
211  return EXIT_FAILURE;
212  if (header != std::string(header_)) {
213  std::cout << "Bad header in matrix.bin" << std::endl;
214  return EXIT_FAILURE;
215  }
216 
217  // Save matrix in YAML format
218  if (vpMatrix::saveMatrixYAML("matrix.yml", M, header.c_str()))
219  std::cout << "Matrix saved in matrix.yml file" << std::endl;
220  else
221  return EXIT_FAILURE;
222 
223  // Read matrix in YAML format
224  vpMatrix M2;
225  if (vpMatrix::loadMatrixYAML("matrix.yml", M2, header_))
226  std::cout << "Matrix loaded from matrix.yml file with header \"" << header_ << "\": \n" << M2 << std::endl;
227  else
228  return EXIT_FAILURE;
229  if (header != std::string(header_)) {
230  std::cout << "Bad header in matrix.mat" << std::endl;
231  return EXIT_FAILURE;
232  }
233  }
234 
235  {
237  std::cout << "R: \n" << R << std::endl;
238  vpMatrix M1(R);
239  std::cout << "M1: \n" << M1 << std::endl;
240  vpMatrix M2(M1);
241  std::cout << "M2: \n" << M2 << std::endl;
242  vpMatrix M3 = R;
243  std::cout << "M3: \n" << M3 << std::endl;
244  vpMatrix M4 = M1;
245  std::cout << "M4: \n" << M4 << std::endl;
246  }
247  {
248 
249  std::cout << "------------------------" << std::endl;
250  std::cout << "--- TEST PRETTY PRINT---" << std::endl;
251  std::cout << "------------------------" << std::endl;
252  vpMatrix M;
253  M.eye(4);
254 
255  std::cout << "call std::cout << M;" << std::endl;
256  std::cout << M << std::endl;
257 
258  std::cout << "call M.print (std::cout, 4);" << std::endl;
259  M.print(std::cout, 4);
260 
261  std::cout << "------------------------" << std::endl;
262  M.resize(3, 3);
263  M.eye(3);
264  M[1][0] = 1.235;
265  M[1][1] = 12.345;
266  M[1][2] = .12345;
267  std::cout << "call std::cout << M;" << std::endl;
268  std::cout << M;
269  std::cout << "call M.print (std::cout, 6);" << std::endl;
270  M.print(std::cout, 6);
271  std::cout << std::endl;
272 
273  std::cout << "------------------------" << std::endl;
274  M[0][0] = -1.235;
275  M[1][0] = -12.235;
276 
277  std::cout << "call std::cout << M;" << std::endl;
278  std::cout << M << std::endl;
279 
280  std::cout << "call M.print (std::cout, 10);" << std::endl;
281  M.print(std::cout, 10);
282  std::cout << std::endl;
283 
284  std::cout << "call M.print (std::cout, 2);" << std::endl;
285  M.print(std::cout, 2);
286  std::cout << std::endl;
287 
288  std::cout << "------------------------" << std::endl;
289  M.resize(3, 3);
290  M.eye(3);
291  M[0][2] = -0.0000000876;
292  std::cout << "call std::cout << M;" << std::endl;
293  std::cout << M << std::endl;
294 
295  std::cout << "call M.print (std::cout, 4);" << std::endl;
296  M.print(std::cout, 4);
297  std::cout << std::endl;
298  std::cout << "call M.print (std::cout, 10, \"M\");" << std::endl;
299  M.print(std::cout, 10, "M");
300  std::cout << std::endl;
301  std::cout << "call M.print (std::cout, 20, \"M\");" << std::endl;
302  M.print(std::cout, 20, "M");
303  std::cout << std::endl;
304 
305  std::cout << "------------------------" << std::endl;
306  std::cout << "--- TEST RESIZE --------" << std::endl;
307  std::cout << "------------------------" << std::endl;
308  std::cout << "5x5" << std::endl;
309  M.resize(5, 5, false);
310  std::cout << M << std::endl;
311  std::cout << "3x2" << std::endl;
312  M.resize(3, 2, false);
313  std::cout << M << std::endl;
314  std::cout << "2x2" << std::endl;
315  M.resize(2, 2, false);
316  std::cout << M << std::endl;
317  std::cout << "------------------------" << std::endl;
318 
320  vpMatrix A(1, 6), B;
321 
322  A = 1.0;
323  // vMe=1.0;
324  B = A * vMe;
325 
326  std::cout << "------------------------" << std::endl;
327  std::cout << "--- TEST vpRowVector * vpColVector" << std::endl;
328  std::cout << "------------------------" << std::endl;
329  vpRowVector r(3);
330  r[0] = 2;
331  r[1] = 3;
332  r[2] = 4;
333 
334  vpColVector c(3);
335  c[0] = 1;
336  c[1] = 2;
337  c[2] = -1;
338 
339  double rc = r * c;
340 
341  r.print(std::cout, 2, "r");
342  c.print(std::cout, 2, "c");
343  std::cout << "r * c = " << rc << std::endl;
344 
345  std::cout << "------------------------" << std::endl;
346  std::cout << "--- TEST vpRowVector * vpMatrix" << std::endl;
347  std::cout << "------------------------" << std::endl;
348  M.resize(3, 3);
349  M.eye(3);
350 
351  M[1][0] = 1.5;
352  M[2][0] = 2.3;
353 
354  vpRowVector rM = r * M;
355 
356  r.print(std::cout, 2, "r");
357  M.print(std::cout, 10, "M");
358  std::cout << "r * M = " << rM << std::endl;
359 
360  std::cout << "------------------------" << std::endl;
361  std::cout << "--- TEST vpGEMM " << std::endl;
362  std::cout << "------------------------" << std::endl;
363  M.resize(3, 3);
364  M.eye(3);
365  vpMatrix N(3, 3);
366  N[0][0] = 2;
367  N[1][0] = 1.2;
368  N[1][2] = 0.6;
369  N[2][2] = 0.25;
370 
371  vpMatrix C(3, 3);
372  C.eye(3);
373 
374  vpMatrix D;
375 
376  // realise the operation D = 2 * M^T * N + 3 C
377  vpGEMM(M, N, 2, C, 3, D, VP_GEMM_A_T);
378  std::cout << D << std::endl;
379  }
380 
381  {
382  std::cout << "------------------------" << std::endl;
383  std::cout << "--- TEST vpMatrix insert() with same colNum " << std::endl;
384  std::cout << "------------------------" << std::endl;
385  unsigned int nb = ctest ? 10 : 100; // 10000;
386  const unsigned int size = ctest ? 10 : 100;
387 
388  vpMatrix m_big(nb * size, 6);
389  std::vector<vpMatrix> submatrices(nb);
390  for (size_t cpt = 0; cpt < submatrices.size(); cpt++) {
391  vpMatrix m(size, 6);
392 
393  for (unsigned int i = 0; i < m.getRows(); i++) {
394  for (unsigned int j = 0; j < m.getCols(); j++) {
395  m[i][j] = getRandomValues(-100.0, 100.0);
396  }
397  }
398 
399  submatrices[cpt] = m;
400  }
401 
402  double t = vpTime::measureTimeMs();
403  for (unsigned int i = 0; i < nb; i++) {
404  m_big.insert(submatrices[(size_t)i], i * size, 0);
405  }
406  t = vpTime::measureTimeMs() - t;
407  std::cout << "Matrix insert(): " << t << " ms" << std::endl;
408 
409  for (unsigned int cpt = 0; cpt < nb; cpt++) {
410  for (unsigned int i = 0; i < size; i++) {
411  for (unsigned int j = 0; j < 6; j++) {
412  if (!vpMath::equal(m_big[cpt * size + i][j], submatrices[(size_t)cpt][i][j],
413  std::numeric_limits<double>::epsilon())) {
414  std::cerr << "Problem with vpMatrix insert()!" << std::endl;
415  return EXIT_FAILURE;
416  }
417  }
418  }
419  }
420 
421  // Try to insert empty matrices
422  vpMatrix m1(2, 3), m2, m3;
423  m1.insert(m2, 0, 0);
424  m3.insert(m2, 0, 0);
425 
426  std::cout << "Insert empty matrices:" << std::endl;
427  std::cout << "m1:\n" << m1 << std::endl;
428  std::cout << "m2:\n" << m2 << std::endl;
429  std::cout << "m3:\n" << m3 << std::endl;
430 
431  std::cout << "\n------------------------" << std::endl;
432  std::cout << "--- TEST vpMatrix stack()" << std::endl;
433  std::cout << "------------------------" << std::endl;
434 
435  {
436  vpMatrix L, L2(2, 6);
437  L2 = 2;
438  L.stack(L2);
439  std::cout << "L:\n" << L << std::endl;
440  L2.resize(3, 6);
441  L2 = 3;
442  L.stack(L2);
443  std::cout << "L:\n" << L << std::endl;
444  }
445 
446  {
447  vpMatrix m_big_stack;
448  t = vpTime::measureTimeMs();
449  for (unsigned int i = 0; i < nb; i++) {
450  m_big_stack.stack(submatrices[(size_t)i]);
451  }
452  t = vpTime::measureTimeMs() - t;
453  std::cout << "Matrix stack(): " << t << " ms" << std::endl;
454 
455  if (!equalMatrix(m_big, m_big_stack)) {
456  std::cerr << "Problem with vpMatrix stack()!" << std::endl;
457  return EXIT_FAILURE;
458  }
459  }
460 
461  std::cout << "\n------------------------" << std::endl;
462  std::cout << "--- TEST vpMatrix stack(vpRowVector)" << std::endl;
463  std::cout << "------------------------" << std::endl;
464 
465  vpMatrix m_big_stack = generateRandomMatrix(10000, ctest ? 10 : 100, -1000.0, 1000.0);
466  std::cout << "m_big_stack: " << m_big_stack.getRows() << "x" << m_big_stack.getCols() << std::endl;
467 
468  vpMatrix m_big_stack_row;
469  t = vpTime::measureTimeMs();
470  for (unsigned int i = 0; i < m_big_stack.getRows(); i++) {
471  m_big_stack_row.stack(m_big_stack.getRow(i));
472  }
473  t = vpTime::measureTimeMs() - t;
474  std::cout << "Matrix stack(vpRowVector): " << t << " ms" << std::endl;
475 
476  if (!equalMatrix(m_big_stack, m_big_stack_row)) {
477  std::cerr << "Problem with vpMatrix stack(vpRowVector)!" << std::endl;
478  return EXIT_FAILURE;
479  }
480 
481  std::cout << "\n------------------------" << std::endl;
482  std::cout << "--- TEST vpMatrix stack(vpColVector)" << std::endl;
483  std::cout << "------------------------" << std::endl;
484 
485  vpMatrix m_big_stack_col;
486  t = vpTime::measureTimeMs();
487  for (unsigned int j = 0; j < m_big_stack.getCols(); j++) {
488  m_big_stack_col.stack(m_big_stack.getCol(j));
489  }
490  t = vpTime::measureTimeMs() - t;
491  std::cout << "Matrix stack(vpColVector): " << t << " ms" << std::endl;
492 
493  if (!equalMatrix(m_big_stack, m_big_stack_col)) {
494  std::cerr << "Problem with vpMatrix stack(vpColVector)!" << std::endl;
495  return EXIT_FAILURE;
496  }
497 
498  std::cout << "\n------------------------" << std::endl;
499  std::cout << "--- TEST vpMatrix::stack()" << std::endl;
500  std::cout << "------------------------" << std::endl;
501 
502  {
503  vpMatrix L, L2(2, 6), L_tmp;
504  L2 = 2;
505  vpMatrix::stack(L_tmp, L2, L);
506  std::cout << "L:\n" << L << std::endl;
507  L2.resize(3, 6);
508  L2 = 3;
509  L_tmp = L;
510  vpMatrix::stack(L_tmp, L2, L);
511  std::cout << "L:\n" << L << std::endl;
512  }
513 
514  {
515  vpMatrix m_big_stack_static, m_big_stack_static_tmp;
516  t = vpTime::measureTimeMs();
517  for (unsigned int i = 0; i < nb; i++) {
518  vpMatrix::stack(m_big_stack_static_tmp, submatrices[(size_t)i], m_big_stack_static);
519  m_big_stack_static_tmp = m_big_stack_static;
520  }
521  t = vpTime::measureTimeMs() - t;
522  std::cout << "Matrix::stack(): " << t << " ms" << std::endl;
523 
524  if (!equalMatrix(m_big, m_big_stack_static)) {
525  std::cerr << "Problem with vpMatrix::stack()!" << std::endl;
526  return EXIT_FAILURE;
527  }
528  }
529 
530  std::cout << "\n------------------------" << std::endl;
531  std::cout << "--- TEST vpMatrix::stack(vpMatrix, vpRowVector, vpMatrix)" << std::endl;
532  std::cout << "------------------------" << std::endl;
533 
534  vpMatrix m_big_stack_static = generateRandomMatrix(ctest ? 100 : 1000, ctest ? 10 : 100, -1000.0, 1000.0);
535  std::cout << "m_big_stack_static: " << m_big_stack_static.getRows() << "x" << m_big_stack_static.getCols() << std::endl;
536 
537  vpMatrix m_big_stack_static_row, m_big_stack_static_row_tmp;
538  t = vpTime::measureTimeMs();
539  for (unsigned int i = 0; i < m_big_stack_static.getRows(); i++) {
540  vpMatrix::stack(m_big_stack_static_row_tmp, m_big_stack_static.getRow(i), m_big_stack_static_row);
541  m_big_stack_static_row_tmp = m_big_stack_static_row;
542  }
543  t = vpTime::measureTimeMs() - t;
544  std::cout << "Matrix::stack(vpMatrix, vpRowVector, vpMatrix): " << t << " ms" << std::endl;
545 
546  if (!equalMatrix(m_big_stack_static, m_big_stack_static_row)) {
547  std::cerr << "Problem with vpMatrix::stack(vpMatrix, vpRowVector, "
548  "vpMatrix)!"
549  << std::endl;
550  return EXIT_FAILURE;
551  }
552 
553  std::cout << "\n------------------------" << std::endl;
554  std::cout << "--- TEST vpMatrix::stack(vpMatrix, vpColVector, vpMatrix)" << std::endl;
555  std::cout << "------------------------" << std::endl;
556 
557  vpMatrix m_big_stack_static_col, m_big_stack_static_col_tmp;
558  t = vpTime::measureTimeMs();
559  for (unsigned int j = 0; j < m_big_stack_static.getCols(); j++) {
560  vpMatrix::stack(m_big_stack_static_col_tmp, m_big_stack_static.getCol(j), m_big_stack_static_col);
561  m_big_stack_static_col_tmp = m_big_stack_static_col;
562  }
563  t = vpTime::measureTimeMs() - t;
564  std::cout << "Matrix::stack(vpMatrix, vpColVector, vpMatrix): " << t << " ms" << std::endl;
565 
566  if (!equalMatrix(m_big_stack_static, m_big_stack_static_col)) {
567  std::cerr << "Problem with vpMatrix::stack(vpMatrix, vpColVector, "
568  "vpMatrix)!"
569  << std::endl;
570  return EXIT_FAILURE;
571  }
572  }
573 
574  {
575  vpMatrix m1(11, 9), m2(3, 4);
576  for (unsigned int i = 0; i < m2.getRows(); i++) {
577  for (unsigned int j = 0; j < m2.getCols(); j++) {
578  m2[i][j] = getRandomValues(-100.0, 100.0);
579  }
580  }
581 
582  unsigned int offset_i = 4, offset_j = 3;
583  m1.insert(m2, offset_i, offset_j);
584 
585  for (unsigned int i = 0; i < m2.getRows(); i++) {
586  for (unsigned int j = 0; j < m2.getCols(); j++) {
587  if (!vpMath::equal(m1[i + offset_i][j + offset_j], m2[i][j], std::numeric_limits<double>::epsilon())) {
588  std::cerr << "Problem with vpMatrix insert()!" << std::endl;
589  return EXIT_FAILURE;
590  }
591  }
592  }
593 
594  offset_i = 4;
595  offset_j = 5;
596  m1.insert(m2, offset_i, offset_j);
597 
598  for (unsigned int i = 0; i < m2.getRows(); i++) {
599  for (unsigned int j = 0; j < m2.getCols(); j++) {
600  if (!vpMath::equal(m1[i + offset_i][j + offset_j], m2[i][j], std::numeric_limits<double>::epsilon())) {
601  std::cerr << "Problem with vpMatrix insert()!" << std::endl;
602  return EXIT_FAILURE;
603  }
604  }
605  }
606 
607  offset_i = 8;
608  offset_j = 5;
609  m1.insert(m2, offset_i, offset_j);
610 
611  for (unsigned int i = 0; i < m2.getRows(); i++) {
612  for (unsigned int j = 0; j < m2.getCols(); j++) {
613  if (!vpMath::equal(m1[i + offset_i][j + offset_j], m2[i][j], std::numeric_limits<double>::epsilon())) {
614  std::cerr << "Problem with vpMatrix insert()!" << std::endl;
615  return EXIT_FAILURE;
616  }
617  }
618  }
619  }
620 
621  {
622  std::cout << "\n------------------------" << std::endl;
623  std::cout << "--- TEST vpMatrix::juxtaposeMatrices()" << std::endl;
624  std::cout << "------------------------" << std::endl;
625 
626  vpMatrix A(5, 6), B(5, 4);
627  for (unsigned int i = 0; i < A.getRows(); i++) {
628  for (unsigned int j = 0; j < A.getCols(); j++) {
629  A[i][j] = i * A.getCols() + j;
630 
631  if (j < B.getCols()) {
632  B[i][j] = (i * B.getCols() + j) * 10;
633  }
634  }
635  }
636 
637  vpMatrix juxtaposeM;
638  vpMatrix::juxtaposeMatrices(A, B, juxtaposeM);
639  std::cout << "juxtaposeM:\n" << juxtaposeM << std::endl;
640  }
641 
642 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
643  {
644  std::vector<vpMatrix> vec_mat;
645  vec_mat.emplace_back(5, 5);
646 
647  vpMatrix A(4, 4), B(4, 4);
648  A = 1;
649  B = 2;
650  vpMatrix res = A + B;
651  std::cout << "\n1) A+B:\n" << res << std::endl;
652 
653  vpMatrix res2;
654  res2 = A + B;
655  std::cout << "\n2) A+B:\n" << res2 << std::endl;
656  }
657 #endif
658 
659  {
660  std::cout << "\n------------------------" << std::endl;
661  std::cout << "--- TEST vpMatrix::hadamard()" << std::endl;
662  std::cout << "------------------------" << std::endl;
663 
664  vpMatrix M1(3, 5), M2(3, 5);
665  for (unsigned int i = 0; i < M1.size(); i++) {
666  M1.data[i] = i;
667  M2.data[i] = i + 2;
668  }
669 
670  std::cout << "M1:\n" << M1 << std::endl;
671  std::cout << "\nM2:\n" << M2 << std::endl;
672  M2 = M1.hadamard(M2);
673  std::cout << "\nRes:\n" << M2 << std::endl;
674  }
675 
676  {
677  std::cout << "\n------------------------" << std::endl;
678  std::cout << "--- TEST vpMatrix::stackColums()" << std::endl;
679  std::cout << "------------------------" << std::endl;
680  vpMatrix M(3, 5);
681  for (unsigned int j = 0; j < M.getCols(); j++) {
682  for (unsigned int i = 0; i < M.getRows(); i++) {
683  M[i][j] = i + j*M.getRows();
684  }
685  }
686  std::cout << "M:\n" << M << std::endl;
687  vpColVector v = M.stackColumns();
688  std::cout << "Column stack: " << v.t() << std::endl;
689  if (M.size() != v.size()) {
690  std::cerr << "Problem in vpMatrix::stackColumns(): size differ" << std::endl;
691  return EXIT_FAILURE;
692  }
693  for (unsigned int i=0; i < v.size(); i++) {
694  if (std::fabs(v[i]-static_cast<double>(i)) > std::numeric_limits<double>::epsilon()) {
695  std::cerr << "Problem in vpMatrix::stackColumns(): content differ" << std::endl;
696  return EXIT_FAILURE;
697  }
698  }
699  }
700 
701  {
702  std::cout << "\n------------------------" << std::endl;
703  std::cout << "--- TEST vpMatrix::stackRows()" << std::endl;
704  std::cout << "------------------------" << std::endl;
705  vpMatrix M(3, 5);
706  for (unsigned int i = 0; i < M.getRows(); i++) {
707  for (unsigned int j = 0; j < M.getCols(); j++) {
708  M[i][j] = i*M.getCols() + j;
709  }
710  }
711  std::cout << "M:\n" << M << std::endl;
712  vpRowVector v = M.stackRows();
713  std::cout << "Rows stack: " << v << std::endl;
714  if (M.size() != v.size()) {
715  std::cerr << "Problem in vpMatrix::stackRows(): size differ" << std::endl;
716  return EXIT_FAILURE;
717  }
718  for (unsigned int i=0; i < v.size(); i++) {
719  if (std::fabs(v[i]-static_cast<double>(i)) > std::numeric_limits<double>::epsilon()) {
720  std::cerr << "Problem in vpMatrix::stackRows(): content differ" << std::endl;
721  return EXIT_FAILURE;
722  }
723  }
724  }
725 
726  {
727  std::cout << "\n------------------------" << std::endl;
728  std::cout << "--- TEST vpMatrix::getCol()" << std::endl;
729  std::cout << "------------------------" << std::endl;
730  vpMatrix A(4,4);
731  for(unsigned int i=0; i < A.getRows(); i++)
732  for(unsigned int j=0; j < A.getCols(); j++)
733  A[i][j] = i*A.getCols()+j;
734 
735  {
736  vpColVector cv = A.getCol(1, 1, 3);
737  vpColVector ref;
738  ref << 5, 9, 13;
739  if (cv != ref) {
740  std::cerr << "Problem in vpMatrix::getCol(): values are different" << std::endl;
741  return EXIT_FAILURE;
742  }
743  }
744  {
745  vpColVector cv = A.getCol(1);
746  vpColVector ref;
747  ref << 1, 5, 9, 13;
748  if (cv != ref) {
749  std::cerr << "Problem in vpMatrix::getCol(): values are different" << std::endl;
750  return EXIT_FAILURE;
751  }
752  }
753  }
754 
755  {
756  std::cout << "\n------------------------" << std::endl;
757  std::cout << "--- TEST vpMatrix::getRow()" << std::endl;
758  std::cout << "------------------------" << std::endl;
759  vpMatrix A(4,4);
760  for(unsigned int i=0; i < A.getRows(); i++)
761  for(unsigned int j=0; j < A.getCols(); j++)
762  A[i][j] = i*A.getCols()+j;
763 
764  {
765  vpRowVector rv = A.getRow(1, 1, 3);
766  vpRowVector ref;
767  ref << 5, 6, 7;
768  if (rv != ref) {
769  std::cerr << "Problem in vpMatrix::getRow(): values are different" << std::endl;
770  return EXIT_FAILURE;
771  }
772  }
773  {
774  vpRowVector rv = A.getRow(1);
775  vpRowVector ref;
776  ref << 4, 5, 6, 7;
777  if (rv != ref) {
778  std::cerr << "Problem in vpMatrix::getRow(): values are different" << std::endl;
779  return EXIT_FAILURE;
780  }
781  }
782  }
783 
784  {
785  std::cout << "\n------------------------" << std::endl;
786  std::cout << "--- TEST vpMatrix::getDiag()" << std::endl;
787  std::cout << "------------------------" << std::endl;
788  vpMatrix A(3,4);
789  for(unsigned int i=0; i < A.getRows(); i++)
790  for(unsigned int j=0; j < A.getCols(); j++)
791  A[i][j] = i*A.getCols()+j;
792 
793  vpColVector diag = A.getDiag();
794  vpColVector ref;
795  ref << 0.0, 5.0, 10.0;
796  if (diag != ref) {
797  std::cerr << "Problem in vpMatrix::getDiag(): values are different" << std::endl;
798  return EXIT_FAILURE;
799  }
800  }
801 
802  std::cout << "\nAll tests succeeded" << std::endl;
803  return EXIT_SUCCESS;
804  } catch (const vpException &e) {
805  std::cout << "Catch an exception: " << e << std::endl;
806  return EXIT_FAILURE;
807  }
808 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
static vpMatrix juxtaposeMatrices(const vpMatrix &A, const vpMatrix &B)
Definition: vpMatrix.cpp:4452
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:305
vpColVector getDiag() const
Definition: vpMatrix.cpp:4228
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:115
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:296
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:4800
error that can be emited by ViSP classes.
Definition: vpException.h:71
unsigned int getRows() const
Definition: vpArray2D.h:289
vpRowVector t() const
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:145
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:291
static bool saveMatrix(const std::string &filename, const vpArray2D< double > &M, bool binary=false, const char *header="")
Definition: vpMatrix.h:727
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Implementation of a rotation matrix and operations on such kind of matrices.
unsigned int getCols() const
Definition: vpArray2D.h:279
vpMatrix hadamard(const vpMatrix &m) const
Definition: vpMatrix.cpp:1789
void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols)
Definition: vpMatrix.cpp:389
vpRowVector getRow(unsigned int i) const
Definition: vpMatrix.cpp:4136
int print(std::ostream &s, unsigned int length, const std::string &intro="") const
Definition: vpMatrix.cpp:4519
vpColVector getCol(unsigned int j) const
Definition: vpMatrix.cpp:4096
static double rad(double deg)
Definition: vpMath.h:108
void vpGEMM(const vpArray2D< double > &A, const vpArray2D< double > &B, const double &alpha, const vpArray2D< double > &C, const double &beta, vpArray2D< double > &D, const unsigned int &ops=0)
Definition: vpGEMM.h:393
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
static bool saveMatrixYAML(const std::string &filename, const vpArray2D< double > &M, const char *header="")
Definition: vpMatrix.h:745
void insert(const vpMatrix &A, unsigned int r, unsigned int c)
Definition: vpMatrix.cpp:4909
void resize(unsigned int i, bool flagNullify=true)
Definition: vpRowVector.h:271
void eye()
Definition: vpMatrix.cpp:492
static bool loadMatrix(const std::string &filename, vpArray2D< double > &M, bool binary=false, char *header=NULL)
Definition: vpMatrix.h:692
static bool loadMatrixYAML(const std::string &filename, vpArray2D< double > &M, char *header=NULL)
Definition: vpMatrix.h:708