source: trunk/test/matrix_lookup_test.cc @ 1134

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

using Index class instead of std::vector<size_t>

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1// $Id: matrix_lookup_test.cc 1134 2008-02-23 22:52:43Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
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/utility/Matrix.h"
25#include "yat/classifier/MatrixLookup.h"
26
27#include <fstream>
28#include <iostream>
29#include <vector>
30
31using namespace theplu::yat;
32
33utility::Matrix matrix(size_t n);
34
35int main(const int argc,const char* argv[])
36{
37  using namespace theplu::yat::classifier;
38
39  std::ostream* error;
40  if (argc>1 && argv[1]==std::string("-v"))
41    error = &std::cerr;
42  else {
43    error = new std::ofstream("/dev/null");
44    if (argc>1)
45      std::cout << "lookup_test -v : for printing extra information\n";
46  }
47  bool ok = true;
48
49  *error << "\nTesting MatrixLookup" << std::endl;
50  *error << "MatrixLookup::MatrixLookup(const utility::Matrix& data)...";
51  utility::Matrix gsl_m1(matrix(2));
52  classifier::MatrixLookup m1(gsl_m1);
53  if (m1.rows()!=gsl_m1.rows() || m1.columns()!=gsl_m1.columns() || 
54      m1(0,0)!=gsl_m1(0,0) || m1(0,1)!=gsl_m1(0,1) || 
55      m1(1,0)!=gsl_m1(1,0) || m1(1,1)!=gsl_m1(1,1) ) {
56    ok =false;
57    *error <<   "ERROR:" << std::endl;
58  }
59  else 
60    *error << "Ok" << std::endl;
61
62 
63  *error << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n" 
64         << "                           const std::vector<size_t>&,\n"
65         << "                           const std::vector<size_t>&)...";
66  utility::Matrix gsl_m2(matrix(4));
67  std::vector<size_t> index_odd;
68  index_odd.push_back(1);
69  index_odd.push_back(3);
70  std::vector<size_t> index_even;
71  index_even.push_back(2);
72  index_even.push_back(0);
73 
74  classifier::MatrixLookup m2(gsl_m2,utility::Index(index_odd), 
75                              utility::Index(index_even));
76  if (m2.rows()!=2 || m2.columns()!=2 || 
77      m2(0,0)!=gsl_m2(1,2) || m2(0,1)!=gsl_m2(1,0) || 
78      m2(1,0)!=gsl_m2(3,2) || m2(1,1)!=gsl_m2(3,0) ) {
79    ok =false;
80    *error <<   "ERROR:" << std::endl;
81  }
82  else
83    *error << "Ok" << std::endl;
84
85  *error << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n" 
86         << "                           const std::vector<size_t>&,\n"
87         << "                           const bool)...";
88  std::vector<size_t> one(1,1);
89  classifier::MatrixLookup m3(gsl_m2,utility::Index(one),true);
90  if (m3.rows()!=1 || m3.columns()!=gsl_m2.columns() || m3(0,0)!=gsl_m2(1,0) || 
91      m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) {
92    ok =false;
93    *error <<   "ERROR:" << std::endl;
94    *error << "m3.rows(): " << m3.rows() << " expected 1" << std::endl; 
95    *error << "m3.columns(): " << m3.columns() << " expected " 
96           << gsl_m2.columns() << std::endl; 
97  }
98  else
99    *error << "Ok" << std::endl;
100
101  *error << "MatrixLookup::MatrixLookup(const MatrixLookup&)...";
102  classifier::MatrixLookup m4(m2);
103  if (m4.rows()!=m2.rows() || m4.columns()!=m2.rows() || m4(0,0)!=m2(0,0) || 
104      m4(0,1)!=m2(0,1) || m4(1,0)!=m2(1,0) || m4(1,1)!=m2(1,1) ) {
105    ok =false;
106    *error <<   "ERROR:" << std::endl;
107  }
108  else
109    *error << "Ok" << std::endl;
110
111  *error << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n" 
112         << "                           const std::vector<size_t>&,\n"
113         << "                           const std::vector<size_t>&)...";
114  classifier::MatrixLookup m5(m2,utility::Index(one),utility::Index(one));
115  if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) {
116    ok =false;
117    *error <<   "ERROR:" << std::endl;
118    *error << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1)
119           << std::endl;
120  }
121  else
122    *error << "Ok" << std::endl;
123 
124  *error << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n" 
125         << "                           const std::vector<size_t>&,\n"
126         << "                           const bool)...";
127  classifier::MatrixLookup m6(m2,utility::Index(one),true);
128  if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) || 
129      m6(0,1)!=m2(1,1)) {
130    ok =false;
131    *error << "ERROR:" << std::endl;
132  }
133  else
134    *error << "Ok" << std::endl;
135
136  *error << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"
137         << "                           const double)...";
138  classifier::MatrixLookup m7(103,112,12);
139  if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) {
140    ok =false;
141    *error << "ERROR:" << std::endl;
142  }
143  else
144    *error << "Ok" << std::endl;
145
146
147  *error << "MatrixLookup::training_data(const std::vector<size_t>)...";
148  const classifier::MatrixLookup* TrnData =m2.training_data(utility::Index(one));
149  if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){
150    ok =false;
151    *error << "ERROR:" << std::endl;
152  }
153  else
154    *error << "Ok" << std::endl;
155  delete TrnData;
156
157  *error << "MatrixLookup::validation_data(const std::vector<size_t>,\n"
158         << "                              const std::vector<size_t>)...";
159  std::vector<size_t> val(23,2);
160  const classifier::MatrixLookup* ValData = 
161    m2.validation_data(utility::Index(one), utility::Index(val));
162  if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){
163    ok =false;
164    *error << "ERROR:" << std::endl;
165  }
166  else
167    *error << "Ok" << std::endl;
168  delete ValData;
169
170  if (ok)
171    *error << "Test Ok." << std::endl;
172  if (error!=&std::cerr)
173    delete error;
174  return (ok ? 0 : -1);
175}
176
177utility::Matrix matrix(size_t n)
178{
179  utility::Matrix res(n,n);
180  for (size_t i=0;i<n;i++)
181    for (size_t j=0;j<n;j++)
182      res(i,j)=10*i+j;
183  return res;
184}
185
186
Note: See TracBrowser for help on using the repository browser.