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;
124 std::vector<double> bench1(4, 3);
125 if (test(
"v", v, bench1) ==
false)
127 std::vector<double> bench2(4, 3. / 6);
129 if (test(
"v", v, bench2) ==
false)
133 std::vector<double> bench3(5, 0);
134 if (test(
"v", v, bench3) ==
false)
140 std::vector<double> bench1(4);
141 for (
unsigned int i = 0; i < v.
size(); i++) {
143 bench1[i] = (double)i;
145 if (test(
"v", v, bench1) ==
false)
150 std::vector<double> bench2;
153 if (test(
"w", w, bench2) ==
false)
156 std::vector<double> bench3;
162 for (
size_t i = 0; i < 4; i++)
166 if (test(
"r2", r2, bench3) ==
false)
172 std::vector<double> bench(4);
173 for (
unsigned int i = 0; i < M.getRows(); i++) {
177 if (test(
"M", M, bench) ==
false)
181 if (test(
"v", v, bench) ==
false)
184 if (test(
"w", w, bench) ==
false)
187 if (test(
"z1", z1, bench) ==
false)
190 if (test(
"z2", z2, bench) ==
false)
199 std::vector<double> bench1;
207 if (test(
"w", w, bench1) ==
false)
211 if (test(
"x", x, bench1) ==
false)
214 std::vector<float> bench2;
219 if (test(
"y1", y1, bench1) ==
false)
222 if (test(
"y2", y2, bench1) ==
false)
229 std::vector<double> bench(3, -1);
231 if (test(
"r2", r2, bench) ==
false)
235 if (test(
"r2", r2, bench) ==
false)
238 std::vector<double> bench3(7, 1);
239 bench3[3] = bench3[4] = bench3[5] = -1;
241 if (test(
"r3", r3, bench3) ==
false)
245 if (test(
"r1", r1, bench3) ==
false)
252 std::cout <<
"test r1: " << r1 << std::endl;
253 std::cout <<
"test r2: " << r2 << std::endl;
255 std::cout <<
"test r1+r2: " << r1 + r2 << std::endl;
256 std::cout <<
"test r: " << r << std::endl;
257 std::vector<double> bench(3, 6);
258 if (test(
"r", r, bench) ==
false)
261 if (test(
"r1", r1, bench) ==
false)
269 std::vector<double> bench(3, -2);
270 if (test(
"r", r, bench) ==
false)
273 if (test(
"r1", r1, bench) ==
false)
282 std::vector<double> bench(5, 5);
283 if (test(
"r", r, bench) ==
false)
302 std::cout <<
"** Test mean" << std::endl;
305 std::cout <<
"Test fails: bad mean " << res << std::endl;
309 std::cout <<
"** Test stdev" << std::endl;
312 std::cout <<
"Test fails: bad stdev " << res << std::endl;
316 std::cout <<
"** Test stdev(bessel)" << std::endl;
319 std::cout <<
"Test fails: bad stdev(bessel) " << res << std::endl;
323 std::cout <<
"** Test median" << std::endl;
326 std::cout <<
"Test fails: bad median " << res << std::endl;
331 std::cout <<
"** Test median (odd)" << std::endl;
335 std::cout <<
"Test fails: bad median (odd) " << res << std::endl;
338 std::cout <<
"r: [" << r <<
"]^T" << std::endl;
339 r.
print(std::cout, 8,
"r");
347 int nbIterations = 1000;
348 unsigned int size = 117;
351 for (
unsigned int cpt = 0; cpt < v.
getRows(); cpt++) {
352 v[cpt] = rand() % 1000 + noise();
355 std::cout <<
"\nv.getRows()=" << v.
getRows() << std::endl;
357 double regular_sum = 0.0;
359 for (
int iteration = 0; iteration < nbIterations; iteration++) {
360 regular_sum += computeRegularSum(v);
364 double sse_sum = 0.0;
366 for (
int iteration = 0; iteration < nbIterations; iteration++) {
371 std::cout <<
"\nregular_sum=" << regular_sum <<
" ; sse_sum=" << sse_sum << std::endl;
372 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
373 std::cout <<
"Speed-up: " << (t_regular / t_sse) <<
"X" << std::endl;
375 if (!
vpMath::equal(regular_sum, sse_sum, std::numeric_limits<double>::epsilon())) {
376 std::cerr <<
"Problem when computing v.sum()!" << std::endl;
380 double regular_sumSquare = 0.0;
382 for (
int iteration = 0; iteration < nbIterations; iteration++) {
383 regular_sumSquare += computeRegularSumSquare(v);
387 double sse_sumSquare = 0.0;
389 for (
int iteration = 0; iteration < nbIterations; iteration++) {
394 std::cout <<
"\nregular_sumSquare=" << regular_sumSquare <<
" ; sse_sumSquare=" << sse_sumSquare << std::endl;
395 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
396 std::cout <<
"Speed-up: " << (t_regular / t_sse) <<
"X" << std::endl;
398 if (!
vpMath::equal(regular_sumSquare, sse_sumSquare, std::numeric_limits<double>::epsilon())) {
399 std::cerr <<
"Problem when computing v.sumSquare()!" << std::endl;
403 double regular_stdev = 0.0;
405 for (
int iteration = 0; iteration < nbIterations; iteration++) {
406 regular_stdev += computeRegularStdev(v);
410 double sse_stdev = 0.0;
412 for (
int iteration = 0; iteration < nbIterations; iteration++) {
417 std::cout <<
"\nregular_stdev=" << regular_stdev <<
" ; sse_stdev=" << sse_stdev << std::endl;
418 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
419 std::cout <<
"Speed-up: " << (t_regular / t_sse) <<
"X" << std::endl;
421 if (!
vpMath::equal(regular_stdev, sse_stdev, std::numeric_limits<double>::epsilon())) {
422 std::cerr <<
"Problem when computing vpColVector::stdev()!" << std::endl;
429 const unsigned int nb = 1000;
430 const unsigned int size = 10000;
431 std::vector<vpColVector> vec(nb);
433 for (
size_t i = 0; i < nb; i++) {
435 for (
unsigned int j = 0; j < size; j++) {
436 v[j] = getRandomValues(-100.0, 100.0);
443 for (
unsigned int i = 0; i < nb; i++) {
444 v_big.insert(i * size, vec[(
size_t)i]);
447 std::cout <<
"\nBig insert: " << t <<
" ms" << std::endl;
449 for (
unsigned int i = 0; i < nb; i++) {
450 for (
unsigned int j = 0; j < size; j++) {
451 if (!
vpMath::equal(v_big[i * size + j], vec[(
size_t)i][j], std::numeric_limits<double>::epsilon())) {
452 std::cerr <<
"Problem in vpColVector insert()!" << std::endl;
463 std::cout <<
"Insert empty vectors:" << std::endl;
464 std::cout <<
"v1: " << v1.t() << std::endl;
465 std::cout <<
"v2: " << v2.
t() << std::endl;
466 std::cout <<
"v3: " << v3.
t() << std::endl;
469 std::cout <<
"\nAll tests succeed" << std::endl;
Implementation of a matrix and operations on matrices.
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)
vpColVector extract(unsigned int r, unsigned int colsize) const
unsigned int getRows() const
unsigned int size() const
Return the number of elements of the 2D array.
VISP_EXPORT double measureTimeMs()
static double median(const vpColVector &v)
unsigned int getCols() const
vpColVector & normalize()
static double mean(const vpColVector &v)
void insert(unsigned int i, const vpColVector &v)
int print(std::ostream &s, unsigned int length, char const *intro=0) const
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)