1 | // $Id: vector_distance_test.cc 1031 2008-02-04 15:44:44Z markus $ |
---|
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 | |
---|
38 | using namespace theplu::yat; |
---|
39 | |
---|
40 | int 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 | |
---|
61 | double dist=statistics::distance(a.begin(),a.end(),b.begin(), |
---|
62 | statistics::euclidean_distance_tag()); |
---|
63 | if(fabs(dist-2.23607)>tolerance) { |
---|
64 | *error << "Error in unweighted Euclidean distance " << std::endl; |
---|
65 | ok=false; |
---|
66 | } |
---|
67 | |
---|
68 | dist=statistics::distance(a.begin(),a.end(),b.begin(), |
---|
69 | statistics::pearson_distance_tag()); |
---|
70 | if(fabs(dist-1.5)>tolerance) { |
---|
71 | *error << "Error in unweighted Pearson distance " << std::endl; |
---|
72 | ok=false; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | // Testing weighted versions |
---|
77 | utility::matrix m(2,3,1); |
---|
78 | m(0,1)=2; |
---|
79 | m(1,0)=0; |
---|
80 | m(1,1)=0; |
---|
81 | utility::matrix w(2,3,1); |
---|
82 | w(0,0)=0; |
---|
83 | classifier::MatrixLookupWeighted mw(m,w); |
---|
84 | classifier::DataLookupWeighted1D aw(mw,0,true); |
---|
85 | classifier::DataLookupWeighted1D bw(mw,1,true); |
---|
86 | |
---|
87 | dist=statistics::distance(aw.begin(),aw.end(),bw.begin(), |
---|
88 | statistics::euclidean_distance_tag()); |
---|
89 | |
---|
90 | if(fabs(dist-sqrt(6))>tolerance) { |
---|
91 | *error << "Error in weighted Euclidean distance " << std::endl; |
---|
92 | ok=false; |
---|
93 | } |
---|
94 | |
---|
95 | dist=statistics::distance(aw.begin(),aw.end(),bw.begin(), |
---|
96 | statistics::pearson_distance_tag()); |
---|
97 | |
---|
98 | if(fabs(dist-2)>tolerance) { |
---|
99 | *error << "Error in weighted Pearson distance " << std::endl; |
---|
100 | ok=false; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | // Test with std::vectors |
---|
105 | std::vector<double> sa(3,1); |
---|
106 | sa[1] = 2; |
---|
107 | std::vector<double> sb(3,0); |
---|
108 | sb[2] = 1; |
---|
109 | |
---|
110 | dist=statistics::distance(sa.begin(),sa.end(),sb.begin(), |
---|
111 | statistics::euclidean_distance_tag()); |
---|
112 | if(fabs(dist-2.23607)>tolerance) { |
---|
113 | *error << "Error in distance for std::vector " << std::endl; |
---|
114 | ok=false; |
---|
115 | } |
---|
116 | |
---|
117 | // Test for a std::list and a std::vector |
---|
118 | std::list<double> la; |
---|
119 | std::copy(sa.begin(),sa.end(),std::back_inserter<std::list<double> >(la)); |
---|
120 | dist=statistics::distance(la.begin(),la.end(),sb.begin(), |
---|
121 | statistics::euclidean_distance_tag()); |
---|
122 | if(fabs(dist-2.23607)>tolerance) { |
---|
123 | *error << "Error in distance for std::list " << std::endl; |
---|
124 | ok=false; |
---|
125 | } |
---|
126 | |
---|
127 | if(!ok) { |
---|
128 | *error << "distance_test failed" << std::endl; |
---|
129 | } |
---|
130 | if (error!=&std::cerr) |
---|
131 | delete error; |
---|
132 | if (ok) |
---|
133 | return 0; |
---|
134 | return -1; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|