Ejemplo n.º 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);
}
Ejemplo n.º 2
0
void bisect_sfdlProverExo::baseline(const mpq_t* input_q, int num_inputs,
                                    mpq_t* output_recomputed, int num_outputs) {

  int m = bisect_sfdl_cons::m;
  int L = bisect_sfdl_cons::L;

  mpq_t temp_q;
  alloc_init_scalar(temp_q);

  mpq_t* a = output_recomputed;
  mpq_t* b = output_recomputed + m;
  mpq_t& f = temp_q;

  for(int i = 0; i < m; i++) {
    mpq_set(a[i], input_q[i]);
    mpq_set(b[i], input_q[i+m]);
  }

  for(int i = 0; i < L; i++) {
    exogenous_fAtMidpt(f, a, b);
    if (mpq_sgn(f) > 0) {
      for(int j = 0; j < m; j++) {
        mpq_add(b[j], a[j], b[j]);
        mpq_div_2exp(b[j], b[j], 1);
      }
    } else {
      for(int j = 0; j < m; j++) {
        mpq_add(a[j], a[j], b[j]);
        mpq_div_2exp(a[j], a[j], 1);
      }
    }
  }
  clear_scalar(temp_q);
}
Ejemplo n.º 3
0
void ssx_get_xNj(SSX *ssx, int j, mpq_t x)
{     int m = ssx->m;
      int n = ssx->n;
      mpq_t *lb = ssx->lb;
      mpq_t *ub = ssx->ub;
      int *stat = ssx->stat;
      int *Q_col = ssx->Q_col;
      int k;
      xassert(1 <= j && j <= n);
      k = Q_col[m+j]; /* x[k] = xN[j] */
      xassert(1 <= k && k <= m+n);
      switch (stat[k])
      {  case SSX_NL:
            /* xN[j] is on its lower bound */
            mpq_set(x, lb[k]); break;
         case SSX_NU:
            /* xN[j] is on its upper bound */
            mpq_set(x, ub[k]); break;
         case SSX_NF:
            /* xN[j] is free variable */
            mpq_set_si(x, 0, 1); break;
         case SSX_NS:
            /* xN[j] is fixed variable */
            mpq_set(x, lb[k]); break;
         default:
            xassert(stat != stat);
      }
      return;
}
Ejemplo n.º 4
0
void generate_random_rational_vector(rational_complex_vector x, int size)
/***************************************************************\
* USAGE: generate a random rational vector of unit length       *
\***************************************************************/
{
  int i;
  rational_complex_vector xR;
  initialize_rational_vector(xR, 2 * size);

  // compute a real unit vector of twice the size
  generate_random_real_rational_vector(xR, 2 * size);

  // setup x from xR
  change_size_rational_vector(x, size);
  for (i = 0; i < size; i++)
  {
    mpq_set(x->coord[i]->re, xR->coord[2*i]->re);
    mpq_set(x->coord[i]->im, xR->coord[2*i + 1]->re);
  }

  // clear
  clear_rational_vector(xR);

  return;
}
Ejemplo n.º 5
0
void Qdet(mpq_t D, Qmat Min)
{
  Qmat M;
  int n, i, j, k;
  mpz_t tmp, Dnum, Dden;

  n = Min->nrows;
  if (n == 1) { mpq_set(det, Min->entry[0][0]); return; }
  M = Qmat(n, n);
  for (i=0; i < n; i++)
    for (j=0; j < n; j++)
      mpq_set(M->entry[i][j], Min->entry[i][j]);
  compute_row_lcm(row_lcm, M);
  compute_col_lcm(col_lcm, M);
  mpz_init(tmp);
  mpz_init(Dnum);
  mpz_init_set_ui(Dden, 1);
  while ((k = choose_row_or_col(M, row_lcm, col_lcm)))
  {
    if (k > 0) /* the choice was row k-1 */
    {
      i = k-1;
      mpz_mul(Dden, Dden, row_lcm[i]);
      for (j=0; j < n; j++)
      {
        mpz_divexact(tmp, row_lcm[i], mpq_denref(M->entry[i][j]));
        mpz_mul(mpq_numref(M->entry[i][j]), mpq_numref(M->entry[i][j]), tmp);
      }
      compute_col_lcm(col_lcm, M);
    }
    else /* the choice was col -k-1 */
    {
      j = -k-1;
      mpz_mul(Dden, Dden, col_lcm[j]);
      for (i=0; i < n; i++)
      {
        mpz_divexact(tmp, col_lcm[j], mpq_denref(M->entry[i][j]));
        mpz_mul(mpq_numref(M->entry[i][j]), mpq_numref(M->entry[i][j]), tmp);
      }
      compute_row_lcm(row_lcm, M);
    }
  }
  mpz_clear(tmp);
  {
    Zmat M_integer;
    M_integer = Zmat_ctor(n, n);
    for (i=0; i < n; i++)
      for (j=0; j < n; j++)
        mpz_set(M_integer[i][j], mpq_numref(M->entry[i][j]));
    Zdet(Dnum, M_integer);
    Zmat_dtor(M_integer);
    mpz_set(mpq_numref(D), Dnum);
    mpz_set(mpq_denref(D), Dden);
    mpq_canonicalize(D);
  }
  Qmat_dtor(M);
}
Ejemplo n.º 6
0
void lps_addsse(
   Sos* sos,
   Var* var,
   const mpq_t weight)
{
   Sse* sse;

   assert(sos      != NULL);
   assert(sos->sid == SOS_SID);
   assert(var      != NULL);   
   assert(var->sid == VAR_SID);

   sse = malloc(sizeof(*sse));
   
   assert(sse != NULL);

   mpq_init(sse->weight);
   mpq_set(sse->weight, weight);
   
   sse->var    = var;
   sse->next   = sos->first;   
   sos->first  = sse;

   sos->sses++;

   sse->var->is_used = TRUE;
}
Ejemplo n.º 7
0
Std$Rational_t *_new(mpq_t *V) {
	Std$Rational_t *R = new(Std$Rational_t);
	R->Type = T;
	mpq_init(R->Value);
	mpq_set(R->Value, V);
	return R;
};
Ejemplo n.º 8
0
void ssx_eval_dj(SSX *ssx, int j, mpq_t dj)
{     int m = ssx->m;
      int n = ssx->n;
      mpq_t *coef = ssx->coef;
      int *A_ptr = ssx->A_ptr;
      int *A_ind = ssx->A_ind;
      mpq_t *A_val = ssx->A_val;
      int *Q_col = ssx->Q_col;
      mpq_t *pi = ssx->pi;
      int k, ptr, end;
      mpq_t temp;
      mpq_init(temp);
      xassert(1 <= j && j <= n);
      k = Q_col[m+j]; /* x[k] = xN[j] */
      xassert(1 <= k && k <= m+n);
      /* j-th column of the matrix N is k-th column of the augmented
         constraint matrix (I | -A) */
      if (k <= m)
      {  /* it is a column of the unity matrix I */
         mpq_sub(dj, coef[k], pi[k]);
      }
      else
      {  /* it is a column of the original constraint matrix -A */
         mpq_set(dj, coef[k]);
         for (ptr = A_ptr[k-m], end = A_ptr[k-m+1]; ptr < end; ptr++)
         {  mpq_mul(temp, A_val[ptr], pi[A_ind[ptr]]);
            mpq_add(dj, dj, temp);
         }
      }
      mpq_clear(temp);
      return;
}
Ejemplo n.º 9
0
void lps_getupper(const Var* var, mpq_t upper)
{
   assert(var      != NULL);
   assert(var->sid == VAR_SID);

   mpq_set(upper, var->upper);
}
Ejemplo n.º 10
0
static PyObject *
GMPy_Rational_Abs(PyObject *x, CTXT_Object *context)
{
    MPQ_Object *result = NULL;

    if (MPQ_Check(x)) {
        if (mpz_sgn(mpq_numref(MPQ(x))) >= 0) {
            Py_INCREF(x);
            return x;
        }
        else {
            if ((result = GMPy_MPQ_New(context))) {
                mpq_set(result->q, MPQ(x));
                mpz_abs(mpq_numref(result->q), mpq_numref(result->q));
            }
            return (PyObject*)result;
        }
    }

    /* This is safe because result is not an incremented reference to an
     * existing value. MPQ is already handled so GMPy_MPQ_From_Rational()
     * can't return an incremented reference to an existing value (which it
     * would do if passed an MPQ).
     */

    if ((result = GMPy_MPQ_From_Rational(x, context))) {
        mpz_abs(mpq_numref(result->q), mpq_numref(result->q));
    }

    return (PyObject*)result;
}
Ejemplo n.º 11
0
void lps_getlower(const Var* var, mpq_t lower)
{
   assert(var      != NULL);
   assert(var->sid == VAR_SID);

   mpq_set(lower, var->lower);
}
Ejemplo n.º 12
0
game_t *game_new(int graph_type, int g_p1, int g_p2, mpq_t p_c)
{
    game_t *game = (game_t *)g_malloc(sizeof(game_t));
    
    game->graph = graph_new(graph_type, g_p1, g_p2);
    
    mpq_init(game->s);
    mpq_set_si(game->s, S_MIN, 1);
    mpq_init(game->t);
    mpq_set_si(game->t, T_MIN, 1);
    mpq_init(game->p_c);
    mpq_set(game->p_c, p_c);
    
    game->initial_config = (int *)g_malloc(sizeof(int) * game->graph->n);
    game->current_config = (int *)g_malloc(sizeof(int) * game->graph->n);
    game->next_config = (int *)g_malloc(sizeof(int) * game->graph->n);
    
    int i;
    for(i = 0; i < game->graph->n; ++i)
    {
        game->initial_config[i] = 0;
        game->current_config[i] = 0;
        game->next_config[i] = 0;
    }
    return game;
}
Ejemplo n.º 13
0
void game_get_payoff_of_player(game_t *game, int i, mpq_t payoff)
{
    GSList *list = graph_get_neighbours_of(game->graph, i);
    int n = g_slist_length(list) + 1;
    
    int delta = game_get_number_of_cooperators_in_list(game, list);
    delta += game->current_config[i];
    //printf("n= %d, d= %d", n, delta);
    
    mpq_t tmp;
    mpq_init(tmp);
    
    //double payoff = 0;
    
    if(game->current_config[i] == COOPERATE)
    {
        //int pay = (delta - 1) + ((n - delta) * -1);
        //printf("pay = %d", pay);
        //payoff = (delta - 1) + (n - delta) * game->s;
        mpq_set_si(tmp, (n - delta), 1);
        mpq_mul(tmp, game->s, tmp);
        
        mpq_set(payoff, tmp);
        mpq_set_si(tmp, (delta - 1), 1);
        mpq_add(payoff, tmp, payoff);
    }
    else
    {
        //payoff = delta * game->t;
        mpq_set_si(tmp, delta, 1);
        mpq_mul(payoff, tmp, game->t);
    }
    
    mpq_clear(tmp);
}
Ejemplo n.º 14
0
 void to_ring_elem(ring_elem &result, const ElementType& a) const
 {
     gmp_QQ b = getmemstructtype(gmp_QQ);
     mpq_init(b);
     mpq_set(b,&a);
     result.poly_val = reinterpret_cast<Nterm*>(b);
 }
Ejemplo n.º 15
0
void lux_v_solve(LUX *lux, int tr, mpq_t x[])
{     int n = lux->n;
      mpq_t *V_piv = lux->V_piv;
      LUXELM **V_row = lux->V_row;
      LUXELM **V_col = lux->V_col;
      int *P_row = lux->P_row;
      int *Q_col = lux->Q_col;
      LUXELM *vij;
      int i, j, k;
      mpq_t *b, temp;
      b = xcalloc(1+n, sizeof(mpq_t));
      for (k = 1; k <= n; k++)
         mpq_init(b[k]), mpq_set(b[k], x[k]), mpq_set_si(x[k], 0, 1);
      mpq_init(temp);
      if (!tr)
      {  /* solve the system V*x = b */
         for (k = n; k >= 1; k--)
         {  i = P_row[k], j = Q_col[k];
            if (mpq_sgn(b[i]) != 0)
            {  mpq_set(x[j], b[i]);
               mpq_div(x[j], x[j], V_piv[i]);
               for (vij = V_col[j]; vij != NULL; vij = vij->c_next)
               {  mpq_mul(temp, vij->val, x[j]);
                  mpq_sub(b[vij->i], b[vij->i], temp);
               }
            }
         }
      }
      else
      {  /* solve the system V'*x = b */
         for (k = 1; k <= n; k++)
         {  i = P_row[k], j = Q_col[k];
            if (mpq_sgn(b[j]) != 0)
            {  mpq_set(x[i], b[j]);
               mpq_div(x[i], x[i], V_piv[i]);
               for (vij = V_row[i]; vij != NULL; vij = vij->r_next)
               {  mpq_mul(temp, vij->val, x[i]);
                  mpq_sub(b[vij->j], b[vij->j], temp);
               }
            }
         }
      }
      for (k = 1; k <= n; k++) mpq_clear(b[k]);
      mpq_clear(temp);
      xfree(b);
      return;
}
Ejemplo n.º 16
0
static void
dumpSkip(mpq_t *t, const symbol_t *scan)
{
    static int          initialized = 0;
    static mpq_t        dt;
    static int          de;
    static int          nu;
    static mpz_t        zde;
    static mpz_t        znu;
    int rnd;

    if (! initialized) {
        mpq_init(dt);
        mpz_init(zde);
        mpz_init(znu);
        initialized = 1;
    }

    if (mpq_equal(*t, scan->start)) {
        return;
    }

    while (dump_tuplet_current != NO_ID) {
        /* stop */
        fprintf(lily_out, " }");
        tuplet_pop(&dump_tuplet_current);
    }

    if (mpq_cmp(*t, scan->start) > 0) {
        fprintf(stderr, "Uh oh -- start time ");
        mpq_out_str(stderr, 10, scan->start);
        fprintf(stderr, " is too low, should be ");
        mpq_out_str(stderr, 10, *t);
        fprintf(stderr, " -- is my voice analysis correct?\n");
        return;
    }
    mpq_sub(dt, scan->start, *t);
    mpq_get_num(znu, dt);
    mpq_get_den(zde, dt);
    nu = mpz_get_ui(znu);
    de = mpz_get_ui(zde);
    rnd = nu / de;
    if (rnd != 0) {
        fprintf(lily_out, " s1*%d", rnd);
        nu -= rnd * de;
    }
    if (! is_two_pow(de)) {
        fprintf(stderr, "Uh oh -- skip now already a tuplet %d/%d??\n", nu, de);
        fprintf(lily_out, " s1*%d/%d", nu, de);
    } else {
        if (nu != 0) {
            fprintf(lily_out, " s%d*%d", de, nu);
        }
    }
    last_dumped_symbol = &sym_any_skip;
    mpq_set(*t, scan->start);
    VPRINTF(" skip to t = ");
    VPRINT_MPQ(*t);
}
Ejemplo n.º 17
0
/**
 * dup an object
 */
lv_t *lisp_dup_item(lv_t *v) {
    lv_t *r;
    lv_t *vptr = v;
    lv_t *rptr;
    assert(v);

    switch(v->type) {
    case l_int:
        r = lisp_create_int(0);
        mpz_set(L_INT(r), L_INT(v));
        return r;
    case l_rational:
        r = lisp_create_rational(1, 1);
        mpq_set(L_RAT(r), L_RAT(v));
        return r;
    case l_float:
        r = lisp_create_float(0.0);
        mpfr_set(L_FLOAT(r), L_FLOAT(v), MPFR_ROUND_TYPE);
        return r;
    case l_bool:
        return v;
    case l_sym:
        return lisp_create_symbol(L_SYM(v));
    case l_str:
        return lisp_create_string(L_STR(v));
    case l_null:
        return v;
    case l_port:
        /* can't really copy this -- it's a socket or a file
           handle, or something else.  */
        return v;
    case l_char:
        return lisp_create_char(L_CHAR(v));
    case l_fn:
        /* can't really copy this either, but it's essentially
           immutable */
        return v;
    case l_err:
        return lisp_create_err(L_ERR(v));
    case l_hash:
        /* FIXME: should really be a copy */
        return v;
    case l_pair:
        r = lisp_create_pair(NULL, NULL);
        rptr = r;

        while(vptr && L_CAR(vptr)) {
            L_CAR(rptr) = lisp_dup_item(L_CAR(vptr));
            vptr = L_CDR(vptr);
            if(vptr) {
                L_CDR(rptr) = lisp_create_pair(NULL, NULL);
                rptr = L_CDR(rptr);
            }
        }
        return r;
    }

    assert(0);
}
Ejemplo n.º 18
0
void ssx_eval_bbar(SSX *ssx)
{     int m = ssx->m;
      int n = ssx->n;
      mpq_t *coef = ssx->coef;
      int *A_ptr = ssx->A_ptr;
      int *A_ind = ssx->A_ind;
      mpq_t *A_val = ssx->A_val;
      int *Q_col = ssx->Q_col;
      mpq_t *bbar = ssx->bbar;
      int i, j, k, ptr;
      mpq_t x, temp;
      mpq_init(x);
      mpq_init(temp);
      /* bbar := 0 */
      for (i = 1; i <= m; i++)
         mpq_set_si(bbar[i], 0, 1);
      /* bbar := - N * xN = - N[1] * xN[1] - ... - N[n] * xN[n] */
      for (j = 1; j <= n; j++)
      {  ssx_get_xNj(ssx, j, x);
         if (mpq_sgn(x) == 0) continue;
         k = Q_col[m+j]; /* x[k] = xN[j] */
         if (k <= m)
         {  /* N[j] is a column of the unity matrix I */
            mpq_sub(bbar[k], bbar[k], x);
         }
         else
         {  /* N[j] is a column of the original constraint matrix -A */
            for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
            {  mpq_mul(temp, A_val[ptr], x);
               mpq_add(bbar[A_ind[ptr]], bbar[A_ind[ptr]], temp);
            }
         }
      }
      /* bbar := inv(B) * bbar */
      bfx_ftran(ssx->binv, bbar, 0);
#if 1
      /* compute value of the objective function */
      /* bbar[0] := c[0] */
      mpq_set(bbar[0], coef[0]);
      /* bbar[0] := bbar[0] + sum{i in B} cB[i] * xB[i] */
      for (i = 1; i <= m; i++)
      {  k = Q_col[i]; /* x[k] = xB[i] */
         if (mpq_sgn(coef[k]) == 0) continue;
         mpq_mul(temp, coef[k], bbar[i]);
         mpq_add(bbar[0], bbar[0], temp);
      }
      /* bbar[0] := bbar[0] + sum{j in N} cN[j] * xN[j] */
      for (j = 1; j <= n; j++)
      {  k = Q_col[m+j]; /* x[k] = xN[j] */
         if (mpq_sgn(coef[k]) == 0) continue;
         ssx_get_xNj(ssx, j, x);
         mpq_mul(temp, coef[k], x);
         mpq_add(bbar[0], bbar[0], temp);
      }
#endif
      mpq_clear(x);
      mpq_clear(temp);
      return;
}
Ejemplo n.º 19
0
void lps_addnzo(
   Lps*        lp,
   Con*        con,
   Var*        var,
   const mpq_t value)
{
   Nzo* nzo;
   
   assert(lps_valid(lp));
   assert(con         != NULL);
   assert(con->sid    == CON_SID);
   assert(var         != NULL);   
   assert(var->sid    == VAR_SID);

   /* Ins LP aufnehmen
    */
   if (lp->next == NULL)
      lps_storage(lp);
      
   nzo = lp->next;

   assert(nzo != NULL);

   lp->next = nzo->var_next;
   lp->nonzeros++;

   mpq_set(nzo->value, value);
      
   /* In die Spalte aufnehmen
    */
   nzo->var      = var;
   nzo->var_prev = NULL;
   nzo->var_next = var->first;
   var->first    = nzo;
   var->size++;
   
   if (nzo->var_next != NULL)
   {
      assert(nzo->var_next->var_prev == NULL);
      
      nzo->var_next->var_prev = nzo;       
   }
   
   /* In die Zeile aufnehmen
    */
   nzo->con      = con;
   nzo->con_prev = NULL;
   nzo->con_next = con->first;
   con->first    = nzo;
   con->size++;
   
   if (nzo->con_next != NULL)
   {
      assert(nzo->con_next->con_prev == NULL);
      
      nzo->con_next->con_prev = nzo;       
   }
   assert(lps_valid(lp));
}
Ejemplo n.º 20
0
void lps_setval(
   Nzo*  nzo,
   const mpq_t value)
{
   assert(nzo != NULL);

   mpq_set(nzo->value, value);
}
Ejemplo n.º 21
0
void lps_getval(
   const Nzo* nzo,
   mpq_t      value)
{
   assert(nzo != NULL);

   mpq_set(value, nzo->value);
}
Ejemplo n.º 22
0
void lps_getcost(
   const Var* var,
   mpq_t      cost)
{
   assert(var      != NULL);
   assert(var->sid == VAR_SID);
   
   mpq_set(cost, var->cost);
}
Ejemplo n.º 23
0
static void
do_staff_multibar_aggregate(staff_p f)
{
    note_p      multibar_open = NULL;   // defy gcc warnings
    mpq_t       time_sig;
    int         multibar = 0;

    mpq_init(time_sig);

    for (symbol_p scan = f->unvoiced.front; scan != NULL; scan = scan->next) {
        int must_close = 1;
        note_p note;

        switch (scan->type) {
        case SYM_TIME_SIGNATURE:
            mpq_set(time_sig, scan->symbol.time_signature.duration);
            break;

        case SYM_TUPLET:
        case SYM_REPEAT:
            break;

        case SYM_NOTE:
            note = &scan->symbol.note;

            if (! (note->flags & FLAG_REST)) {
                break;
            }
            if (! mpq_equal(time_sig, note->duration)) {
                break;
            }

            // Ah, there it is: a multibar rest
            must_close = 0;
            if (multibar == 0) {
                multibar_open = note;
            } else {
                note->multibar = 0;     // suppress
            }
            multibar++;
            break;

        case SYM_CHORD:
            fprintf(stderr, "Ooopppsssss... a CHORD here?\n");
            break;

        default:
            must_close = 0;
            break;
        }

        if (must_close && multibar > 0) {
            multibar_open->multibar = multibar;
            multibar = 0;
        }
    }
}
Ejemplo n.º 24
0
	Rational::Rational(const Rational& source)
	{
		mpq_init(number);
		mpq_set(number, source.number);

	#ifdef TRACE_OUTPUT
		UpdateNumberStr();
	#endif
	}
Ejemplo n.º 25
0
void lps_setcost(
   Var*        var,
   const mpq_t cost)
{
   assert(var      != NULL);
   assert(var->sid == VAR_SID);

   mpq_set(var->cost, cost);
}
Ejemplo n.º 26
0
Archivo: depth.c Proyecto: hvds/seq
bool search_depth(void) {
    bool result;
    mpq_set(stack[0].q, rone);
    stack[0].count = 0;
    stack[0].actual = 1;
    result = try_depth(0);
    report_final();
    return result;
}
Ejemplo n.º 27
0
void lps_setstartval(
   Var*        var,
   const mpq_t startval)
{
   assert(var      != NULL);
   assert(var->sid == VAR_SID);
   
   mpq_set(var->startval, startval);
}
Ejemplo n.º 28
0
void lps_setvalue(
   Var*        var,
   const mpq_t value)
{
   assert(var      != NULL);
   assert(var->sid == VAR_SID);
   
   mpq_set(var->value, value);
}
Ejemplo n.º 29
0
void lps_setscale(
   Con*        con,
   const mpq_t scale)
{
   assert(con      != NULL);
   assert(con->sid == CON_SID);
   
   mpq_set(con->scale, scale);
}
Ejemplo n.º 30
0
void lps_getrhs(
   const Con* con,
   mpq_t      rhs)
{
   assert(con      != NULL);
   assert(con->sid == CON_SID);

   mpq_set(rhs, con->rhs);
}