1 | // $Id$ |
---|
2 | |
---|
3 | #include <c++_tools/classifier/KernelLookup.h> |
---|
4 | #include <c++_tools/classifier/DataLookup2D.h> |
---|
5 | |
---|
6 | |
---|
7 | #include <cassert> |
---|
8 | #ifndef NDEBUG |
---|
9 | #include <algorithm> |
---|
10 | #endif |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace classifier { |
---|
14 | |
---|
15 | KernelLookup::KernelLookup(const Kernel& kernel, const bool own) |
---|
16 | : DataLookup2D(own), kernel_(&kernel) |
---|
17 | { |
---|
18 | column_index_.reserve(kernel.size()); |
---|
19 | for(size_t i=0; i<kernel.size(); i++) |
---|
20 | column_index_.push_back(i); |
---|
21 | row_index_=column_index_; |
---|
22 | } |
---|
23 | |
---|
24 | KernelLookup::KernelLookup(const Kernel& kernel, |
---|
25 | const std::vector<size_t>& row, |
---|
26 | const std::vector<size_t>& column, |
---|
27 | const bool owner) |
---|
28 | : DataLookup2D(row,column,owner), kernel_(&kernel) |
---|
29 | { |
---|
30 | // Checking that each row index is less than kernel.rows() |
---|
31 | assert(row.empty() || |
---|
32 | *(std::max_element(row.begin(),row.end()))<kernel_->size()); |
---|
33 | // Checking that each column index is less than kernel.column() |
---|
34 | assert(column.empty() || |
---|
35 | *(std::max_element(column.begin(),column.end()))<kernel_->size()); |
---|
36 | |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | KernelLookup::KernelLookup(const KernelLookup& kernel, |
---|
41 | const std::vector<size_t>& row, |
---|
42 | const std::vector<size_t>& column) |
---|
43 | : DataLookup2D(kernel,row,column), kernel_(kernel.kernel_) |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | KernelLookup::KernelLookup(const KernelLookup& kernel) |
---|
49 | : DataLookup2D(kernel), kernel_(kernel.kernel_) |
---|
50 | { |
---|
51 | // Checking that no index is out of range |
---|
52 | assert(row_index_.empty() || |
---|
53 | *(max_element(row_index_.begin(), row_index_.end()))<kernel_->size()); |
---|
54 | assert(column_index_.empty() || |
---|
55 | *(max_element(column_index_.begin(), column_index_.end()))< |
---|
56 | kernel_->size()); |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | KernelLookup::KernelLookup(const KernelLookup& kl, |
---|
62 | const std::vector<size_t>& index, |
---|
63 | const bool row) |
---|
64 | : DataLookup2D(kl,index,row), kernel_(kl.kernel_) |
---|
65 | { |
---|
66 | // Checking that no index is out of range |
---|
67 | assert(row_index_.empty() || |
---|
68 | *(max_element(row_index_.begin(), row_index_.end()))<kernel_->size()); |
---|
69 | assert(column_index_.empty() || |
---|
70 | *(max_element(column_index_.begin(), column_index_.end()))< |
---|
71 | kernel_->size()); |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | KernelLookup::~KernelLookup(void) |
---|
77 | { |
---|
78 | if (owner_) |
---|
79 | delete kernel_; |
---|
80 | } |
---|
81 | |
---|
82 | const KernelLookup* |
---|
83 | KernelLookup::training_data(const std::vector<size_t>& train) const |
---|
84 | { |
---|
85 | return new KernelLookup(*this,train,train); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | const KernelLookup* |
---|
90 | KernelLookup::training_data(const std::vector<size_t>& features, |
---|
91 | const std::vector<size_t>& train) const |
---|
92 | { |
---|
93 | const Kernel* kernel = kernel_->selected(features); |
---|
94 | return new KernelLookup(*kernel, train, train, true); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | const KernelLookup* |
---|
99 | KernelLookup::validation_data(const std::vector<size_t>& train, |
---|
100 | const std::vector<size_t>& validation) const |
---|
101 | { |
---|
102 | return new KernelLookup(*this,train,validation); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | const KernelLookup* |
---|
107 | KernelLookup::validation_data(const std::vector<size_t>& features, |
---|
108 | const std::vector<size_t>& train, |
---|
109 | const std::vector<size_t>& validation) const |
---|
110 | { |
---|
111 | const Kernel* kernel = kernel_->selected(features); |
---|
112 | return new KernelLookup(*kernel, train, validation, true); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | const KernelLookup* |
---|
117 | KernelLookup::selected(const std::vector<size_t>& inputs) const |
---|
118 | { |
---|
119 | const Kernel* kernel = kernel_->selected(inputs); |
---|
120 | return new KernelLookup(*kernel, row_index_, column_index_, true); |
---|
121 | } |
---|
122 | |
---|
123 | }} // of namespace classifier and namespace theplu |
---|