1 | // $Id$ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
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 "Kernel.h" |
---|
24 | #include "DataLookup1D.h" |
---|
25 | #include "DataLookupWeighted1D.h" |
---|
26 | #include "KernelFunction.h" |
---|
27 | #include "MatrixLookup.h" |
---|
28 | #include "MatrixLookupWeighted.h" |
---|
29 | |
---|
30 | #include "yat/utility/Exception.h" |
---|
31 | |
---|
32 | #include <cassert> |
---|
33 | #include <vector> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace classifier { |
---|
38 | |
---|
39 | Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf, |
---|
40 | const bool own) |
---|
41 | : ml_(&data), mlw_(0), kf_(&kf), ref_count_w_(NULL) |
---|
42 | { |
---|
43 | if (own) |
---|
44 | ref_count_ = new unsigned int(1); |
---|
45 | else |
---|
46 | ref_count_ = NULL; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, |
---|
51 | const bool own) |
---|
52 | : ml_(NULL), mlw_(&data), kf_(&kf) |
---|
53 | { |
---|
54 | if (own){ |
---|
55 | ref_count_w_ = new unsigned int(1); |
---|
56 | } |
---|
57 | else { |
---|
58 | ref_count_w_ = NULL; |
---|
59 | } |
---|
60 | ref_count_ = NULL; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index) |
---|
65 | : kf_(other.kf_) |
---|
66 | { |
---|
67 | |
---|
68 | if (other.weighted()){ |
---|
69 | mlw_ = new MatrixLookupWeighted(*other.mlw_, utility::Index(index), |
---|
70 | utility::Index(other.mlw_->columns())); |
---|
71 | ref_count_w_ = new unsigned int(1); |
---|
72 | ml_=NULL; |
---|
73 | ref_count_ = NULL; |
---|
74 | } |
---|
75 | else{ |
---|
76 | ml_ = new MatrixLookup(*other.ml_, utility::Index(index),true); |
---|
77 | ref_count_ = new unsigned int(1); |
---|
78 | mlw_=NULL; |
---|
79 | ref_count_w_ = NULL; |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | Kernel::~Kernel() |
---|
86 | { |
---|
87 | if (ref_count_) |
---|
88 | if (!--(*ref_count_)) |
---|
89 | delete ml_; |
---|
90 | |
---|
91 | if (ref_count_w_) |
---|
92 | if (!--(*ref_count_w_)) |
---|
93 | delete mlw_; |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | const MatrixLookup& Kernel::data(void) const |
---|
99 | { |
---|
100 | if (weighted()) |
---|
101 | throw utility::runtime_error("Kernel::data when Kernel is weighted"); |
---|
102 | assert(ml_); |
---|
103 | return *ml_; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | const MatrixLookupWeighted& Kernel::data_weighted(void) const |
---|
108 | { |
---|
109 | if (!weighted()) |
---|
110 | throw utility::runtime_error("Kernel:data_weighted when Kernel is unweighted"); |
---|
111 | assert(mlw_); |
---|
112 | return *mlw_; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | double Kernel::element(const DataLookup1D& vec, const size_t i) const |
---|
117 | { |
---|
118 | if (weighted()) |
---|
119 | return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false)); |
---|
120 | else |
---|
121 | return kf_->operator()(vec,DataLookup1D(*ml_,i, false)); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const |
---|
126 | { |
---|
127 | if (weighted()) |
---|
128 | return kf_->operator()(vec, DataLookupWeighted1D(*mlw_,i, false)); |
---|
129 | else |
---|
130 | return kf_->operator()(vec, DataLookup1D(*ml_,i, false)); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | size_t Kernel::size(void) const |
---|
135 | { |
---|
136 | if (weighted()) |
---|
137 | return mlw_->columns(); |
---|
138 | assert(ml_); |
---|
139 | return ml_->columns(); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | bool Kernel::weighted(void) const |
---|
144 | { |
---|
145 | return mlw_; |
---|
146 | } |
---|
147 | |
---|
148 | }}} // of namespace classifier, yat, and theplu |
---|