Example #1
0
void game_get_alpha(game_t *game, mpq_t alpha)
{
    mpq_t max_t_1;
    mpq_t min_s_0;
    
    mpq_init(max_t_1);
    mpq_init(min_s_0);
    
    //MAX(game->t, 1)
    if(mpq_cmp_si(game->t, 1, 1) > 0)
    {
        mpq_set(max_t_1, game->t);
    }
    else
    {
        mpq_set_si(max_t_1, 1, 1);
    }
    
    //MIN(game->s, 0)
    if(mpq_cmp_si(game->s, 0, 1) < 0)
    {
        mpq_set(min_s_0, game->s);
    }
    else
    {
        mpq_set_si(min_s_0, 0, 1);
    }
    
    //return MAX(game->t, 1) - MIN(game->s, 0);
    mpq_sub(alpha, max_t_1, min_s_0);
    
    mpq_clear(max_t_1);
    mpq_clear(min_s_0);
}
Example #2
0
//Overloads == to compare a point to an integer; both X and Y components must match the integer to be true
bool Point::operator ==(const int& nRight)
{
	if(mpq_cmp_si(this->nX, nRight, 1) == 0 && mpq_cmp_si(this->nY, nRight, 1) == 0)
	{
		return true;
	}

	return false;
}
Example #3
0
File: ovm_mpq.c Project: pcpa/owl
void
ovm_q_le(oregister_t *l, oregister_t *r)
{
    l->t = t_word;
    switch (r->t) {
	case t_void:
	    l->v.w = mpq_sgn(oqr(l)) <= 0;
	    break;
	case t_word:
	    l->v.w = mpq_cmp_si(oqr(l), r->v.w, 1) <= 0;
	    break;
	case t_float:
	    l->v.w = mpq_get_d(oqr(l)) <= r->v.d;
	    break;
	case t_mpz:
	    mpq_set_z(oqr(r), ozr(r));
	    l->v.w = mpq_cmp(oqr(l), oqr(r)) <= 0;
	    break;
	case t_rat:
	    mpq_set_si(oqr(r), rat_num(r->v.r), rat_den(r->v.r));
	    l->v.w = mpq_cmp(oqr(l), oqr(r)) <= 0;
	    break;
	case t_mpq:
	    l->v.w = mpq_cmp(oqr(l), oqr(r)) <= 0;
	    break;
	case t_mpr:
	    mpfr_set_z(orr(l), ozr(l), thr_rnd);
	    l->v.w = mpfr_lessequal_p(orr(l), orr(r));
	    break;
	default:
	    ovm_raise(except_not_a_real_number);
    }
}
Example #4
0
static CRATIONAL *_div(CRATIONAL *a, CRATIONAL *b, bool invert)
{
    if (mpq_cmp_si(b->n, 0, 1) == 0)
    {
        GB.Error(GB_ERR_ZERO);
        return NULL;
    }
    else
        return RATIONAL_make(a, b, mpq_div);
}
Example #5
0
gboolean game_compute_p_ij(game_t *game, int i, mpq_t *p_ij)
{    
    GSList *list = graph_get_neighbours_of(game->graph, i);
    int n = g_slist_length(list);
    
    mpq_t sum;
    mpq_init(sum);
    mpq_set_si(sum, 0, 1);
    
    int j, k;
    for(j = 0; j < n; ++j)
    {
        k = GPOINTER_TO_INT(g_slist_nth_data(list, j));
        game_compute_p_i(game, i, k, p_ij[j]);
        mpq_canonicalize(p_ij[j]);
        mpq_add(sum, sum, p_ij[j]);
    }
    
    for(j = 0; j < n; ++j)
    {
        if(mpq_cmp_si(sum, 0, 1) == 0)
        {
            mpq_set_si(p_ij[j], 0, 1);
        }
        else
        {
            mpq_div(p_ij[j], p_ij[j], sum);
            mpq_canonicalize(p_ij[j]);
        }
    }
    
    mpq_clear(sum);
    
    int res = mpq_cmp_si(sum, 0, 1);
    return (res == 0);
}
Example #6
0
void game_compute_p_i(game_t *game, int i, int j, mpq_t p_i)
{
    mpq_t payoff_i;
    mpq_t payoff_j;
    mpq_t alpha;
    mpq_t tmp;
    mpq_t tmp2;
    
    mpq_init(payoff_i);
    mpq_init(payoff_j);
    mpq_init(alpha);
    mpq_init(tmp);
    mpq_init(tmp2);
    
    game_get_payoff_of_player(game, i, payoff_i);
    game_get_payoff_of_player(game, j, payoff_j);
    game_get_alpha(game, alpha);

    int n_i = graph_number_of_neighbours_of(game->graph, i);
    int n_j = graph_number_of_neighbours_of(game->graph, j);
    int m = MAX(n_i, n_j);
    
    mpq_sub(tmp, payoff_j, payoff_i);
    //gmp_printf(" %Qd - %Qd = %Qd", payoff_j, payoff_i,tmp);
    //max(P_j - P_i, 0)
    if(mpq_cmp_si(tmp, 0, 1) < 0)
    {
        mpq_set_si(tmp, 0, 1);
    }
    
    mpq_set_si(tmp2, m, 1);
    mpq_mul(tmp2, tmp2, alpha);
    
    mpq_div(p_i, tmp, tmp2);
    
    mpq_clear(payoff_i);
    mpq_clear(payoff_j);
    mpq_clear(alpha);
    mpq_clear(tmp);
    mpq_clear(tmp2);
}
Example #7
0
extern "C" inline int mask_mpq_cmp_si(mpq_t x, long int i, long int j) { return mpq_cmp_si(x,i,j); }
Example #8
0
	bool operator<=(const int num1, const Rational& num2)
	{
		return mpq_cmp_si(num2.number, num1, 1) >= 0;
	}
Example #9
0
	bool operator<(const Rational& num1, const int num2)
	{
		return mpq_cmp_si(num1.number, num2, 1) < 0;
	}
Example #10
0
int print_coeff(FILE *OUT, rational_complex_number z, int somethingPrinted)
/***************************************************************\
* USAGE: prints z to OUT in user-friendly way                 *
\***************************************************************/
{
  int rV = -2; // 0 if z is zero (nothing printed), 1 if z is one (nothing printed), -1 if z is -1 (nothing printed), -2 otherwise (something printed)
  int base = 10;

  if (mpq_cmp_ui(z->re, 0, 1) == 0)
  { // real part is zero
    if (mpq_cmp_ui(z->im, 0, 1) == 0)
    { // imag part is zero
      rV = 0;
    }
    else
    { // imag part is nonzero
      if (somethingPrinted && mpq_sgn(z->im) >= 0)
        fprintf(OUT, "+");

      if (mpq_cmp_ui(z->im, 1, 1) == 0)
        fprintf(OUT, "I");
      else if (mpq_cmp_si(z->im, -1, 1) == 0)
        fprintf(OUT, "-I");
      else
      {
        mpq_out_str(OUT, base, z->im);
        fprintf(OUT, "*I");
      }
    }
  }
  else
  { // real part is nonzero
    if (mpq_cmp_ui(z->im, 0, 1) == 0)
    { // imag part is zero
      if (mpq_cmp_ui(z->re, 1, 1) == 0)
      { // value is 1
        rV = 1;
      }
      else if (mpq_cmp_si(z->re, -1, 1) == 0)
      { // value is -1
        rV = -1;
      }
      else
      {
        if (somethingPrinted && mpq_sgn(z->re) >= 0)
          fprintf(OUT, "+");
        mpq_out_str(OUT, base, z->re);
      }
    }
    else
    { // imag part is nonzero
      if (somethingPrinted)
        fprintf(OUT, "+");
      fprintf(OUT, "(");
      mpq_out_str(OUT, base, z->re);
      if (mpq_sgn(z->im) >= 0)
        fprintf(OUT, "+");
      mpq_out_str(OUT, base, z->im);
      fprintf(OUT, "*I)");
    }
  }

  return rV;
}
Example #11
0
void
check_data (void)
{
  static const struct {
    const char     *q;
    mpir_si         n;
    mpir_ui         d;
    int            want;
  } data[] = {
    { "0", 0, 1, 0 },
    { "0", 0, 123, 0 },
    { "0", 0, GMP_UI_MAX, 0 },
    { "1", 0, 1, 1 },
    { "1", 0, 123, 1 },
    { "1", 0, GMP_UI_MAX, 1 },
    { "-1", 0, 1, -1 },
    { "-1", 0, 123, -1 },
    { "-1", 0, GMP_UI_MAX, -1 },

    { "123", 123, 1, 0 },
    { "124", 123, 1, 1 },
    { "122", 123, 1, -1 },

    { "-123", 123, 1, -1 },
    { "-124", 123, 1, -1 },
    { "-122", 123, 1, -1 },

    { "123", -123, 1, 1 },
    { "124", -123, 1, 1 },
    { "122", -123, 1, 1 },

    { "-123", -123, 1, 0 },
    { "-124", -123, 1, -1 },
    { "-122", -123, 1, 1 },

    { "5/7", 3,4, -1 },
    { "5/7", -3,4, 1 },
    { "-5/7", 3,4, -1 },
    { "-5/7", -3,4, 1 },
  };

  mpq_t  q;
  int    i, got;

  mpq_init (q);

  for (i = 0; i < numberof (data); i++)
    {
      mpq_set_str_or_abort (q, data[i].q, 0);
      MPQ_CHECK_FORMAT (q);

      got = mpq_cmp_si (q, data[i].n, data[i].d);
      if (SGN(got) != data[i].want)
        {
          printf ("mpq_cmp_si wrong\n");
        error:
          mpq_trace ("  q", q);
          printf ("  n=%Md\n", data[i].n);
          printf ("  d=%Mu\n", data[i].d);
          printf ("  got=%d\n", got);
          printf ("  want=%Md\n", data[i].want);
          abort ();
        }

      if (data[i].n == 0)
        {
          got = mpq_cmp_si (q, 0L, data[i].d);
          if (SGN(got) != data[i].want)
            {
              printf ("mpq_cmp_si wrong\n");
              goto error;
            }
        }
    }

  mpq_clear (q);
}