ViSP
testMatrix.cpp
1 /****************************************************************************
2  *
3  * $Id: testMatrix.cpp 5242 2015-01-30 18:22:41Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Test some vpMatrix functionalities.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
48 #include <visp/vpConfig.h>
49 #include <visp/vpDebug.h>
50 #include <visp/vpMath.h>
51 #include <visp/vpHomogeneousMatrix.h>
52 #include <visp/vpVelocityTwistMatrix.h>
53 #include <visp/vpParseArgv.h>
54 #include <visp/vpGEMM.h>
55 
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 
60 // List of allowed command line options
61 #define GETOPTARGS "h"
62 
63 void usage(const char *name, const char *badparam);
64 bool getOptions(int argc, const char **argv);
65 
71 void usage(const char *name, const char *badparam)
72 {
73  fprintf(stdout, "\n\
74 Test some vpMatrix functionalities.\n\
75 \n\
76 SYNOPSIS\n\
77  %s [-h]\n", name);
78 
79  fprintf(stdout, "\n\
80 OPTIONS: Default\n\
81  -h\n\
82  Print the help.\n");
83 
84  if (badparam)
85  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
86 }
94 bool getOptions(int argc, const char **argv)
95 {
96  const char *optarg_;
97  int c;
98  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
99 
100  switch (c) {
101  case 'h': usage(argv[0], NULL); return false; break;
102 
103  default:
104  usage(argv[0], optarg_);
105  return false; break;
106  }
107  }
108 
109  if ((c == 1) || (c == -1)) {
110  // standalone param or error
111  usage(argv[0], NULL);
112  std::cerr << "ERROR: " << std::endl;
113  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
114  return false;
115  }
116 
117  return true;
118 }
119 
120 
121 int
122 main(int argc, const char ** argv)
123 {
124  try {
125  // Read the command line options
126  if (getOptions(argc, argv) == false) {
127  exit (-1);
128  }
129  {
130  vpMatrix M(4,5);
131  int val = 0;
132  for(unsigned int i=0; i<M.getRows(); i++) {
133  for(unsigned int j=0; j<M.getCols(); j++) {
134  M[i][j] = val++;
135  }
136  }
137  std::cout <<"M ";
138  M.print (std::cout, 4);
139 
140  vpMatrix N;
141  N.init(M, 0, 1, 2, 3);
142  std::cout <<"N ";
143  N.print (std::cout, 4);
144  }
145  {
146 
147  std::cout << "------------------------" << std::endl;
148  std::cout << "--- TEST PRETTY PRINT---" << std::endl;
149  std::cout << "------------------------" << std::endl;
150  vpMatrix M ;
151  M.eye(4);
152 
153  std::cout << "call std::cout << M;" << std::endl;
154  std::cout << M << std::endl;
155 
156  std::cout << "call M.print (std::cout, 4);" << std::endl;
157  M.print (std::cout, 4);
158 
159  std::cout << "------------------------" << std::endl;
160  M.resize(3,3) ;
161  M.eye(3);
162  M[1][0]=1.235;
163  M[1][1]=12.345;
164  M[1][2]=.12345;
165  std::cout << "call std::cout << M;" << std::endl;
166  std::cout << M;
167  std::cout << "call M.print (std::cout, 6);" << std::endl;
168  M.print (std::cout, 6);
169  std::cout << std::endl;
170 
171  std::cout << "------------------------" << std::endl;
172  M[0][0]=-1.235;
173  M[1][0]=-12.235;
174 
175  std::cout << "call std::cout << M;" << std::endl;
176  std::cout << M << std::endl;
177 
178  std::cout << "call M.print (std::cout, 10);" << std::endl;
179  M.print (std::cout, 10);
180  std::cout << std::endl;
181 
182  std::cout << "call M.print (std::cout, 2);" << std::endl;
183  M.print (std::cout, 2);
184  std::cout << std::endl;
185 
186  std::cout << "------------------------" << std::endl;
187  M.resize(3,3) ;
188  M.eye(3);
189  M[0][2]=-0.0000000876;
190  std::cout << "call std::cout << M;" << std::endl;
191  std::cout << M << std::endl;
192 
193  std::cout << "call M.print (std::cout, 4);" << std::endl;
194  M.print (std::cout, 4);
195  std::cout << std::endl;
196  std::cout << "call M.print (std::cout, 10, \"M\");" << std::endl;
197  M.print (std::cout, 10, "M");
198  std::cout << std::endl;
199  std::cout << "call M.print (std::cout, 20, \"M\");" << std::endl;
200  M.print (std::cout, 20, "M");
201  std::cout << std::endl;
202 
203 
204  std::cout << "------------------------" << std::endl;
205  std::cout << "--- TEST RESIZE --------" << std::endl;
206  std::cout << "------------------------" << std::endl;
207  std::cout << "5x5" << std::endl;
208  M.resize(5,5,false);
209  std::cout << M << std::endl;
210  std::cout << "3x2" << std::endl;
211  M.resize(3,2,false);
212  std::cout << M << std::endl;
213  std::cout << "2x2" << std::endl;
214  M.resize(2,2,false);
215  std::cout << M << std::endl;
216  std::cout << "------------------------" << std::endl;
217 
219  vpMatrix A(1,6),B;
220 
221  A=1.0;
222  //vMe=1.0;
223  B=A*vMe;
224 
225  std::cout << "------------------------" << std::endl;
226  std::cout << "--- TEST vpRowVector * vpColVector" << std::endl;
227  std::cout << "------------------------" << std::endl;
228  vpRowVector r(3);
229  r[0] = 2;
230  r[1] = 3;
231  r[2] = 4;
232 
233  vpColVector c(3);
234  c[0] = 1;
235  c[1] = 2;
236  c[2] = -1;
237 
238  double rc = r * c;
239 
240  r.print(std::cout, 2, "r");
241  c.print(std::cout, 2, "c");
242  std::cout << "r * c = " << rc << std::endl;
243 
244  std::cout << "------------------------" << std::endl;
245  std::cout << "--- TEST vpRowVector * vpMatrix" << std::endl;
246  std::cout << "------------------------" << std::endl;
247  M.resize(3,3) ;
248  M.eye(3);
249 
250  M[1][0] = 1.5;
251  M[2][0] = 2.3;
252 
253  vpRowVector rM = r * M;
254 
255  r.print(std::cout, 2, "r");
256  M.print(std::cout, 10, "M");
257  std::cout << "r * M = " << rM << std::endl;
258 
259  std::cout << "------------------------" << std::endl;
260  std::cout << "--- TEST vpGEMM " << std::endl;
261  std::cout << "------------------------" << std::endl;
262  M.resize(3,3) ;
263  M.eye(3);
264  vpMatrix N(3, 3);
265  N[0][0] = 2;
266  N[1][0] = 1.2;
267  N[1][2] = 0.6;
268  N[2][2] = 0.25;
269 
270  vpMatrix C(3, 3);
271  C.eye(3);
272 
273  vpMatrix D;
274 
275  //realise the operation D = 2 * M^T * N + 3 C
276  vpGEMM(M, N, 2, C, 3, D, VP_GEMM_A_T);
277  std::cout << D << std::endl;
278  return 0;
279  }
280  }
281  catch(vpException e) {
282  std::cout << "Catch an exception: " << e << std::endl;
283  return 1;
284  }
285 }
286 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:199
void init()
Initialization of the object matrix.
Definition: vpMatrix.cpp:98
Definition of the row vector class.
Definition: vpRowVector.h:73
error that can be emited by ViSP classes.
Definition: vpException.h:76
int print(std::ostream &s, unsigned int length, char const *intro=0)
Definition: vpMatrix.cpp:2937
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
void vpGEMM(const vpMatrix &A, const vpMatrix &B, const double &alpha, const vpMatrix &C, const double &beta, vpMatrix &D, const unsigned int &ops=0)
This function performs generalized matrix multiplication: D = alpha*op(A)*op(B) + beta*op(C)...
Definition: vpGEMM.h:331
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpRowVector.h:98
void eye(unsigned int n)
Definition: vpMatrix.cpp:1252
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72