source: trunk/test/distance_test.cc @ 1233

Last change on this file since 1233 was 1233, checked in by Peter, 14 years ago

working on ticket:223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1// $Id: distance_test.cc 1233 2008-03-15 03:22:10Z 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 "Suite.h"
25
26#include "yat/classifier/DataLookupWeighted1D.h"
27#include "yat/classifier/MatrixLookupWeighted.h"
28#include "yat/statistics/EuclideanDistance.h"
29#include "yat/statistics/PearsonDistance.h"
30#include "yat/utility/Matrix.h"
31#include "yat/utility/Vector.h"
32
33#include <cassert>
34#include <fstream>
35#include <iostream>
36#include <list>
37#include <vector>
38
39
40using namespace theplu::yat;
41
42int main(int argc, char* argv[])
43{ 
44  theplu::yat::test::Suite suite(argc, argv);
45  suite.err() << "testing distance" << std::endl;
46 
47  utility::Vector a(3,1);
48  a(1) = 2;
49  utility::Vector b(3,0);
50  b(2) = 1;
51 
52  double tolerance=1e-4;
53  statistics::EuclideanDistance eucl_dist;
54  double dist=eucl_dist(a.begin(),a.end(),b.begin());
55  if(std::abs(dist-2.23607)>tolerance) {
56    suite.err() << "Error in unweighted Euclidean distance " << std::endl;
57    suite.add(false);
58  }
59 
60  statistics::PearsonDistance pear_dist;
61  dist=pear_dist(a.begin(),a.end(),b.begin());
62  if(std::abs(dist-1.5)>tolerance) {
63    suite.err() << "Error in unweighted Pearson distance " << std::endl;
64    suite.add(false);
65  }
66 
67 
68  // Testing weighted versions
69  utility::Matrix m(2,3,1);
70  m(0,1)=2;
71  m(1,0)=0;
72  m(1,1)=0;
73  utility::Matrix w(2,3,1);
74  w(0,0)=0;
75  classifier::MatrixLookupWeighted mw(m,w);
76  classifier::DataLookupWeighted1D aw(mw,0,true);
77  classifier::DataLookupWeighted1D bw(mw,1,true);
78 
79  dist=eucl_dist(aw.begin(),aw.end(),bw.begin());
80 
81  if(std::abs(dist-sqrt(6))>tolerance) {
82    suite.err() << "Error in weighted Euclidean distance " << std::endl;
83    suite.add(false);
84  }
85 
86  dist=pear_dist(aw.begin(),aw.end(),bw.begin());
87 
88  if(std::abs(dist-2)>tolerance) {
89    suite.err() << "Error in weighted Pearson distance " << std::endl;
90    suite.add(false);
91  }
92 
93   
94  // Test with std::vectors
95  std::vector<double> sa(3,1);
96  sa[1] = 2;
97  std::vector<double> sb(3,0);
98  sb[2] = 1;
99 
100  dist=eucl_dist(sa.begin(),sa.end(),sb.begin()); 
101  if(std::abs(dist-2.23607)>tolerance) {
102    suite.err() << "Error in distance for std::vector " << std::endl;
103    suite.add(false);
104  }
105 
106  // Test for a std::list and a std::vector
107  std::list<double> la;
108  std::copy(sa.begin(),sa.end(),std::back_inserter<std::list<double> >(la));
109  dist=eucl_dist(la.begin(),la.end(),sb.begin());
110  if(std::abs(dist-2.23607)>tolerance) {
111    suite.err() << "Error in distance for std::list " << std::endl;
112    suite.add(false);
113  }
114 
115  return suite.return_value();
116}
117
118
Note: See TracBrowser for help on using the repository browser.