Go to the documentation of this file.00001
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005
00006
00007
00008
00009
00010 #include "vpdfl_gaussian_kernel_pdf.h"
00011
00012 #include <vcl_cstdlib.h>
00013 #include <vcl_cassert.h>
00014 #include <vcl_string.h>
00015 #include <vcl_cmath.h>
00016
00017 #include <vnl/vnl_math.h>
00018 #include <vpdfl/vpdfl_gaussian_kernel_pdf_sampler.h>
00019 #include <vpdfl/vpdfl_sampler_base.h>
00020
00021
00022
00023 vpdfl_gaussian_kernel_pdf::vpdfl_gaussian_kernel_pdf()
00024 {
00025 }
00026
00027
00028
00029 vpdfl_gaussian_kernel_pdf::~vpdfl_gaussian_kernel_pdf()
00030 {
00031 }
00032
00033
00034
00035
00036 double vpdfl_gaussian_kernel_pdf::operator()(const vnl_vector<double>& x0) const
00037 {
00038 int n = x_.size();
00039 assert(n>0);
00040 int dim = x_[0].size();
00041 double p;
00042 const vnl_vector<double>* x = &x_[0];
00043 const double* w = width_.data_block();
00044 double k = 1.0/(n*vcl_pow(2*vnl_math::pi,0.5*dim));
00045 double sum = 0;
00046
00047 for (int i=0;i<n;++i)
00048 {
00049 double M = vnl_vector_ssd(x[i],x0)/(w[i]*w[i]);
00050 if (M<20)
00051 sum += vcl_exp(-0.5*M)/vcl_pow(w[i],dim);
00052 }
00053
00054 p = k*sum;
00055
00056 return p;
00057 }
00058
00059
00060 double vpdfl_gaussian_kernel_pdf::log_p(const vnl_vector<double>& x) const
00061 {
00062 return vcl_log(vpdfl_gaussian_kernel_pdf::operator()(x));
00063 }
00064
00065
00066
00067
00068 vpdfl_sampler_base* vpdfl_gaussian_kernel_pdf::new_sampler() const
00069 {
00070 vpdfl_gaussian_kernel_pdf_sampler *i = new vpdfl_gaussian_kernel_pdf_sampler;
00071 i->set_model(*this);
00072 return i;
00073 }
00074
00075
00076
00077
00078 void vpdfl_gaussian_kernel_pdf::gradient(vnl_vector<double>& ,
00079 vnl_vector<double>const& ,
00080 double& ) const
00081 {
00082 vcl_cerr<<"vpdfl_gaussian_kernel_pdf::gradient() Not yet implemented.\n";
00083 vcl_abort();
00084 }
00085
00086
00087
00088 void vpdfl_gaussian_kernel_pdf::nearest_plausible(vnl_vector<double>& , double ) const
00089 {
00090 vcl_cerr<<"vpdfl_gaussian_kernel_pdf::nearest_plausible() Not yet implemented.\n";
00091 vcl_abort();
00092 }
00093
00094
00095
00096
00097
00098 vcl_string vpdfl_gaussian_kernel_pdf::is_a() const
00099 {
00100 static vcl_string class_name_ = "vpdfl_gaussian_kernel_pdf";
00101 return class_name_;
00102 }
00103
00104
00105
00106
00107
00108 bool vpdfl_gaussian_kernel_pdf::is_class(vcl_string const& s) const
00109 {
00110 return vpdfl_kernel_pdf::is_class(s) || s==vpdfl_gaussian_kernel_pdf::is_a();
00111 }
00112
00113
00114
00115
00116
00117 short vpdfl_gaussian_kernel_pdf::version_no() const
00118 {
00119 return 1;
00120 }
00121
00122
00123
00124
00125
00126 vpdfl_pdf_base* vpdfl_gaussian_kernel_pdf::clone() const
00127 {
00128 return new vpdfl_gaussian_kernel_pdf(*this);
00129 }
00130
00131
00132
00133
00134
00135
00136 void vpdfl_gaussian_kernel_pdf::print_summary(vcl_ostream& os) const
00137 {
00138 vpdfl_kernel_pdf::print_summary(os);
00139 }
00140
00141
00142
00143
00144
00145 void vpdfl_gaussian_kernel_pdf::b_write(vsl_b_ostream& bfs) const
00146 {
00147 vsl_b_write(bfs,is_a());
00148 vsl_b_write(bfs,version_no());
00149 vpdfl_kernel_pdf::b_write(bfs);
00150 }
00151
00152
00153
00154
00155
00156 void vpdfl_gaussian_kernel_pdf::b_read(vsl_b_istream& bfs)
00157 {
00158 if (!bfs) return;
00159
00160 vcl_string name;
00161 vsl_b_read(bfs,name);
00162 if (name != is_a())
00163 {
00164 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vpdfl_gaussian_kernel_pdf &)\n"
00165 << " Attempted to load object of type "
00166 << name <<" into object of type " << is_a() << '\n';
00167 bfs.is().clear(vcl_ios::badbit);
00168 return;
00169 }
00170
00171 short version;
00172 vsl_b_read(bfs,version);
00173 switch (version)
00174 {
00175 case (1):
00176 vpdfl_kernel_pdf::b_read(bfs);
00177 break;
00178 default:
00179 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vpdfl_gaussian_kernel_pdf &)\n"
00180 << " Unknown version number "<< version << '\n';
00181 bfs.is().clear(vcl_ios::badbit);
00182 return;
00183 }
00184 }
00185
00186