source: trunk/test/matrix_lookup_test.cc @ 2161

Last change on this file since 2161 was 2161, checked in by Peter, 13 years ago

updating copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1// $Id: matrix_lookup_test.cc 2161 2010-01-19 23:59:48Z peter $
2
3/*
4  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "yat/utility/Matrix.h"
26#include "yat/classifier/MatrixLookup.h"
27
28#include <fstream>
29#include <iostream>
30#include <vector>
31
32using namespace theplu::yat;
33
34utility::Matrix matrix(size_t n);
35
36int main(int argc, char* argv[])
37{
38  using namespace theplu::yat::classifier;
39
40  test::Suite suite(argc, argv);
41
42  suite.err() << "\nTesting MatrixLookup" << std::endl;
43  suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix& data)...";
44  utility::Matrix gsl_m1(matrix(2));
45  classifier::MatrixLookup m1(gsl_m1);
46  if (m1.rows()!=gsl_m1.rows() || m1.columns()!=gsl_m1.columns() || 
47      m1(0,0)!=gsl_m1(0,0) || m1(0,1)!=gsl_m1(0,1) || 
48      m1(1,0)!=gsl_m1(1,0) || m1(1,1)!=gsl_m1(1,1) ) {
49    suite.add(false);
50    suite.err() <<  "ERROR:" << std::endl;
51  }
52  else 
53    suite.err() << "Ok" << std::endl;
54
55  suite.add(suite.test_stream(m1));
56
57 
58  suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n" 
59         << "                           const std::vector<size_t>&,\n"
60         << "                           const std::vector<size_t>&)...";
61  utility::Matrix gsl_m2(matrix(4));
62  std::vector<size_t> index_odd;
63  index_odd.push_back(1);
64  index_odd.push_back(3);
65  std::vector<size_t> index_even;
66  index_even.push_back(2);
67  index_even.push_back(0);
68 
69  classifier::MatrixLookup m2(gsl_m2,utility::Index(index_odd), 
70                              utility::Index(index_even));
71  if (m2.rows()!=2 || m2.columns()!=2 || 
72      m2(0,0)!=gsl_m2(1,2) || m2(0,1)!=gsl_m2(1,0) || 
73      m2(1,0)!=gsl_m2(3,2) || m2(1,1)!=gsl_m2(3,0) ) {
74    suite.add(false);
75    suite.err() <<  "ERROR:" << std::endl;
76    suite.err() << "rows: " << m2.rows() << " expected 2\n"
77           << "columns: " << m2.columns() << " expected 2\n"
78           << "(0,0): " << m2(0,0) << " expected " << gsl_m2(1,2) << "\n"
79           << "(0,1): " << m2(1,0) << " expected " << gsl_m2(1,0) << "\n"
80           << "(1,0): " << m2(0,1) << " expected " << gsl_m2(3,2) << "\n"
81           << "(0,0): " << m2(0,0) << " expected " << gsl_m2(3,0) << "\n";
82  }
83  else
84    suite.err() << "Ok" << std::endl;
85
86  suite.err() << "MatrixLookup::MatrixLookup(const utility::Matrix&,\n" 
87         << "                           const std::vector<size_t>&,\n"
88         << "                           const bool)...";
89  std::vector<size_t> one(1,1);
90  classifier::MatrixLookup m3(gsl_m2,utility::Index(one),true);
91  if (m3.rows()!=1 || m3.columns()!=gsl_m2.columns() || m3(0,0)!=gsl_m2(1,0) || 
92      m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) {
93    suite.add(false);
94    suite.err() <<  "ERROR:" << std::endl;
95    suite.err() << "m3.rows(): " << m3.rows() << " expected 1" << std::endl; 
96    suite.err() << "m3.columns(): " << m3.columns() << " expected " 
97           << gsl_m2.columns() << std::endl; 
98  }
99  else
100    suite.err() << "Ok" << std::endl;
101
102  suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&)...";
103  classifier::MatrixLookup m4(m2);
104  if (m4.rows()!=m2.rows() || m4.columns()!=m2.rows() || m4(0,0)!=m2(0,0) || 
105      m4(0,1)!=m2(0,1) || m4(1,0)!=m2(1,0) || m4(1,1)!=m2(1,1) ) {
106    suite.add(false);
107    suite.err() <<  "ERROR:" << std::endl;
108  }
109  else
110    suite.err() << "Ok" << std::endl;
111
112  suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n" 
113         << "                           const std::vector<size_t>&,\n"
114         << "                           const std::vector<size_t>&)...";
115  classifier::MatrixLookup m5(m2,utility::Index(one),utility::Index(one));
116  if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) {
117    suite.add(false);
118    suite.err() <<  "ERROR:" << std::endl;
119    suite.err() << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1)
120           << std::endl;
121  }
122  else
123    suite.err() << "Ok" << std::endl;
124 
125  suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n" 
126         << "                           const std::vector<size_t>&,\n"
127         << "                           const bool)...";
128  classifier::MatrixLookup m6(m2,utility::Index(one),true);
129  if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) || 
130      m6(0,1)!=m2(1,1)) {
131    suite.add(false);
132    suite.err() << "ERROR:" << std::endl;
133  }
134  else
135    suite.err() << "Ok" << std::endl;
136
137  suite.err() << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"
138         << "                           const double)...";
139  classifier::MatrixLookup m7(103,112,12);
140  if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) {
141    suite.add(false);
142    suite.err() << "ERROR:" << std::endl;
143  }
144  else
145    suite.err() << "Ok" << std::endl;
146
147
148  suite.err() << "MatrixLookup::training_data(const std::vector<size_t>)...";
149  const classifier::MatrixLookup* TrnData =
150    new MatrixLookup(m2, utility::Index(one), false);
151  if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){
152    suite.add(false);
153    suite.err() << "ERROR:" << std::endl;
154  }
155  else
156    suite.err() << "Ok" << std::endl;
157  delete TrnData;
158
159  suite.err() << "MatrixLookup::validation_data(const std::vector<size_t>,\n"
160         << "                              const std::vector<size_t>)...";
161  std::vector<size_t> val(23,1);
162  const classifier::MatrixLookup* ValData = 
163    new MatrixLookup(m2, utility::Index(val), false);
164  if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){
165    suite.add(false);
166    suite.err() << "ERROR:" << std::endl;
167  }
168  else
169    suite.err() << "Ok" << std::endl;
170  delete ValData;
171
172  classifier::MatrixLookup const_m(2,2, 0);
173  suite.test_concept_container2d(const_m);
174
175  return suite.return_value();
176}
177
178utility::Matrix matrix(size_t n)
179{
180  utility::Matrix res(n,n);
181  for (size_t i=0;i<n;i++)
182    for (size_t j=0;j<n;j++)
183      res(i,j)=10*i+j;
184  return res;
185}
186
187
Note: See TracBrowser for help on using the repository browser.