# source:trunk/test/vector_distance_test.cc@900

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

Refs #251. Fixed so the unweighted distances are at the forward-iterator level. To test this I added a distance calculation between a list and a vector in vector_distance_test (also fixed its indentation)

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