source: trunk/test/vector_distance_test.cc @ 926

Last change on this file since 926 was 926, checked in by Markus Ringnér, 15 years ago

Fixing minor mistakes so things gets installed with make install

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// $Id: vector_distance_test.cc 926 2007-10-03 06:11:31Z markus $
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#include <list>
14#include <vector>
15
16
17using namespace theplu::yat;
18
19int main(const int argc,const char* argv[])
20
21{ 
22  std::ostream* error;
23  if (argc>1 && argv[1]==std::string("-v"))
24    error = &std::cerr;
25  else {
26    error = new std::ofstream("/dev/null");
27    if (argc>1)
28      std::cout << "vector_distance_test -v : for printing extra information\n";
29  }
30  *error << "testing vector_distance" << std::endl;
31  bool ok = true;
32 
33  utility::vector a(3,1);
34  a(1) = 2;
35  utility::vector b(3,0);
36  b(2) = 1;
37 
38  double tolerance=1e-4;
39 
40  double dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
41                                          statistics::euclidean_vector_distance_tag());
42  if(fabs(dist-2.23607)>tolerance) {
43    *error << "Error in unweighted Euclidean vector_distance " << std::endl;
44    ok=false;
45  }
46 
47  dist=statistics::vector_distance(a.begin(),a.end(),b.begin(),
48                                   statistics::pearson_vector_distance_tag()); 
49  if(fabs(dist-1.5)>tolerance) {
50    *error << "Error in unweighted Pearson vector_distance " << std::endl;
51    ok=false;
52  }
53 
54 
55  // Testing weighted versions
56  utility::matrix m(2,3,1);
57  m(0,1)=2;
58  m(1,0)=0;
59  m(1,1)=0;
60  utility::matrix w(2,3,1);
61  w(0,0)=0;
62  classifier::MatrixLookupWeighted mw(m,w);
63  classifier::DataLookupWeighted1D aw(mw,0,true);
64  classifier::DataLookupWeighted1D bw(mw,1,true);
65 
66  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
67                                   statistics::euclidean_vector_distance_tag());
68 
69  if(fabs(dist-2)>tolerance) {
70    *error << "Error in weighted Euclidean vector_distance " << std::endl;
71    ok=false;
72  }
73 
74  dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),
75                                   statistics::pearson_vector_distance_tag());
76 
77  if(fabs(dist-2)>tolerance) {
78    *error << "Error in weighted Pearson vector_distance " << std::endl;
79    ok=false;
80  }
81 
82   
83  // Test with std::vectors
84  std::vector<double> sa(3,1);
85  sa[1] = 2;
86  std::vector<double> sb(3,0);
87  sb[2] = 1;
88 
89  dist=statistics::vector_distance(sa.begin(),sa.end(),sb.begin(),
90                                   statistics::euclidean_vector_distance_tag()); 
91  if(fabs(dist-2.23607)>tolerance) {
92    *error << "Error in vector_distance for std::vector " << std::endl;
93    ok=false;
94  }
95 
96  // Test for a std::list and a std::vector
97  std::list<double> la;
98  std::copy(sa.begin(),sa.end(),std::back_inserter<std::list<double> >(la));
99  dist=statistics::vector_distance(la.begin(),la.end(),sb.begin(),
100                                   statistics::euclidean_vector_distance_tag()); 
101  if(fabs(dist-2.23607)>tolerance) {
102    *error << "Error in vector_distance for std::list " << std::endl;
103    ok=false;
104  }
105 
106  if(!ok) {
107    *error << "vector_distance_test failed" << std::endl;
108  }
109  if (error!=&std::cerr)
110    delete error;
111  if (ok=true) 
112    return 0;
113  return -1;
114}
115
116
Note: See TracBrowser for help on using the repository browser.