48 #include <visp3/core/vpColVector.h> 49 #include <visp3/core/vpGaussRand.h> 50 #include <visp3/core/vpMath.h> 54 bool test(
const std::string &s,
const vpColVector &v,
const std::vector<double> &bench)
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;
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;
77 for (
unsigned int i = 0; i < v.
getRows(); i++) {
84 double computeRegularSumSquare(
const vpColVector &v)
86 double sum_square = 0.0;
88 for (
unsigned int i = 0; i < v.
getRows(); i++) {
89 sum_square += v[i] * v[i];
97 double mean_value = computeRegularSum(v) / v.
getRows();
98 double sum_squared_diff = 0.0;
100 for (
unsigned int i = 0; i < v.
size(); i++) {
101 sum_squared_diff += (v[i] - mean_value) * (v[i] - mean_value);
104 double divisor = (double)v.
size();
106 return std::sqrt(sum_squared_diff / divisor);
109 double getRandomValues(
const double min,
const double max)
111 return (max - min) * ((double)rand() / (double)RAND_MAX) + min;
120 std::cerr <<
"Issue with vpColVector comparison operator." << std::endl;
125 std::cerr <<
"Issue with vpColVector comparison operator." << std::endl;
130 std::cerr <<
"Issue with vpColVector comparison operator." << std::endl;
139 std::vector<double> bench1(4, 3);
140 if (test(
"v", v, bench1) ==
false)
142 std::vector<double> bench2(4, 3. / 6);
144 if (test(
"v", v, bench2) ==
false)
148 std::vector<double> bench3(5, 0);
149 if (test(
"v", v, bench3) ==
false)
155 std::vector<double> bench1(4);
156 for (
unsigned int i = 0; i < v.
size(); i++) {
158 bench1[i] = (double)i;
160 if (test(
"v", v, bench1) ==
false)
165 std::vector<double> bench2;
168 if (test(
"w", w, bench2) ==
false)
171 std::vector<double> bench3;
177 for (
size_t i = 0; i < 4; i++)
181 if (test(
"r2", r2, bench3) ==
false)
187 std::vector<double> bench(4);
188 for (
unsigned int i = 0; i < M.getRows(); i++) {
192 if (test(
"M", M, bench) ==
false)
196 if (test(
"v", v, bench) ==
false)
199 if (test(
"w", w, bench) ==
false)
202 if (test(
"z1", z1, bench) ==
false)
205 if (test(
"z2", z2, bench) ==
false)
214 std::vector<double> bench1;
222 if (test(
"w", w, bench1) ==
false)
226 if (test(
"x", x, bench1) ==
false)
229 std::vector<float> bench2;
234 if (test(
"y1", y1, bench1) ==
false)
237 if (test(
"y2", y2, bench1) ==
false)
244 std::vector<double> bench(3, -1);
246 if (test(
"r2", r2, bench) ==
false)
250 if (test(
"r2", r2, bench) ==
false)
253 std::vector<double> bench3(7, 1);
254 bench3[3] = bench3[4] = bench3[5] = -1;
256 if (test(
"r3", r3, bench3) ==
false)
260 if (test(
"r1", r1, bench3) ==
false)
267 std::cout <<
"test r1: " << r1 << std::endl;
268 std::cout <<
"test r2: " << r2 << std::endl;
270 std::cout <<
"test r1+r2: " << r1 + r2 << std::endl;
271 std::cout <<
"test r: " << r << std::endl;
272 std::vector<double> bench(3, 6);
273 if (test(
"r", r, bench) ==
false)
276 if (test(
"r1", r1, bench) ==
false)
284 std::vector<double> bench(3, -2);
285 if (test(
"r", r, bench) ==
false)
288 if (test(
"r1", r1, bench) ==
false)
297 std::vector<double> bench(5, 5);
298 if (test(
"r", r, bench) ==
false)
317 std::cout <<
"** Test mean" << std::endl;
320 std::cout <<
"Test fails: bad mean " << res << std::endl;
324 std::cout <<
"** Test stdev" << std::endl;
327 std::cout <<
"Test fails: bad stdev " << res << std::endl;
331 std::cout <<
"** Test stdev(bessel)" << std::endl;
334 std::cout <<
"Test fails: bad stdev(bessel) " << res << std::endl;
338 std::cout <<
"** Test median" << std::endl;
341 std::cout <<
"Test fails: bad median " << res << std::endl;
346 std::cout <<
"** Test median (odd)" << std::endl;
350 std::cout <<
"Test fails: bad median (odd) " << res << std::endl;
353 std::cout <<
"r: [" << r <<
"]^T" << std::endl;
354 r.
print(std::cout, 8,
"r");
362 int nbIterations = 1000;
363 unsigned int size = 117;
366 for (
unsigned int cpt = 0; cpt < v.
getRows(); cpt++) {
367 v[cpt] = rand() % 1000 + noise();
370 std::cout <<
"\nv.getRows()=" << v.
getRows() << std::endl;
372 double regular_sum = 0.0;
374 for (
int iteration = 0; iteration < nbIterations; iteration++) {
375 regular_sum += computeRegularSum(v);
379 double sse_sum = 0.0;
381 for (
int iteration = 0; iteration < nbIterations; iteration++) {
386 std::cout <<
"\nregular_sum=" << regular_sum <<
" ; sse_sum=" << sse_sum << std::endl;
387 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
388 std::cout <<
"Speed-up: " << (t_regular / t_sse) <<
"X" << std::endl;
390 if (!
vpMath::equal(regular_sum, sse_sum, std::numeric_limits<double>::epsilon())) {
391 std::cerr <<
"Problem when computing v.sum()!" << std::endl;
395 double regular_sumSquare = 0.0;
397 for (
int iteration = 0; iteration < nbIterations; iteration++) {
398 regular_sumSquare += computeRegularSumSquare(v);
402 double sse_sumSquare = 0.0;
404 for (
int iteration = 0; iteration < nbIterations; iteration++) {
409 std::cout <<
"\nregular_sumSquare=" << regular_sumSquare <<
" ; sse_sumSquare=" << sse_sumSquare << std::endl;
410 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
411 std::cout <<
"Speed-up: " << (t_regular / t_sse) <<
"X" << std::endl;
413 if (!
vpMath::equal(regular_sumSquare, sse_sumSquare, std::numeric_limits<double>::epsilon())) {
414 std::cerr <<
"Problem when computing v.sumSquare()!" << std::endl;
418 double regular_stdev = 0.0;
420 for (
int iteration = 0; iteration < nbIterations; iteration++) {
421 regular_stdev += computeRegularStdev(v);
425 double sse_stdev = 0.0;
427 for (
int iteration = 0; iteration < nbIterations; iteration++) {
432 std::cout <<
"\nregular_stdev=" << regular_stdev <<
" ; sse_stdev=" << sse_stdev << std::endl;
433 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
434 std::cout <<
"Speed-up: " << (t_regular / t_sse) <<
"X" << std::endl;
436 if (!
vpMath::equal(regular_stdev, sse_stdev, std::numeric_limits<double>::epsilon())) {
437 std::cerr <<
"Problem when computing vpColVector::stdev()!" << std::endl;
444 const unsigned int nb = 1000;
445 const unsigned int size = 10000;
446 std::vector<vpColVector> vec(nb);
448 for (
size_t i = 0; i < nb; i++) {
450 for (
unsigned int j = 0; j < size; j++) {
451 v[j] = getRandomValues(-100.0, 100.0);
458 for (
unsigned int i = 0; i < nb; i++) {
459 v_big.insert(i * size, vec[(
size_t)i]);
462 std::cout <<
"\nBig insert: " << t <<
" ms" << std::endl;
464 for (
unsigned int i = 0; i < nb; i++) {
465 for (
unsigned int j = 0; j < size; j++) {
466 if (!
vpMath::equal(v_big[i * size + j], vec[(
size_t)i][j], std::numeric_limits<double>::epsilon())) {
467 std::cerr <<
"Problem in vpColVector insert()!" << std::endl;
478 std::cout <<
"Insert empty vectors:" << std::endl;
479 std::cout <<
"v1: " << v1.t() << std::endl;
480 std::cout <<
"v2: " << v2.
t() << std::endl;
481 std::cout <<
"v3: " << v3.
t() << std::endl;
485 std::cout <<
"** Test conversion to/from std::vector" << std::endl;
486 std::vector<double> std_vector(5);
487 for (
size_t i = 0; i < std_vector.size(); i++) {
488 std_vector[i] = (double) i;
491 if (test(
"v", v, std_vector) ==
false)
496 if (test(
"v", v, std_vector) ==
false)
499 std::cout <<
"\nAll tests succeed" << std::endl;
Implementation of a matrix and operations on matrices.
vpColVector extract(unsigned int r, unsigned int colsize) const
static double stdev(const vpColVector &v, const bool useBesselCorrection=false)
static bool equal(double x, double y, double s=0.001)
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getCols() const
VISP_EXPORT double measureTimeMs()
static double median(const vpColVector &v)
int print(std::ostream &s, unsigned int length, char const *intro=0) const
vpColVector & normalize()
static double mean(const vpColVector &v)
unsigned int getRows() const
void insert(unsigned int i, const vpColVector &v)
std::vector< double > toStdVector()
Class for generating random number with normal probability density.
Implementation of column vector and the associated operations.
void init(const vpColVector &v, unsigned int r, unsigned int nrows)
void resize(const unsigned int i, const bool flagNullify=true)