Visual Servoing Platform  version 3.4.1 under development (2021-10-17)
testColVector.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 vpColVector functionalities.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
45 #include <stdio.h>
46 #include <stdlib.h>
47 
48 #include <visp3/core/vpColVector.h>
49 #include <visp3/core/vpGaussRand.h>
50 #include <visp3/core/vpMath.h>
51 
52 namespace
53 {
54 bool test(const std::string &s, const vpColVector &v, const std::vector<double> &bench)
55 {
56  static unsigned int cpt = 0;
57  std::cout << "** Test " << ++cpt << std::endl;
58  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v.t() << "]^T" << std::endl;
59  if (bench.size() != v.size()) {
60  std::cout << "Test fails: bad size wrt bench" << std::endl;
61  return false;
62  }
63  for (unsigned int i = 0; i < v.size(); i++) {
64  if (std::fabs(v[i] - bench[i]) > std::fabs(v[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 double getRandomValues(double min, double max)
74 {
75  return (max - min) * ((double)rand() / (double)RAND_MAX) + min;
76 }
77 }
78 
79 int main()
80 {
81  {
82  vpColVector v1(7, 0.1), v2;
83  if (v1 == v2) {
84  std::cerr << "Issue with vpColVector comparison operator." << std::endl;
85  return EXIT_FAILURE;
86  }
87  v2 = v1;
88  if (v1 != v2) {
89  std::cerr << "Issue with vpColVector comparison operator." << std::endl;
90  return EXIT_FAILURE;
91  }
92  v2[3] = 0.2;
93  if (v1 == v2) {
94  std::cerr << "Issue with vpColVector comparison operator." << std::endl;
95  return EXIT_FAILURE;
96  }
97  }
98  {
99  vpColVector v;
100 
101  v.resize(4);
102  v = 3;
103  std::vector<double> bench1(4, 3);
104  if (test("v", v, bench1) == false)
105  return EXIT_FAILURE;
106  std::vector<double> bench2(4, 3. / 6);
107  v.normalize();
108  if (test("v", v, bench2) == false)
109  return EXIT_FAILURE;
110 
111  v.resize(5, 1, true);
112  std::vector<double> bench3(5, 0);
113  if (test("v", v, bench3) == false)
114  return EXIT_FAILURE;
115  }
116 
117  {
118  vpColVector v(4);
119  std::vector<double> bench1(4);
120  for (unsigned int i = 0; i < v.size(); i++) {
121  v[i] = (double)i;
122  bench1[i] = (double)i;
123  }
124  if (test("v", v, bench1) == false)
125  return EXIT_FAILURE;
126 
127  vpColVector w;
128  w.init(v, 0, 2);
129  std::vector<double> bench2;
130  bench2.push_back(0);
131  bench2.push_back(1);
132  if (test("w", w, bench2) == false)
133  return EXIT_FAILURE;
134 
135  std::vector<double> bench3;
136  bench3.push_back(1);
137  bench3.push_back(2);
138  bench3.push_back(3);
139 
140  vpColVector r1;
141  for (size_t i = 0; i < 4; i++)
142  r1.stack((double)i);
143 
144  vpColVector r2 = r1.extract(1, 3);
145  if (test("r2", r2, bench3) == false)
146  return EXIT_FAILURE;
147  }
148 
149  {
150  vpMatrix M(4, 1);
151  std::vector<double> bench(4);
152  for (unsigned int i = 0; i < M.getRows(); i++) {
153  M[i][0] = i;
154  bench[i] = i;
155  }
156  if (test("M", M, bench) == false)
157  return EXIT_FAILURE;
158  vpColVector v;
159  v = M;
160  if (test("v", v, bench) == false)
161  return EXIT_FAILURE;
162  vpColVector w(M);
163  if (test("w", w, bench) == false)
164  return EXIT_FAILURE;
165  vpColVector z1(bench);
166  if (test("z1", z1, bench) == false)
167  return EXIT_FAILURE;
168  vpColVector z2 = bench;
169  if (test("z2", z2, bench) == false)
170  return EXIT_FAILURE;
171  }
172 
173  {
174  vpColVector v(3);
175  v[0] = 1;
176  v[1] = 2;
177  v[2] = 3;
178  std::vector<double> bench1;
179  bench1.push_back(3);
180  bench1.push_back(6);
181  bench1.push_back(9);
182 
183  vpColVector w = v * 3;
184  // v is unchanged
185  // w is now equal to : [3 6 9]
186  if (test("w", w, bench1) == false)
187  return EXIT_FAILURE;
188 
189  vpColVector x(w);
190  if (test("x", x, bench1) == false)
191  return EXIT_FAILURE;
192 
193  std::vector<float> bench2;
194  bench2.push_back(3);
195  bench2.push_back(6);
196  bench2.push_back(9);
197  vpColVector y1(bench2);
198  if (test("y1", y1, bench1) == false)
199  return EXIT_FAILURE;
200  vpColVector y2 = bench2;
201  if (test("y2", y2, bench1) == false)
202  return EXIT_FAILURE;
203  }
204 
205  {
206  vpColVector r1(3, 1);
207  vpColVector r2 = -r1;
208  std::vector<double> bench(3, -1);
209  // v contains [-1 -1 -1]
210  if (test("r2", r2, bench) == false)
211  return EXIT_FAILURE;
212  r2.stack(-2);
213  bench.push_back(-2);
214  if (test("r2", r2, bench) == false)
215  return EXIT_FAILURE;
216  vpColVector r3 = vpColVector::stack(r1, r2);
217  std::vector<double> bench3(7, 1);
218  bench3[3] = bench3[4] = bench3[5] = -1;
219  bench3[6] = -2;
220  if (test("r3", r3, bench3) == false)
221  return EXIT_FAILURE;
222 
223  r1.stack(r2);
224  if (test("r1", r1, bench3) == false)
225  return EXIT_FAILURE;
226  }
227 
228  {
229  vpColVector r1(3, 2);
230  vpColVector r2(3, 4);
231  std::cout << "test r1: " << r1 << std::endl;
232  std::cout << "test r2: " << r2 << std::endl;
233  vpColVector r = r1 + r2;
234  std::cout << "test r1+r2: " << r1 + r2 << std::endl;
235  std::cout << "test r: " << r << std::endl;
236  std::vector<double> bench(3, 6);
237  if (test("r", r, bench) == false)
238  return EXIT_FAILURE;
239  r1 += r2;
240  if (test("r1", r1, bench) == false)
241  return EXIT_FAILURE;
242  }
243 
244  {
245  vpColVector r1(3, 2);
246  vpColVector r2(3, 4);
247  vpColVector r = r1 - r2;
248  std::vector<double> bench(3, -2);
249  if (test("r", r, bench) == false)
250  return EXIT_FAILURE;
251  r1 -= r2;
252  if (test("r1", r1, bench) == false)
253  return EXIT_FAILURE;
254  }
255 
256  {
257  vpColVector r(5, 1);
258  r.clear();
259  r.resize(5);
260  r = 5;
261  std::vector<double> bench(5, 5);
262  if (test("r", r, bench) == false)
263  return EXIT_FAILURE;
264  }
265 
266  {
267  // Test mean, median and standard deviation against Matlab with rng(0) and
268  // rand(10,1)*10
269  vpColVector r(10);
270  r[0] = 8.1472;
271  r[1] = 9.0579;
272  r[2] = 1.2699;
273  r[3] = 9.1338;
274  r[4] = 6.3236;
275  r[5] = 0.9754;
276  r[6] = 2.7850;
277  r[7] = 5.4688;
278  r[8] = 9.5751;
279  r[9] = 9.6489;
280 
281  std::cout << "** Test mean" << std::endl;
282  double res = vpColVector::mean(r);
283  if (!vpMath::equal(res, 6.2386, 0.001)) {
284  std::cout << "Test fails: bad mean " << res << std::endl;
285  return EXIT_FAILURE;
286  }
287 
288  std::cout << "** Test stdev" << std::endl;
289  res = vpColVector::stdev(r);
290  if (!vpMath::equal(res, 3.2810, 0.001)) {
291  std::cout << "Test fails: bad stdev " << res << std::endl;
292  return EXIT_FAILURE;
293  }
294 
295  std::cout << "** Test stdev(bessel)" << std::endl;
296  res = vpColVector::stdev(r, true);
297  if (!vpMath::equal(res, 3.4585, 0.001)) {
298  std::cout << "Test fails: bad stdev(bessel) " << res << std::endl;
299  return EXIT_FAILURE;
300  }
301 
302  std::cout << "** Test median" << std::endl;
303  res = vpColVector::median(r);
304  if (!vpMath::equal(res, 7.2354, 0.001)) {
305  std::cout << "Test fails: bad median " << res << std::endl;
306  return EXIT_FAILURE;
307  }
308 
309  // Test median with odd number of elements
310  std::cout << "** Test median (odd)" << std::endl;
311  r.stack(1.5761);
312  res = vpColVector::median(r);
313  if (!vpMath::equal(res, 6.3236, 0.001)) {
314  std::cout << "Test fails: bad median (odd) " << res << std::endl;
315  return EXIT_FAILURE;
316  }
317  std::cout << "r: [" << r << "]^T" << std::endl;
318  r.print(std::cout, 8, "r");
319  }
320 
321  {
322  // Test insert with big vector
323  unsigned int nb = 1000;
324  const unsigned int size = 10000;
325  std::vector<vpColVector> vec(nb);
326 
327  for (size_t i = 0; i < nb; i++) {
328  vpColVector v(size);
329  for (unsigned int j = 0; j < size; j++) {
330  v[j] = getRandomValues(-100.0, 100.0);
331  }
332  vec[i] = v;
333  }
334 
335  vpColVector v_big(nb * size);
336  double t = vpTime::measureTimeMs();
337  for (unsigned int i = 0; i < nb; i++) {
338  v_big.insert(i * size, vec[(size_t)i]);
339  }
340  t = vpTime::measureTimeMs() - t;
341  std::cout << "\nBig insert: " << t << " ms" << std::endl;
342 
343  for (unsigned int i = 0; i < nb; i++) {
344  for (unsigned int j = 0; j < size; j++) {
345  if (!vpMath::equal(v_big[i * size + j], vec[(size_t)i][j], std::numeric_limits<double>::epsilon())) {
346  std::cerr << "Problem in vpColVector insert()!" << std::endl;
347  return EXIT_FAILURE;
348  }
349  }
350  }
351 
352  // Try to insert empty vpColVector
353  vpColVector v1(2), v2, v3;
354  v1.insert(0, v2);
355  v3.insert(0, v2);
356 
357  std::cout << "Insert empty vectors:" << std::endl;
358  std::cout << "v1: " << v1.t() << std::endl;
359  std::cout << "v2: " << v2.t() << std::endl;
360  std::cout << "v3: " << v3.t() << std::endl;
361  }
362 
363  {
364  std::cout << "** Test conversion to/from std::vector" << std::endl;
365  std::vector<double> std_vector(5);
366  for (size_t i = 0; i < std_vector.size(); i++) {
367  std_vector[i] = (double) i;
368  }
369  vpColVector v(std_vector);
370  if (test("v", v, std_vector) == false)
371  return EXIT_FAILURE;
372 
373  std_vector.clear();
374  std_vector = v.toStdVector();
375  if (test("v", v, std_vector) == false)
376  return EXIT_FAILURE;
377  }
378  std::cout << "\nAll tests succeed" << std::endl;
379  return EXIT_SUCCESS;
380 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:293
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:220
unsigned int getRows() const
Definition: vpArray2D.h:289
vpRowVector t() const
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:291
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static double median(const vpColVector &v)
unsigned int getCols() const
Definition: vpArray2D.h:279
vpColVector & normalize()
void clear()
Definition: vpColVector.h:175
static double mean(const vpColVector &v)
void insert(unsigned int i, const vpColVector &v)
std::vector< double > toStdVector()
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:310
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void stack(double d)
void init(const vpColVector &v, unsigned int r, unsigned int nrows)
static double stdev(const vpColVector &v, bool useBesselCorrection=false)