Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "pdf1d_flat.h"
00009
00010 #include <vcl_cassert.h>
00011 #include <vcl_string.h>
00012 #include <vcl_cmath.h>
00013
00014 #include <pdf1d/pdf1d_flat_sampler.h>
00015 #include <pdf1d/pdf1d_sampler.h>
00016
00017
00018
00019 pdf1d_flat::pdf1d_flat()
00020 {
00021 set(0,1);
00022 }
00023
00024 pdf1d_flat::pdf1d_flat(double lo, double hi)
00025 {
00026 set(lo,hi);
00027 }
00028
00029
00030
00031 pdf1d_flat::~pdf1d_flat()
00032 {
00033 }
00034
00035
00036
00037
00038 void pdf1d_flat::set(double lo, double hi)
00039 {
00040 assert(hi>lo);
00041 lo_ = lo;
00042 hi_ = hi;
00043
00044 double w = hi-lo;
00045 p_ = 1.0/(w);
00046 log_p_ = vcl_log(p_);
00047
00048 pdf1d_pdf::set_mean(0.5*(lo+hi));
00049 pdf1d_pdf::set_variance(w*w/12.0);
00050 }
00051
00052
00053
00054
00055 pdf1d_sampler* pdf1d_flat::new_sampler() const
00056 {
00057 pdf1d_flat_sampler *i = new pdf1d_flat_sampler;
00058 i->set_model(*this);
00059 return i;
00060 }
00061
00062
00063
00064 double pdf1d_flat::operator()(double x) const
00065 {
00066 if (x>=lo_ && x<=hi_) return p_;
00067 return 0;
00068 }
00069
00070
00071
00072 double pdf1d_flat::log_p(double x) const
00073 {
00074 if (x>=lo_ && x<=hi_) return log_p_;
00075 return 0;
00076 }
00077
00078
00079 double pdf1d_flat::cdf(double x) const
00080 {
00081 if (x<=lo_) return 0;
00082 if (x>=hi_) return 1;
00083 return p_*(x-lo_);
00084 }
00085
00086
00087
00088
00089 bool pdf1d_flat::cdf_is_analytic() const
00090 {
00091 return true;
00092 }
00093
00094
00095
00096
00097 double pdf1d_flat::gradient(double x,
00098 double& p) const
00099 {
00100 if (x>=lo_ && x<=hi_) p=p_;
00101 else p=0.0;
00102 return 0;
00103 }
00104
00105
00106
00107
00108 double pdf1d_flat::log_prob_thresh(double ) const
00109 {
00110 return log_p_;
00111 }
00112
00113
00114
00115 double pdf1d_flat::nearest_plausible(double x, double ) const
00116 {
00117 if (x>hi_) return hi_;
00118 if (x<lo_) return lo_;
00119 return x;
00120 }
00121
00122
00123
00124
00125
00126 vcl_string pdf1d_flat::is_a() const
00127 {
00128 static vcl_string class_name_ = "pdf1d_flat";
00129 return class_name_;
00130 }
00131
00132
00133
00134
00135
00136 bool pdf1d_flat::is_class(vcl_string const& s) const
00137 {
00138 return pdf1d_pdf::is_class(s) || s==pdf1d_flat::is_a();
00139 }
00140
00141
00142
00143
00144
00145 short pdf1d_flat::version_no() const
00146 {
00147 return 1;
00148 }
00149
00150
00151
00152
00153
00154 pdf1d_pdf* pdf1d_flat::clone() const
00155 {
00156 return new pdf1d_flat(*this);
00157 }
00158
00159
00160
00161
00162
00163
00164 void pdf1d_flat::print_summary(vcl_ostream& os) const
00165 {
00166 os<<"Range ["<<lo_<<","<<hi_<<"]";
00167 }
00168
00169
00170
00171
00172
00173 void pdf1d_flat::b_write(vsl_b_ostream& bfs) const
00174 {
00175 vsl_b_write(bfs,is_a());
00176 vsl_b_write(bfs,version_no());
00177 vsl_b_write(bfs,lo_);
00178 vsl_b_write(bfs,hi_);
00179 }
00180
00181
00182
00183
00184
00185 void pdf1d_flat::b_read(vsl_b_istream& bfs)
00186 {
00187 if (!bfs) return;
00188
00189 vcl_string name;
00190 vsl_b_read(bfs,name);
00191 if (name != is_a())
00192 {
00193 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, pdf1d_flat &)\n";
00194 vcl_cerr << " Attempted to load object of type ";
00195 vcl_cerr << name <<" into object of type " << is_a() << vcl_endl;
00196 bfs.is().clear(vcl_ios::badbit);
00197 return;
00198 }
00199
00200 short version;
00201 vsl_b_read(bfs,version);
00202 switch (version)
00203 {
00204 case (1):
00205 vsl_b_read(bfs,lo_);
00206 vsl_b_read(bfs,hi_);
00207 break;
00208 default:
00209 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, pdf1d_flat &)\n";
00210 vcl_cerr << " Unknown version number "<< version << vcl_endl;
00211 bfs.is().clear(vcl_ios::badbit);
00212 return;
00213 }
00214
00215 set(lo_,hi_);
00216 }
00217
00218