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

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

A first suggestion for how to adress #250. Also removed contamination of namespace std (see #251).

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