ViSP  3.0.0
testMatrix.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test some vpMatrix functionalities.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpMath.h>
47 #include <visp3/core/vpHomogeneousMatrix.h>
48 #include <visp3/core/vpVelocityTwistMatrix.h>
49 #include <visp3/core/vpGEMM.h>
50 
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 bool test(const std::string &s, const vpMatrix &M, const std::vector<double> &bench)
55 {
56  static unsigned int cpt = 0;
57  std::cout << "** Test " << ++cpt << std::endl;
58  std::cout << s << "(" << M.getRows() << "," << M.getCols() << ") = \n" << M << std::endl;
59  if(bench.size() != M.size()) {
60  std::cout << "Test fails: bad size wrt bench" << std::endl;
61  return false;
62  }
63  for (unsigned int i=0; i<M.size(); i++) {
64  if (std::fabs(M.data[i]-bench[i]) > std::fabs(M.data[i])*std::numeric_limits<double>::epsilon()) {
65  std::cout << "Test fails: bad content" << std::endl;
66  return false;
67  }
68  }
69 
70  return true;
71 }
72 
73 int
74 main()
75 {
76  try {
77  int err = 1;
78  {
79  vpColVector c(6, 1);
80  vpRowVector r(6, 1);
81  std::vector<double> bench(6, 1);
82  vpMatrix M1(c);
83  if (test("M1", M1, bench) == false)
84  return err;
85  vpMatrix M2(r);
86  if (test("M2", M2, bench) == false)
87  return err;
88  }
89  {
90  vpMatrix M(4,5);
91  int val = 0;
92  for(unsigned int i=0; i<M.getRows(); i++) {
93  for(unsigned int j=0; j<M.getCols(); j++) {
94  M[i][j] = val++;
95  }
96  }
97  std::cout <<"M ";
98  M.print (std::cout, 4);
99 
100  vpMatrix N;
101  N.init(M, 0, 1, 2, 3);
102  std::cout <<"N ";
103  N.print (std::cout, 4);
104 
105  if (vpMatrix::saveMatrix("matrix.mat", M, false, "My 4-by-5 matrix"))
106  std::cout << "Matrix saved in matrix.mat file" << std::endl;
107  else
108  return err;
109 
110  vpMatrix M1;
111  if (vpMatrix::loadMatrix("matrix.mat", M1, false))
112  std::cout << "Matrix loaded from matrix.mat file: \n" << M1 << std::endl;
113  else
114  return err;
115 
116  if (vpMatrix::saveMatrixYAML("matrix.yml", M, "My 4-by-5 matrix"))
117  std::cout << "Matrix saved in matrix.yml file" << std::endl;
118  else
119  return err;
120 
121  vpMatrix M2;
122  if (vpMatrix::loadMatrixYAML("matrix.yml", M2))
123  std::cout << "Matrix loaded from matrix.yml file: \n" << M2 << std::endl;
124  else
125  return err;
126  }
127  {
129  std::cout << "R: \n" << R << std::endl;
130  vpMatrix M1(R);
131  std::cout << "M1: \n" << M1 << std::endl;
132  vpMatrix M2(M1);
133  std::cout << "M2: \n" << M2 << std::endl;
134  vpMatrix M3 = R;
135  std::cout << "M3: \n" << M3 << std::endl;
136  vpMatrix M4 = M1;
137  std::cout << "M4: \n" << M4 << std::endl;
138  }
139  {
140 
141  std::cout << "------------------------" << std::endl;
142  std::cout << "--- TEST PRETTY PRINT---" << std::endl;
143  std::cout << "------------------------" << std::endl;
144  vpMatrix M ;
145  M.eye(4);
146 
147  std::cout << "call std::cout << M;" << std::endl;
148  std::cout << M << std::endl;
149 
150  std::cout << "call M.print (std::cout, 4);" << std::endl;
151  M.print (std::cout, 4);
152 
153  std::cout << "------------------------" << std::endl;
154  M.resize(3,3) ;
155  M.eye(3);
156  M[1][0]=1.235;
157  M[1][1]=12.345;
158  M[1][2]=.12345;
159  std::cout << "call std::cout << M;" << std::endl;
160  std::cout << M;
161  std::cout << "call M.print (std::cout, 6);" << std::endl;
162  M.print (std::cout, 6);
163  std::cout << std::endl;
164 
165  std::cout << "------------------------" << std::endl;
166  M[0][0]=-1.235;
167  M[1][0]=-12.235;
168 
169  std::cout << "call std::cout << M;" << std::endl;
170  std::cout << M << std::endl;
171 
172  std::cout << "call M.print (std::cout, 10);" << std::endl;
173  M.print (std::cout, 10);
174  std::cout << std::endl;
175 
176  std::cout << "call M.print (std::cout, 2);" << std::endl;
177  M.print (std::cout, 2);
178  std::cout << std::endl;
179 
180  std::cout << "------------------------" << std::endl;
181  M.resize(3,3) ;
182  M.eye(3);
183  M[0][2]=-0.0000000876;
184  std::cout << "call std::cout << M;" << std::endl;
185  std::cout << M << std::endl;
186 
187  std::cout << "call M.print (std::cout, 4);" << std::endl;
188  M.print (std::cout, 4);
189  std::cout << std::endl;
190  std::cout << "call M.print (std::cout, 10, \"M\");" << std::endl;
191  M.print (std::cout, 10, "M");
192  std::cout << std::endl;
193  std::cout << "call M.print (std::cout, 20, \"M\");" << std::endl;
194  M.print (std::cout, 20, "M");
195  std::cout << std::endl;
196 
197 
198  std::cout << "------------------------" << std::endl;
199  std::cout << "--- TEST RESIZE --------" << std::endl;
200  std::cout << "------------------------" << std::endl;
201  std::cout << "5x5" << std::endl;
202  M.resize(5,5,false);
203  std::cout << M << std::endl;
204  std::cout << "3x2" << std::endl;
205  M.resize(3,2,false);
206  std::cout << M << std::endl;
207  std::cout << "2x2" << std::endl;
208  M.resize(2,2,false);
209  std::cout << M << std::endl;
210  std::cout << "------------------------" << std::endl;
211 
213  vpMatrix A(1,6),B;
214 
215  A=1.0;
216  //vMe=1.0;
217  B=A*vMe;
218 
219  std::cout << "------------------------" << std::endl;
220  std::cout << "--- TEST vpRowVector * vpColVector" << std::endl;
221  std::cout << "------------------------" << std::endl;
222  vpRowVector r(3);
223  r[0] = 2;
224  r[1] = 3;
225  r[2] = 4;
226 
227  vpColVector c(3);
228  c[0] = 1;
229  c[1] = 2;
230  c[2] = -1;
231 
232  double rc = r * c;
233 
234  r.print(std::cout, 2, "r");
235  c.print(std::cout, 2, "c");
236  std::cout << "r * c = " << rc << std::endl;
237 
238  std::cout << "------------------------" << std::endl;
239  std::cout << "--- TEST vpRowVector * vpMatrix" << std::endl;
240  std::cout << "------------------------" << std::endl;
241  M.resize(3,3) ;
242  M.eye(3);
243 
244  M[1][0] = 1.5;
245  M[2][0] = 2.3;
246 
247  vpRowVector rM = r * M;
248 
249  r.print(std::cout, 2, "r");
250  M.print(std::cout, 10, "M");
251  std::cout << "r * M = " << rM << std::endl;
252 
253  std::cout << "------------------------" << std::endl;
254  std::cout << "--- TEST vpGEMM " << std::endl;
255  std::cout << "------------------------" << std::endl;
256  M.resize(3,3) ;
257  M.eye(3);
258  vpMatrix N(3, 3);
259  N[0][0] = 2;
260  N[1][0] = 1.2;
261  N[1][2] = 0.6;
262  N[2][2] = 0.25;
263 
264  vpMatrix C(3, 3);
265  C.eye(3);
266 
267  vpMatrix D;
268 
269  //realise the operation D = 2 * M^T * N + 3 C
270  vpGEMM(M, N, 2, C, 3, D, VP_GEMM_A_T);
271  std::cout << D << std::endl;
272  return 0;
273  }
274  }
275  catch(vpException e) {
276  std::cout << "Catch an exception: " << e << std::endl;
277  return 1;
278  }
279 }
280 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
static bool loadMatrix(const std::string &filename, vpArray2D< double > &M, const bool binary=false, char *header=NULL)
Definition: vpMatrix.h:518
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:70
error that can be emited by ViSP classes.
Definition: vpException.h:73
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:156
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
Implementation of a rotation matrix and operations on such kind of matrices.
void init(const vpMatrix &M, unsigned int r, unsigned int c, unsigned int nrows, unsigned int ncols)
Definition: vpMatrix.cpp:137
Implementation of a velocity twist matrix and operations on such kind of matrices.
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Definition: vpMatrix.cpp:2649
static double rad(double deg)
Definition: vpMath.h:104
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpRowVector.h:199
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:358
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
int print(std::ostream &s, unsigned int length, char const *intro=0) const
static bool saveMatrixYAML(const std::string &filename, const vpArray2D< double > &M, const char *header="")
Definition: vpMatrix.h:567
static bool saveMatrix(const std::string &filename, const vpArray2D< double > &M, const bool binary=false, const char *header="")
Definition: vpMatrix.h:550
void eye()
Definition: vpMatrix.cpp:194
static bool loadMatrixYAML(const std::string &filename, vpArray2D< double > &M, char *header=NULL)
Definition: vpMatrix.h:533