Ignore:
Timestamp:
Mar 15, 2008, 5:00:54 PM (14 years ago)
Author:
Peter
Message:

working on #223

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/distance_test.cc

    r1233 r1234  
    4040using namespace theplu::yat;
    4141
     42void check_equality(double, double, test::Suite&, const std::string&,
     43                    u_int N=1);
     44utility::Matrix data(void);
     45
     46template<class Distance>
     47void test_distance(Distance, theplu::yat::test::Suite&);
     48
     49template<class Distance>
     50void test_duplicate(Distance, theplu::yat::test::Suite&);
     51
     52template<class Distance>
     53void test_rescaling(Distance, theplu::yat::test::Suite&);
     54
     55template<class Distance>
     56void test_unity_weights(Distance, theplu::yat::test::Suite&);
     57
     58template<class Distance>
     59void test_self_distance(Distance, theplu::yat::test::Suite&);
     60
     61template<class Distance>
     62void test_symmetry(Distance, theplu::yat::test::Suite&);
     63
     64template<class Distance>
     65void test_zero_weight(Distance, theplu::yat::test::Suite&);
     66
     67utility::Matrix weight(void);
     68
    4269int main(int argc, char* argv[])
    4370
     
    5279  double tolerance=1e-4;
    5380  statistics::EuclideanDistance eucl_dist;
     81  suite.err() << "testing EuclideanDistance" << std::endl;
     82  test_distance(eucl_dist, suite);
    5483  double dist=eucl_dist(a.begin(),a.end(),b.begin());
    5584  if(std::abs(dist-2.23607)>tolerance) {
     
    5988 
    6089  statistics::PearsonDistance pear_dist;
     90  suite.err() << "testing PearsonDistance" << std::endl;
     91  test_distance(pear_dist, suite);
    6192  dist=pear_dist(a.begin(),a.end(),b.begin());
    6293  if(std::abs(dist-1.5)>tolerance) {
     
    113144  }
    114145 
     146
    115147  return suite.return_value();
    116148}
    117149
    118150
     151void check_equality(double dist1, double dist2, test::Suite& suite,
     152                    const std::string& msg, u_int N)
     153{
     154  if (!suite.equal(dist1, dist2, N)) {
     155    suite.err() << "Error: " << msg << " failed.\n";
     156    suite.err() << "  dist1: " << dist1 << "\n";
     157    suite.err() << "  dist2: " << dist2 << "\n";
     158    suite.err() << "  difference: " << dist1-dist2 << "\n";
     159    suite.err() << "  error bound: " << suite.last_error_bound() << "\n";
     160    suite.add(false);
     161  }
     162}
     163
     164
     165utility::Matrix data(void)
     166{
     167  utility::Matrix res(2,10);
     168  for (size_t i = 0; i<res.columns(); ++i){
     169    res(0,i) = i*i+1;
     170    res(1,i) = 2*i+3;
     171  }
     172  return res;
     173}
     174
     175template<class Distance>
     176void test_distance(Distance dist, theplu::yat::test::Suite& suite)
     177{
     178  test_duplicate(dist, suite);
     179  test_rescaling(dist, suite);
     180  test_unity_weights(dist, suite);
     181  test_self_distance(dist, suite);
     182  test_symmetry(dist, suite);
     183  test_zero_weight(dist, suite);
     184}
     185
     186template<class Distance>
     187void test_duplicate(Distance dist, theplu::yat::test::Suite& suite)
     188{
     189  utility::Matrix x(data());
     190  utility::Matrix x2(x.rows(), 2*x.columns());
     191  for (size_t i=0; i<x.rows(); ++i){
     192    std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i));
     193    std::copy(x.begin_row(i), x.end_row(i), x2.begin_row(i)+x.columns());
     194  }
     195  utility::Matrix w(weight());
     196  utility::Matrix w2(w.rows(), 2*w.columns());
     197  for (size_t i=0; i<w.rows(); ++i){
     198    std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i));
     199    std::copy(w.begin_row(i), w.end_row(i), w2.begin_row(i)+w.columns());
     200  }
     201  classifier::MatrixLookupWeighted ml(x2, w2);
     202  double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     203  for (size_t i=0; i<w.columns(); ++i)
     204    w2(0,i)=0.0;
     205  double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     206  check_equality(dist1, dist2, suite, "duplicate property");
     207}
     208
     209template<class Distance>
     210void test_rescaling(Distance dist, theplu::yat::test::Suite& suite)
     211{
     212  utility::Matrix x=data();
     213  utility::Matrix w=weight();
     214  classifier::MatrixLookupWeighted ml(x,w);
     215  double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     216  w *= 2.13;
     217  double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     218  check_equality(dist1, dist2, suite, "rescaling", 10);
     219}
     220
     221template<class Distance>
     222void test_unity_weights(Distance dist, theplu::yat::test::Suite& suite)
     223{
     224  utility::Matrix x=data();
     225  classifier::MatrixLookupWeighted ml(x);
     226  double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     227  double dist2 = dist(x.begin_row(0), x.end_row(0), x.begin_row(1));
     228  check_equality(dist1, dist2, suite, "unity weights", 10);
     229}
     230
     231template<class Distance>
     232void test_self_distance(Distance dist, theplu::yat::test::Suite& suite)
     233{
     234  utility::Matrix x = data();
     235  double self = dist(x.begin(), x.end(), x.begin());
     236  if (!suite.equal(self, 0.0)){
     237    suite.err() << "error: self distance is " << self << "\n"
     238                << "supposed to be zero.\n";
     239    suite.add(false);
     240  }
     241}
     242
     243
     244template<class Distance>
     245void test_symmetry(Distance dist, theplu::yat::test::Suite& suite)
     246{
     247  utility::Matrix x = data();
     248  double distab = dist(x.begin_row(0), x.end_row(0), x.begin_row(1));
     249  double distba = dist(x.begin_row(1), x.end_row(1), x.begin_row(0));
     250  check_equality(distab, distba, suite, "symmetry test");
     251}
     252
     253
     254template<class Distance>
     255void test_zero_weight(Distance dist, theplu::yat::test::Suite& suite)
     256{
     257  utility::Matrix x=data();
     258  utility::Matrix w=weight();
     259  classifier::MatrixLookupWeighted ml(x,w);
     260  w(0,0) = 0.0;
     261  double dist1 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     262  w(0,0) = 100*std::numeric_limits<double>().epsilon();
     263  double dist2 = dist(ml.begin_row(0), ml.end_row(0), ml.begin_row(1));
     264  check_equality(dist1, dist2, suite, "zero weight", 1000);
     265}
     266
     267utility::Matrix weight(void)
     268{
     269  utility::Matrix res(2,10);
     270  for (size_t i = 0; i<res.columns(); ++i){
     271    res(0,i) = 1.0/(1+i);
     272    res(1,i) = 1.0-0.1*i;
     273  }
     274  return res;
     275}
     276
Note: See TracChangeset for help on using the changeset viewer.