// file: bw_occ_1.cc
//

// isip include files
//
#include "bw_train.h"
#include "bw_train_constants.h"

// method: comp_focc_cc
//
// arguments:
//  float_8& for_occ : (output) the forward occupancy part
//  float_4** trans : (input) the transition matrix of the current state
//  float_8** for_probt : (input) the forward probability of current frame
//  float_8** for_probt1 : (input) the forward prob. of previous frame
//  int_4 mod_ind : (input) the current model index
//  int_4 loc_st : (input) number of states in current model
//  int_4 cur_st : (input) the local state index
//  int_4 frame : (input) the index of current frame
//
// return a logical flag to indicate success
//
// this method calculates the occupancies of a state
// in a model
//
logical_1 comp_focc_cc(float_8& for_occ_a, float_4** trans_a,
		       float_8** for_probt_a, float_8** for_probt1_a,
		       int_4 mod_ind_a, int_4 loc_st_a, int_4 cur_st_a,
		       int_4 frame_a) {
  
  // local variables
  //
  float_8 tmp_trans = BW_LOG_ZERO;
  float_8 tmp_trans1 = BW_LOG_ZERO;
  float_8 tmp_score = BW_LOG_ZERO;
  
  // compute the transition to current state
  //
  tmp_trans = trans_a[0][cur_st_a-1];
  
  // compute the forward occupancy
  //
  if (frame_a == 1) {
    for_occ_a = for_probt_a[mod_ind_a][1] + tmp_trans;
  }
  else {
    
    // reset variable
    //
    tmp_score = BW_LOG_ZERO;
    
    // compute the sum
    //
    for (int_4 i = 2; i < loc_st_a; i++) {
      tmp_trans1 = trans_a[i-1][cur_st_a-1];
      tmp_score = log_add_cc(tmp_score, (for_probt1_a[mod_ind_a][i] +
					 tmp_trans1));
    }
    for_occ_a = log_add_cc((for_probt_a[mod_ind_a][1] + tmp_trans),
			   tmp_score);
  }
  
  if (for_occ_a <= BW_LOG_ZERO) {
    for_occ_a = BW_LOG_ZERO;
  }

  // exit gracefully
  //
  return(ISIP_TRUE);
}
