void run_viterbi(ModelParams *model_params, Observation *observations, int *state_indices, double *state_probabilities, double *log_prob) { double **log_delta; int **psi; double *log_pi; double **log_a; int i, j; long t; double max_logprob, this_logprob; int max_index, index; double **bprob, **eprob, **alpha, **beta, **gamma, mll; /* Find probabilities by solving for gammas: */ fprintf(stderr, "Starting viterbi\n"); bprob = alloc_double_matrix(model_params->T, model_params->N); eprob = alloc_double_matrix(model_params->T, model_params->N); alpha = alloc_double_matrix(model_params->T, model_params->N); beta = alloc_double_matrix(model_params->T, model_params->N); gamma = alloc_double_matrix(model_params->T, model_params->N); calc_bprobs(model_params, observations, bprob); calc_eprobs(model_params, observations, eprob); calc_alphas(model_params, observations, alpha, bprob, eprob, &mll); calc_betas(model_params, observations, beta, bprob, eprob); calc_gammas(model_params, alpha, beta, gamma); /* Calculate logs of parameters for easier manipulation. */ log_pi = alloc_double_vector(model_params->N); for (i = 0; i < model_params->N; i++) { log_pi[i] = log((model_params->pi)[i]); } log_a = alloc_double_matrix(model_params->N, model_params->N); for (i = 0; i < model_params->N; i++) { for (j = 0; j < model_params->N; j++) { log_a[i][j] = log(model_params->a[i][j]); } } log_delta = alloc_double_matrix(model_params->T, model_params->N); psi = alloc_int_matrix(model_params->T, model_params->N); /* Initialization */ for (i = 0; i < model_params->N; i++) { log_delta[0][i] = log_pi[i] + log(bprob[0][i]) + log(eprob[0][i]); psi[0][i] = 0; } /* Recursion */ for (t = 1; t < model_params->T; t++) { for (i = 0; i < model_params->N; i++) { max_logprob = -99999999.0; for (j = 0; j < model_params->N; j++) { this_logprob = log_delta[t-1][j] + log_a[j][i]; if (this_logprob > max_logprob) { max_logprob = this_logprob; max_index = j; } } log_delta[t][i] = max_logprob + log(bprob[t][i]) + log(eprob[t][i]); psi[t][i] = max_index; } } /* Termination */ *log_prob = -99999999.0; state_indices[model_params->T - 1] = 1; for (i = 0; i < model_params->N; i++) { if (log_delta[model_params->T - 1][i] > *log_prob) { *log_prob = log_delta[model_params->T - 1][i]; state_indices[model_params->T - 1] = i; } } /* Traceback */ for (t = model_params->T - 2; t >= 0; t--) { state_indices[t] = psi[t + 1][state_indices[t + 1]]; } /* free memory */ free_double_vector(log_pi); free_double_matrix(log_a, model_params->N); free_double_matrix(log_delta, model_params->T); free_int_matrix(psi, model_params->N); fprintf(stderr, "Done with viterbi\n"); for (t = 0; t < model_params->T; t++) { index = state_indices[t]; state_probabilities[t] = gamma[t][index]; /* fprintf(stderr, "time %ld probability %lf for state %d\n", t, state_probabilities[t], index); */ } free_double_matrix(eprob, model_params->T); free_double_matrix(alpha, model_params->T); free_double_matrix(beta, model_params->T); free_double_matrix(gamma, model_params->T); }
static GLenum convert_surf(knot_str_type *s_knot, knot_str_type *t_knot, surface_attribs *attrib, GLfloat **new_ctrl, GLint *s_n_ctrl, GLint *t_n_ctrl) { GLfloat **tmp_ctrl; GLfloat *ctrl_offset; GLint tmp_n_control; GLint i,j,t_cnt,s_cnt; GLint tmp_stride; GLint dim; GLenum err; /* valid range is empty? */ if((s_knot->unified_knot !=NULL && s_knot->unified_nknots==0) || (t_knot->unified_knot !=NULL && t_knot->unified_nknots==0)) { if(s_knot->unified_knot) { free(s_knot->unified_knot); s_knot->unified_knot=NULL; } if(t_knot->unified_knot) { free(t_knot->unified_knot); t_knot->unified_knot=NULL; } *s_n_ctrl=0; *t_n_ctrl=0; return GLU_NO_ERROR; } t_cnt=attrib->tknot_count-attrib->torder; s_cnt=attrib->sknot_count-attrib->sorder; if((tmp_ctrl=(GLfloat **)malloc(sizeof(GLfloat *)*t_cnt))==NULL) return GLU_OUT_OF_MEMORY; if((err=explode_knot(s_knot))!=GLU_NO_ERROR) { free(tmp_ctrl); if(s_knot->unified_knot) { free(s_knot->unified_knot); s_knot->unified_knot=NULL; } return err; } if(s_knot->unified_knot) { free(s_knot->unified_knot); s_knot->unified_knot=NULL; } if((err=calc_alphas(s_knot))!=GLU_NO_ERROR) { free(tmp_ctrl); free(s_knot->new_knot); return err; } free(s_knot->new_knot); ctrl_offset=attrib->ctrlarray; dim=attrib->dim; for(i=0;i<t_cnt;i++) { if((err=calc_new_ctrl_pts(ctrl_offset,attrib->s_stride,s_knot, dim,&(tmp_ctrl[i]),&tmp_n_control))!=GLU_NO_ERROR) { for(--i;i<=0;i--) free(tmp_ctrl[i]); free(tmp_ctrl); free(s_knot->alpha); return err; } ctrl_offset+=attrib->t_stride; } free(s_knot->alpha); tmp_stride=dim*tmp_n_control; if((*new_ctrl=(GLfloat *)malloc(sizeof(GLfloat)*tmp_stride*t_cnt)) ==NULL) { for(i=0;i<t_cnt;i++) free(tmp_ctrl[i]); free(tmp_ctrl); return GLU_OUT_OF_MEMORY; } for(i=0;i<tmp_n_control;i++) for(j=0;j<t_cnt;j++) MEMCPY(*new_ctrl+j*dim+i*dim*t_cnt,tmp_ctrl[j]+dim*i, sizeof(GLfloat)*dim); for(i=0;i<t_cnt;i++) free(tmp_ctrl[i]); free(tmp_ctrl); *s_n_ctrl=tmp_n_control; if((tmp_ctrl=(GLfloat **)malloc(sizeof(GLfloat *)*(*s_n_ctrl)))==NULL) { return GLU_OUT_OF_MEMORY; } if((err=explode_knot(t_knot))!=GLU_NO_ERROR) { free(tmp_ctrl); if(t_knot->unified_knot) { free(t_knot->unified_knot); t_knot->unified_knot=NULL; } return err; } if(t_knot->unified_knot) { free(t_knot->unified_knot); t_knot->unified_knot=NULL; } if((err=calc_alphas(t_knot))!=GLU_NO_ERROR) { free(tmp_ctrl); free(t_knot->new_knot); return err; } free(t_knot->new_knot); ctrl_offset=*new_ctrl; for(i=0;i<(*s_n_ctrl);i++) { if((err=calc_new_ctrl_pts(ctrl_offset,dim,t_knot, dim,&(tmp_ctrl[i]),&tmp_n_control))!=GLU_NO_ERROR) { for(--i;i<=0;i--) free(tmp_ctrl[i]); free(tmp_ctrl); free(t_knot->alpha); return err; } ctrl_offset+=dim*t_cnt; } free(t_knot->alpha); free(*new_ctrl); tmp_stride=dim*tmp_n_control; if((*new_ctrl=(GLfloat *)malloc(sizeof(GLfloat)*tmp_stride*(*s_n_ctrl))) ==NULL) { for(i=0;i<(*s_n_ctrl);i++) free(tmp_ctrl[i]); free(tmp_ctrl); return GLU_OUT_OF_MEMORY; } for(i=0;i<(*s_n_ctrl);i++) { MEMCPY(*new_ctrl+i*tmp_stride,tmp_ctrl[i],sizeof(GLfloat)*tmp_stride); free(tmp_ctrl[i]); } free(tmp_ctrl); *t_n_ctrl=tmp_n_control; return GLU_NO_ERROR; }