template <class GF_q, class real> void sum_prod_alg_abstract<GF_q, real>::compute_probs(
      array1vd_t& ro)
   {
   //ensure the output vector has the right length
   ro.init(this->length_n);

   //initialise some helper variables
   int num_of_elements = GF_q::elements();
   real a_n = real(0.0);
   int size_of_M_n = 0;
   int pos_m;
   for (int loop_n = 0; loop_n < this->length_n; loop_n++)
      {
      ro(loop_n) = this->received_probs(loop_n);
      size_of_M_n = this->M_n(loop_n).size();
      for (int loop_e = 0; loop_e < num_of_elements; loop_e++)
         {
         for (int loop_m = 0; loop_m < size_of_M_n; loop_m++)
            {
            pos_m = this->M_n(loop_n)(loop_m) - 1;//we count from 0
            ro(loop_n)(loop_e) *= this->marginal_probs(pos_m, loop_n).r_mxn(
                  loop_e);
            }
         //Use appropriate clipping method
         perform_clipping(ro(loop_n)(loop_e));
         }
      //Note the following step is not strictly necessary apart from making the result
      //look neater - however it only adds a small overhead

      //normalise the result so that q_n_0+q_n_1=1
      a_n = ro(loop_n).sum();
      assertalways(a_n!=real(0.0));
      ro(loop_n) /= a_n;
      }
   }
Ejemplo n.º 2
0
void sum_prod_alg_trad<GF_q, real>::spa_init(const array1vd_t& recvd_probs)
   {

   //initialise the marginal prob values
   int num_of_elements = GF_q::elements();
   real tmp_prob = real(0.0);
   real alpha = real(0.0);

   //ensure we don't have zero probabilities
   //and normalise the probs at the same time

   this->received_probs.init(recvd_probs.size());
   for (int loop_n = 0; loop_n < this->length_n; loop_n++)
      {
      this->received_probs(loop_n).init(num_of_elements);
      alpha = real(0.0);
      for (int loop_e = 0; loop_e < num_of_elements; loop_e++)
         {
         tmp_prob = recvd_probs(loop_n)(loop_e);
         //Clipping HACK
         perform_clipping(tmp_prob);
         this->received_probs(loop_n)(loop_e) = tmp_prob;
         alpha += tmp_prob;
         }
      assertalways(alpha!=real(0.0));
      this->received_probs(loop_n) /= alpha;
      }

   //this uses the description of the algorithm as given by
   //MacKay in Information Theory, Inference and Learning Algorithms(2003)
   //on page 560 - chapter 47.3

   //some helper variables
   int pos = 0;
   int non_zeros = 0;

   //simply set q_mxn(0)=P_n(0)=P(x_n=0) and q_mxn(1)=P_n(1)=P(x_n=1)
   for (int loop_m = 0; loop_m < this->dim_m; loop_m++)
      {
      non_zeros = this->N_m(loop_m).size();
      for (int loop_n = 0; loop_n < non_zeros; loop_n++)
         {
         pos = this->N_m(loop_m)(loop_n) - 1;//we count from zero;
         this->marginal_probs(loop_m, pos).q_mxn = this->received_probs(pos);
         this->marginal_probs(loop_m, pos).r_mxn.init(num_of_elements);
         this->marginal_probs(loop_m, pos).r_mxn = 0.0;
         }
      }

#if DEBUG>=2
   libbase::trace << " Memory Usage:\n ";
   libbase::trace << this->marginal_probs.size()
   * sizeof(sum_prod_alg_abstract<GF_q,real>::marginals) / double(1 << 20)
   << " MB" << std::endl;

   libbase::trace << std::endl << "The marginal matrix is given by:" << std::endl;
   this->print_marginal_probs(libbase::trace);
#endif

   }