ViSP  2.9.0
testMatrix.cpp
1 /****************************************************************************
2  *
3  * $Id: testMatrix.cpp 4658 2014-02-09 09:50:14Z 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  vpTRACE("------------------------");
131  vpTRACE("--- TEST PRETTY PRINT---");
132  vpTRACE("------------------------");
133  vpMatrix M ;
134  M.eye(4);
135 
136  vpTRACE("call std::cout << M;");
137  std::cout << M << std::endl;
138 
139  vpTRACE("call M.print (std::cout, 4);");
140  M.print (std::cout, 4);
141 
142  vpTRACE("------------------------");
143  M.resize(3,3) ;
144  M.eye(3);
145  M[1][0]=1.235;
146  M[1][1]=12.345;
147  M[1][2]=.12345;
148  vpTRACE("call std::cout << M;");
149  std::cout << M;
150  vpTRACE("call M.print (std::cout, 6);");
151  M.print (std::cout, 6);
152 
153  vpTRACE("------------------------");
154  M[0][0]=-1.235;
155  M[1][0]=-12.235;
156 
157  vpTRACE("call std::cout << M;");
158  std::cout << M;
159 
160  vpTRACE("call M.print (std::cout, 10);");
161  M.print (std::cout, 10);
162 
163  vpTRACE("call M.print (std::cout, 2);");
164  M.print (std::cout, 2);
165 
166  vpTRACE("------------------------");
167  M.resize(3,3) ;
168  M.eye(3);
169  M[0][2]=-0.0000000876;
170  vpTRACE("call std::cout << M;");
171  std::cout << M;
172 
173  vpTRACE("call M.print (std::cout, 4);");
174  M.print (std::cout, 4);
175  vpTRACE("call M.print (std::cout, 10, \"M\");");
176  M.print (std::cout, 10, "M");
177  vpTRACE("call M.print (std::cout, 20, \"M\");");
178  M.print (std::cout, 20, "M");
179 
180 
181  vpTRACE("------------------------");
182  vpTRACE("--- TEST RESIZE --------");
183  vpTRACE("------------------------");
184  vpCTRACE << "5x5" << std::endl;
185  M.resize(5,5,false);
186  vpCTRACE << std::endl<< M;
187  vpCTRACE << "3x2" << std::endl;
188  M.resize(3,2,false);
189  vpCTRACE <<std::endl<< M;
190  vpCTRACE << "2x2" << std::endl;
191  M.resize(2,2,false);
192  vpCTRACE << std::endl<<M;
193  vpTRACE("------------------------");
194 
195 
197  vpMatrix A(1,6),B;
198 
199  A=1.0;
200  //vMe=1.0;
201  B=A*vMe;
202 
203  vpTRACE("------------------------");
204  vpTRACE("--- TEST vpRowVector * vpColVector");
205  vpTRACE("------------------------");
206  vpRowVector r(3);
207  r[0] = 2;
208  r[1] = 3;
209  r[2] = 4;
210 
211  vpColVector c(3);
212  c[0] = 1;
213  c[1] = 2;
214  c[2] = -1;
215 
216  double rc = r * c;
217 
218  r.print(std::cout, 2, "r");
219  c.print(std::cout, 2, "c");
220  std::cout << "r * c = " << rc << std::endl;
221 
222  vpTRACE("------------------------");
223  vpTRACE("--- TEST vpRowVector * vpMatrix");
224  vpTRACE("------------------------");
225  M.resize(3,3) ;
226  M.eye(3);
227 
228  M[1][0] = 1.5;
229  M[2][0] = 2.3;
230 
231  vpRowVector rM = r * M;
232 
233  r.print(std::cout, 2, "r");
234  M.print(std::cout, 10, "M");
235  std::cout << "r * M = " << rM << std::endl;
236 
237  vpTRACE("------------------------");
238  vpTRACE("--- TEST vpGEMM ");
239  vpTRACE("------------------------");
240  M.resize(3,3) ;
241  M.eye(3);
242  vpMatrix N(3, 3);
243  N[0][0] = 2;
244  N[1][0] = 1.2;
245  N[1][2] = 0.6;
246  N[2][2] = 0.25;
247 
248  vpMatrix C(3, 3);
249  C.eye(3);
250 
251  vpMatrix D;
252 
253  //realise the operation D = 2 * M^T * N + 3 C
254  vpGEMM(M, N, 2, C, 3, D, VP_GEMM_A_T);
255  std::cout << D << std::endl;
256  return 0;
257  }
258  catch(vpException e) {
259  std::cout << "Catch an exception: " << e << std::endl;
260  return 1;
261  }
262 }
263 
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:183
#define vpTRACE
Definition: vpDebug.h:418
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:2668
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
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
#define vpCTRACE
Definition: vpDebug.h:341
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void eye(unsigned int n)
Definition: vpMatrix.cpp:1181
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72