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