537 |
05 Mar 06 |
peter |
// $Id$ |
537 |
05 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 |
537 |
05 Mar 06 |
peter |
8 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
537 |
05 Mar 06 |
peter |
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 |
|
1237 |
15 Mar 08 |
peter |
27 |
#include "Suite.h" |
1237 |
15 Mar 08 |
peter |
28 |
|
1121 |
22 Feb 08 |
peter |
29 |
#include "yat/utility/Matrix.h" |
675 |
10 Oct 06 |
jari |
30 |
#include "yat/classifier/DataLookup1D.h" |
675 |
10 Oct 06 |
jari |
31 |
#include "yat/classifier/KernelLookup.h" |
675 |
10 Oct 06 |
jari |
32 |
#include "yat/classifier/Kernel_SEV.h" |
675 |
10 Oct 06 |
jari |
33 |
#include "yat/classifier/MatrixLookup.h" |
675 |
10 Oct 06 |
jari |
34 |
#include "yat/classifier/PolynomialKernelFunction.h" |
675 |
10 Oct 06 |
jari |
35 |
|
537 |
05 Mar 06 |
peter |
36 |
#include <fstream> |
537 |
05 Mar 06 |
peter |
37 |
#include <iostream> |
537 |
05 Mar 06 |
peter |
38 |
#include <vector> |
537 |
05 Mar 06 |
peter |
39 |
|
680 |
11 Oct 06 |
jari |
40 |
using namespace theplu::yat; |
537 |
05 Mar 06 |
peter |
41 |
|
1237 |
15 Mar 08 |
peter |
42 |
int main(int argc, char* argv[]) |
537 |
05 Mar 06 |
peter |
43 |
{ |
680 |
11 Oct 06 |
jari |
44 |
using namespace theplu::yat::classifier; |
537 |
05 Mar 06 |
peter |
45 |
|
1237 |
15 Mar 08 |
peter |
46 |
theplu::yat::test::Suite suite(argc, argv); |
537 |
05 Mar 06 |
peter |
47 |
|
1237 |
15 Mar 08 |
peter |
48 |
suite.err() << "\nTesting KernelLookup" << std::endl; |
1121 |
22 Feb 08 |
peter |
49 |
utility::Matrix data_core(1,5); |
537 |
05 Mar 06 |
peter |
50 |
for (size_t i=0; i<data_core.columns(); i++) |
537 |
05 Mar 06 |
peter |
51 |
data_core(0,i)=i; |
537 |
05 Mar 06 |
peter |
52 |
classifier::MatrixLookup data(data_core); |
537 |
05 Mar 06 |
peter |
53 |
classifier::PolynomialKernelFunction kf; |
537 |
05 Mar 06 |
peter |
54 |
classifier::Kernel_SEV kernel(data,kf); |
537 |
05 Mar 06 |
peter |
55 |
|
1237 |
15 Mar 08 |
peter |
56 |
suite.err() << "KernelLookup::KernelLookup(const Kernel&)..."; |
537 |
05 Mar 06 |
peter |
57 |
classifier::KernelLookup k1(kernel); |
628 |
05 Sep 06 |
peter |
58 |
if (k1.rows()!=kernel.size() || k1.columns()!=kernel.size()) { |
1237 |
15 Mar 08 |
peter |
59 |
suite.add(false); |
1237 |
15 Mar 08 |
peter |
60 |
suite.err() << "ERROR:" << std::endl; |
1237 |
15 Mar 08 |
peter |
61 |
suite.err() << "Dimensions do not agree." << std::endl; |
537 |
05 Mar 06 |
peter |
62 |
} |
537 |
05 Mar 06 |
peter |
63 |
else { |
4200 |
19 Aug 22 |
peter |
64 |
for (size_t i=0; i<k1.rows(); i++) |
4200 |
19 Aug 22 |
peter |
65 |
for (size_t j=0; j<k1.columns(); j++) |
537 |
05 Mar 06 |
peter |
66 |
if (k1(i,j)!=kernel(i,j)) { |
1237 |
15 Mar 08 |
peter |
67 |
suite.add(false); |
1237 |
15 Mar 08 |
peter |
68 |
suite.err() << "ERROR:\n" |
537 |
05 Mar 06 |
peter |
69 |
<< "KernelLookup::KernelLookup(const Kernel& data)" |
537 |
05 Mar 06 |
peter |
70 |
<< std::endl; |
4200 |
19 Aug 22 |
peter |
71 |
suite.err() << "k(" << i << "," << j << ") is " << k1(i,j) |
537 |
05 Mar 06 |
peter |
72 |
<< "expected " << kernel(i,j) << std::endl; |
537 |
05 Mar 06 |
peter |
73 |
} |
1237 |
15 Mar 08 |
peter |
74 |
if (suite.ok()) |
1237 |
15 Mar 08 |
peter |
75 |
suite.err() << "Ok." << std::endl; |
537 |
05 Mar 06 |
peter |
76 |
} |
537 |
05 Mar 06 |
peter |
77 |
|
537 |
05 Mar 06 |
peter |
78 |
std::vector<size_t> index_odd; |
537 |
05 Mar 06 |
peter |
79 |
index_odd.push_back(1); |
537 |
05 Mar 06 |
peter |
80 |
index_odd.push_back(3); |
537 |
05 Mar 06 |
peter |
81 |
std::vector<size_t> index_even; |
537 |
05 Mar 06 |
peter |
82 |
index_even.push_back(2); |
537 |
05 Mar 06 |
peter |
83 |
index_even.push_back(0); |
537 |
05 Mar 06 |
peter |
84 |
index_even.push_back(4); |
1237 |
15 Mar 08 |
peter |
85 |
suite.err() << "KernelLookup::KernelLookup(const Kernel&,\n" |
537 |
05 Mar 06 |
peter |
86 |
<< " const vector<size_t>&,\n" |
537 |
05 Mar 06 |
peter |
87 |
<< " const vector<size_t>&)..."; |
1134 |
23 Feb 08 |
peter |
88 |
classifier::KernelLookup k2(kernel,utility::Index(index_odd), |
1134 |
23 Feb 08 |
peter |
89 |
utility::Index(index_even)); |
537 |
05 Mar 06 |
peter |
90 |
if (k2.rows()!=index_odd.size() || k2.columns()!=index_even.size()) { |
1237 |
15 Mar 08 |
peter |
91 |
suite.add(false); |
1237 |
15 Mar 08 |
peter |
92 |
suite.err() << "ERROR:" << std::endl; |
1237 |
15 Mar 08 |
peter |
93 |
suite.err() << "Dimensions do not agree." << std::endl; |
537 |
05 Mar 06 |
peter |
94 |
} |
4200 |
19 Aug 22 |
peter |
95 |
for (size_t i=0; i<k2.rows(); i++) |
4200 |
19 Aug 22 |
peter |
96 |
for (size_t j=0; j<k2.columns(); j++) |
537 |
05 Mar 06 |
peter |
97 |
if (k2(i,j)!=kernel(index_odd[i],index_even[j])) { |
1237 |
15 Mar 08 |
peter |
98 |
suite.add(false); |
1237 |
15 Mar 08 |
peter |
99 |
suite.err() << "ERROR:\n" |
537 |
05 Mar 06 |
peter |
100 |
<< "KernelLookup::KernelLookup(const Kernel& data)" |
537 |
05 Mar 06 |
peter |
101 |
<< std::endl; |
4200 |
19 Aug 22 |
peter |
102 |
suite.err() << "k(" << i << "," << j << ") is " << k2(i,j) |
537 |
05 Mar 06 |
peter |
103 |
<< "expected " << kernel(index_odd[i],index_even[j]) << std::endl; |
537 |
05 Mar 06 |
peter |
104 |
} |
1237 |
15 Mar 08 |
peter |
105 |
if (suite.ok()) |
1237 |
15 Mar 08 |
peter |
106 |
suite.err() << "Ok." << std::endl; |
4200 |
19 Aug 22 |
peter |
107 |
|
1237 |
15 Mar 08 |
peter |
108 |
suite.err() << "KernelLookup::KernelLookup(const KernelLookup&,\n" |
537 |
05 Mar 06 |
peter |
109 |
<< " const vector<size_t>&,\n" |
537 |
05 Mar 06 |
peter |
110 |
<< " const vector<size_t>&)..."; |
537 |
05 Mar 06 |
peter |
111 |
std::vector<size_t> one(1,1); |
1134 |
23 Feb 08 |
peter |
112 |
classifier::KernelLookup k3(k2,utility::Index(one),utility::Index(one)); |
537 |
05 Mar 06 |
peter |
113 |
if (k3.rows()!=one.size() || k3.columns()!=one.size()) { |
1237 |
15 Mar 08 |
peter |
114 |
suite.add(false); |
1237 |
15 Mar 08 |
peter |
115 |
suite.err() << "ERROR:" << std::endl; |
1237 |
15 Mar 08 |
peter |
116 |
suite.err() << "Dimensions do not agree." << std::endl; |
537 |
05 Mar 06 |
peter |
117 |
} |
537 |
05 Mar 06 |
peter |
118 |
else if (k3(0,0)!=k2(1,1)){ |
1237 |
15 Mar 08 |
peter |
119 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
120 |
suite.err() << "ERROR:\n k3(0,0) found to be " << k3(0,0) |
537 |
05 Mar 06 |
peter |
121 |
<< " expected " << k2(1,1) << std::endl; |
537 |
05 Mar 06 |
peter |
122 |
} |
537 |
05 Mar 06 |
peter |
123 |
else |
1237 |
15 Mar 08 |
peter |
124 |
suite.err() << "Ok." << std::endl; |
537 |
05 Mar 06 |
peter |
125 |
|
1237 |
15 Mar 08 |
peter |
126 |
suite.err() << "KernelLookup::KernelLookup(const KernelLookup&)..."; |
537 |
05 Mar 06 |
peter |
127 |
classifier::KernelLookup k4(k2); |
538 |
05 Mar 06 |
peter |
128 |
if (k4.rows()!=k2.rows() || k4.columns()!=k2.columns()) { |
1237 |
15 Mar 08 |
peter |
129 |
suite.add(false); |
1237 |
15 Mar 08 |
peter |
130 |
suite.err() << "ERROR:" << std::endl; |
1237 |
15 Mar 08 |
peter |
131 |
suite.err() << "Dimensions do not agree." << std::endl; |
1237 |
15 Mar 08 |
peter |
132 |
suite.err() << "Dimension: rows " << k4.rows() << " columns " << k4.columns() |
538 |
05 Mar 06 |
peter |
133 |
<< "\nOriginal: rows " << k2.rows() << " columns " << k2.columns() |
538 |
05 Mar 06 |
peter |
134 |
<< std::endl; |
537 |
05 Mar 06 |
peter |
135 |
} |
537 |
05 Mar 06 |
peter |
136 |
else { |
4200 |
19 Aug 22 |
peter |
137 |
for (size_t i=0; i<k4.rows(); i++) |
4200 |
19 Aug 22 |
peter |
138 |
for (size_t j=0; j<k4.columns(); j++) |
537 |
05 Mar 06 |
peter |
139 |
if (k4(i,j)!=k2(i,j)) { |
1237 |
15 Mar 08 |
peter |
140 |
suite.add(false); |
1237 |
15 Mar 08 |
peter |
141 |
suite.err() << "ERROR:\n" |
537 |
05 Mar 06 |
peter |
142 |
<< "KernelLookup::KernelLookup(const KernelLookup&)" |
537 |
05 Mar 06 |
peter |
143 |
<< std::endl; |
4200 |
19 Aug 22 |
peter |
144 |
suite.err() << "k(" << i << "," << j << ") is " << k4(i,j) |
537 |
05 Mar 06 |
peter |
145 |
<< "expected " << k2(i,j) << std::endl; |
537 |
05 Mar 06 |
peter |
146 |
} |
537 |
05 Mar 06 |
peter |
147 |
} |
4200 |
19 Aug 22 |
peter |
148 |
|
1134 |
23 Feb 08 |
peter |
149 |
KernelLookup k5(k1,utility::Index(index_even),utility::Index(index_even)); |
559 |
11 Mar 06 |
peter |
150 |
std::vector<size_t> index5; |
559 |
11 Mar 06 |
peter |
151 |
index5.push_back(0); |
559 |
11 Mar 06 |
peter |
152 |
index5.push_back(2); |
1167 |
26 Feb 08 |
peter |
153 |
const KernelLookup* k6 = new KernelLookup(k5,utility::Index(index5), |
4200 |
19 Aug 22 |
peter |
154 |
utility::Index(index5)); |
4200 |
19 Aug 22 |
peter |
155 |
for (size_t s=0; s<k6->rows(); s++) |
4200 |
19 Aug 22 |
peter |
156 |
for (size_t t=0; t<k6->rows(); t++) |
1237 |
15 Mar 08 |
peter |
157 |
suite.add((*k6)(s,t)==(*k6)(t,s)); |
559 |
11 Mar 06 |
peter |
158 |
|
2143 |
15 Jan 10 |
peter |
159 |
suite.test_concept_container2d(*k6); |
2143 |
15 Jan 10 |
peter |
160 |
|
1237 |
15 Mar 08 |
peter |
161 |
return suite.return_value(); |
537 |
05 Mar 06 |
peter |
162 |
} |