std::vector<std::vector<quantum_number_t>> quantum_number_eigenvalues2(many_body_op_t const& op, atom_diag const& atom) {

 auto commutator = op * atom.get_h_atomic() - atom.get_h_atomic() * op;
 if (!commutator.is_zero()) TRIQS_RUNTIME_ERROR << "The operator is not a quantum number";

 auto d = atom.get_full_hilbert_space_dim();
 matrix<quantum_number_t> M(d, d);
 std::vector<std::vector<quantum_number_t>> result;

 for (int bl = 0; bl < atom.n_blocks(); ++bl) {
  auto dim = atom.get_block_dim(bl);
  for (auto const& x : op) {
   auto b_m = atom.matrix_element_of_monomial(x.monomial, bl);
   if (b_m.first == -1) continue;
   M(atom.index_range_of_block(b_m.first), atom.index_range_of_block(bl)) += real(x.coef * b_m.second);
  }
 }
 if (!is_diagonal(M)) TRIQS_RUNTIME_ERROR << "The Matrix of the operator is not diagonal !!!";

 for (int bl = 0; bl < atom.n_blocks(); ++bl) {
   auto dim = atom.get_block_dim(bl);
   result.push_back(std::vector<quantum_number_t>(dim, 0));
   for (int i = 0; i < dim; ++i) result.back()[i] = M(atom.flatten_block_index(bl, i), atom.flatten_block_index(bl, i));
 }

 return result;
}
Exemple #2
0
/* return 1 if a three attack 
can occur on horizon OR diagonal lines.*/
int three_attack(Game_state* state, int player, Pair** rmap) {
  int i, j, t_x, t_y;
  int h_left, h_right, v_upper, v_lower;
  int to_connect = state->num_to_connect;
  int lines = num_of_win_places(state->width, state->height, to_connect);

  for(i = 0; i < lines; i++) {
    /* does opponent have n-1 in a row on a line ? */
    if(!state->score_array[player][i] == to_connect-1)
      continue;

    /* are those n-1 contiguous in a row? */
    if(!has_n_in_row(state, rmap, player, i, to_connect-1))
      continue;
    
    v_upper = v_lower = h_left = h_right = 0;

    /* is there a blank either side, two on one side? vulnerable! */
    if(is_diagonal(state, rmap, i)) {
      for(j = 0; j < state->num_to_connect; j++) {
        t_x = rmap[i][j].x;
        t_y = rmap[i][j].y;
        /* upper right vertical */
        if(valid_pos(state, t_x+1, t_y+1) && blank(state, t_x+1, t_y+1))
          v_upper = 1;
        /* upper left vertical */
        if(valid_pos(state, t_x-1, t_y+1) && blank(state, t_x-1, t_y+1))
          v_upper = 1;
        /* lower right vertical */
        if(valid_pos(state, t_x+1, t_y-1) && blank(state, t_x+1, t_y-1))
          v_lower = 1;
        /* lower left vertical */
        if(valid_pos(state, t_x-1, t_y-1) && blank(state, t_x-1, t_y-1))
          v_lower = 1;

        if(v_upper && v_lower)
          return 1;
      }
    } else { 
      for(j = 0; j < state->num_to_connect; j++) {
        t_x = rmap[i][j].x;
        t_y = rmap[i][j].y;

        /* check as we go for horizontal 3-attack */
        if(state->board[t_x][t_y] == player) {
          /* left (horizontal) side is blank */
          if(valid_pos(state, t_x-1, t_y) && blank(state, t_x-1, t_y)) {
            h_left = 1;
          }
          /* right (horizontal) side is blank */
          if(valid_pos(state, t_x+1, t_y) && blank(state, t_x+1, t_y)) {
            h_right = 1;
          }
        }
        if(h_left && h_right)
          return 1;
      }
    }
  }
  /* if both blank conditions are not met, not three attack */
  return 0;
}
Exemple #3
0
/* have 2 lines passing around a point ie
 _ _ _ _ x _ _
 _ _ x x o _ _
 _ _ x x o _ _
 _ x o o x _ _
 _ o o x o x _

*/
int arrow_attack(Game_state* state, int player, Pair** rmap, int s) {
  /*
  given all other points are filled:

  1 2
  3 4

  have an arrow if:
   if no 3 but 2 and 4 - op places 3, we place 1
   if no 4 but 1 and 3 - op places 4, we place 2

  more generally, looking for a 3 with one blank.
  above the blank should be a blank (obv) with 3.
  must be diagonal.
  */
  int to_connect = state->num_to_connect;
  int lines = num_of_win_places(state->width, state->height, to_connect);
  int i, j, k, l;
  int t_x, t_y, b_x = -1, b_y = -1;
  
  for(i = 0; i < lines; i++) {
    /* not a diagonal, don't care */
    if(!is_diagonal(state, rmap, i))
      continue;

    /* make sure we've got 3 on the row w/ one blank */
    if(!state->score_array[player][i] == to_connect-1 && 
          state->score_array[other(player)][i] == 0)
      continue;

    /* get the blank x,y */
    for(j = 0; j < state->num_to_connect; j++) {
      t_x = rmap[i][j].x;
      t_y = rmap[i][j].y;
      if(state->board[t_x][t_y] == C4_NONE) {
        b_x = t_x;
        b_y = t_y;
        break;
      }
    }

    /* diagonal above blank */
    if(valid_pos(state, b_x, b_y+1)) {
      k = 0;
      while((l = state->map[b_x][b_y+1][k]) != -1) {
        if(is_diagonal(state, rmap, l) && 
            state->score_array[player][l] == to_connect-s &&
              state->score_array[other(player)][l] == 0) {
          return 1;
        }
        k++;
      }
    }

    /* diagonal below blank */
    if(valid_pos(state, b_x, b_y-1)) {
      k = 0;
      while((l = state->map[b_x][b_y-1][k]) != -1) {
        if(is_diagonal(state, rmap, l) && 
            state->score_array[player][l] == to_connect-s &&
              state->score_array[other(player)][l] == 0) {
          return 1;
        }
        k++;
      }
    }

    if(valid_pos(state, b_x-1, b_y-2)) {
      k = 0;
      while((l = state->map[b_x-1][b_y-2][k]) != -1) {
        if(is_diagonal(state, rmap, l) && 
            state->score_array[player][l] == to_connect-s &&
              state->score_array[other(player)][l] == 0) {
          return 1;
        }
        k++;
      }
    }
  }
  return 0;
}
Exemple #4
0
MRI *
MRIfilterMorphology(MRI *mri_src, MRI *mri_dst)
{
    int     width, height, depth, x, y, z, xk, yk, zk, xi, yi, zi, nsix, nvox,
            total ;
    BUFTYPE val ;

    if (!mri_dst)
    {
        mri_dst = MRIclone(mri_src, NULL) ;
    }

    width = mri_src->width ;
    height = mri_src->height ;
    depth = mri_src->depth ;

    /*  mri_dst = MRIremove1dStructures(mri_src, mri_dst, 10000, 2) ;*/

    total = 0 ;
    do
    {
        nvox = 0 ;
        for (z = 0 ; z < depth ; z++)
        {
            for (y = 0 ; y < height ; y++)
            {
                for (x = 0 ; x < width ; x++)
                {
                    val = MRIgetVoxVal(mri_dst, x, y, z, 0) ;
                    if (x == 159 && y == 138 && z == 101)
                    {
                        DiagBreak() ;
                    }
                    if (val < WM_MIN_VAL)
                    {
                        continue ;
                    }
                    nsix = 0 ;
                    for (zk = -1 ; zk <= 1 ; zk++)
                    {
                        for (yk = -1 ; yk <= 1 ; yk++)
                        {
                            for (xk = -1 ; xk <= 1 ; xk++)
                            {
                                if ((fabs(xk) + fabs(yk) + fabs(zk)) <= 1)
                                {
                                    continue ;
                                }
                                xi = mri_dst->xi[x+xk] ;
                                yi = mri_dst->yi[y+yk] ;
                                zi = mri_dst->zi[z+zk] ;
                                if (xi == 159 && yi == 138 && zi == 101)
                                {
                                    DiagBreak() ;
                                }
                                if (MRIgetVoxVal(mri_dst, xi, yi, zi, 0) >= WM_MIN_VAL)
                                {
                                    if (xk && MRIgetVoxVal(mri_dst, xi, y, z, 0) >= WM_MIN_VAL)
                                    {
                                        continue ;
                                    }
                                    if (yk && MRIgetVoxVal(mri_dst, x, yi, z, 0) >= WM_MIN_VAL)
                                    {
                                        continue ;
                                    }
                                    if (zk && MRIgetVoxVal(mri_dst, x, y, zi, 0) >= WM_MIN_VAL)
                                    {
                                        continue ;
                                    }
                                    if (xk)
                                    {
                                        if (xi == 141 && y == 132 && z == 30)
                                        {
                                            DiagBreak() ;
                                        }
                                        MRIsetVoxVal(mri_dst, xi, y, z, 0, DIAGONAL_FILL) ;
                                        if (is_diagonal(mri_dst, xi, y, z))
                                        {
                                            MRIsetVoxVal(mri_dst, xi, y, z, 0, 0) ;
                                        }
                                        else
                                        {
                                            nvox++ ;
                                        }
                                    }
                                    if (yk)
                                    {
                                        MRIsetVoxVal(mri_dst, x, yi, z, 0, DIAGONAL_FILL) ;
                                        if (is_diagonal(mri_dst, x, yi, z))
                                        {
                                            MRIsetVoxVal(mri_dst, x, yi, z, 0, 0) ;
                                        }
                                        else
                                        {
                                            nvox++ ;
                                        }
                                    }
                                    if (zk)
                                    {
                                        MRIsetVoxVal(mri_dst, x, y, zi, 0, DIAGONAL_FILL) ;
                                        if (is_diagonal(mri_dst, x, y, zi))
                                        {
                                            MRIsetVoxVal(mri_dst, x, y, zi, 0, 0) ;
                                        }
                                        else
                                        {
                                            nvox++ ;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        total += nvox ;
    }
    while (nvox > 0) ;
    fprintf(stderr, "%d diagonally connected voxels added...\n", total) ;

    return(mri_dst) ;
}