Go to the documentation of this file.00001
00002 #ifndef vpdl_kernel_base_h_
00003 #define vpdl_kernel_base_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <vpdl/vpdl_multi_cmp_dist.h>
00016 #include <vpdl/vpdt/vpdt_access.h>
00017 #include <vcl_cassert.h>
00018 #include <vcl_vector.h>
00019
00020
00021
00022
00023
00024
00025 template<class T, unsigned int n=0>
00026 class vpdl_kernel_base : public vpdl_multi_cmp_dist<T,n>
00027 {
00028 public:
00029
00030 typedef typename vpdt_field_default<T,n>::type vector;
00031
00032 typedef typename vpdt_field_traits<vector>::matrix_type matrix;
00033
00034
00035 vpdl_kernel_base() {}
00036
00037
00038 vpdl_kernel_base(const vcl_vector<vector>& samplez)
00039 : samples_(samplez) {}
00040
00041
00042 unsigned int num_components() const { return samples_.size(); }
00043
00044
00045 virtual unsigned int dimension() const
00046 {
00047 if (n > 0 || num_components() == 0)
00048 return n;
00049 return vpdt_size(samples_[0]);
00050 }
00051
00052
00053 virtual void add_sample(const vector& s)
00054 {
00055
00056 assert(vpdt_size(s) == this->dimension() || num_components() == 0);
00057 samples_.push_back(s);
00058 }
00059
00060
00061 virtual void clear_samples()
00062 {
00063 samples_.clear();
00064 }
00065
00066
00067 virtual void set_samples(const vcl_vector<vector>& samplez)
00068 {
00069 samples_ = samplez;
00070 }
00071
00072
00073 const vcl_vector<vector>& samples() const
00074 {
00075 return samples_;
00076 }
00077
00078
00079
00080 virtual void compute_mean(vector& mean) const
00081 {
00082 const unsigned int d = this->dimension();
00083 vpdt_set_size(mean,d);
00084 vpdt_fill(mean,T(0));
00085 if (samples_.empty())
00086 return;
00087 typedef typename vcl_vector<vector>::const_iterator samp_itr;
00088 for (samp_itr s = samples_.begin(); s != samples_.end(); ++s) {
00089 mean += *s;
00090 }
00091 mean /= T(samples_.size());
00092 }
00093
00094 private:
00095
00096 vcl_vector<vector> samples_;
00097 };
00098
00099
00100
00101
00102 template<class T, unsigned int n=0>
00103 class vpdl_kernel_fbw_base : public vpdl_kernel_base<T,n>
00104 {
00105 public:
00106
00107 typedef typename vpdt_field_default<T,n>::type vector;
00108
00109 typedef typename vpdt_field_traits<vector>::matrix_type matrix;
00110
00111
00112 vpdl_kernel_fbw_base()
00113 : bandwidth_(T(1)) {}
00114
00115
00116 vpdl_kernel_fbw_base(const vcl_vector<vector>& samplez, T bandwid = T(1))
00117 : vpdl_kernel_base<T,n>(samplez), bandwidth_(bandwid) {}
00118
00119
00120 T bandwidth() const { return bandwidth_; }
00121
00122
00123 void set_bandwidth(T b) { bandwidth_ = b; }
00124
00125
00126 virtual T kernel_norm_const() const = 0;
00127
00128
00129
00130
00131 virtual T norm_const() const
00132 {
00133 return kernel_norm_const()/this->num_components();
00134 }
00135
00136 private:
00137
00138 T bandwidth_;
00139 };
00140
00141
00142
00143
00144 template<class T, unsigned int n=0>
00145 class vpdl_kernel_vbw_base : public vpdl_kernel_base<T,n>
00146 {
00147 public:
00148
00149 typedef typename vpdt_field_default<T,n>::type vector;
00150
00151 typedef typename vpdt_field_traits<vector>::matrix matrix;
00152
00153
00154 vpdl_kernel_vbw_base(unsigned int var_dim = n)
00155 : vpdl_kernel_base<T,n>(var_dim) {}
00156
00157
00158 vpdl_kernel_vbw_base(const vcl_vector<vector>& samplez,
00159 const vcl_vector<T>& bandwidthz)
00160 : vpdl_kernel_base<T,n>(samplez), bandwidths_(bandwidthz) {}
00161
00162
00163 virtual void add_sample(const vector& s)
00164 {
00165 vpdl_kernel_base<T,n>::add_sample(s);
00166 bandwidths_.push_back(T(1));
00167 }
00168
00169
00170 virtual void add_sample(const vector& s, T bw)
00171 {
00172 vpdl_kernel_base<T,n>::add_sample(s);
00173 bandwidths_.push_back(bw);
00174 }
00175
00176
00177 virtual void clear_samples()
00178 {
00179 vpdl_kernel_base<T,n>::clear_samples();
00180 bandwidths_.clear();
00181 }
00182
00183
00184 virtual void set_samples(const vcl_vector<vector>& samplez)
00185 {
00186 vpdl_kernel_base<T,n>::set_samples(samplez);
00187 bandwidths_.clear();
00188 bandwidths_.resize(samplez.size(),T(1));
00189 }
00190
00191
00192 virtual void set_samples(const vcl_vector<vector>& samplez,
00193 const vcl_vector<T>& bandwidthz)
00194 {
00195 assert(samplez.size() == bandwidthz.size());
00196 vpdl_kernel_base<T,n>::set_samples(samplez);
00197 bandwidths_ = bandwidthz;
00198 }
00199
00200
00201 const vcl_vector<T>& bandwidths() const { return bandwidths_; }
00202
00203 private:
00204
00205 vcl_vector<T> bandwidths_;
00206 };
00207
00208
00209 #endif // vpdl_kernel_base_h_