コード例 #1
0
ファイル: lattice.cpp プロジェクト: vbraun/lattice_phi4
/* Acceptance of new_site depends on the sign at the lattice site and
 * a probabilistic factor 
 */
inline bool Lattice::cluster_try_add(bool positive, Phi phi, Site new_site) {
  if ((get_phi(new_site) > 0) != positive)
    return false;
  if (cluster.find(new_site) != cluster.end())
    return false;
  const RR probability = 
    1 - gsl_sf_exp(-2 * phi * get_phi(new_site));
  if (random() < probability) {
    cluster.insert(new_site);
    cluster_queue.push_back(new_site);
    return true;
  }
  return false;
}
コード例 #2
0
ファイル: lattice.cpp プロジェクト: vbraun/lattice_phi4
void Lattice::flip_cluster() {
  assert(cluster_queue.empty());
  for (auto ci = cluster.begin(); ci != cluster.end(); ci++) {
    get_phi(*ci) *= -1;
  }
  cluster.clear();
}
コード例 #3
0
void RegistrationResult::show(std::ostream &out) const {
  algebra::VectorD<4> quaternion = R_.get_quaternion();
  out << "Name: " << get_name() << " Image index: " << get_image_index()
      << " Projection index: " << get_projection_index()
      << " (Phi,Theta,Psi) = ( " << get_phi() << " , " << get_theta() << " , "
      << get_psi() << " ) | Shift (x,y) " << get_shift()
      << " CCC = " << get_ccc() << " Quaternion " << quaternion;
}
コード例 #4
0
//! Writes a result line to a file
void RegistrationResult::write(std::ostream &out) const {
  algebra::VectorD<4> quaternion = R_.get_quaternion();
  char c = '|';
  out << get_image_index() << c << get_projection_index() << c << get_phi() << c
      << get_theta() << c << get_psi() << c << quaternion[0] << c
      << quaternion[1] << c << quaternion[2] << c << quaternion[3] << c
      << get_shift()[0] << c << get_shift()[1] << c << get_ccc() << c
      << std::endl;
}
コード例 #5
0
ファイル: lattice.cpp プロジェクト: vbraun/lattice_phi4
Lattice::ZZ noinline Lattice::step_Wolff(const Site& site) {
  const bool positive = (get_phi(site) > 0);
  cluster.clear();
  cluster.insert(site);
  cluster_queue.clear();
  cluster_queue.push_back(site);
  while (!cluster_queue.empty()) {
    Site site = cluster_queue.back();
    cluster_queue.pop_back();
    Phi phi = get_phi(site);
    SiteXY nbhd = get_site_xy(site);
    cluster_try_add(positive, phi, nbhd.prev_x);
    cluster_try_add(positive, phi, nbhd.next_x);
    cluster_try_add(positive, phi, nbhd.prev_y);
    cluster_try_add(positive, phi, nbhd.next_y);
  }
  const ZZ cluster_size = cluster.size();
  flip_cluster();
  return cluster_size;
}
コード例 #6
0
ファイル: roche.c プロジェクト: cyamauch/eggx
int main()
{
    int i,j,key,sl=0,wl=1 ;
    int shape_i = 11, shape_j = 4 ;
    float f=MRATIO_F ;
    float x,y,zx,zy,z,ph,ms_w,ms_h ;
    float zran=(ZMAX-ZMIN) ;
    float zcen=(ZMAX+ZMIN)/2.0 ;
    color_prms cl = {
	EGGX_COLOR_BEGIN,			/* カラーパターン */
	CP_CONTRAST | CP_BRIGHTNESS | CP_GAMMA,	/* フラグ */
	1.0,					/* コントラスト */
	0.0,					/* ブライトネス*/
	1.0,					/* γ */
    } ;
    int win ;
    int cl_r,cl_g,cl_b ;

    win=gopen(WINWIDTH,WINHEIGHT) ;	/* ウィンドゥのタイトル */
    /* 座標系を変更する */
    coordinate(win, 0,0, XMIN,YMIN, 
	       WINWIDTH/(XMAX-XMIN), WINHEIGHT/(YMAX-YMIN)) ;
    layer(win,sl,wl) ;

    puts("【キーボードでの操作方法】") ;
    puts("'PageUp','PageDown'  … 質量比変更") ;
    puts("'c','C'              … カラーパターン") ;
    puts("'↑','↓','←','→'  … カラー調整") ;
    puts("'[',']'              … コントラスト") ;
    puts("'{','}'              … ブライトネス") ;
    puts("'<','>'              … γ補正") ;
    puts("'s'                  … 画像を保存") ;
    puts("'q','Esc'            … 終了") ;

    ms_w=(float)(XMAX-XMIN)/XSAMPLES ;	/* メッシュ1個分のサイズ */
    ms_h=(float)(YMAX-YMIN)/YSAMPLES ;

    do{
	/* ウィンドゥのタイトル */
	winname(win,"ロッシュワールド('s'キーで画像save) f=%g zcen=%g zran=%g",
		f,zcen,zran) ;
	for( i=0 ; i<XSAMPLES ; i++ ){	/* ポテンシャルを色で表現 */
	    x=XMIN+ms_w*i ;
	    for( j=0 ; j<YSAMPLES ; j++ ){
		y=YMIN+ms_h*j ;
		ph=get_phi(x+ms_w/2.0,y+ms_w/2.0,f) ;
		generatecolor(&cl,zcen-zran/2,zcen+zran/2,ph,
			      &cl_r,&cl_g,&cl_b) ;
		newrgbcolor(win,cl_r,cl_g,cl_b) ;
		fillrect(win,x,y,ms_w*1.5,ms_h*1.5) ;
	    }
	}
	newpen(win,1) ;
	for( i=0 ; i<XSAMPLES ; i++ ){	/* テスト粒子に働く力を矢印で表現 */
	    x=XMIN+ms_w*(i+0.5) ;
	    for( j=0 ; j<YSAMPLES ; j++ ){
		y=YMIN+ms_h*(j+0.5) ;
		zx=get_fx(x,y,f) ;
		zy=get_fy(x,y,f) ;
		zx *= VXSCALE ;
		zy *= VYSCALE ;
		z=sqrt(zx*zx+zy*zy) ;
		if( z <= VCARMAX ){
		    drawarrow(win,x-zx,y-zy,x+zx,y+zy,0.3,0.2,
			      shape_i*10+shape_j) ;
		}
	    }
	}
	sl ^= 1 ;				/* XORをとってレイヤを切替 */
	wl ^= 1 ;
	layer( win,sl,wl ) ;
	key=ggetch() ;				/* キー入力があるまで待つ */
	if( key == 0x002 ) f += 0.1 ;		/* PageUp */
	else if( key == 0x006 ) f -= 0.1 ;	/* PageDown */
	else if( key == 0x01e ) zcen += 0.1 ;	/* ↑ */
	else if( key == 0x01f ) zcen -= 0.1 ;	/* ↓ */
	else if( key == 0x01c ) zran += 0.1 ;	/* → */
	else if( key == 0x01d ) zran -= 0.1 ;	/* ← */
	else if( key == 'i' ) {
	    shape_i += 1 ;
	    if ( 12 < shape_i ) shape_i = 10 ;
	}
	else if( key == 'j' ) {
	    shape_j += 1 ;
	    if ( 7 < shape_j ) shape_j = 1 ;
	}
	else if( key == 'c' ){			/* 'c','C'キーでカラーパターン変更 */
	    cl.colormode++ ;
	    if( EGGX_COLOR_BEGIN+EGGX_COLOR_NUM <= cl.colormode )
		cl.colormode=EGGX_COLOR_BEGIN ;
	}
	else if( key == 'C' ){
	    cl.colormode-- ;
	    if( cl.colormode < EGGX_COLOR_BEGIN )
		cl.colormode=EGGX_COLOR_BEGIN+EGGX_COLOR_NUM-1 ;
	}
	else if( key == '[' ){			/* '[',']'でコントラスト変更 */
	    cl.contrast += 0.05 ;
	    if( 1 < cl.contrast ) cl.contrast = 1 ;
	}
	else if( key == ']' ){
	    cl.contrast -= 0.05 ;
	    if( cl.contrast < 0 ) cl.contrast = 0 ;
	}
	else if( key == '{' ){			/* '{','}'でブライトネス変更 */
	    cl.brightness += 0.05 ;
	    if( 1 < cl.brightness ) cl.brightness = 1 ;
	}
	else if( key == '}' ){
	    cl.brightness -= 0.05 ;
	    if( cl.brightness < 0 ) cl.brightness = 0 ;
	}
	else if( key == '<' ){			/* '<','>'でγ変更 */
	    cl.gamma += 0.025 ;
	}
	else if( key == '>' ){
	    cl.gamma -= 0.025 ;
	    if( cl.gamma <= 0 ) cl.gamma = 0.025 ;
	}
	else if( key == 's' ){			/* 's'キーで保存 */
#ifdef USE_NETPBM
	    saveimg( win,sl,XMIN,YMIN,XMAX,YMAX,
		     "pnmtops -noturn -dpi 72 -equalpixels -psfilter -flate -ascii85",256,
		     "roche_f=%g.eps",f) ;
	    printf("画像を保存: filename='roche_f=%g.eps'\n",f) ;
#else
#ifdef USE_IMAGEMAGICK
	    saveimg( win,sl,XMIN,YMIN,XMAX,YMAX,"convert",256,
		     "roche_f=%g.png",f) ;
	    printf("画像を保存: filename='roche_f=%g.png'\n",f) ;
#else
	    saveimg( win,sl,XMIN,YMIN,XMAX,YMAX,"",256,
		     "roche_f=%g.ppm",f) ;
	    printf("画像を保存: filename='roche_f=%g.ppm'\n",f) ;
#endif
#endif
	}
	if( f < 0 ) f=0 ;
	if( zran < 0 ) zran=0.1 ;
    } while( key != 0x01b && key != 'q' ) ;	/* ESCキーか 'q'キーで終了 */

    gcloseall() ;
    return(0) ;
}
コード例 #7
0
ファイル: compute_quad2_density.cpp プロジェクト: dampir/Fem
static void solve(double* density, double& time)
{
	PREV_DENSITY = new double[XY_LEN];
	for (int j = 0; j < OY_LEN + 1; j++)
	{
		for (int i = 0; i < OX_LEN + 1; i++)
		{
			PREV_DENSITY[OX_LEN_1 * j + i] = analytical_solution(0, OX[i], OY[j]);
		}
	}
// for jakoby convergence test 
	for (int j = 1; j < OY_LEN; j++)
	{
		for (int i = 0; i < OX_LEN; i++)
		{
			PREV_DENSITY[OX_LEN_1 * j + i] = 0;
		}
	}

	int i = 0, j = 0, tl = 0;
	double timeStart = 0, timeEnd=0;
#ifdef _OPENMP
	// printf("OPENMP THREADS COUNT = %d\n", omp_get_max_threads());
	long count = 0;
	// dummy parallel section to get all threads running
	#pragma omp parallel private(i,j)
	{
		_InterlockedIncrement(&count);
	}
#endif

#ifdef _OPENMP
//	printf("OPENMP timer function is used!\n");
	timeStart = omp_get_wtime();
#else
//	printf("Standart timer function is used!\n");
	StartTimer();
#endif
	fflush(stdout);
	double* phi = new double[XY_LEN];
	printf("%d\n", TIME_STEP_CNT);
	int iter_count = 3;
	for (tl = 1; tl <= TIME_STEP_CNT; tl++)
	{
		for (int k = 0; k <= OX_LEN; k++)
		{
			density[k] = analytical_solution(OX[k], BB, TIME);
			density[OX_LEN_1 * OY_LEN + k] = analytical_solution(OX[k], UB, TIME);
		}
		for (int u = 0; u <= OY_LEN; u++)
		{
			density[OX_LEN_1 * u] = analytical_solution(LB, OY[u], TIME);
			density[OX_LEN_1 * u + OX_LEN] = analytical_solution(RB, OY[u], TIME);
		}

		for (int j = 0; j < OY_LEN + 1; j++)
		{
			for (int i = 0; i < OX_LEN_1; i++)
			{
				PREV_DENSITY[OX_LEN_1 * j + i] = analytical_solution(0, OX[i], OY[j]);
			}
		}

#ifdef _OPENMP
	#pragma omp parallel for collapse(2) private(i, j)
#endif
		for (int j = 1; j < OY_LEN; ++j)
			for (int i = 1; i < OX_LEN; ++i)
				phi[OX_LEN_1 * j + i] = get_phi(i, j);

		for (int j = 1; j < OY_LEN; ++j)
		{
			for (int i = 1; i < OX_LEN; ++i)
			{
				PREV_DENSITY[OX_LEN_1 * j + i] = 0.;
			}
		}

		int iter = 0;
		
		//printf("%s\n", "Jakoby start");
		while(iter < iter_count)
		{
			//if(iter == 1)
			{
				printf("%s %d\n", "PREV_DENSITY", iter);
				__print_matrix11(PREV_DENSITY, OX_LEN+1, OY_LEN+1);
				printf("%s %d\n", "density", iter);
				__print_matrix11(density, OX_LEN+1, OY_LEN+1);
			}
			//printf("%s = %d\n", "Iter", iter);
			for (int j = 1; j < OY_LEN; ++j)
			{
				for (int i = 1; i < OX_LEN; ++i)
				{
					density[OX_LEN_1 * j + i] = -1/9*(
						1.5*(
							PREV_DENSITY[OX_LEN_1 * j + i - 1] + // left
							PREV_DENSITY[OX_LEN_1 * (j - 1) + i] + // upper
							PREV_DENSITY[OX_LEN_1 * j + i + 1] + // right
							PREV_DENSITY[OX_LEN_1 * (j + 1) + i] // bottom
							) + 
						0.25*(
						PREV_DENSITY[OX_LEN_1 * (j + 1) + i + 1] + // bottom right
						PREV_DENSITY[OX_LEN_1 * (j + 1) + i - 1] + // bottom left
						PREV_DENSITY[OX_LEN_1 * (j - 1) + i - 1] + // upper right
						PREV_DENSITY[OX_LEN_1 * (j - 1) + i + 1] // upper left
						)) + 
						phi[OX_LEN_1 * j + i];
				}
			}
			memcpy(PREV_DENSITY, density, XY_LEN * sizeof(double));
			iter++;
		}
	}
#ifdef _OPENMP
	timeEnd = omp_get_wtime();
	time = (timeEnd-timeStart);
//	printf("time %f s.\n", time);
#else
	time = GetTimer()/1000;
//	printf("time %f s.\n", time/1000);
#endif
	delete[] PREV_DENSITY;
	delete[] phi;
}
コード例 #8
0
ファイル: pviterbi.c プロジェクト: tempbottle/ghmm
/*============================================================================*/
int *ghmm_dpmodel_viterbi_variable_tb(ghmm_dpmodel *mo, ghmm_dpseq * X, ghmm_dpseq * Y,
				 double *log_p, int *path_length,
				 int start_traceback_with) {
#define CUR_PROC "ghmm_dpmodel_viterbi"
  int u, v, j, i, off_x, off_y, current_state_index;
  double value, max_value, previous_prob;  
  plocal_store_t *pv;
  int *state_seq = NULL;
  int emission;
  double log_b_i, log_in_a_ij;
  double (*log_in_a)(plocal_store_t*, int, int, ghmm_dpseq*, ghmm_dpseq*, int, int);

  /* printf("---- viterbi -----\n"); */
  i_list * state_list;
  state_list = ighmm_list_init_list();
  log_in_a = &sget_log_in_a;
  /* int len_path  = mo->N*len; the length of the path is not known apriori */

/*   if (mo->model_type & kSilentStates &&  */
/*       mo->silent != NULL &&  */
/*       mo->topo_order == NULL) { */
/*     ghmm_dmodel_topo_order( mo );  */
/*   } */

  /* Allocate the matrices log_in_a, log_b,Vektor phi, phi_new, Matrix psi */
  pv = pviterbi_alloc(mo, X->length, Y->length);
  if (!pv)                        { GHMM_LOG_QUEUED(LCONVERTED); goto STOP; }

  /* Precomputing the log(a_ij) and log(bj(ot)) */
  pviterbi_precompute(mo, pv);
  /* Initialize the lookback matrix (for positions [-offsetX,0], [-1, len_y]*/
  init_phi(pv, X, Y);
  
  /* u > max_offset_x , v starts -1 to allow states with offset_x == 0 
     which corresponds to a series of gap states before reading the first 
     character of x at position x=0, y=v */
  /** THIS IS THE MAIN RECURRENCE **/
  for (u = mo->max_offset_x + 1; u < X->length; u++) {
    for (v = -mo->max_offset_y; v < Y->length; v++) {
      for (j = 0; j < mo->N; j++) 
	{
	  /** initialization of phi (lookback matrix), psi (traceback) **/
	  set_phi(pv, u, v, j, +1);
	  set_psi(pv, u, v, j, -1);
	}
      
      for (i = 0; i < mo->N; i++) {
	/* Determine the maximum */
	/* max_phi = phi[i] + log_in_a[j][i] ... */
	if (!(mo->model_type & GHMM_kSilentStates) || !mo->silent[i] ) {
	  max_value = -DBL_MAX;
	  set_psi(pv, u, v, i, -1);
	  for (j = 0; j < mo->s[i].in_states; j++) {
	    /* look back in the phi matrix at the offsets */
	    previous_prob = get_phi(pv, u, v, mo->s[i].offset_x, mo->s[i].offset_y, mo->s[i].in_id[j]);
	    log_in_a_ij = (*log_in_a)(pv, i, j, X, Y, u, v);
	    if ( previous_prob != +1 && log_in_a_ij != +1) {
	      value = previous_prob + log_in_a_ij;
	      if (value > max_value) {
		max_value = value;
		set_psi(pv, u, v, i, mo->s[i].in_id[j]);
	      }
	    }
	    else
	      {;} /* fprintf(stderr, " %d --> %d = %f, \n", i,i,v->log_in_a[i][i]); */
	  }

	  emission = ghmm_dpmodel_pair(ghmm_dpseq_get_char(X, mo->s[i].alphabet, u), 
			      ghmm_dpseq_get_char(Y, mo->s[i].alphabet, v),
			      mo->size_of_alphabet[mo->s[i].alphabet],
			      mo->s[i].offset_x, mo->s[i].offset_y);
#ifdef DEBUG
	  if (emission > ghmm_dpmodel_emission_table_size(mo, i)){
	    printf("State %i\n", i);
	    ghmm_dpmodel_state_print(&(mo->s[i]));
	    printf("charX: %i charY: %i alphabet size: %i emission table: %i emission index: %i\n", 
		   ghmm_dpseq_get_char(X, mo->s[i].alphabet, u),
		   ghmm_dpseq_get_char(Y, mo->s[i].alphabet, v),
		   mo->size_of_alphabet[mo->s[i].alphabet],
		   ghmm_dpmodel_emission_table_size(mo, i), emission);
	  }
#endif
	  log_b_i = log_b(pv, i, ghmm_dpmodel_pair(ghmm_dpseq_get_char(X, mo->s[i].alphabet, u), 
				      ghmm_dpseq_get_char(Y, mo->s[i].alphabet, v),
				      mo->size_of_alphabet[mo->s[i].alphabet],
				      mo->s[i].offset_x, mo->s[i].offset_y));

	  /* No maximum found (that is, state never reached)
	     or the output O[t] = 0.0: */
	  if (max_value == -DBL_MAX ||/* and then also: (v->psi[t][j] == -1) */
	      log_b_i == +1 ) {
	    set_phi(pv, u, v, i, +1);
	  }
	  else
	    set_phi(pv, u, v, i, max_value + log_b_i);
	}
      } /* complete time step for emitting states */
    
	/* last_osc = osc; */ 
        /* save last transition class */

      /*if (mo->model_type & kSilentStates) { 
	p__viterbi_silent( mo, t, v );
	}*/ /* complete time step for silent states */
      
      /**************
    for (j = 0; j < mo->N; j++) 
      {      
	printf("\npsi[%d],in:%d, phi=%f\n", t, v->psi[t][j], v->phi[j]);
       }
      
    for (i = 0; i < mo->N; i++){
      printf("%d\t", former_matchcount[i]);
    }

    for (i = 0; i < mo->N; i++){
      printf("%d\t", recent_matchcount[i]);
    }
      ****************/
    } /* End for v in Y */
    /* Next character in X */
    push_back_phi(pv, Y->length);
  } /* End for u in X */

  /* Termination */
  max_value = -DBL_MAX;
  ighmm_list_append(state_list, -1);
  /* if start_traceback_with is -1 (it is by default) search for the most 
     likely state at the end of both sequences */
  if (start_traceback_with == -1) {
    for (j = 0; j < mo->N; j++){
#ifdef DEBUG
      printf("phi(len_x)(len_y)(%i)=%f\n", j, get_phi(pv, u, Y->length-1, 0, 0, j));
#endif
      if ( get_phi(pv, u, Y->length-1, 0, 0, j) != +1 && 
	   get_phi(pv, u, Y->length-1, 0, 0, j) > max_value) { 
	max_value = get_phi(pv, X->length-1, Y->length-1, 0, 0, j);
	state_list->last->val = j;
      }
    }
  }
  /* this is the special traceback mode for the d & c algorithm that also 
     connects the traceback to the first state of the rest of the path */
  else {
#ifdef DEBUG
    printf("D & C traceback from state %i!\n", start_traceback_with);
    printf("Last characters emitted X: %i, Y: %i\n", 
	   ghmm_dpseq_get_char(X, mo->s[start_traceback_with].alphabet, 
			       X->length-1),
	   ghmm_dpseq_get_char(Y, mo->s[start_traceback_with].alphabet, 
			       Y->length-1));
    for (j = 0; j < mo->N; j++){
      printf("phi(len_x)(len_y)(%i)=%f\n", j, get_phi(pv, X->length-1, Y->length-1, 0, 0, j)); 
    }
#endif
    max_value = get_phi(pv, X->length-1, Y->length-1, 0, 0, start_traceback_with);
    if (max_value != 1 && max_value > -DBL_MAX)
      state_list->last->val = start_traceback_with;
  }
  if (max_value == -DBL_MAX) {
    /* Sequence can't be generated from the model! */
    *log_p = +1;
    /* Backtracing doesn't work, because state_seq[*] allocated with -1 */
    /* for (t = len - 2; t >= 0; t--)
       state_list->last->val = -1;    */
  }
  else {
    /* Backtracing, should put DEL path nicely */
    *log_p = max_value;
    /* removed the handling of silent states here */
    /* start trace back at the end of both sequences */
    u = X->length - 1;
    v = Y->length - 1;
    current_state_index = state_list->first->val;
    off_x = mo->s[current_state_index].offset_x;
    off_y = mo->s[current_state_index].offset_y;
    while (u - off_x >= -1 && v - off_y >= -1 && current_state_index != -1) { 
      /* while (u > 0 && v > 0) { */
      /* look up the preceding state and save it in the first position of the
	 state list */
      /* printf("Current state %i at (%i,%i) -> preceding state %i\n", 
	 current_state_index, u, v, get_psi(pv, u, v, current_state_index)); */
      /* update the current state */
      current_state_index = get_psi(pv, u, v, current_state_index);
      if (current_state_index != -1)
	ighmm_list_insert(state_list, current_state_index);
      /* move in the alignment matrix */
      u -= off_x;
      v -= off_y; 
      /* get the next offsets */
      off_x = mo->s[current_state_index].offset_x;
      off_y = mo->s[current_state_index].offset_y;
    }
  }
  
  /* Free the memory space */
  pviterbi_free(&pv, mo->N, X->length, Y->length, mo->max_offset_x , 
		mo->max_offset_y);
  /* printf("After traceback: last state = %i\n", state_list->last->val); */
  state_seq = ighmm_list_to_array(state_list);
  *path_length = state_list->length;
  /* PRINT PATH */
  
/*   fprintf(stderr, "Viterbi path: " ); */
/*   int t; */
/*   for(t=0; t < *path_length; t++) */
/*     if (state_seq[t] >= 0) fprintf(stderr, " %d ",  state_seq[t]); */
/*   fprintf(stderr, "\n Freeing ... \n");  */
  return (state_seq);
STOP:     /* Label STOP from ARRAY_[CM]ALLOC */
  /* Free the memory space */
  pviterbi_free(&pv, mo->N, X->length, Y->length, mo->max_offset_x, 
		mo->max_offset_y);
  m_free(state_seq);
  ighmm_list_free(state_list);
  return NULL;
#undef CUR_PROC
} /* viterbi */
コード例 #9
0
ファイル: pviterbi.c プロジェクト: tempbottle/ghmm
/*============================================================================*/
static void init_phi(plocal_store_t * pv, ghmm_dpseq * X, ghmm_dpseq * Y) {
#ifdef DEBUG
  int emission;
#endif
  int u, v, j, i, off_x, y;
  double log_in_a_ij;
  double value, max_value, previous_prob, log_b_i;  
  /* printf("ghmm_dpmodel_viterbi init\n"); */
  ghmm_dpmodel * mo = pv->mo;
  double (*log_in_a)(plocal_store_t*, int, int, ghmm_dpseq*, ghmm_dpseq*, 
		     int, int);
  log_in_a = &sget_log_in_a;

  /* Initialize the lookback matrix (for positions [-offsetX,0], [0, len_y]*/
  for (off_x=0; off_x<mo->max_offset_x + 1; off_x++)
    for (y=0; y<Y->length + mo->max_offset_y + 1; y++)
      for (j=0; j<mo->N; j++) {
	pv->phi[off_x][y][j] = +1;
      }
    if ( mo->model_type & GHMM_kSilentStates ) { /* could go into silent state at t=0 */

    /*p__viterbi_silent( mo, t=0, v);*/
  }
  /*for (j = 0; j < mo->N; j++)
    {
      printf("\npsi[%d],in:%d, phi=%f\n", t, v->psi[t][j], v->phi[j]);
    }

  for( i = 0; i < mo->N; i++){
    printf("%d\t", former_matchcount[i]);
  }
  for (i = 0; i < mo->N; i++){
    printf("%d\t", recent_matchcount[i]);
  }*/
  
  /* initialize for offsets > 1 (u < max_offset_x, v < max_offset_y) */
  /* this is in principle the same as the main recurrence but adds initial
     probabilities to states that cannot be inhabitated at u=0, v=0 because
     of greater offsets than one 
     iteration start is u=-1 v=-1 to allow states with offset_x == 0 
     which corresponds to a series of gap states before reading the first 
     character of x at position x=0, y=v or equally for offset_y == 0 */
  /* u, v <= max offsets */
    for (u = -1; u <= mo->max_offset_x; u++) {
      for (v = -mo->max_offset_y; v < Y->length; v++) {
	for (j = 0; j < mo->N; j++) 
	  {
	    /** initialization of phi (lookback matrix), psi (traceback) **/
	    set_phi(pv, u, v, j, +1);
	    set_psi(pv, u, v, j, -1);
	  }
	/* for each state i */
	for (i = 0; i < mo->N; i++) {
	/* Determine the maximum */
	/* max_phi = phi[i] + log_in_a[j][i] ... */
	  if (!(mo->model_type & GHMM_kSilentStates) || !mo->silent[i] ) {
	    max_value = -DBL_MAX;
	    set_psi(pv, u, v, i, -1);
	    for (j = 0; j < mo->s[i].in_states; j++) {
	      /* look back in the phi matrix at the offsets */
	      previous_prob = get_phi(pv, u, v, mo->s[i].offset_x, 
				      mo->s[i].offset_y, mo->s[i].in_id[j]);
	      log_in_a_ij = (*log_in_a)(pv, i, j, X, Y, u, v);
	      if ( previous_prob != +1 && log_in_a_ij != +1) {
		value = previous_prob + log_in_a_ij;
		if (value > max_value) {
		  max_value = value;
		  set_psi(pv, u, v, i, mo->s[i].in_id[j]);
		}
	      }
	      else
		{;} /* fprintf(stderr, " %d --> %d = %f, \n", i,i,v->log_in_a[i][i]); */
	    }
#ifdef DEBUG
	    emission = ghmm_dpmodel_pair(ghmm_dpseq_get_char(X, mo->s[i].alphabet, u), 
				ghmm_dpseq_get_char(Y, mo->s[i].alphabet, v),
				mo->size_of_alphabet[mo->s[i].alphabet],
				mo->s[i].offset_x, mo->s[i].offset_y);
	    if (emission > ghmm_dpmodel_emission_table_size(mo, i)){
	      printf("State %i\n", i);
	      ghmm_dpmodel_state_print(&(mo->s[i]));
	      printf("charX: %i charY: %i alphabet size: %i emission table: %i emission index: %i\n", 
		     ghmm_dpseq_get_char(X, mo->s[i].alphabet, u),
		     ghmm_dpseq_get_char(Y, mo->s[i].alphabet, v),
		     mo->size_of_alphabet[mo->s[i].alphabet],
		     ghmm_dpmodel_emission_table_size(mo, i), emission);
	    }
#endif
	    log_b_i = log_b(pv, i, ghmm_dpmodel_pair(ghmm_dpseq_get_char(X, mo->s[i].alphabet, u), 
					ghmm_dpseq_get_char(Y, mo->s[i].alphabet, v),
					mo->size_of_alphabet[mo->s[i].alphabet],
					mo->s[i].offset_x, mo->s[i].offset_y));
	    
	    /* this is the difference from the main loop:
	       check whether this state could be an initial state and add the
	       initial probability */
	    if (log_b_i == +1 ) {
	      set_phi(pv, u, v, i, +1);
	    }
	    else {
	      if (max_value == -DBL_MAX)
		set_phi(pv, u, v, i, +1);
	      else
		set_phi(pv, u, v, i, max_value);
	      /* if (mo->s[i].pi != 0 && mo->s[i].offset_x - 1 == u && 
		 mo->s[i].offset_y - 1 == v) { */
	      if (mo->s[i].log_pi != 1 && mo->s[i].offset_x - 1 == u && 
		  mo->s[i].offset_y - 1 == v) {
		set_phi(pv, u, v, i, mo->s[i].log_pi);
#ifdef DEBUG
		printf("Initial log prob state %i at (%i, %i) = %f\n", i, u, v, get_phi(pv, u, v, 0, 0, i));
		printf("Characters emitted X: %i, Y: %i\n", 
		       ghmm_dpseq_get_char(X, mo->s[i].alphabet, u),
		       ghmm_dpseq_get_char(Y, mo->s[i].alphabet, v));
#endif
	      }
	      if (get_phi(pv, u, v, 0, 0, i) != 1)
		set_phi(pv, u, v, i, get_phi(pv, u, v, 0, 0, i) + log_b_i);
	    }
	  }
	  /* if (v == 0) {
	     printf"(%i, %i, %i) preceding %i\n", u, v, i, pv->psi[u][v][i]);
	     } */
	} /* complete time step for emitting states */
	
	/* last_osc = osc; */
	/* save last transition class */
	
	/*if (mo->model_type & kSilentStates) { 
	  p__viterbi_silent( mo, t, v );
	  }*/ /* complete time step for silent states */
	
	/**************
    for (j = 0; j < mo->N; j++) 
      {      
	printf("\npsi[%d],in:%d, phi=%f\n", t, v->psi[t][j], v->phi[j]);
       }
      
    for (i = 0; i < mo->N; i++){
      printf("%d\t", former_matchcount[i]);
    }

    for (i = 0; i < mo->N; i++){
      printf("%d\t", recent_matchcount[i]);
    }
      ****************/
      } /* End for v in Y */
    /* Next character in X */
    /* push back the old phi values */
      push_back_phi(pv, Y->length);
    } /* End for u in X */
}