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