ViSP  3.0.0
testColVector.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 vpColVector functionalities.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
44 #include <stdlib.h>
45 #include <stdio.h>
46 
47 #include <visp3/core/vpMath.h>
48 #include <visp3/core/vpColVector.h>
49 
50 
51 bool test(const std::string &s, const vpColVector &v, const std::vector<double> &bench)
52 {
53  static unsigned int cpt = 0;
54  std::cout << "** Test " << ++cpt << std::endl;
55  std::cout << s << "(" << v.getRows() << "," << v.getCols() << ") = [" << v.t() << "]^T" << std::endl;
56  if(bench.size() != v.size()) {
57  std::cout << "Test fails: bad size wrt bench" << std::endl;
58  return false;
59  }
60  for (unsigned int i=0; i<v.size(); i++) {
61  if (std::fabs(v[i]-bench[i]) > std::fabs(v[i])*std::numeric_limits<double>::epsilon()) {
62  std::cout << "Test fails: bad content" << std::endl;
63  return false;
64  }
65  }
66 
67  return true;
68 }
69 
70 
71 
72 int main()
73 {
74  int err = 1;
75 
76  {
77  vpColVector v;
78 
79  v.resize(4);
80  v = 3;
81  std::vector<double> bench1(4, 3);
82  if (test("v", v, bench1) == false)
83  return err;
84  std::vector<double> bench2(4, 3./6);
85  v.normalize();
86  if (test("v", v, bench2) == false)
87  return err;
88 
89  v.resize(5, 1, true);
90  std::vector<double> bench3(5, 0);
91  if (test("v", v, bench3) == false)
92  return err;
93  }
94 
95  {
96  vpColVector v(4);
97  std::vector<double> bench1(4);
98  for(unsigned int i=0; i<v.size(); i++) {
99  v[i] = (double)i;
100  bench1[i] = (double)i;
101  }
102  if (test("v", v, bench1) == false)
103  return err;
104 
105  vpColVector w;
106  w.init(v, 0, 2);
107  std::vector<double> bench2;
108  bench2.push_back(0);
109  bench2.push_back(1);
110  if (test("w", w, bench2) == false)
111  return err;
112 
113  std::vector<double> bench3;
114  bench3.push_back(1);
115  bench3.push_back(2);
116  bench3.push_back(3);
117 
118  vpColVector r1;
119  for(size_t i=0; i<4; i++)
120  r1.stack((double)i);
121 
122  vpColVector r2 = r1.extract(1, 3);
123  if (test("r2", r2, bench3) == false)
124  return err;
125  }
126  {
127  vpMatrix M(4, 1);
128  std::vector<double> bench(4);
129  for(unsigned int i=0; i<M.getRows(); i++) {
130  M[i][0] = i;
131  bench[i] = i;
132  }
133  if (test("M", M, bench) == false)
134  return err;
135  vpColVector v;
136  v = M;
137  if (test("v", v, bench) == false)
138  return err;
139  vpColVector w(M);
140  if (test("w", w, bench) == false)
141  return err;
142  vpColVector z1(bench);
143  if (test("z1", z1, bench) == false)
144  return err;
145  vpColVector z2 = bench;
146  if (test("z2", z2, bench) == false)
147  return err;
148  }
149  {
150  vpColVector v(3);
151  v[0] = 1;
152  v[1] = 2;
153  v[2] = 3;
154  std::vector<double> bench1;
155  bench1.push_back(3);
156  bench1.push_back(6);
157  bench1.push_back(9);
158 
159  vpColVector w = v * 3;
160  // v is unchanged
161  // w is now equal to : [3 6 9]
162  if (test("w", w, bench1) == false)
163  return err;
164 
165  vpColVector x(w);
166  if (test("x", x, bench1) == false)
167  return err;
168 
169  std::vector<float> bench2;
170  bench2.push_back(3);
171  bench2.push_back(6);
172  bench2.push_back(9);
173  vpColVector y1(bench2);
174  if (test("y1", y1, bench1) == false)
175  return err;
176  vpColVector y2 = bench2;
177  if (test("y2", y2, bench1) == false)
178  return err;
179  }
180  {
181  vpColVector r1(3, 1);
182  vpColVector r2 = -r1;
183  std::vector<double> bench(3,-1);
184  // v contains [-1 -1 -1]
185  if (test("r2", r2, bench) == false)
186  return err;
187  r2.stack(-2);
188  bench.push_back(-2);
189  if (test("r2", r2, bench) == false)
190  return err;
191  vpColVector r3 = vpColVector::stack(r1, r2);
192  std::vector<double> bench3(7, 1);
193  bench3[3] = bench3[4] = bench3[5] = -1;
194  bench3[6] = -2;
195  if (test("r3", r3, bench3) == false)
196  return err;
197 
198  r1.stack(r2);
199  if (test("r1", r1, bench3) == false)
200  return err;
201  }
202  {
203  vpColVector r1(3, 2);
204  vpColVector r2(3, 4);
205  std::cout << "test r1: " << r1 << std::endl;
206  std::cout << "test r2: " << r2 << std::endl;
207  vpColVector r = r1 + r2;
208  std::cout << "test r1+r2: " << r1+r2 << std::endl;
209  std::cout << "test r: " << r << std::endl;
210  std::vector<double> bench(3, 6);
211  if (test("r", r, bench) == false)
212  return err;
213  r1 += r2;
214  if (test("r1", r1, bench) == false)
215  return err;
216  }
217  {
218  vpColVector r1(3, 2);
219  vpColVector r2(3, 4);
220  vpColVector r = r1 - r2;
221  std::vector<double> bench(3, -2);
222  if (test("r", r, bench) == false)
223  return err;
224  r1 -= r2;
225  if (test("r1", r1, bench) == false)
226  return err;
227  }
228  {
229  vpColVector r(5, 1);
230  r.clear();
231  r.resize(5);
232  r = 5;
233  std::vector<double> bench(5, 5);
234  if (test("r", r, bench) == false)
235  return err;
236  }
237  {
238  // Test mean, median and standard deviation against Matlab with rng(0) and rand(10,1)*10
239  vpColVector r(10);
240  r[0] = 8.1472;
241  r[1] = 9.0579;
242  r[2] = 1.2699;
243  r[3] = 9.1338;
244  r[4] = 6.3236;
245  r[5] = 0.9754;
246  r[6] = 2.7850;
247  r[7] = 5.4688;
248  r[8] = 9.5751;
249  r[9] = 9.6489;
250 
251  std::cout << "** Test mean" << std::endl;
252  double res = vpColVector::mean(r);
253  if(!vpMath::equal(res, 6.2386, 0.001)) {
254  std::cout << "Test fails: bad mean " << res << std::endl;
255  return err;
256  }
257 
258  std::cout << "** Test stdev" << std::endl;
259  res = vpColVector::stdev(r);
260  if(!vpMath::equal(res, 3.2810, 0.001)) {
261  std::cout << "Test fails: bad stdev " << res << std::endl;
262  return err;
263  }
264 
265  std::cout << "** Test stdev(bessel)" << std::endl;
266  res = vpColVector::stdev(r, true);
267  if(!vpMath::equal(res, 3.4585, 0.001)) {
268  std::cout << "Test fails: bad stdev(bessel) " << res << std::endl;
269  return err;
270  }
271 
272  std::cout << "** Test median" << std::endl;
273  res = vpColVector::median(r);
274  if(!vpMath::equal(res, 7.2354, 0.001)) {
275  std::cout << "Test fails: bad median " << res << std::endl;
276  return err;
277  }
278 
279  // Test median with odd number of elements
280  std::cout << "** Test median (odd)" << std::endl;
281  r.stack(1.5761);
282  res = vpColVector::median(r);
283  if(!vpMath::equal(res, 6.3236, 0.001)) {
284  std::cout << "Test fails: bad median (odd) " << res << std::endl;
285  return err;
286  }
287  std::cout << "r: [" << r << "]^T" << std::endl;
288  r.print(std::cout, 8, "r");
289  }
290  std::cout << "All tests succeed" << std::endl;
291  return 0;
292 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:146
void stack(const double &d)
static double stdev(const vpColVector &v, const bool useBesselCorrection=false)
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:297
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
static double median(const vpColVector &v)
int print(std::ostream &s, unsigned int length, char const *intro=0) const
vpColVector & normalize()
void clear()
Definition: vpColVector.h:106
vpRowVector t() const
static double mean(const vpColVector &v)
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void init(const vpColVector &v, unsigned int r, unsigned int nrows)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:217