Go to the documentation of this file.00001 #ifndef bsl_opinion_h_
00002 #define bsl_opinion_h_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <vcl_iostream.h>
00020
00021 #include <vcl_limits.h>
00022 #include <vcl_cmath.h>
00023
00024 class bsl_opinion
00025 {
00026 public:
00027 bsl_opinion() : u_(1.0f), b_(0.0f), a_(0.5f) {}
00028 bsl_opinion(float u, float b) : u_(u), b_(b), a_(0.5) {}
00029 bsl_opinion(float u, float b, float a) : u_(u), b_(b), a_(a) {}
00030
00031
00032 bsl_opinion(float b) { b_=b; u_=1.0f-b; a_ = 0.5f;}
00033
00034
00035 bsl_opinion(bsl_opinion const& o) : u_(o.u()), b_(o.b()), a_(o.a()) {}
00036
00037
00038 ~bsl_opinion() {}
00039
00040 float u() const { return u_; }
00041
00042 float b() const { return b_; }
00043
00044 float a() const { return a_; }
00045
00046 float d() const { return 1.0f-b_-u_; }
00047
00048 float expectation() const { return b_+u_*a_; }
00049
00050 void set(float u, float b) { u_=u; b_=b; }
00051
00052 void set_u(float u) { u_=u; }
00053
00054 void set_b(float b) { b_=b; u_=1.0f-b; }
00055
00056 void set_a(float a) { a_ = a; }
00057
00058 bool operator==(bsl_opinion const& o) const { return b_==o.b() && u_==o.u() && a_==o.a(); }
00059
00060 private:
00061 float u_;
00062 float b_;
00063 float a_;
00064 };
00065
00066 inline bsl_opinion fuse(bsl_opinion const& lhs, bsl_opinion const& rhs)
00067 {
00068 float bA = lhs.b();
00069 float bB = rhs.b();
00070 float uA = lhs.u();
00071 float uB = rhs.u();
00072
00073
00074 float kappa = uA + uB - uA*uB;
00075
00076
00077 if (vcl_abs(uA) > vcl_numeric_limits<float>::epsilon() || vcl_abs(uB) > vcl_numeric_limits<float>::epsilon()) {
00078 float b = (bA*uB + bB*uA)/kappa;
00079 float u = uA*uB/kappa;
00080 return bsl_opinion(u,b,lhs.a());
00081 }
00082 else {
00083 bsl_opinion op((bA+bB)/2.0f); op.set_a(lhs.a());
00084 return op;
00085 }
00086 }
00087
00088 inline bool operator> (bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() > rhs.b(); }
00089 inline bool operator>=(bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() >= rhs.b(); }
00090 inline bool operator< (bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() < rhs.b(); }
00091
00092 inline bsl_opinion operator+(bsl_opinion const& lhs, bsl_opinion const& rhs) { return bsl_opinion(lhs.b()+rhs.b()); }
00093 inline float operator+(bsl_opinion const& lhs, float rhs) { return lhs.b() + rhs; }
00094 inline float operator*(bsl_opinion const& lhs, bsl_opinion const& rhs) { return lhs.b() * rhs.b(); }
00095
00096 inline bsl_opinion operator/(float o1, bsl_opinion const& o2) { return bsl_opinion(o1/o2.b()); }
00097 inline bsl_opinion operator/(bsl_opinion const& o1, bsl_opinion const& o2) { return bsl_opinion(o1.b()/o2.b()); }
00098
00099 inline vcl_ostream& operator<< (vcl_ostream& s, bsl_opinion const& o) { s << "bsl_opinion [b=" << o.b() << " u=" << o.u() << ']' << vcl_endl; return s; }
00100
00101
00102 #endif
00103