source:trunk/test/vector_distance_test.cc@895

Last change on this file since 895 was 895, checked in by Peter, 16 years ago

Another try, hm...

• Property svn:eol-style set to `native`
• Property svn:keywords set to `Id`
File size: 2.4 KB
Line
1// \$Id: vector_distance_test.cc 895 2007-09-25 20:39:14Z peter \$
2
3#include "yat/classifier/DataLookupWeighted1D.h"
4#include "yat/classifier/MatrixLookupWeighted.h"
5#include "yat/statistics/euclidean_vector_distance.h"
6#include "yat/statistics/pearson_vector_distance.h"
7#include "yat/utility/matrix.h"
8#include "yat/utility/vector.h"
9
10#include <cassert>
11#include <fstream>
12#include <iostream>
13
14
15using namespace theplu::yat;
16
17int main(const int argc,const char* argv[])
18
19{
20  std::ostream* error;
21  if (argc>1 && argv[1]==std::string("-v"))
22    error = &std::cerr;
23  else {
24    error = new std::ofstream("/dev/null");
25    if (argc>1)
26      std::cout << "vector_distance_test -v : for printing extra information\n";
27  }
28  *error << "testing vector_distance" << std::endl;
29  bool ok = true;
30
31  utility::vector a(3,1);
32  a(1) = 2;
33  utility::vector b(3,0);
34  b(2) = 1;
35
36  double tolerance=1e-4;
37
38  double dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
39                                          statistics::euclidean_vector_distance_tag());
40  if(fabs(dist-2.23607)>tolerance) {
41    *error << "Error in unweighted Euclidean vector_distance: " << std::endl;
42    ok=false;
43  }
44
45  dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
46                                          statistics::pearson_vector_distance_tag());
47  if(fabs(dist-1.5)>tolerance) {
48    *error << "Error in unweighted Pearson vector_distance: " << std::endl;
49    ok=false;
50  }
51
52
53  // Testing weighted versions
54  utility::matrix m(2,3,1);
55  m(0,1)=2;
56  m(1,0)=0;
57  m(1,1)=0;
58  utility::matrix w(2,3,1);
59  w(0,0)=0;
60  classifier::MatrixLookupWeighted mw(m,w);
61  classifier::DataLookupWeighted1D aw(mw,0,true);
62  classifier::DataLookupWeighted1D bw(mw,1,true);
63
64  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
65                                          statistics::euclidean_vector_distance_tag());
66
67  if(fabs(dist-2)>tolerance) {
68    *error << "Error in weighted Euclidean vector_distance: " << std::endl;
69    ok=false;
70  }
71
72  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
73                                          statistics::pearson_vector_distance_tag());
74
75  if(fabs(dist-2)>tolerance) {
76    *error << "Error in weighted Pearson vector_distance: " << std::endl;
77    ok=false;
78  }
79
80
81  // Test with reference to a vector_distance_tag
82  statistics::euclidean_vector_distance_tag tag;
83  statistics::vector_distance_tag& tagref=tag;
84  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),tagref);
85  *error << "Dist: " << dist << std::endl;
86
87  if (error!=&std::cerr)
88    delete error;
89
90  if (ok=true)
91    return 0;
92  return -1;
93}
94
95
Note: See TracBrowser for help on using the repository browser.