ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testMatrix.cpp
1 /****************************************************************************
2  *
3  * $Id: testMatrix.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
68 void usage(const char *name, const char *badparam)
69 {
70  fprintf(stdout, "\n\
71 Test some vpMatrix functionalities.\n\
72 \n\
73 SYNOPSIS\n\
74  %s [-h]\n", name);
75 
76  fprintf(stdout, "\n\
77 OPTIONS: Default\n\
78  -h\n\
79  Print the help.\n");
80 
81  if (badparam)
82  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
83 }
91 bool getOptions(int argc, const char **argv)
92 {
93  const char *optarg;
94  int c;
95  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
96 
97  switch (c) {
98  case 'h': usage(argv[0], NULL); return false; break;
99 
100  default:
101  usage(argv[0], optarg);
102  return false; break;
103  }
104  }
105 
106  if ((c == 1) || (c == -1)) {
107  // standalone param or error
108  usage(argv[0], NULL);
109  std::cerr << "ERROR: " << std::endl;
110  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
111  return false;
112  }
113 
114  return true;
115 }
116 
117 
118 int
119 main(int argc, const char ** argv)
120 {
121  // Read the command line options
122  if (getOptions(argc, argv) == false) {
123  exit (-1);
124  }
125 
126  vpTRACE("------------------------");
127  vpTRACE("--- TEST PRETTY PRINT---");
128  vpTRACE("------------------------");
129  vpMatrix M ;
130  M.eye(4);
131 
132  vpTRACE("call std::cout << M;");
133  std::cout << M << std::endl;
134 
135  vpTRACE("call M.print (std::cout, 4);");
136  M.print (std::cout, 4);
137 
138  vpTRACE("------------------------");
139  M.resize(3,3) ;
140  M.eye(3);
141  M[1][0]=1.235;
142  M[1][1]=12.345;
143  M[1][2]=.12345;
144  vpTRACE("call std::cout << M;");
145  std::cout << M;
146  vpTRACE("call M.print (std::cout, 6);");
147  M.print (std::cout, 6);
148 
149  vpTRACE("------------------------");
150  M[0][0]=-1.235;
151  M[1][0]=-12.235;
152 
153  vpTRACE("call std::cout << M;");
154  std::cout << M;
155 
156  vpTRACE("call M.print (std::cout, 10);");
157  M.print (std::cout, 10);
158 
159  vpTRACE("call M.print (std::cout, 2);");
160  M.print (std::cout, 2);
161 
162  vpTRACE("------------------------");
163  M.resize(3,3) ;
164  M.eye(3);
165  M[0][2]=-0.0000000876;
166  vpTRACE("call std::cout << M;");
167  std::cout << M;
168 
169  vpTRACE("call M.print (std::cout, 4);");
170  M.print (std::cout, 4);
171  vpTRACE("call M.print (std::cout, 10, \"M\");");
172  M.print (std::cout, 10, "M");
173  vpTRACE("call M.print (std::cout, 20, \"M\");");
174  M.print (std::cout, 20, "M");
175 
176 
177  vpTRACE("------------------------");
178  vpTRACE("--- TEST RESIZE --------");
179  vpTRACE("------------------------");
180  vpCTRACE << "5x5" << std::endl;
181  M.resize(5,5,false);
182  vpCTRACE << std::endl<< M;
183  vpCTRACE << "3x2" << std::endl;
184  M.resize(3,2,false);
185  vpCTRACE <<std::endl<< M;
186  vpCTRACE << "2x2" << std::endl;
187  M.resize(2,2,false);
188  vpCTRACE << std::endl<<M;
189  vpTRACE("------------------------");
190 
191 
193  vpMatrix A(1,6),B;
194 
195  A=1.0;
196  //vMe=1.0;
197  B=A*vMe;
198 
199  vpTRACE("------------------------");
200  vpTRACE("--- TEST vpRowVector * vpColVector");
201  vpTRACE("------------------------");
202  vpRowVector r(3);
203  r[0] = 2;
204  r[1] = 3;
205  r[2] = 4;
206 
207  vpColVector c(3);
208  c[0] = 1;
209  c[1] = 2;
210  c[2] = -1;
211 
212  double rc = r * c;
213 
214  r.print(std::cout, 2, "r");
215  c.print(std::cout, 2, "c");
216  std::cout << "r * c = " << rc << std::endl;
217 
218  vpTRACE("------------------------");
219  vpTRACE("--- TEST vpRowVector * vpMatrix");
220  vpTRACE("------------------------");
221  M.resize(3,3) ;
222  M.eye(3);
223 
224  M[1][0] = 1.5;
225  M[2][0] = 2.3;
226 
227  vpRowVector rM = r * M;
228 
229  r.print(std::cout, 2, "r");
230  M.print(std::cout, 10, "M");
231  std::cout << "r * M = " << rM << std::endl;
232 
233  vpTRACE("------------------------");
234  vpTRACE("--- TEST vpGEMM ");
235  vpTRACE("------------------------");
236  M.resize(3,3) ;
237  M.eye(3);
238  vpMatrix N(3, 3);
239  N[0][0] = 2;
240  N[1][0] = 1.2;
241  N[1][2] = 0.6;
242  N[2][2] = 0.25;
243 
244  vpMatrix C(3, 3);
245  C.eye(3);
246 
247  vpMatrix D;
248 
249  //realise the operation D = 2 * M^T * N + 3 C
250  vpGEMM(M, N, 2, C, 3, D, VP_GEMM_A_T);
251  std::cout << D << std::endl;
252 
253 }
254 
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:174
#define vpTRACE
Definition: vpDebug.h:401
Definition of the row vector class.
Definition: vpRowVector.h:73
int print(std::ostream &s, unsigned int length, char const *intro=0)
Definition: vpMatrix.cpp:2615
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:327
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void eye(unsigned int n)
Definition: vpMatrix.cpp:1132
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72