source: trunk/test/matrix_lookup_test.cc @ 680

Last change on this file since 680 was 680, checked in by Jari Häkkinen, 17 years ago

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1// $Id: matrix_lookup_test.cc 680 2006-10-11 17:49:03Z jari $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/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,index_odd, index_even);
75  if (m2.rows()!=2 || m2.columns()!=2 || 
76      m2(0,0)!=gsl_m2(1,2) || m2(0,1)!=gsl_m2(1,0) || 
77      m2(1,0)!=gsl_m2(3,2) || m2(1,1)!=gsl_m2(3,0) ) {
78    ok =false;
79    *error <<   "ERROR:" << std::endl;
80  }
81  else
82    *error << "Ok" << std::endl;
83
84  *error << "MatrixLookup::MatrixLookup(const utility::matrix&,\n" 
85         << "                           const std::vector<size_t>&,\n"
86         << "                           const bool)...";
87  std::vector<size_t> one(1,1);
88  classifier::MatrixLookup m3(gsl_m2,one,true);
89  if (m3.rows()!=1 || m3.columns()!=gsl_m2.columns() || m3(0,0)!=gsl_m2(1,0) || 
90      m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) {
91    ok =false;
92    *error <<   "ERROR:" << std::endl;
93    *error << "m3.rows(): " << m3.rows() << " expected 1" << std::endl; 
94    *error << "m3.columns(): " << m3.columns() << " expected " 
95           << gsl_m2.columns() << std::endl; 
96  }
97  else
98    *error << "Ok" << std::endl;
99
100  *error << "MatrixLookup::MatrixLookup(const MatrixLookup&)...";
101  classifier::MatrixLookup m4(m2);
102  if (m4.rows()!=m2.rows() || m4.columns()!=m2.rows() || m4(0,0)!=m2(0,0) || 
103      m4(0,1)!=m2(0,1) || m4(1,0)!=m2(1,0) || m4(1,1)!=m2(1,1) ) {
104    ok =false;
105    *error <<   "ERROR:" << std::endl;
106  }
107  else
108    *error << "Ok" << std::endl;
109
110  *error << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n" 
111         << "                           const std::vector<size_t>&,\n"
112         << "                           const std::vector<size_t>&)...";
113  classifier::MatrixLookup m5(m2,one,one);
114  if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) {
115    ok =false;
116    *error <<   "ERROR:" << std::endl;
117    *error << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1)
118           << std::endl;
119  }
120  else
121    *error << "Ok" << std::endl;
122 
123  *error << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n" 
124         << "                           const std::vector<size_t>&,\n"
125         << "                           const bool)...";
126  classifier::MatrixLookup m6(m2,one,true);
127  if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) || 
128      m6(0,1)!=m2(1,1)) {
129    ok =false;
130    *error << "ERROR:" << std::endl;
131  }
132  else
133    *error << "Ok" << std::endl;
134
135  *error << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"
136         << "                           const double)...";
137  classifier::MatrixLookup m7(103,112,12);
138  if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) {
139    ok =false;
140    *error << "ERROR:" << std::endl;
141  }
142  else
143    *error << "Ok" << std::endl;
144
145
146  *error << "MatrixLookup::training_data(const std::vector<size_t>)...";
147  const classifier::MatrixLookup* TrnData = m2.training_data(one);
148  if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){
149    ok =false;
150    *error << "ERROR:" << std::endl;
151  }
152  else
153    *error << "Ok" << std::endl;
154  delete TrnData;
155
156  *error << "MatrixLookup::validation_data(const std::vector<size_t>,\n"
157         << "                              const std::vector<size_t>)...";
158  std::vector<size_t> val(23,2);
159  const classifier::MatrixLookup* ValData = m2.validation_data(one, val);
160  if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){
161    ok =false;
162    *error << "ERROR:" << std::endl;
163  }
164  else
165    *error << "Ok" << std::endl;
166  delete ValData;
167
168  if (ok)
169    *error << "Test Ok." << std::endl;
170  if (error!=&std::cerr)
171    delete error;
172  return (ok ? 0 : -1);
173}
174
175utility::matrix matrix(size_t n)
176{
177  utility::matrix res(n,n);
178  for (size_t i=0;i<n;i++)
179    for (size_t j=0;j<n;j++)
180      res(i,j)=10*i+j;
181  return res;
182}
183
184
Note: See TracBrowser for help on using the repository browser.