Пример #1
0
static PyObject *
GMPy_Rational_FloorDiv(PyObject *x, PyObject *y, CTXT_Object *context)
{
    MPZ_Object *result;
    MPQ_Object *tempq;

    CHECK_CONTEXT(context);

    result = GMPy_MPZ_New(context);
    tempq = GMPy_MPQ_New(context);
    if (!result || !tempq) {
        Py_XDECREF((PyObject*)result);
        Py_XDECREF((PyObject*)tempq);
        return NULL;
    }

    if (MPQ_Check(x) && MPQ_Check(y)) {
        if (mpq_sgn(MPQ(y)) == 0) {
            ZERO_ERROR("division or modulo by zero");
            goto error;
        }
        mpq_div(tempq->q, MPQ(x), MPQ(y));
        mpz_fdiv_q(result->z, mpq_numref(tempq->q), mpq_denref(tempq->q));
        Py_DECREF((PyObject*)tempq);
        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);
            goto error;
        }
        if (mpq_sgn(tempy->q) == 0) {
            ZERO_ERROR("division or modulo by zero");
            Py_DECREF((PyObject*)tempx);
            Py_DECREF((PyObject*)tempy);
            goto error;
        }

        mpq_div(tempq->q, tempx->q, tempy->q);
        mpz_fdiv_q(result->z, mpq_numref(tempq->q), mpq_denref(tempq->q));
        Py_DECREF((PyObject*)tempx);
        Py_DECREF((PyObject*)tempy);
        Py_DECREF((PyObject*)tempq);
        return (PyObject*)result;
    }

    Py_DECREF((PyObject*)result);
    Py_RETURN_NOTIMPLEMENTED;

  error:
    Py_DECREF((PyObject*)result);
    Py_DECREF((PyObject*)tempq);
    return NULL;
}
Пример #2
0
Файл: ovm_mpq.c Проект: pcpa/owl
void
ovm_q_div(oregister_t *l, oregister_t *r)
{
    switch (r->t) {
	case t_void:
	    ovm_raise(except_floating_point_error);
	case t_word:
	    mpq_set_si(oqr(r), r->v.w, 1);
	    mpq_div(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_div(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_div(oqr(l), oqr(l), oqr(r));
	    check_mpq(l);
	    break;
	case t_mpq:
	    mpq_div(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_div(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;
	    check_cdd(l);
	    break;
	case t_cqq:
	    l->t = t_cqq;
	    mpq_set_ui(oqi(l), 0, 1);
	    cqq_div(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_div(occ(l), occ(l), occ(r), thr_rndc);
	    check_mpc(l);
	    break;
	default:
	    ovm_raise(except_not_a_number);
    }
}
Пример #3
0
void ssx_update_pi(SSX *ssx)
{     int m = ssx->m;
      int n = ssx->n;
      mpq_t *pi = ssx->pi;
      mpq_t *cbar = ssx->cbar;
      int p = ssx->p;
      int q = ssx->q;
      mpq_t *aq = ssx->aq;
      mpq_t *rho = ssx->rho;
      int i;
      mpq_t new_dq, temp;
      mpq_init(new_dq);
      mpq_init(temp);
      xassert(1 <= p && p <= m);
      xassert(1 <= q && q <= n);
      /* compute d[q] in the adjacent basis */
      mpq_div(new_dq, cbar[q], aq[p]);
      /* update the vector of simplex multipliers */
      for (i = 1; i <= m; i++)
      {  if (mpq_sgn(rho[i]) == 0) continue;
         mpq_mul(temp, new_dq, rho[i]);
         mpq_sub(pi[i], pi[i], temp);
      }
      mpq_clear(new_dq);
      mpq_clear(temp);
      return;
}
Пример #4
0
int AB_Value_DivValue(AB_VALUE *v1, const AB_VALUE *v2) {
  assert(v1);
  assert(v2);

  mpq_div(v1->value, v1->value, v2->value);
  return 0;
}
Пример #5
0
AlkValue AlkValue::operator/(const AlkValue &right) const
{
  AlkValue result;
  mpq_div(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;
}
Пример #6
0
static RIFFIOSuccess
cbTupletDesc(NIFFIOTagContext *pctxTag, niffTupletDesc *p)
{
    static int initialized = 0;
    static mpq_t r1;
    static mpq_t r2;

    if (cbTagStart(pctxTag, p, cbTupletDesc)) {

        if (! initialized) {
            mpq_init(r1);
            mpq_init(r2);
            initialized = 1;
        }

        tuplet_current = malloc(sizeof(*tuplet_current));
        mpq_init(tuplet_current->ratio);
        rat2mpq(r1, &p->transformRatioAB);
        rat2mpq(r2, &p->transformRatioCD);
        mpq_div(tuplet_current->ratio, r2, r1);
        tuplet_current->den = p->transformRatioAB.numerator;
        tuplet_current->num = 1;
        while (tuplet_current->num < tuplet_current->den) {
            tuplet_current->num *= 2;
        }
        tuplet_current->num /= 2;
    }

    cbTagEnd(pctxTag);

    return RIFFIO_OK;
}
Пример #7
0
void ssx_update_cbar(SSX *ssx)
{     int m = ssx->m;
      int n = ssx->n;
      mpq_t *cbar = ssx->cbar;
      int p = ssx->p;
      int q = ssx->q;
      mpq_t *ap = ssx->ap;
      int j;
      mpq_t temp;
      mpq_init(temp);
      xassert(1 <= p && p <= m);
      xassert(1 <= q && q <= n);
      /* compute d[q] in the adjacent basis */
      /* d.new[q] = d[q] / alfa[p,q] */
      mpq_div(cbar[q], cbar[q], ap[q]);
      /* update reduced costs of other non-basic variables */
      for (j = 1; j <= n; j++)
      {  if (j == q) continue;
         /* d.new[j] = d[j] - (alfa[p,j] / alfa[p,q]) * d[q] */
         if (mpq_sgn(ap[j]) == 0) continue;
         mpq_mul(temp, ap[j], cbar[q]);
         mpq_sub(cbar[j], cbar[j], temp);
      }
      mpq_clear(temp);
      return;
}
Пример #8
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;
}
Пример #9
0
static PyObject *
GMPy_Rational_DivMod(PyObject *x, PyObject *y, CTXT_Object *context)
{
    MPQ_Object *tempx, *tempy, *rem;
    MPZ_Object *quo;
    PyObject *result;

    CHECK_CONTEXT(context);

    result = PyTuple_New(2);
    rem = GMPy_MPQ_New(context);
    quo = GMPy_MPZ_New(context);
    if (!result || !rem || !quo) {
        Py_XDECREF(result);
        Py_XDECREF((PyObject*)rem);
        Py_XDECREF((PyObject*)quo);
        return NULL;
    }

    if (IS_RATIONAL(x) && IS_RATIONAL(y)) {
        tempx = GMPy_MPQ_From_Number(x, context);
        tempy = GMPy_MPQ_From_Number(y, context);
        if (!tempx || !tempy) {
            SYSTEM_ERROR("could not convert Rational to mpq");
            goto error;
        }
        if (mpq_sgn(tempy->q) == 0) {
            ZERO_ERROR("division or modulo by zero");
            goto error;
        }

        mpq_div(rem->q, tempx->q, tempy->q);
        mpz_fdiv_q(quo->z, mpq_numref(rem->q), mpq_denref(rem->q));
        /* Need to calculate x - quo * y. */
        mpq_set_z(rem->q, quo->z);
        mpq_mul(rem->q, rem->q, tempy->q);
        mpq_sub(rem->q, tempx->q, rem->q);
        Py_DECREF((PyObject*)tempx);
        Py_DECREF((PyObject*)tempy);
        PyTuple_SET_ITEM(result, 0, (PyObject*)quo);
        PyTuple_SET_ITEM(result, 1, (PyObject*)rem);
        return result;
    }

    Py_DECREF((PyObject*)result);
    Py_RETURN_NOTIMPLEMENTED;

  error:
    Py_XDECREF((PyObject*)tempx);
    Py_XDECREF((PyObject*)tempy);
    Py_DECREF((PyObject*)rem);
    Py_DECREF((PyObject*)quo);
    Py_DECREF(result);
    return NULL;
}
Пример #10
0
static PyObject *
GMPy_Rational_DivMod(PyObject *x, PyObject *y, CTXT_Object *context)
{
    MPQ_Object *tempx = NULL, *tempy = NULL, *rem = NULL;
    MPZ_Object *quo = NULL;
    PyObject *result = NULL;

    if (!(result = PyTuple_New(2)) ||
        !(rem = GMPy_MPQ_New(context)) ||
        !(quo = GMPy_MPZ_New(context))) {

        /* LCOV_EXCL_START */
        goto error;
        /* LCOV_EXCL_STOP */
    }

    if (IS_RATIONAL(x) && IS_RATIONAL(y)) {

        if (!(tempx = GMPy_MPQ_From_Number(x, context)) ||
            !(tempy = GMPy_MPQ_From_Number(y, context))) {

            /* LCOV_EXCL_START */
            goto error;
            /* LCOV_EXCL_STOP */
        }
        if (mpq_sgn(tempy->q) == 0) {
            ZERO_ERROR("division or modulo by zero");
            goto error;
        }

        mpq_div(rem->q, tempx->q, tempy->q);
        mpz_fdiv_q(quo->z, mpq_numref(rem->q), mpq_denref(rem->q));
        /* Need to calculate x - quo * y. */
        mpq_set_z(rem->q, quo->z);
        mpq_mul(rem->q, rem->q, tempy->q);
        mpq_sub(rem->q, tempx->q, rem->q);
        Py_DECREF((PyObject*)tempx);
        Py_DECREF((PyObject*)tempy);
        PyTuple_SET_ITEM(result, 0, (PyObject*)quo);
        PyTuple_SET_ITEM(result, 1, (PyObject*)rem);
        return result;
    }

    /* LCOV_EXCL_START */
    SYSTEM_ERROR("Internal error in GMPy_Rational_DivMod().");
  error:
    Py_XDECREF((PyObject*)tempx);
    Py_XDECREF((PyObject*)tempy);
    Py_XDECREF((PyObject*)rem);
    Py_XDECREF((PyObject*)quo);
    Py_XDECREF(result);
    return NULL;
    /* LCOV_EXCL_STOP */
}
Пример #11
0
	Rational operator/(const Rational& num1, const Rational& num2)
	{
		Rational res;

		if (num2 == 0)
		{
		//	throw MathException(MATH_ERROR_OVERFLOW);
		}

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

		return res;
	}
Пример #12
0
obj rational_div( obj a, obj b )
{
    mpq_t r, a1, b1;

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

    if (mpq_sgn( b1 ) == 0) {
        scheme_error( "dividing ~s by zero", 1, a );
    }

    mpq_init(r);
    mpq_div(r, a1, b1);
    return rational_compact(r);
}
Пример #13
0
Файл: ovm_mpq.c Проект: pcpa/owl
void
ovm_q_rem(oregister_t *l, oregister_t *r)
{
    switch (r->t) {
	case t_void:
	     ovm_raise(except_floating_point_error);
	case t_word:
	    if (r->v.w == 0)
		ovm_raise(except_floating_point_error);
	    if (r->v.w > 0)
		mpz_mul_ui(ozr(r), ozs(l), r->v.w);
	    else {
		mpz_set_si(ozr(r), r->v.w);
		mpz_mul(ozr(r), ozs(l), ozr(r));
	    }
	    mpz_tdiv_r(ozr(l), ozr(l), ozr(r));
	    mpq_canonicalize(oqr(l));
	    check_mpq(l);
	    break;
	case t_float:
	    l->t = t_float;
	    l->v.d = fmod(mpq_get_d(oqr(l)), r->v.d);
	    break;
	case t_mpz:
	    mpz_mul(ozr(r), ozs(l), ozr(r));
	    mpz_tdiv_r(ozr(l), ozr(l), ozr(r));
	    check_mpq(l);
	    break;
	case t_rat:
	    mpq_set_si(oqr(r), rat_num(r->v.r), rat_den(r->v.r));
	case t_mpq:
	    mpq_div(oqr(l), oqr(l), oqr(r));
	    mpz_tdiv_r(ozr(l), ozr(l), ozs(l));
	    mpz_mul(ozs(l), ozs(l), ozs(r));
	    mpq_canonicalize(oqr(l));
	    if (mpq_sgn(oqr(r)) < 0)
		mpq_neg(oqr(l), oqr(l));
	    check_mpq(l);
	    break;
	case t_mpr:
	    l->t = t_mpr;
	    mpfr_set_q(orr(l), oqr(l), thr_rnd);
	    mpfr_fmod(orr(l), orr(l), orr(r), thr_rnd);
	    break;
	 default:
	     ovm_raise(except_not_a_real_number);
    }
}
Пример #14
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;
}
Пример #15
0
static PyObject *
GMPy_Rational_Mod(PyObject *x, PyObject *y, CTXT_Object *context)
{
    mpz_t tempz;
    MPQ_Object *tempx, *tempy, *result;

    CHECK_CONTEXT(context);

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

    if (IS_RATIONAL(x) && IS_RATIONAL(y)) {
        tempx = GMPy_MPQ_From_Number(x, context);
        tempy = GMPy_MPQ_From_Number(y, context);
        if (!tempx || !tempy) {
            SYSTEM_ERROR("could not convert Rational to mpq");
            goto error;
        }
        if (mpq_sgn(tempy->q) == 0) {
            ZERO_ERROR("division or modulo by zero");
            goto error;
        }

        mpz_inoc(tempz);
        mpq_div(result->q, tempx->q, tempy->q);
        mpz_fdiv_q(tempz, mpq_numref(result->q), mpq_denref(result->q));
        /* Need to calculate x - tempz * y. */
        mpq_set_z(result->q, tempz);
        mpq_mul(result->q, result->q, tempy->q);
        mpq_sub(result->q, tempx->q, result->q);
        mpz_cloc(tempz);
        Py_DECREF((PyObject*)tempx);
        Py_DECREF((PyObject*)tempy);
        return (PyObject*)result;
    }

    Py_DECREF((PyObject*)result);
    Py_RETURN_NOTIMPLEMENTED;

  error:
    Py_XDECREF((PyObject*)tempx);
    Py_XDECREF((PyObject*)tempy);
    Py_DECREF((PyObject*)result);
    return NULL;
}
Пример #16
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;
}
Пример #17
0
Файл: ovm_mpq.c Проект: pcpa/owl
void
ovm_q_trunc2(oregister_t *l, oregister_t *r)
{
    switch (r->t) {
	case t_void:
	     ovm_raise(except_floating_point_error);
	case t_word:
	    if (r->v.w == 0)
		ovm_raise(except_floating_point_error);
	    l->t = t_mpz;
	    if (r->v.w > 0)
		mpz_mul_ui(ozr(r), ozs(l), r->v.w);
	    else {
		mpz_set_si(ozr(r), r->v.w);
		mpz_mul(ozr(r), ozs(l), ozr(r));
	    }
	    mpz_tdiv_q(ozr(l), ozr(l), ozr(r));
	    check_mpz(l);
	    break;
	case t_float:
	    ovm_trunc_d(l, mpq_get_d(oqr(l)) / r->v.d);
	    break;
	case t_mpz:
	    mpz_tdiv_q(ozr(l), ozr(l), ozr(r));
	    check_mpz(l);
	    break;
	case t_rat:
	    mpq_set_si(oqr(r), rat_num(r->v.r), rat_den(r->v.r));
	case t_mpq:
	    l->t = t_mpz;
	    mpq_div(oqr(l), oqr(l), oqr(r));
	    mpz_tdiv_q(ozr(l), ozr(l), ozs(l));
	    check_mpz(l);
	    break;
	case t_mpr:
	    mpfr_set_q(orr(l), oqr(l), thr_rnd);
	    ovm_trunc_r(l, orr(r));
	    break;
	 default:
	     ovm_raise(except_not_a_real_number);
    }
}
Пример #18
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);
}
Пример #19
0
int mpq_mat_is_reduced(mpq_mat_t mu, mpq_mat_t GS, double delta, double eta){

   //want to return 1 if this data could come from a reduced matrix 0 otherwise

   mpq_mat_t gs_len;
   mpq_mat_init( gs_len, 1, GS->c);

   mpq_t temp, temp1, temp2;
   mpq_init(temp);
   mpq_init(temp1);
   mpq_init(temp2);

   long i, j;
   int result = 1;
   for (i = 0; (i < GS->r) && (result == 1); i++){
      mpq_mat_row_inner_product(gs_len->entries[i], GS, i, GS, i);
      if (i > 0){
         mpq_div(temp, gs_len->entries[i], gs_len->entries[i-1]);
         mpq_mul(temp1, mu->entries[i*mu->r + i-1], mu->entries[i*mu->r + i-1]); 
         mpq_add(temp, temp, temp1);
         mpq_set_d(temp1, delta - eta*eta);
         if (mpq_cmp(temp, temp1) < 0){
            result = 0;
         }
         else{
            mpq_set_d(temp2, eta);
            for( j = 0 ; (j < i) && (result == 1); j++)
               if ( mpq_cmp( mu->entries[i*mu->r + j], temp2) >= 0){
                  result = 0;
               }
         }
// if temp < (3/4 or 1/(delta - eta^2))==temp1 then not reduced...
      }
   }

   mpq_clear(temp);
   mpq_clear(temp1);
   mpq_clear(temp2);
   mpq_mat_clear(gs_len);
   return result;
}
Пример #20
0
static PyObject *
Pympq_div(PyObject *self, PyObject *args)
{
    PympqObject *result;
    PyObject *other;

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

    if ((result = (PympqObject*)Pympq_new())) {
        if (mpq_sgn(Pympq_AS_MPQ(other)) == 0) {
            ZERO_ERROR("'mpq' division by zero");
            Py_DECREF((PyObject*)result);
            result = 0;
        }
        else {
            mpq_div(result->q, Pympq_AS_MPQ(self), Pympq_AS_MPQ(other));
        }
    }

    Py_DECREF(self);
    Py_DECREF(other);
    return (PyObject*)result;
}
Пример #21
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);
}
Пример #22
0
static void eliminate(LUX *lux, LUXWKA *wka, LUXELM *piv, int flag[],
      mpq_t work[])
{     DMP *pool = lux->pool;
      LUXELM **F_row = lux->F_row;
      LUXELM **F_col = lux->F_col;
      mpq_t *V_piv = lux->V_piv;
      LUXELM **V_row = lux->V_row;
      LUXELM **V_col = lux->V_col;
      int *R_len = wka->R_len;
      int *R_head = wka->R_head;
      int *R_prev = wka->R_prev;
      int *R_next = wka->R_next;
      int *C_len = wka->C_len;
      int *C_head = wka->C_head;
      int *C_prev = wka->C_prev;
      int *C_next = wka->C_next;
      LUXELM *fip, *vij, *vpj, *viq, *next;
      mpq_t temp;
      int i, j, p, q;
      mpq_init(temp);
      /* determine row and column indices of the pivot v[p,q] */
      xassert(piv != NULL);
      p = piv->i, q = piv->j;
      /* remove p-th (pivot) row from the active set; it will never
         return there */
      if (R_prev[p] == 0)
         R_head[R_len[p]] = R_next[p];
      else
         R_next[R_prev[p]] = R_next[p];
      if (R_next[p] == 0)
         ;
      else
         R_prev[R_next[p]] = R_prev[p];
      /* remove q-th (pivot) column from the active set; it will never
         return there */
      if (C_prev[q] == 0)
         C_head[C_len[q]] = C_next[q];
      else
         C_next[C_prev[q]] = C_next[q];
      if (C_next[q] == 0)
         ;
      else
         C_prev[C_next[q]] = C_prev[q];
      /* store the pivot value in a separate array */
      mpq_set(V_piv[p], piv->val);
      /* remove the pivot from p-th row */
      if (piv->r_prev == NULL)
         V_row[p] = piv->r_next;
      else
         piv->r_prev->r_next = piv->r_next;
      if (piv->r_next == NULL)
         ;
      else
         piv->r_next->r_prev = piv->r_prev;
      R_len[p]--;
      /* remove the pivot from q-th column */
      if (piv->c_prev == NULL)
         V_col[q] = piv->c_next;
      else
         piv->c_prev->c_next = piv->c_next;
      if (piv->c_next == NULL)
         ;
      else
         piv->c_next->c_prev = piv->c_prev;
      C_len[q]--;
      /* free the space occupied by the pivot */
      mpq_clear(piv->val);
      dmp_free_atom(pool, piv, sizeof(LUXELM));
      /* walk through p-th (pivot) row, which already does not contain
         the pivot v[p,q], and do the following... */
      for (vpj = V_row[p]; vpj != NULL; vpj = vpj->r_next)
      {  /* get column index of v[p,j] */
         j = vpj->j;
         /* store v[p,j] in the working array */
         flag[j] = 1;
         mpq_set(work[j], vpj->val);
         /* remove j-th column from the active set; it will return there
            later with a new length */
         if (C_prev[j] == 0)
            C_head[C_len[j]] = C_next[j];
         else
            C_next[C_prev[j]] = C_next[j];
         if (C_next[j] == 0)
            ;
         else
            C_prev[C_next[j]] = C_prev[j];
         /* v[p,j] leaves the active submatrix, so remove it from j-th
            column; however, v[p,j] is kept in p-th row */
         if (vpj->c_prev == NULL)
            V_col[j] = vpj->c_next;
         else
            vpj->c_prev->c_next = vpj->c_next;
         if (vpj->c_next == NULL)
            ;
         else
            vpj->c_next->c_prev = vpj->c_prev;
         C_len[j]--;
      }
      /* now walk through q-th (pivot) column, which already does not
         contain the pivot v[p,q], and perform gaussian elimination */
      while (V_col[q] != NULL)
      {  /* element v[i,q] has to be eliminated */
         viq = V_col[q];
         /* get row index of v[i,q] */
         i = viq->i;
         /* remove i-th row from the active set; later it will return
            there with a new length */
         if (R_prev[i] == 0)
            R_head[R_len[i]] = R_next[i];
         else
            R_next[R_prev[i]] = R_next[i];
         if (R_next[i] == 0)
            ;
         else
            R_prev[R_next[i]] = R_prev[i];
         /* compute gaussian multiplier f[i,p] = v[i,q] / v[p,q] and
            store it in the matrix F */
         fip = dmp_get_atom(pool, sizeof(LUXELM));
         fip->i = i, fip->j = p;
         mpq_init(fip->val);
         mpq_div(fip->val, viq->val, V_piv[p]);
         fip->r_prev = NULL;
         fip->r_next = F_row[i];
         fip->c_prev = NULL;
         fip->c_next = F_col[p];
         if (fip->r_next != NULL) fip->r_next->r_prev = fip;
         if (fip->c_next != NULL) fip->c_next->c_prev = fip;
         F_row[i] = F_col[p] = fip;
         /* v[i,q] has to be eliminated, so remove it from i-th row */
         if (viq->r_prev == NULL)
            V_row[i] = viq->r_next;
         else
            viq->r_prev->r_next = viq->r_next;
         if (viq->r_next == NULL)
            ;
         else
            viq->r_next->r_prev = viq->r_prev;
         R_len[i]--;
         /* and also from q-th column */
         V_col[q] = viq->c_next;
         C_len[q]--;
         /* free the space occupied by v[i,q] */
         mpq_clear(viq->val);
         dmp_free_atom(pool, viq, sizeof(LUXELM));
         /* perform gaussian transformation:
            (i-th row) := (i-th row) - f[i,p] * (p-th row)
            note that now p-th row, which is in the working array,
            does not contain the pivot v[p,q], and i-th row does not
            contain the element v[i,q] to be eliminated */
         /* walk through i-th row and transform existing non-zero
            elements */
         for (vij = V_row[i]; vij != NULL; vij = next)
         {  next = vij->r_next;
            /* get column index of v[i,j] */
            j = vij->j;
            /* v[i,j] := v[i,j] - f[i,p] * v[p,j] */
            if (flag[j])
            {  /* v[p,j] != 0 */
               flag[j] = 0;
               mpq_mul(temp, fip->val, work[j]);
               mpq_sub(vij->val, vij->val, temp);
               if (mpq_sgn(vij->val) == 0)
               {  /* new v[i,j] is zero, so remove it from the active
                     submatrix */
                  /* remove v[i,j] from i-th row */
                  if (vij->r_prev == NULL)
                     V_row[i] = vij->r_next;
                  else
                     vij->r_prev->r_next = vij->r_next;
                  if (vij->r_next == NULL)
                     ;
                  else
                     vij->r_next->r_prev = vij->r_prev;
                  R_len[i]--;
                  /* remove v[i,j] from j-th column */
                  if (vij->c_prev == NULL)
                     V_col[j] = vij->c_next;
                  else
                     vij->c_prev->c_next = vij->c_next;
                  if (vij->c_next == NULL)
                     ;
                  else
                     vij->c_next->c_prev = vij->c_prev;
                  C_len[j]--;
                  /* free the space occupied by v[i,j] */
                  mpq_clear(vij->val);
                  dmp_free_atom(pool, vij, sizeof(LUXELM));
               }
            }
         }
         /* now flag is the pattern of the set v[p,*] \ v[i,*] */
         /* walk through p-th (pivot) row and create new elements in
            i-th row, which appear due to fill-in */
         for (vpj = V_row[p]; vpj != NULL; vpj = vpj->r_next)
         {  j = vpj->j;
            if (flag[j])
            {  /* create new non-zero v[i,j] = 0 - f[i,p] * v[p,j] and
                  add it to i-th row and j-th column */
               vij = dmp_get_atom(pool, sizeof(LUXELM));
               vij->i = i, vij->j = j;
               mpq_init(vij->val);
               mpq_mul(vij->val, fip->val, work[j]);
               mpq_neg(vij->val, vij->val);
               vij->r_prev = NULL;
               vij->r_next = V_row[i];
               vij->c_prev = NULL;
               vij->c_next = V_col[j];
               if (vij->r_next != NULL) vij->r_next->r_prev = vij;
               if (vij->c_next != NULL) vij->c_next->c_prev = vij;
               V_row[i] = V_col[j] = vij;
               R_len[i]++, C_len[j]++;
            }
            else
            {  /* there is no fill-in, because v[i,j] already exists in
                  i-th row; restore the flag, which was reset before */
               flag[j] = 1;
            }
         }
         /* now i-th row has been completely transformed and can return
            to the active set with a new length */
         R_prev[i] = 0;
         R_next[i] = R_head[R_len[i]];
         if (R_next[i] != 0) R_prev[R_next[i]] = i;
         R_head[R_len[i]] = i;
      }
      /* at this point q-th (pivot) column must be empty */
      xassert(C_len[q] == 0);
      /* walk through p-th (pivot) row again and do the following... */
      for (vpj = V_row[p]; vpj != NULL; vpj = vpj->r_next)
      {  /* get column index of v[p,j] */
         j = vpj->j;
         /* erase v[p,j] from the working array */
         flag[j] = 0;
         mpq_set_si(work[j], 0, 1);
         /* now j-th column has been completely transformed, so it can
            return to the active list with a new length */
         C_prev[j] = 0;
         C_next[j] = C_head[C_len[j]];
         if (C_next[j] != 0) C_prev[C_next[j]] = j;
         C_head[C_len[j]] = j;
      }
      mpq_clear(temp);
      /* return to the factorizing routine */
      return;
}
Пример #23
0
void mpq_mat_GS(mpq_mat_t mu, mpq_mat_t GS, mpq_mat_t mat){

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

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

  //I'm going to use mu[0] to store <GS[i], GS[i]> until the end

   mpq_t temp;
   mpq_init(temp);

   long i, j, k;
   //setting GS[0] := mat[0]
   for (i = 0; i < mat->c; i++)
      mpq_set(GS->entries[i], mat->entries[i]);

   mpq_mat_row_inner_product(mu->entries[0], GS, 0, GS, 0);

   //mu[i,i] := 1
   for (i = 1; i < mu->r; i++)
      mpq_set_ui(mu->entries[i*mu->c + i], 1L, 1L);

   for (i = 1; i < mat->r; i++){
      //in this loop we want to find GS[i] := mat[i] - sum (mu[i,j]*mat[j])
      //start with GS[i] = mat[i] then for each j < i compute mu and subtract
      for (k = 0; k < mat->c; k++)
         mpq_set(GS->entries[i*mat->c + k], mat->entries[i*mat->c + k]);

      for (j = 0; j < i; j++){
         //temp will be the numerator of mu[i,j] which is <mat[i], GS[j]>
         mpq_mat_row_inner_product(temp, mat, i, GS, j);

         if (mpq_sgn(mu->entries[j]) != 0)
            mpq_div(mu->entries[i*mu->c + j], temp, mu->entries[j]);
         else
            mpq_set_ui(mu->entries[i*mu->c + j], 0L, 1L);

         //now need GS[i] := GS[i] - mu[i,j]*GS[j]
         for (k=0; k < mat->c; k++){
            //temp = mu[i,j] * GS[j,k]
            mpq_mul(temp, mu->entries[i*mu->c + j], GS->entries[j*GS->c + k]);
            mpq_neg(temp, temp);
            mpq_add(GS->entries[i*GS->c + k], GS->entries[i*GS->c + k], temp);
         }
      }
      if (i+1 < mu->c) 
         mpq_mat_row_inner_product(mu->entries[i], GS, i, GS, i);
   }

   mpq_set_ui(mu->entries[0], 1L, 1L);
   for (k = 1; k < mu->c; k++)
      mpq_set_ui(mu->entries[k], 0L, 1L);

   mpq_clear(temp);
   return;
}
Пример #24
0
static int
bar_number(mpq_t *now, mpq_t remain)
{
    int         n = 0;
    symbol_p    scan;
    mpq_t       t;
    mpq_t       t2;
    mpq_t       start;
    mpq_t       end;
    mpq_t       num;

    mpq_init(t);
    mpq_init(t2);
    mpq_init(start);
    mpq_init(end);
    mpq_init(num);

    mpq_set_si(start, 0, 1);

    if (time_line == NULL) {
        time_line = symbol_create(start);
        time_line->type = SYM_TIME_SIGNATURE;
        mpq_init(time_line->symbol.time_signature.duration);
        mpq_set_si(time_line->symbol.time_signature.duration, 1, 1);
        time_line->next = NULL;
    }
    scan = time_line;
    
    if (scan->next == NULL) {
        mpq_set(end, *now);
    } else {
        mpq_set(end, scan->next->start);
    }
    mpq_set(remain, *now);
    mpq_sub(remain, remain, xly_t_partial);
    VPRINTF("now = "); VPRINT_MPQ(*now);
    VPRINTF("\n start = "); VPRINT_MPQ(start);
    VPRINTF("\n end = "); VPRINT_MPQ(end);
    VPRINTF("\n remain = "); VPRINT_MPQ(remain);
    VPRINTF("\n");

    while (scan != NULL && mpq_cmp(*now, end) >= 0) {
        int nu;
        int de;

        VPRINTF("\n now = "); VPRINT_MPQ(*now);
        VPRINTF("\n end = "); VPRINT_MPQ(end);

        mpq_sub(t, end, start);
        VPRINTF("\n t = "); VPRINT_MPQ(t);
        mpq_set(t2, t);
        mpq_div(t, t2, scan->symbol.time_signature.duration);
        VPRINTF("\n t = "); VPRINT_MPQ(t);
        mpq2rat(t, &nu, &de);
        VPRINTF("\n t = "); VPRINT_MPQ(t);
        n += nu / de;   /* Discount partial bars */
        mpq_set_si(num, n, 1);
        mpq_mul(t, scan->symbol.time_signature.duration, num);
        mpq_sub(remain, remain, t);
        VPRINTF("\n remain = "); VPRINT_MPQ(remain);
        VPRINTF("\n");

        mpq_set(start, end);
        if (scan->next == NULL || scan->next->next == NULL) {
            mpq_set(end, *now);
        } else {
            mpq_set(end, scan->next->next->start);
        }
        scan = scan->next;
    }

    mpq_clear(t);
    mpq_clear(start);
    mpq_clear(end);
    mpq_clear(num);

    return n;
}
Пример #25
0
int main (int argc, char **argv){

    mp_int num1,den1,num2,den2;
    mp_rat q1,q2,q3,q4;
    //mp_rat *bernoulli;
    int i;
    clock_t start,stop;

    if (argc < 5) {
	fprintf(stderr, "usage: %s integer integer integer integer \n", argv[0]);
	exit(EXIT_FAILURE);
    }

    mp_init_multi(&num1,&den1,&num2,&den2,NULL);

    mp_get_str(argv[1], &num1, 10);
    mp_get_str(argv[2], &den1, 10);
    mp_get_str(argv[3], &num2, 10);
    mp_get_str(argv[4], &den2, 10);

    printf("Numerator   1: ");mp_print(&num1);puts("");
    printf("Denominator 1: ");mp_print(&den1);puts("");
    printf("Numerator   2: ");mp_print(&num2);puts("");
    printf("Denominator 2: ");mp_print(&den2);puts("");

    mpq_init_multi(&q1,&q2,&q3,&q4,NULL);puts("000");

    mpq_set(&q1,&num1,&den1);puts("111");
    printf("Rational1: ");mpq_print(&q1);puts("");
    mpq_set(&q2,&num2,&den2);puts("222");
    printf("Rational2: ");mpq_print(&q2);puts("");

    mpq_add(&q1,&q2,&q3);;
    printf("R1 + R2 = ");mpq_print(&q3);puts("");
    mpq_sub(&q1,&q2,&q3);
    printf("R1 - R2 = ");mpq_print(&q3);puts("");
    mpq_mul(&q1,&q2,&q3);
    printf("R1 * R2 = ");mpq_print(&q3);puts("");
    mpq_div(&q1,&q2,&q3);
    printf("R1 / R2 = ");mpq_print(&q3);puts("");
    //mpq_pow_d(&q1,123,&q3);
    printf("R1 ^ 123 = ");mpq_print(&q3);puts("");
    printf("cmp(R1, R2) = %d\n",mpq_cmp(&q1,&q2));
    printf("cmp(R2, R1) = %d\n",mpq_cmp(&q2,&q1));
    printf("cmp(R1, R1) = %d\n",mpq_cmp(&q1,&q1));
    mp_set_int(&num2,123);
    //mp_expt(&num1,&num2,&num1);
    printf("num1 ^123 = ");mp_fwrite(&num1,10,stdout);puts("");
    mpq_set_epsilon(50);
//mpq_set_int(&q1,128,2);
//mpq_set_int(&q1,529,1849);
//mpq_set_int(&q1,3481,11664);
//mpq_set_int(&q1,1764,1849);
mpq_set_int(&q1,44521,46656);
    mpq_sqrt(&q1,&q1);
    printf("sqrt(R1) = ");mpq_print(&q1);puts("");
    //SeidelBernoulli(20);
    //bernoulli = malloc(sizeof(mp_rat) * 102);
    start = clock();
    //bern_rat_init(10);
    //bhbern(50);
    stop = clock();
    puts("bernoulli");
    for(i=0;i<=(50);i++){
      //mpq_print(&bern_array[i]);printf("  %d\n",i);
    }
    mpq_bernoulli(10,&q1);
    printf("B_10 = ");mpq_print(&q1);puts("");
    mpq_bernoulli(4,&q1);
    printf("B_4 = ");mpq_print(&q1);puts("");
    mpq_bernoulli(0,&q1);
    printf("B_0 = ");mpq_print(&q1);puts("");
    mpq_bernoulli(1,&q1);
    printf("B_1 = ");mpq_print(&q1);puts("");
    mpq_bernoulli(2,&q1);
    printf("B_3 = ");mpq_print(&q1);puts("");
    mpq_bernoulli(100,&q1);
    printf("B_100 = ");mpq_print(&q1);puts("");
    mpq_bernoulli(98,&q1);
    printf("B_98 = ");mpq_print(&q1);puts("");
    printf("Time: %6.6f\n",( (double)stop - (double)start )/((double)CLOCKS_PER_SEC)  );
    mpq_bernoulli_free();
    exit(EXIT_SUCCESS);
}
Пример #26
0
double
hyperd(N, K, n, k)
{
	static mpq_t num, num2, den, q;
	static mpq_t p2, t;
	static mpf_t f;
	static int first = 1;
	double d;
	double p;

	if (first) {
		mpq_init(num);
		mpq_init(num2);
		mpq_init(den);
		mpq_init(q);
		mpf_init(f);
		mpq_init(p2);
		mpq_init(t);
		first = 0;
	}

	switch (hyperd_mode) {
	case 0:
		mP(&num, K, k);

		mP(&p2, N - K, n - k);
		mpq_mul(num2, num, p2);

		mH(&p2, k + 1, n - k);
		mpq_mul(num, num2, p2);

		mP(&den, N, n);

		mpq_div(q, num, den);

		mpf_set_q(f, q);

		d = mpf_get_d(f);

		break;
	case 1:
	xx:

		mC(&num2, K, k);
		mC(&p2, N - K, n - k);
		mpq_mul(num, num2, p2);

		mC(&den, N, n);

		mpq_div(q, num, den);

		mpf_set_q(f, q);

		d = mpf_get_d(f);
		break;
	case 2:
		if (k == K || n - k == N - K || n == N) {
			goto xx;
		}

		if (k == 0 || n - k == 0 || n == 0) {
			goto xx;
		}

		p = (double)n / N;

		d = dB(K, p, k) * dB(N - K, p, n - k) / dB(N, p, n);

		if (d < 3e-14) {
			goto xx;
		}

		break;
	default:
		fprintf(stderr, "internal error\n");
		exit(1);
		/* NOTREACHED */
	}

	return d;
}
Пример #27
0
 ///@brief test doc
 bool divide(ElementType& result, const ElementType& a, const ElementType& b) const {
     if (is_zero(b)) return false;
     mpq_div(&result, &a, &b);
     return true;
 }
Пример #28
0
void generate_random_real_rational_vector(rational_complex_vector x, int size)
/***************************************************************\
* USAGE: generate a random real rational vector of unit length  *
\***************************************************************/
{ 
  // set x to the correct size & set to zero
  change_size_rational_vector(x, size);
  set_zero_rational_vector(x);

  if (size == 1)
  { // setup x to either 1 or -1
    mpq_set_si(x->coord[0]->re, rand() % 2 ? 1 : -1, 1);
  }
  else if (size > 1)
  { // setup x to real unit vector
    int i, base = 10;
    char *str = NULL;
    mpq_t sum_sqr, *t = (mpq_t *)errMalloc((size - 1) * sizeof(mpq_t)), *t_sqr = (mpq_t *)errMalloc((size - 1) * sizeof(mpq_t));

    // initialize and set to random numbers
    mpq_init(sum_sqr);
    mpq_set_ui(sum_sqr, 0, 1);
    for (i = 0; i < size - 1; i++)
    {
      mpq_init(t[i]);
      mpq_init(t_sqr[i]);
    
      create_random_number_str(&str);
      mpq_set_str(t[i], str, base);
      mpq_canonicalize(t[i]);

      mpq_mul(t_sqr[i], t[i], t[i]);
      mpq_add(sum_sqr, sum_sqr, t_sqr[i]);
    }

    // setup the first entry
    mpq_set_ui(x->coord[0]->re, 1, 1);
    mpq_add(x->coord[0]->im, x->coord[0]->re, sum_sqr); // 1 + sum(t^2)
    mpq_sub(sum_sqr, x->coord[0]->re, sum_sqr); // 1 - sum(t^2)
    mpq_div(x->coord[0]->re, sum_sqr, x->coord[0]->im); // (1 - sum(t^2)) / (1 + sum(t^2))
    mpq_set_ui(x->coord[0]->im, 0, 1);

    // compute x[0] + 1
    mpq_set_ui(sum_sqr, 1, 1);
    mpq_add(sum_sqr, sum_sqr, x->coord[0]->re);

    // setup other entries: t[i] * (x[0] + 1)
    for (i = 1; i < size; i++)
      mpq_mul(x->coord[i]->re, t[i-1], sum_sqr);

    // clear memory
    mpq_clear(sum_sqr);
    for (i = 0; i < size - 1; i++)
    {
      mpq_clear(t[i]);
      mpq_clear(t_sqr[i]);
    }
    free(t);
    free(t_sqr);
    free(str);
  }

  return;
}
Пример #29
0
AlkValue & AlkValue::operator/=(const AlkValue & right)
{
  mpq_div(d->m_val.get_mpq_t(), d->m_val.get_mpq_t(), right.d->m_val.get_mpq_t());
  d->m_val.canonicalize();
  return *this;
}
Пример #30
0
static PyObject *
GMPy_MPQ_Factory(PyObject *self, PyObject *args, PyObject *keywds)
{
    MPQ_Object *result, *temp;
    PyObject *n, *m;
    int base = 10;
    Py_ssize_t argc, keywdc = 0;
    static char *kwlist[] = {"s", "base", NULL };
    CTXT_Object *context = NULL;

    if (self && CTXT_Check(self)) {
        context = (CTXT_Object*)self;
    }
    else {
        CHECK_CONTEXT(context);
    }

    argc = PyTuple_Size(args);
    if (keywds) {
        keywdc = PyDict_Size(keywds);
    }

    if (argc + keywdc > 2) {
        TYPE_ERROR("mpq() takes at most 2 arguments");
        return NULL;
    }

    if (argc + keywdc == 0) {
        if ((result = GMPy_MPQ_New(context))) {
            mpq_set_ui(result->q, 0, 1);
        }
        return (PyObject*)result;
    }

    if (argc == 0) {
        TYPE_ERROR("mpq() requires at least one non-keyword argument");
        return NULL;
    }

    n = PyTuple_GetItem(args, 0);

    /* Handle the case where the first argument is a string. */
    if (PyStrOrUnicode_Check(n)) {
        /* keyword base is legal */
        if (keywdc || argc > 1) {
            if (!(PyArg_ParseTupleAndKeywords(args, keywds, "O|i", kwlist, &n, &base))) {
                return NULL;
            }
        }

        if ((base != 0) && ((base < 2) || (base > 62))) {
            VALUE_ERROR("base for mpq() must be 0 or in the interval [2, 62]");
            return NULL;
        }

        return (PyObject*)GMPy_MPQ_From_PyStr(n, base, context);
    }

    /* Handle 1 argument. It must be non-complex number. */
    if (argc == 1) {
        if (IS_REAL(n)) {
            return (PyObject*)GMPy_MPQ_From_Number(n, context);
        }
    }

    /* Handle 2 arguments. Both arguments must be integer or rational. */
    if (argc == 2) {
        m = PyTuple_GetItem(args, 1);

        if (IS_RATIONAL(n) && IS_RATIONAL(m)) {
           result = GMPy_MPQ_From_Rational(n, context);
           temp = GMPy_MPQ_From_Rational(m, context);
           if (!result || !temp) {
               Py_XDECREF((PyObject*)result);
               Py_XDECREF((PyObject*)temp);
               return NULL;
            }

            if (mpq_sgn(temp->q) == 0) {
                ZERO_ERROR("zero denominator in mpq()");
                Py_DECREF((PyObject*)result);
                Py_DECREF((PyObject*)temp);
                return NULL;
            }

            mpq_div(result->q, result->q, temp->q);
            Py_DECREF((PyObject*)temp);
            return (PyObject*)result;
        }
    }

    TYPE_ERROR("mpq() requires numeric or string argument");
    return NULL;
}