47 #include <visp3/core/vpMath.h>
48 #include <visp3/core/vpColVector.h>
49 #include <visp3/core/vpGaussRand.h>
52 bool test(
const std::string &s,
const vpColVector &v,
const std::vector<double> &bench)
54 static unsigned int cpt = 0;
55 std::cout <<
"** Test " << ++cpt << std::endl;
56 std::cout << s <<
"(" << v.
getRows() <<
"," << v.
getCols() <<
") = [" << v.
t() <<
"]^T" << std::endl;
57 if(bench.size() != v.
size()) {
58 std::cout <<
"Test fails: bad size wrt bench" << std::endl;
61 for (
unsigned int i=0; i<v.
size(); i++) {
62 if (std::fabs(v[i]-bench[i]) > std::fabs(v[i])*std::numeric_limits<double>::epsilon()) {
63 std::cout <<
"Test fails: bad content" << std::endl;
74 for(
unsigned int i = 0; i < v.
getRows(); i++) {
81 double computeRegularSumSquare(
const vpColVector &v) {
82 double sum_square = 0.0;
84 for(
unsigned int i = 0; i < v.
getRows(); i++) {
85 sum_square += v[i] * v[i];
92 double mean_value = computeRegularSum(v) / v.
getRows();
93 double sum_squared_diff = 0.0;
95 for(
unsigned int i = 0; i < v.
size(); i++) {
96 sum_squared_diff += (v[i]-mean_value) * (v[i]-mean_value);
99 double divisor = (double) v.
size();
101 return std::sqrt(sum_squared_diff / divisor);
113 std::vector<double> bench1(4, 3);
114 if (test(
"v", v, bench1) ==
false)
116 std::vector<double> bench2(4, 3./6);
118 if (test(
"v", v, bench2) ==
false)
122 std::vector<double> bench3(5, 0);
123 if (test(
"v", v, bench3) ==
false)
129 std::vector<double> bench1(4);
130 for(
unsigned int i=0; i<v.
size(); i++) {
132 bench1[i] = (double)i;
134 if (test(
"v", v, bench1) ==
false)
139 std::vector<double> bench2;
142 if (test(
"w", w, bench2) ==
false)
145 std::vector<double> bench3;
151 for(
size_t i=0; i<4; i++)
155 if (test(
"r2", r2, bench3) ==
false)
161 std::vector<double> bench(4);
162 for(
unsigned int i=0; i<M.getRows(); i++) {
166 if (test(
"M", M, bench) ==
false)
170 if (test(
"v", v, bench) ==
false)
173 if (test(
"w", w, bench) ==
false)
176 if (test(
"z1", z1, bench) ==
false)
179 if (test(
"z2", z2, bench) ==
false)
188 std::vector<double> bench1;
196 if (test(
"w", w, bench1) ==
false)
200 if (test(
"x", x, bench1) ==
false)
203 std::vector<float> bench2;
208 if (test(
"y1", y1, bench1) ==
false)
211 if (test(
"y2", y2, bench1) ==
false)
218 std::vector<double> bench(3,-1);
220 if (test(
"r2", r2, bench) ==
false)
224 if (test(
"r2", r2, bench) ==
false)
227 std::vector<double> bench3(7, 1);
228 bench3[3] = bench3[4] = bench3[5] = -1;
230 if (test(
"r3", r3, bench3) ==
false)
234 if (test(
"r1", r1, bench3) ==
false)
241 std::cout <<
"test r1: " << r1 << std::endl;
242 std::cout <<
"test r2: " << r2 << std::endl;
244 std::cout <<
"test r1+r2: " << r1+r2 << std::endl;
245 std::cout <<
"test r: " << r << std::endl;
246 std::vector<double> bench(3, 6);
247 if (test(
"r", r, bench) ==
false)
250 if (test(
"r1", r1, bench) ==
false)
258 std::vector<double> bench(3, -2);
259 if (test(
"r", r, bench) ==
false)
262 if (test(
"r1", r1, bench) ==
false)
271 std::vector<double> bench(5, 5);
272 if (test(
"r", r, bench) ==
false)
290 std::cout <<
"** Test mean" << std::endl;
293 std::cout <<
"Test fails: bad mean " << res << std::endl;
297 std::cout <<
"** Test stdev" << std::endl;
300 std::cout <<
"Test fails: bad stdev " << res << std::endl;
304 std::cout <<
"** Test stdev(bessel)" << std::endl;
307 std::cout <<
"Test fails: bad stdev(bessel) " << res << std::endl;
311 std::cout <<
"** Test median" << std::endl;
314 std::cout <<
"Test fails: bad median " << res << std::endl;
319 std::cout <<
"** Test median (odd)" << std::endl;
323 std::cout <<
"Test fails: bad median (odd) " << res << std::endl;
326 std::cout <<
"r: [" << r <<
"]^T" << std::endl;
327 r.
print(std::cout, 8,
"r");
335 int nbIterations = 1000;
336 unsigned int size = 117;
339 for(
unsigned int cpt = 0; cpt < v.
getRows(); cpt++) {
340 v[cpt] = rand() % 1000 + noise();
343 std::cout <<
"\nv.getRows()=" << v.
getRows() << std::endl;
345 double regular_sum = 0.0;
347 for(
int iteration = 0; iteration < nbIterations; iteration++) {
348 regular_sum += computeRegularSum(v);
352 double sse_sum = 0.0;
354 for(
int iteration = 0; iteration < nbIterations; iteration++) {
359 std::cout <<
"\nregular_sum=" << regular_sum <<
" ; sse_sum=" << sse_sum << std::endl;
360 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
361 std::cout <<
"Speed-up: " << (t_regular/t_sse) <<
"X" << std::endl;
363 if( !
vpMath::equal(regular_sum, sse_sum, std::numeric_limits<double>::epsilon()) ) {
364 std::cerr <<
"Problem when computing v.sum()!" << std::endl;
369 double regular_sumSquare = 0.0;
371 for(
int iteration = 0; iteration < nbIterations; iteration++) {
372 regular_sumSquare += computeRegularSumSquare(v);
376 double sse_sumSquare = 0.0;
378 for(
int iteration = 0; iteration < nbIterations; iteration++) {
383 std::cout <<
"\nregular_sumSquare=" << regular_sumSquare <<
" ; sse_sumSquare=" << sse_sumSquare << std::endl;
384 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
385 std::cout <<
"Speed-up: " << (t_regular/t_sse) <<
"X" << std::endl;
387 if( !
vpMath::equal(regular_sumSquare, sse_sumSquare, std::numeric_limits<double>::epsilon()) ) {
388 std::cerr <<
"Problem when computing v.sumSquare()!" << std::endl;
393 double regular_stdev = 0.0;
395 for(
int iteration = 0; iteration < nbIterations; iteration++) {
396 regular_stdev += computeRegularStdev(v);
400 double sse_stdev = 0.0;
402 for(
int iteration = 0; iteration < nbIterations; iteration++) {
407 std::cout <<
"\nregular_stdev=" << regular_stdev <<
" ; sse_stdev=" << sse_stdev << std::endl;
408 std::cout <<
"t_regular=" << t_regular <<
" ms ; t_sse=" << t_sse <<
" ms" << std::endl;
409 std::cout <<
"Speed-up: " << (t_regular/t_sse) <<
"X" << std::endl;
411 if( !
vpMath::equal(regular_stdev, sse_stdev, std::numeric_limits<double>::epsilon()) ) {
412 std::cerr <<
"Problem when computing vpColVector::stdev()!" << std::endl;
417 std::cout <<
"\nAll tests succeed" << std::endl;
Implementation of a matrix and operations on matrices.
vpColVector extract(unsigned int r, unsigned int colsize) const
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)
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getCols() const
Return the number of columns of the 2D array.
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
Return the number of rows of the 2D array.
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)