Go to the documentation of this file.00001 
00002 #ifndef bsta_gaussian_indep_h_
00003 #define bsta_gaussian_indep_h_
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 #include "bsta_gaussian.h"
00017 #include <vnl/vnl_vector_fixed.h>
00018 #include <vcl_iostream.h>
00019 #include <vnl/vnl_random.h>
00020 
00021 
00022 
00023 template <class T, unsigned n>
00024 class bsta_gaussian_indep : public bsta_gaussian<T,n>
00025 {
00026  public:
00027   typedef vnl_vector_fixed<T,n> covar_type;
00028   typedef typename bsta_gaussian<T,n>::vector_type vector_;
00029 
00030   
00031   bsta_gaussian_indep()
00032   : bsta_gaussian<T,n>(), diag_covar_(T(0)), det_covar_(T(0)) {}
00033 
00034   
00035   bsta_gaussian_indep(const vnl_vector_fixed<T,n>& mean,
00036                       const covar_type& covar)
00037   : bsta_gaussian<T,n>(mean), diag_covar_(covar), det_covar_(T(-1))
00038   { compute_det(); }
00039 
00040   
00041   const covar_type& diag_covar() const
00042   { return diag_covar_; }
00043 
00044   
00045   const covar_type& covar() const
00046   { return diag_covar_; }
00047 
00048   
00049   void set_covar(const covar_type& covar)
00050   { diag_covar_ = covar; compute_det(); }
00051 
00052   
00053   T dist_prob_density(const T& sqr_mahal_dist) const
00054   {
00055     if (det_covar_ <= 0)
00056       return T(0);
00057     return static_cast<T>(vcl_sqrt(1/(det_covar_*two_pi_power<n>::value()))
00058          * vcl_exp(-sqr_mahal_dist/2));
00059   }
00060 
00061   
00062   T prob_density(const vnl_vector_fixed<T,n>& pt) const
00063   {
00064     return dist_prob_density(sqr_mahalanobis_dist(pt));
00065   }
00066 
00067   
00068   T probability(const vnl_vector_fixed<T,n>& min_pt,
00069                 const vnl_vector_fixed<T,n>& max_pt) const;
00070 
00071   
00072   T sqr_mahalanobis_dist(const vnl_vector_fixed<T,n>& pt) const;
00073 
00074   
00075   T det_covar() const { return det_covar_; }
00076 
00077   
00078   vector_ sample(vnl_random& ran_gen) const
00079   {
00080     vector_ d = bsta_gaussian<T,n>::mean_;
00081     covar_type v = diag_covar_;
00082     for (unsigned j = 0; j < n; j++) {
00083       v[j] = (T)(vcl_sqrt(v[j])*ran_gen.normal());
00084     }
00085     vector_ sum = d+v;
00086     return sum;
00087   }
00088 
00089   
00090   covar_type gradient(const vnl_vector_fixed<T,n>& pt) const;
00091   
00092  protected:
00093   
00094   covar_type diag_covar_;
00095 
00096   
00097   T det_covar_;
00098 
00099  private:
00100   
00101   void compute_det();
00102 };
00103 
00104 template <class T , unsigned n>
00105 inline vcl_ostream& operator<< (vcl_ostream& os,
00106                                 bsta_gaussian_indep<T, n> const& g)
00107 {
00108   os << "gauss_indep:mean(" << g.mean() << ")\n"
00109      << "gauss_indep:covar(" << g.diag_covar() << ")\n";
00110   return os;
00111 }
00112 
00113 #endif // bsta_gaussian_indep_h_