source: trunk/test/vector_distance_test.cc @ 1050

Last change on this file since 1050 was 1050, checked in by Peter, 15 years ago

Simplifying distance structure

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: vector_distance_test.cc 1050 2008-02-07 18:47:34Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson, Markus Ringnér
5
6  This file is part of the yat library, http://trac.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/classifier/DataLookupWeighted1D.h"
25#include "yat/classifier/MatrixLookupWeighted.h"
26#include "yat/statistics/euclidean_distance.h"
27#include "yat/statistics/pearson_distance.h"
28#include "yat/utility/matrix.h"
29#include "yat/utility/vector.h"
30
31#include <cassert>
32#include <fstream>
33#include <iostream>
34#include <list>
35#include <vector>
36
37
38using namespace theplu::yat;
39
40int main(const int argc,const char* argv[])
41
42{ 
43  std::ostream* error;
44  if (argc>1 && argv[1]==std::string("-v"))
45    error = &std::cerr;
46  else {
47    error = new std::ofstream("/dev/null");
48    if (argc>1)
49      std::cout << "distance_test -v : for printing extra information\n";
50  }
51  *error << "testing distance" << std::endl;
52  bool ok = true;
53 
54  utility::vector a(3,1);
55  a(1) = 2;
56  utility::vector b(3,0);
57  b(2) = 1;
58 
59  double tolerance=1e-4;
60  statistics::EuclideanDistance eucl_dist;
61  double dist=eucl_dist(a.begin(),a.end(),b.begin());
62  if(fabs(dist-2.23607)>tolerance) {
63    *error << "Error in unweighted Euclidean distance " << std::endl;
64    ok=false;
65  }
66 
67  statistics::PearsonDistance pear_dist;
68  dist=pear_dist(a.begin(),a.end(),b.begin());
69  if(fabs(dist-1.5)>tolerance) {
70    *error << "Error in unweighted Pearson distance " << std::endl;
71    ok=false;
72  }
73 
74 
75  // Testing weighted versions
76  utility::matrix m(2,3,1);
77  m(0,1)=2;
78  m(1,0)=0;
79  m(1,1)=0;
80  utility::matrix w(2,3,1);
81  w(0,0)=0;
82  classifier::MatrixLookupWeighted mw(m,w);
83  classifier::DataLookupWeighted1D aw(mw,0,true);
84  classifier::DataLookupWeighted1D bw(mw,1,true);
85 
86  dist=eucl_dist(aw.begin(),aw.end(),bw.begin());
87 
88  if(fabs(dist-sqrt(6))>tolerance) {
89    *error << "Error in weighted Euclidean distance " << std::endl;
90    ok=false;
91  }
92 
93  dist=pear_dist(aw.begin(),aw.end(),bw.begin());
94 
95  if(fabs(dist-2)>tolerance) {
96    *error << "Error in weighted Pearson distance " << std::endl;
97    ok=false;
98  }
99 
100   
101  // Test with std::vectors
102  std::vector<double> sa(3,1);
103  sa[1] = 2;
104  std::vector<double> sb(3,0);
105  sb[2] = 1;
106 
107  dist=eucl_dist(sa.begin(),sa.end(),sb.begin()); 
108  if(fabs(dist-2.23607)>tolerance) {
109    *error << "Error in distance for std::vector " << std::endl;
110    ok=false;
111  }
112 
113  // Test for a std::list and a std::vector
114  std::list<double> la;
115  std::copy(sa.begin(),sa.end(),std::back_inserter<std::list<double> >(la));
116  dist=eucl_dist(la.begin(),la.end(),sb.begin());
117  if(fabs(dist-2.23607)>tolerance) {
118    *error << "Error in distance for std::list " << std::endl;
119    ok=false;
120  }
121 
122  if(!ok) {
123    *error << "distance_test failed" << std::endl;
124  }
125  if (error!=&std::cerr)
126    delete error;
127  if (ok) 
128    return 0;
129  return -1;
130}
131
132
Note: See TracBrowser for help on using the repository browser.