Example #1
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);
}
Example #2
0
void bisect_sfdlProverExo::exogenous_fAtMidpt(mpq_t& f, mpq_t* a, mpq_t* b) {
  mpq_t t2;
  mpq_t t3;

  alloc_init_scalar(t2);
  alloc_init_scalar(t3);

  mpq_set_ui(f, 0, 1);
  int m = bisect_sfdl_cons::m;

  int rctr = 0;
  for(int i = 0; i < m; i++) {
    mpq_add(t2, a[i], b[i]);
    mpq_set_si(t3, bisect_sfdl_cons::fAtMidpt_FUNCTION_STATIC_RANDOM_INT[rctr++], 1);
    mpq_mul(t2, t3, t2);
    mpq_div_2exp(t2, t2, 1);
    mpq_add(f, f, t2);
  }


  for(int i = 0; i < m; i++) {
    for(int j = 0; j < m; j++) {
      mpq_add(t2, a[i], b[i]);
      mpq_add(t3, a[j], b[j]);
      mpq_mul(t2, t2, t3);
      mpq_set_si(t3, bisect_sfdl_cons::fAtMidpt_FUNCTION_STATIC_RANDOM_INT[rctr++], 1);
      mpq_mul(t2, t2, t3);
      mpq_div_2exp(t2, t2, 2);
      mpq_add(f, f, t2);
    }
  }

  mpq_clear(t2);
  mpq_clear(t3);
}
Example #3
0
static PyObject *
GMPy_Rational_Add(PyObject *x, PyObject *y, CTXT_Object *context)
{
    MPQ_Object *result;

    if (!(result = GMPy_MPQ_New(context)))
        return NULL;

    if (MPQ_Check(x) && MPQ_Check(y)) {
        mpq_add(result->q, MPQ(x), MPQ(y));
        return (PyObject*)result;
    }

    if (IS_RATIONAL(x) && IS_RATIONAL(y)) {
        MPQ_Object *tempx, *tempy;

        tempx = GMPy_MPQ_From_Number(x, context);
        tempy = GMPy_MPQ_From_Number(y, context);
        if (!tempx || !tempy) {
            Py_XDECREF((PyObject*)tempx);
            Py_XDECREF((PyObject*)tempy);
            Py_DECREF((PyObject*)result);
            return NULL;
        }

        mpq_add(result->q, tempx->q, tempy->q);
        Py_DECREF((PyObject*)tempx);
        Py_DECREF((PyObject*)tempy);
        return (PyObject*)result;
    }

    Py_DECREF((PyObject*)result);
    Py_RETURN_NOTIMPLEMENTED;
}
Example #4
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;
}
Example #5
0
File: ovm_mpq.c Project: pcpa/owl
void
ovm_q_add(oregister_t *l, oregister_t *r)
{
    switch (r->t) {
	case t_void:
	    break;
	case t_word:
	    mpq_set_si(oqr(r), r->v.w, 1);
	    mpq_add(oqr(l), oqr(l), oqr(r));
	    check_mpq(l);
	    break;
	case t_float:
	    l->t = t_float;
	    l->v.d = mpq_get_d(oqr(l)) + r->v.d;
	    break;
	case t_mpz:
	    mpz_set_ui(ozs(r), 1);
	    mpq_add(oqr(l), oqr(l), oqr(r));
	    check_mpq(l);
	    break;
	case t_rat:
	    mpq_set_si(oqr(r), rat_num(r->v.r), rat_den(r->v.r));
	    mpq_add(oqr(l), oqr(l), oqr(r));
	    check_mpq(l);
	    break;
	case t_mpq:
	    mpq_add(oqr(l), oqr(l), oqr(r));
	    check_mpq(l);
	    break;
	case t_mpr:
	    l->t = t_mpr;
	    mpfr_set_q(orr(l), oqr(l), thr_rnd);
	    mpfr_add(orr(l), orr(l), orr(r), thr_rnd);
	    break;
	case t_cdd:
	    l->t = t_cdd;
	    l->v.dd = mpq_get_d(oqr(l)) + r->v.dd;
	    break;
	case t_cqq:
	    l->t = t_cqq;
	    mpq_set_ui(oqi(l), 0, 1);
	    cqq_add(oqq(l), oqq(l), oqq(r));
	    check_cqq(l);
	    break;
	case t_mpc:
	    l->t = t_mpc;
	    mpc_set_q(occ(l), oqr(l), thr_rndc);
	    mpc_add(occ(l), occ(l), occ(r), thr_rndc);
	    break;
	default:
	    ovm_raise(except_not_a_number);
    }
}
Example #6
0
AlkValue AlkValue::operator+(const AlkValue &right) const
{
  AlkValue result;
  mpq_add(result.d->m_val.get_mpq_t(), d->m_val.get_mpq_t(), right.d->m_val.get_mpq_t());
  result.d->m_val.canonicalize();
  return result;
}
Example #7
0
/* not canonicalized */
void _lsrt_mpq_set_decimal(mpq_t q, const char *ptr, int radix)
{
  mpz_t num, dec, exp;
  mpq_t addend;
  int ex, expdigits;
  int s, e;
  char ch = '+';

  if (radix != 10)
    lsrt_error("unsupported radix for exact decimal number: %d", radix);

  mpz_init_set_ui(num, 0);
  mpz_init_set_ui(dec, 0);
  mpz_init(exp);
  mpq_init(addend);

  e = 0;
  sscanf(ptr, "%[+-]%n", &ch, &e);
  ptr += e;

  e = 0;
  gmp_sscanf(ptr, "%Zd%n", num, &e);
  ptr += e;

  s = 0;
  e = 0;
  expdigits = 0;
  gmp_sscanf(ptr, ".%n%Zd%n", &s, dec, &e);
  ptr += e;
  if (e >= s)
    expdigits = e - s;

  s = 0;
  e = 0;
  ex = 0;
  sscanf(ptr, "@%n%d%n", &s, &ex, &e);
  if (e - s >= 5 || ex >= 256)
    lsrt_error("decimal number out of range");

  mpz_set(mpq_numref(q), dec);
  mpz_ui_pow_ui(mpq_denref(q), radix, expdigits);
  mpq_set_z(addend, num);
  mpq_add(q, q, addend);

  if (ex > 0) {
    mpz_ui_pow_ui(exp, 10, ex);   /* exponent is always in radix 10 */
    mpz_mul(mpq_numref(q), mpq_numref(q), exp);
  } else if (ex < 0) {
    mpz_ui_pow_ui(exp, 10, -ex);
    mpz_mul(mpq_denref(q), mpq_denref(q), exp);
  }

  if (ch == '-')
    mpz_neg(mpq_numref(q), mpq_numref(q));

  mpq_clear(addend);
  mpz_clear(exp);
  mpz_clear(num);
  mpz_clear(dec);
}
Example #8
0
	//-------------------------------------------------- operator +
	Rationel& Rationel::addin(Rationel& res, const Rationel& n)
	{
		if (isZero(n)) return res;
		if (isZero(res)) return res = n;
		mpq_add( (mpq_ptr)&res.gmp_rep, (mpq_srcptr)&res.gmp_rep, (mpq_srcptr)&n.gmp_rep );
		return res;
	}
Example #9
0
obj string_to_rational_obj( char *str, unsigned radix )
{
    char *f;
    mpq_t b, w;

    f = strchr( str, '+' );       /* support WHOLE+NUM/DEN syntax */
    if (f) {
        MP_INT *num;
        mpz_t numa;
        if (!str2rat( &b, f+1, radix )) {
            return FALSE_OBJ;
        }
        *f = '\0';
        num = mpq_numref(w);
        if (!str2big( num, str, radix )) {
            *f = '+';
            return FALSE_OBJ;
        }
        *f = '+';
        mpz_init_set_si( mpq_denref(w), 1 );
        mpq_add( b, b, w );
    } else {
        if (!str2rat( &b, str, radix )) {
            return FALSE_OBJ;
        }
    }
    return rational_compact( b );
}
Example #10
0
void ssx_eval_row(SSX *ssx)
{     int m = ssx->m;
      int n = ssx->n;
      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 *rho = ssx->rho;
      mpq_t *ap = ssx->ap;
      int j, k, ptr;
      mpq_t temp;
      mpq_init(temp);
      for (j = 1; j <= n; j++)
      {  /* ap[j] := - N'[j] * rho (inner product) */
         k = Q_col[m+j]; /* x[k] = xN[j] */
         if (k <= m)
            mpq_neg(ap[j], rho[k]);
         else
         {  mpq_set_si(ap[j], 0, 1);
            for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
            {  mpq_mul(temp, A_val[ptr], rho[A_ind[ptr]]);
               mpq_add(ap[j], ap[j], temp);
            }
         }
      }
      mpq_clear(temp);
      return;
}
Example #11
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;
}
void
check_all (mpq_ptr x, mpq_ptr y, mpq_ptr want_add, mpq_ptr want_sub)
{
  mpq_t  got;
  int    neg_x, neg_y, swap;

  mpq_init (got);

  MPQ_CHECK_FORMAT (want_add);
  MPQ_CHECK_FORMAT (want_sub);
  MPQ_CHECK_FORMAT (x);
  MPQ_CHECK_FORMAT (y);

  for (swap = 0; swap <= 1; swap++)
    {
      for (neg_x = 0; neg_x <= 1; neg_x++)
        {
          for (neg_y = 0; neg_y <= 1; neg_y++)
            {
              mpq_add (got, x, y);
              MPQ_CHECK_FORMAT (got);
              if (! mpq_equal (got, want_add))
                {
                  printf ("mpq_add wrong\n");
                  mpq_trace ("  x   ", x);
                  mpq_trace ("  y   ", y);
                  mpq_trace ("  got ", got);
                  mpq_trace ("  want", want_add);
                  abort ();
                }

              mpq_sub (got, x, y);
              MPQ_CHECK_FORMAT (got);
              if (! mpq_equal (got, want_sub))
                {
                  printf ("mpq_sub wrong\n");
                  mpq_trace ("  x   ", x);
                  mpq_trace ("  y   ", y);
                  mpq_trace ("  got ", got);
                  mpq_trace ("  want", want_sub);
                  abort ();
                }


              mpq_neg (y, y);
              mpq_swap (want_add, want_sub);
            }

          mpq_neg (x, x);
          mpq_swap (want_add, want_sub);
          mpq_neg (want_add, want_add);
          mpq_neg (want_sub, want_sub);
        }

      mpq_swap (x, y);
      mpq_neg (want_sub, want_sub);
    }

  mpq_clear (got);
}
Example #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);
}
Example #14
0
int AB_Value_AddValue(AB_VALUE *v1, const AB_VALUE *v2) {
  assert(v1);
  assert(v2);

  mpq_add(v1->value, v1->value, v2->value);
  return 0;
}
Example #15
0
void refine_sqrt_upper(mpq_t sqrt_x, mpq_t x, int digits)
/***************************************************************\
* USAGE: refine a rational upper bound on the sqrt(x) to the    *
*    requested tolerance: 10^-digits                            *
\***************************************************************/
{ // assume (sqrt_x)^2 >= x > 0 && sqrt_x > 0 && digits >= 0
  mpq_t new_sqrt_x, beta, tol;

  // initialize
  mpq_init(new_sqrt_x);
  mpq_init(beta);
  mpq_init(tol);

  // setup tol = (1/10)^digits
  mpq_set_ui(tol, 1, 10);
  exponentiate_mpq(tol, tol, digits);

  // loop until we have refined to the tol
  do
  { // compute new_sqrt_x & beta 
    mpq_mul(beta, sqrt_x, sqrt_x);       // (sqrt_x)^2
    mpq_sub(beta, beta, x);              // (sqrt_x)^2 - x

    mpq_add(new_sqrt_x, sqrt_x, sqrt_x); // 2*sqrt_x 
    mpq_div(beta, beta, new_sqrt_x);     // ((sqrt_x)^2 - x)/(2*sqrt_x)
    mpq_sub(new_sqrt_x, sqrt_x, beta);   // sqrt_x - ((sqrt_x)^2 - x)/(2*sqrt_x)

    // determine if 2*beta <= tol
    mpq_add(beta, beta, beta);
    if (mpq_cmp(beta, tol) <= 0)
    { // success!!
      break;
    }
    else
    { // update sqrt_x & try again
      mpq_set(sqrt_x, new_sqrt_x);
    }
  } while (1);
  
  // clear
  mpq_clear(new_sqrt_x);
  mpq_clear(beta);
  mpq_clear(tol);

  return;
}
Example #16
0
	Rational operator+(const Rational& num1, const Rational& num2)
	{
		Rational res;

		mpq_add(res.number, num1.number, num2.number);

		return res;
	}
Example #17
0
void Rational::addProduct(const Rational& op1, const Rational& op2)
{
   mpq_t prod;
   mpq_init(prod);
   mpq_mul(prod, op1.number, op2.number);
   mpq_add(number, number, prod);
   mpq_canonicalize(number);
   mpq_clear(prod);
}
Example #18
0
	Rational operator+(const int num1, const Rational& num2)
	{
		Rational res;
		Rational _num1(num1);

		mpq_add(res.number, _num1.number, num2.number);

		return res;
	}
Example #19
0
	Rationel& Rationel::addin(Rationel& res, const Integer& n)
	{
		if (Givaro::isZero(n)) return res;
		Rationel nn ;
		setInteger(nn,n);
		if (isZero(res)) return res = nn;
		mpq_add( (mpq_ptr)&res.gmp_rep, (mpq_srcptr)&res.gmp_rep, (mpq_srcptr)&nn.gmp_rep );
		return res;
	}
Example #20
0
static void from_double(mpq_t n, double f, int level)
{
    double fa;
    int nfa;
    mpq_t ni, nn;
    bool neg;

    //fprintf(stderr, "from_double: %.14g\n", f);

    if (level >= 10)
        goto __DEFAULT;

    fa = fabs(f);
    if (fa >= 1E8 || fa <= 1E-8)
        goto __DEFAULT;

    neg = (f < 0);

    nfa = (int)fa;
    if (nfa >= 1)
        fa -= nfa;

    //fprintf(stderr, "fa = %.14g %.14g\n", fa, (fa*1E8) - (int)(fa*1E8));

    if (nfa && fa < 1E-8)
    {
        mpq_set_si(n, 0, 1);
    }
    else if (((fa*1E8) - (int)(fa*1E8)) < 1E-8)
    {
        mpq_set_si(n, (int)(fa*1E8), 100000000);
    }
    else
    {
        mpq_init(ni);
        from_double(ni, 1 / fa, level + 1);
        mpq_inv(n, ni);
        mpq_clear(ni);
    }

    mpq_init(nn);
    mpq_set_si(nn, nfa, 1);
    mpq_add(n, n, nn);
    mpq_clear(nn);

    if (neg)
        mpq_neg(n, n);

    mpq_canonicalize(n);

    return;

__DEFAULT:

    mpq_set_d(n, f);
}
Example #21
0
void generate_random_rational(rational_complex_number x)
/***************************************************************\
* USAGE: generate a random rational number of unit modulus      *
\***************************************************************/
{
  int base = 10;
  char *str = NULL;
  mpq_t t, t_sqr;

  mpq_init(t);
  mpq_init(t_sqr);  

  // generate a random number
  create_random_number_str(&str);
  mpq_set_str(t, str, base);
  mpq_canonicalize(t);

  // compute t_sqr = t^2
  mpq_mul(t_sqr, t, t);

  // compute denominator
  mpq_set_ui(x->im, 1, 1);
  mpq_add(x->im, x->im, t_sqr); // 1 + t^2

  // compute numerator
  mpq_set_ui(x->re, 1, 1);
  mpq_sub(x->re, x->re, t_sqr); // 1 - t^2

  // divide to compute real part
  mpq_div(x->re, x->re, x->im);

  // compute imaginary part
  mpq_set_ui(x->im, 1, 1);
  mpq_add(x->im, x->re, x->im); // 1 + x->re
  mpq_mul(x->im, x->im, t);     // t*(1+x->re)

  // clear memory
  mpq_clear(t);
  mpq_clear(t_sqr);
  free(str);

  return;
}
Example #22
0
obj rational_plus( obj a, obj b )
{
    mpq_t r, a1, b1;

    OBJ_TO_MPQ(a1, a);
    OBJ_TO_MPQ(b1, b);

    mpq_init(r);
    mpq_add(r, a1, b1);
    return rational_compact(r);
}
Example #23
0
static PyObject *
GMPy_Rational_Add(PyObject *x, PyObject *y, CTXT_Object *context)
{
    MPQ_Object *result = NULL;

    if (!(result = GMPy_MPQ_New(context))) {
        /* LCOV_EXCL_START */
        return NULL;
        /* LCOV_EXCL_STOP */
    }

    if (MPQ_Check(x) && MPQ_Check(y)) {
        mpq_add(result->q, MPQ(x), MPQ(y));
        return (PyObject*)result;
    }

    if (IS_RATIONAL(x) && IS_RATIONAL(y)) {
        MPQ_Object *tempx = NULL, *tempy = NULL;

        if (!(tempx = GMPy_MPQ_From_Number(x, context)) ||
            !(tempy = GMPy_MPQ_From_Number(y, context))) {
            /* LCOV_EXCL_START */
            Py_XDECREF((PyObject*)tempx);
            Py_XDECREF((PyObject*)tempy);
            Py_DECREF((PyObject*)result);
            return NULL;
            /* LCOV_EXCL_STOP */
        }

        mpq_add(result->q, tempx->q, tempy->q);
        Py_DECREF((PyObject*)tempx);
        Py_DECREF((PyObject*)tempy);
        return (PyObject*)result;
    }

    /* LCOV_EXCL_START */
    SYSTEM_ERROR("Internal error in GMPy_Rational_Add().");
    Py_DECREF((PyObject*)result);
    return NULL;
    /* LCOV_EXCL_STOP */
}
Example #24
0
static inline rsexp add_fix_fix (RState* r, rsexp lhs, rsexp rhs)
{
    rsexp sum;

    ensure (sum = smi_to_fixreal (r, R_ZERO));

    mpq_add (fixreal_value (sum),
             fixreal_value (lhs),
             fixreal_value (rhs));

    return fixreal_normalize (sum);
}
Example #25
0
void ssx_update_bbar(SSX *ssx)
{     int m = ssx->m;
      int n = ssx->n;
      mpq_t *bbar = ssx->bbar;
      mpq_t *cbar = ssx->cbar;
      int p = ssx->p;
      int q = ssx->q;
      mpq_t *aq = ssx->aq;
      int i;
      mpq_t temp;
      mpq_init(temp);
      xassert(1 <= q && q <= n);
      if (p < 0)
      {  /* xN[q] is double-bounded and goes to its opposite bound */
         /* nop */;
      }
      else
      {  /* xN[q] becomes xB[p] in the adjacent basis */
         /* xB.new[p] = xN[q] + delta xN[q] */
         xassert(1 <= p && p <= m);
         ssx_get_xNj(ssx, q, temp);
         mpq_add(bbar[p], temp, ssx->delta);
      }
      /* update values of other basic variables depending on xN[q] */
      for (i = 1; i <= m; i++)
      {  if (i == p) continue;
         /* xB.new[i] = xB[i] + alfa[i,q] * delta xN[q] */
         if (mpq_sgn(aq[i]) == 0) continue;
         mpq_mul(temp, aq[i], ssx->delta);
         mpq_add(bbar[i], bbar[i], temp);
      }
#if 1
      /* update value of the objective function */
      /* z.new = z + d[q] * delta xN[q] */
      mpq_mul(temp, cbar[q], ssx->delta);
      mpq_add(bbar[0], bbar[0], temp);
#endif
      mpq_clear(temp);
      return;
}
Example #26
0
void mpq_mat_row_add(mpq_mat_t mat1, ulong r1, mpq_mat_t mat2, ulong r2){

   if (mat2->c != mat1->c){
      printf("FLINT exception: dimensions don't match\n");
      abort();
   }

   long i;
   for (i = 0; i< mat1->c; i++)
      mpq_add(mat1->entries[r1*mat1->c + i], mat2->entries[r2*mat2->c + i], mat1->entries[r1*mat1->c + i]);

   return;

}
Example #27
0
File: gc.c Project: carthy/beard
void
test_gc_get_rational (void* data)
{
	mpq_t* num  = GC_NEW_RATIONAL(runtime);
	mpq_t* num2 = GC_NEW_RATIONAL(runtime);

	mpq_set_ui(*num, 2, 3);
	mpq_set_ui(*num2, 2, 3);
	mpq_add(*num, *num, *num2);

	tt_assert(mpq_cmp_ui(*num, 4, 3) == 0);

end:;
}
Example #28
0
static PyObject *
Pympq_add(PyObject *self, PyObject *args)
{
    PympqObject *result;
    PyObject *other;

    PARSE_TWO_MPQ(other, "add() requires 'mpq','mpq' arguments");

    if ((result = (PympqObject*)Pympq_new()))
        mpq_add(result->q, Pympq_AS_MPQ(self), Pympq_AS_MPQ(other));

    Py_DECREF(self);
    Py_DECREF(other);
    return (PyObject*)result;
}
Example #29
0
mpq_t* hash_mpq_find(hash_mpq* h,
                     int operation, mpq_t operand1, mpq_t operand2) {
  unsigned long  i;

  i  = ((unsigned long)mpz_get_si(mpq_numref(operand1))+101) % h->keys;
  i *= ((unsigned long)mpz_get_si(mpq_denref(operand1))+ 11) % h->keys;
  i *= ((unsigned long)mpz_get_si(mpq_numref(operand2))) % h->keys;
  i *= ((unsigned long)mpz_get_si(mpq_denref(operand2))) % h->keys;
  i %= h->keys;

  if (h->operation[i] == operation        &&
      mpq_equal(h->operand1[i], operand1) &&
      mpq_equal(h->operand2[i], operand2)) {
    //fprintf(stderr,"*");
    return &(h->result[i]);
  }
  //fprintf(stderr,".");

  if (h->operation[i] == HASH_OPERATION_NOT_USED) {
    mpq_init(h->operand1[i]);
    mpq_init(h->operand2[i]);
    mpq_init(h->result  [i]);
  }

  h->operation[i] = operation;
  mpq_set(h->operand1[i], operand1);
  mpq_set(h->operand2[i], operand2);

  if (operation == HASH_OPERATION_ADD)
    mpq_add(h->result[i], operand1, operand2);

  //else if (operation == HASH_OPERATION_SUB)
  //  mpq_sub(h->result[i], operand1, operand2);

  else //if (operation == HASH_OPERATION_MUL)
    mpq_mul(h->result[i], operand1, operand2);

  //else
  //  mpq_div(h->result[i], operand1, operand2);

  return &(h->result[i]);
}
Example #30
0
File: pp.c Project: hvds/seq
void pp_init_spare(int target) {
	int i;
	pp_pp *pp, *top;
	mpq_t spare;

	QINIT(&spare, "pp_study spare");
	top = pplist[0];
	mpq_set_si(top->spare, -target, 1);
	for (i = 0; i < pplistsize; ++i) {
		int g;

		pp = pplist[i];
		mpz_sub(mpq_numref(spare), pp->total, pp->min_discard);
		mpz_set(mpq_denref(spare), pp->denominator);
		mpq_canonicalize(spare);
		mpq_add(top->spare, top->spare, spare);
	}
	QCLEAR(&spare, "pp_study spare");
	Dprintf("study: spare = %Qd\n", top->spare);
}