1 | // $Id: matrix_lookup_test.cc 2226 2010-03-24 14:40:02Z 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 | |
---|
32 | using namespace theplu::yat; |
---|
33 | |
---|
34 | utility::Matrix matrix(size_t n); |
---|
35 | |
---|
36 | int 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), |
---|
91 | utility::Index(gsl_m2.columns())); |
---|
92 | if (m3.rows()!=1 || m3.columns()!=gsl_m2.columns() || m3(0,0)!=gsl_m2(1,0) || |
---|
93 | m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) { |
---|
94 | suite.add(false); |
---|
95 | suite.err() << "ERROR:" << std::endl; |
---|
96 | suite.err() << "m3.rows(): " << m3.rows() << " expected 1" << std::endl; |
---|
97 | suite.err() << "m3.columns(): " << m3.columns() << " expected " |
---|
98 | << gsl_m2.columns() << std::endl; |
---|
99 | } |
---|
100 | else |
---|
101 | suite.err() << "Ok" << std::endl; |
---|
102 | |
---|
103 | suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&)..."; |
---|
104 | classifier::MatrixLookup m4(m2); |
---|
105 | if (m4.rows()!=m2.rows() || m4.columns()!=m2.rows() || m4(0,0)!=m2(0,0) || |
---|
106 | m4(0,1)!=m2(0,1) || m4(1,0)!=m2(1,0) || m4(1,1)!=m2(1,1) ) { |
---|
107 | suite.add(false); |
---|
108 | suite.err() << "ERROR:" << std::endl; |
---|
109 | } |
---|
110 | else |
---|
111 | suite.err() << "Ok" << std::endl; |
---|
112 | |
---|
113 | suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup& data\n" |
---|
114 | << " const std::vector<size_t>&,\n" |
---|
115 | << " const std::vector<size_t>&)..."; |
---|
116 | classifier::MatrixLookup m5(m2,utility::Index(one),utility::Index(one)); |
---|
117 | if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) { |
---|
118 | suite.add(false); |
---|
119 | suite.err() << "ERROR:" << std::endl; |
---|
120 | suite.err() << "MatrixLookup is " << m5(0,0) << " expected " << m2(1,1) |
---|
121 | << std::endl; |
---|
122 | } |
---|
123 | else |
---|
124 | suite.err() << "Ok" << std::endl; |
---|
125 | |
---|
126 | suite.err() << "MatrixLookup::MatrixLookup(const MatrixLookup&,\n" |
---|
127 | << " const std::vector<size_t>&,\n" |
---|
128 | << " const bool)..."; |
---|
129 | classifier::MatrixLookup m6(m2,utility::Index(one),true); |
---|
130 | if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) || |
---|
131 | m6(0,1)!=m2(1,1)) { |
---|
132 | suite.add(false); |
---|
133 | suite.err() << "ERROR:" << std::endl; |
---|
134 | } |
---|
135 | else |
---|
136 | suite.err() << "Ok" << std::endl; |
---|
137 | |
---|
138 | suite.err() << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n" |
---|
139 | << " const double)..."; |
---|
140 | classifier::MatrixLookup m7(103,112,12); |
---|
141 | if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) { |
---|
142 | suite.add(false); |
---|
143 | suite.err() << "ERROR:" << std::endl; |
---|
144 | } |
---|
145 | else |
---|
146 | suite.err() << "Ok" << std::endl; |
---|
147 | |
---|
148 | |
---|
149 | suite.err() << "MatrixLookup::training_data(const std::vector<size_t>)..."; |
---|
150 | const classifier::MatrixLookup* TrnData = |
---|
151 | new MatrixLookup(m2, utility::Index(one), false); |
---|
152 | if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){ |
---|
153 | suite.add(false); |
---|
154 | suite.err() << "ERROR:" << std::endl; |
---|
155 | } |
---|
156 | else |
---|
157 | suite.err() << "Ok" << std::endl; |
---|
158 | delete TrnData; |
---|
159 | |
---|
160 | suite.err() << "MatrixLookup::validation_data(const std::vector<size_t>,\n" |
---|
161 | << " const std::vector<size_t>)..."; |
---|
162 | std::vector<size_t> val(23,1); |
---|
163 | const classifier::MatrixLookup* ValData = |
---|
164 | new MatrixLookup(m2, utility::Index(val), false); |
---|
165 | if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){ |
---|
166 | suite.add(false); |
---|
167 | suite.err() << "ERROR:" << std::endl; |
---|
168 | } |
---|
169 | else |
---|
170 | suite.err() << "Ok" << std::endl; |
---|
171 | delete ValData; |
---|
172 | |
---|
173 | classifier::MatrixLookup const_m(2,2, 0); |
---|
174 | suite.test_concept_container2d(const_m); |
---|
175 | |
---|
176 | return suite.return_value(); |
---|
177 | } |
---|
178 | |
---|
179 | utility::Matrix matrix(size_t n) |
---|
180 | { |
---|
181 | utility::Matrix res(n,n); |
---|
182 | for (size_t i=0;i<n;i++) |
---|
183 | for (size_t j=0;j<n;j++) |
---|
184 | res(i,j)=10*i+j; |
---|
185 | return res; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|