43 #include <visp3/core/vpColVector.h>
44 #include <visp3/core/vpGaussRand.h>
45 #include <visp3/core/vpMath.h>
47 #ifdef ENABLE_VISP_NAMESPACE
53 bool test(
const std::string &s,
const vpColVector &v,
const std::vector<double> &bench)
55 static unsigned int cpt = 0;
56 std::cout <<
"** Test " << ++cpt << std::endl;
57 std::cout << s <<
"(" << v.
getRows() <<
"," << v.
getCols() <<
") = [" << v.
t() <<
"]^T" << std::endl;
58 if (bench.size() != v.
size()) {
59 std::cout <<
"Test fails: bad size wrt bench" << std::endl;
62 for (
unsigned int i = 0; i < v.
size(); i++) {
63 if (std::fabs(v[i] - bench[i]) > std::fabs(v[i]) * std::numeric_limits<double>::epsilon()) {
64 std::cout <<
"Test fails: bad content" << std::endl;
72 double getRandomValues(
double min,
double max) {
return (max - min) * ((double)rand() / (double)RAND_MAX) + min; }
80 std::cerr <<
"Issue with vpColVector comparison operator." << std::endl;
85 std::cerr <<
"Issue with vpColVector comparison operator." << std::endl;
90 std::cerr <<
"Issue with vpColVector comparison operator." << std::endl;
99 std::vector<double> bench1(4, 3);
100 if (test(
"v", v, bench1) ==
false)
102 std::vector<double> bench2(4, 3. / 6);
104 if (test(
"v", v, bench2) ==
false)
108 std::vector<double> bench3(5, 0);
109 if (test(
"v", v, bench3) ==
false)
115 std::vector<double> bench1(4);
116 for (
unsigned int i = 0; i < v.
size(); i++) {
118 bench1[i] = (double)i;
120 if (test(
"v", v, bench1) ==
false)
125 std::vector<double> bench2;
128 if (test(
"w", w, bench2) ==
false)
131 std::vector<double> bench3;
137 for (
size_t i = 0; i < 4; i++)
141 if (test(
"r2", r2, bench3) ==
false)
147 std::vector<double> bench(4);
148 for (
unsigned int i = 0; i < M.getRows(); i++) {
156 if (test(
"v", v, bench) ==
false)
159 if (test(
"w", w, bench) ==
false)
162 if (test(
"z1", z1, bench) ==
false)
165 if (test(
"z2", z2, bench) ==
false)
174 std::vector<double> bench1;
182 if (test(
"w", w, bench1) ==
false)
186 if (test(
"x", x, bench1) ==
false)
189 std::vector<float> bench2;
194 if (test(
"y1", y1, bench1) ==
false)
197 if (test(
"y2", y2, bench1) ==
false)
204 std::vector<double> bench(3, -1);
206 if (test(
"r2", r2, bench) ==
false)
210 if (test(
"r2", r2, bench) ==
false)
213 std::vector<double> bench3(7, 1);
214 bench3[3] = bench3[4] = bench3[5] = -1;
216 if (test(
"r3", r3, bench3) ==
false)
220 if (test(
"r1", r1, bench3) ==
false)
227 std::cout <<
"test r1: " << r1 << std::endl;
228 std::cout <<
"test r2: " << r2 << std::endl;
230 std::cout <<
"test r1+r2: " << r1 + r2 << std::endl;
231 std::cout <<
"test r: " << r << std::endl;
232 std::vector<double> bench(3, 6);
233 if (test(
"r", r, bench) ==
false)
236 if (test(
"r1", r1, bench) ==
false)
244 std::vector<double> bench(3, -2);
245 if (test(
"r", r, bench) ==
false)
248 if (test(
"r1", r1, bench) ==
false)
257 std::vector<double> bench(5, 5);
258 if (test(
"r", r, bench) ==
false)
277 std::cout <<
"** Test mean" << std::endl;
280 std::cout <<
"Test fails: bad mean " << res << std::endl;
284 std::cout <<
"** Test stdev" << std::endl;
287 std::cout <<
"Test fails: bad stdev " << res << std::endl;
291 std::cout <<
"** Test stdev(bessel)" << std::endl;
294 std::cout <<
"Test fails: bad stdev(bessel) " << res << std::endl;
298 std::cout <<
"** Test median" << std::endl;
301 std::cout <<
"Test fails: bad median " << res << std::endl;
306 std::cout <<
"** Test median (odd)" << std::endl;
310 std::cout <<
"Test fails: bad median (odd) " << res << std::endl;
313 std::cout <<
"r: [" << r <<
"]^T" << std::endl;
314 r.
print(std::cout, 8,
"r");
319 unsigned int nb = 1000;
320 const unsigned int size = 10000;
321 std::vector<vpColVector> vec(nb);
323 for (
size_t i = 0; i < nb; i++) {
325 for (
unsigned int j = 0; j < size; j++) {
326 v[j] = getRandomValues(-100.0, 100.0);
333 for (
unsigned int i = 0; i < nb; i++) {
334 v_big.insert(i * size, vec[(
size_t)i]);
337 std::cout <<
"\nBig insert: " << t <<
" ms" << std::endl;
339 for (
unsigned int i = 0; i < nb; i++) {
340 for (
unsigned int j = 0; j < size; j++) {
341 if (!
vpMath::equal(v_big[i * size + j], vec[(
size_t)i][j], std::numeric_limits<double>::epsilon())) {
342 std::cerr <<
"Problem in vpColVector insert()!" << std::endl;
353 std::cout <<
"Insert empty vectors:" << std::endl;
354 std::cout <<
"v1: " << v1.t() << std::endl;
355 std::cout <<
"v2: " << v2.
t() << std::endl;
356 std::cout <<
"v3: " << v3.
t() << std::endl;
360 std::cout <<
"** Test conversion to/from std::vector" << std::endl;
361 std::vector<double> std_vector(5);
362 for (
size_t i = 0; i < std_vector.size(); i++) {
363 std_vector[i] = (double)i;
366 if (test(
"v", v, std_vector) ==
false)
371 if (test(
"v", v, std_vector) ==
false)
376 std::cout <<
"** Test operator == and operator !=" << std::endl;
379 std::cout <<
"v: " << v.
t() <<
" != " << val << std::endl;
383 std::cout <<
"v: " << v.
t() <<
" == " << val << std::endl;
387 std::cout <<
"v: " << v.
t() <<
" == " << val << std::endl;
391 std::cout <<
"\nAll tests succeed" << std::endl;
unsigned int getCols() const
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getRows() const
Implementation of column vector and the associated operations.
vpColVector extract(unsigned int r, unsigned int colsize) const
vpColVector & normalize()
static double median(const vpColVector &v)
int print(std::ostream &s, unsigned int length, char const *intro=0) const
void init(const vpColVector &v, unsigned int r, unsigned int nrows)
std::vector< double > toStdVector() const
static double mean(const vpColVector &v)
static double stdev(const vpColVector &v, bool useBesselCorrection=false)
void insert(unsigned int i, const vpColVector &v)
void resize(unsigned int i, bool flagNullify=true)
static bool equal(double x, double y, double threshold=0.001)
Implementation of a matrix and operations on matrices.
VISP_EXPORT double measureTimeMs()