 Timestamp:
 Oct 15, 2008, 7:59:21 PM (14 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/distance_test.cc
r1487 r1582 26 26 #include "yat/statistics/EuclideanDistance.h" 27 27 #include "yat/statistics/PearsonDistance.h" 28 #include "yat/utility/DataIterator.h" 28 29 #include "yat/utility/Matrix.h" 29 30 #include "yat/utility/MatrixWeighted.h" 30 31 #include "yat/utility/Vector.h" 32 #include "yat/utility/WeightIterator.h" 31 33 32 34 #include <cassert> … … 43 45 unsigned long int N=1); 44 46 utility::Matrix data(void); 47 utility::MatrixWeighted data_weighted(void); 45 48 46 49 template<class Distance> … … 85 88 test_distance(eucl_dist, suite, 100); 86 89 double dist=eucl_dist(a.begin(),a.end(),b.begin()); 87 if (std::abs(dist2.23607)>tolerance) {90 if (!suite.equal(dist, 2.23606797749978988178)) { 88 91 suite.err() << "Error in unweighted Euclidean distance " << std::endl; 89 92 suite.add(false); … … 94 97 test_distance(pear_dist, suite, 1000); 95 98 dist=pear_dist(a.begin(),a.end(),b.begin()); 96 if (std::abs(dist1.5)>tolerance) {99 if (!suite.equal(dist, 1.5)) { 97 100 suite.err() << "Error in unweighted Pearson distance " << std::endl; 98 101 suite.add(false); … … 105 108 m(1,0)=0; 106 109 m(1,1)=0; 107 utility::Matrix w(2,3,1);108 w(0,0)=0;109 classifier::MatrixLookupWeighted mw(m ,w);110 utility::MatrixWeighted m_w(m); 111 m_w(0,0).weight()=0; 112 classifier::MatrixLookupWeighted mw(m_w); 110 113 classifier::DataLookupWeighted1D aw(mw,0,true); 111 114 classifier::DataLookupWeighted1D bw(mw,1,true); … … 113 116 dist=eucl_dist(aw.begin(),aw.end(),bw.begin()); 114 117 115 if (std::abs(distsqrt(6))>tolerance) {118 if (!suite.equal_sqrt(dist, sqrt(6))) { 116 119 suite.err() << "Error in weighted Euclidean distance " << std::endl; 117 120 suite.add(false); … … 120 123 dist=pear_dist(aw.begin(),aw.end(),bw.begin()); 121 124 122 if (std::abs(dist2)>tolerance) {125 if (!suite.equal(dist, 2)) { 123 126 suite.err() << "Error in weighted Pearson distance " << std::endl; 124 127 suite.add(false); … … 172 175 } 173 176 177 utility::MatrixWeighted data_weighted(void) 178 { 179 utility::Matrix x = data(); 180 utility::Matrix w = weight(); 181 utility::MatrixWeighted res(x.rows(), x.columns()); 182 std::copy(x.begin(), x.end(), utility::data_iterator(res.begin())); 183 std::copy(w.begin(), w.end(), utility::weight_iterator(res.begin())); 184 return res; 185 } 186 174 187 template<class Distance> 175 188 void test_distance(Distance dist, theplu::yat::test::Suite& suite, … … 188 201 unsigned long int N) 189 202 { 190 utility::Matrix x(data());191 utility::Matrix x2(x.rows(), 2*x.columns());203 utility::MatrixWeighted x(data_weighted()); 204 utility::MatrixWeighted mw(x.rows(), 2*x.columns()); 192 205 for (size_t i=0; i<x.rows(); ++i){ 193 std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i)); 194 std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i)+x.columns()); 195 } 196 utility::Matrix w(weight()); 197 utility::Matrix w2(w.rows(), 2*w.columns()); 198 for (size_t i=0; i<w.rows(); ++i){ 199 std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i)); 200 std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i)+w.columns()); 201 } 202 classifier::MatrixLookupWeighted ml(x2, w2); 203 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 204 for (size_t i=0; i<w.columns(); ++i) 205 w2(0,i)=0.0; 206 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 206 std::copy(x.begin_row(i), x.end_row(i), mw.begin_row(i)); 207 std::copy(x.begin_row(i), x.end_row(i), mw.begin_row(i)+x.columns()); 208 } 209 double dist1 = dist(mw.begin_row(0), mw.end_row(0), mw.begin_row(1)); 210 for (size_t i=0; i<x.columns(); ++i) 211 mw(0,i).weight()=0.0; 212 double dist2 = dist(mw.begin_row(0), mw.end_row(0), mw.begin_row(1)); 207 213 check_equality(dist1, dist2, suite, "duplicate property", N); 208 214 } … … 212 218 unsigned long int N) 213 219 { 214 utility::Matrix x=data(); 215 utility::Matrix w=weight(); 216 classifier::MatrixLookupWeighted ml(x,w); 217 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 218 w *= 2.13; 219 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 220 utility::MatrixWeighted wx=data_weighted(); 221 double dist1 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1)); 222 // rescale weights 223 for (size_t i=0; i<wx.rows(); ++i) 224 for (size_t j=0; j<wx.columns(); ++j) 225 wx(i,j).weight() *= 2.13; 226 double dist2 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1)); 220 227 check_equality(dist1, dist2, suite, "rescaling", N); 221 228 } … … 261 268 unsigned long int N) 262 269 { 263 utility::Matrix x=data(); 264 utility::Matrix w=weight(); 265 classifier::MatrixLookupWeighted ml(x,w); 266 w(0,0) = 0.0; 267 double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 268 w(0,0) = 100*std::numeric_limits<double>().epsilon(); 269 double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1)); 270 utility::MatrixWeighted wx=data_weighted(); 271 wx(0,0).weight() = 0.0; 272 double dist1 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1)); 273 wx(0,0).weight() = 100*std::numeric_limits<double>().epsilon(); 274 double dist2 = dist(wx.begin_row(0), wx.end_row(0), wx.begin_row(1)); 270 275 check_equality(dist1, dist2, suite, "zero weight", N); 271 276 }
Note: See TracChangeset
for help on using the changeset viewer.