示例#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
void lp_arrange_inequality2(LP* lp) {
  int  i, j, col;

  lp_arrange_inequality(lp);

  for (i = 0; i < lp->rows; i ++) {
    if (lp->row_equality[i] != LP_EQUALITY_LE)
      continue;
    for (j = 0; j < lp->A->row[i]->nonzeros; j ++) {
      col = lp->A->row[i]->i[j];
      if (is_const_var(lp, col))
        continue;
      if (lp->A->column[col]->nonzeros != 1)
        continue;
      if (mpq_sgn(*matrix_get_element_ptr(lp->A, i, col)) < 0)
        continue;
      if (mpq_sgn(*vector_get_element_ptr(lp->c, col)) < 0)
        continue;
      if (!lp->lower.is_valid[col] || mpq_sgn(lp->lower.bound[col]) < 0)
        continue;
      if (lp->upper.is_valid[col])
        continue;
      lp->row_equality[i] = LP_EQUALITY_EQ;
      putchar('_');
      break;
    }
  }
}
示例#3
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;
}
示例#4
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;
}
示例#5
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;
}
示例#6
0
void fmpq_poly_scalar_div_mpq(fmpq_poly_t rop, const fmpq_poly_t op, const mpq_t c)
{
    fmpz_t r, s;

    if (mpq_sgn(c) == 0)
    {
        printf("Exception: division by zero in fmpq_poly_scalar_div_mpq\n");
        abort();
    }
    
    if (fmpq_poly_is_zero(op))
    {
        fmpq_poly_zero(rop);
        return;
    }
    
    fmpq_poly_fit_length(rop, op->length);
    _fmpq_poly_set_length(rop, op->length);
    
    fmpz_init(r);
    fmpz_init(s);
    fmpz_set_mpz(r, mpq_numref(c));
    fmpz_set_mpz(s, mpq_denref(c));
    
    _fmpq_poly_scalar_div_mpq(rop->coeffs, rop->den, 
                              op->coeffs, op->den, op->length, r, s);
    
    fmpz_clear(r);
    fmpz_clear(s);
}
示例#7
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);
}
示例#8
0
文件: qsign.c 项目: cjgeyer/rcdd
SEXP qsign(SEXP foo)
{
    if (! isString(foo))
        error("argument must be character");
    int n = LENGTH(foo);

    SEXP bar, bark;
    PROTECT(bar = allocVector(INTSXP, n));
    PROTECT(bark = ATTRIB(foo));
    if (bark != R_NilValue)
        SET_ATTRIB(bar, duplicate(bark));
    UNPROTECT(1);

    mpq_t value;
    mpq_init(value);

    for (int k = 0; k < n; k++) {
        const char *zstr = CHAR(STRING_ELT(foo, k));
        if (mpq_set_str(value, zstr, 10) == -1) {
            mpq_clear(value);
            error("error converting string to GMP rational");
        }
        mpq_canonicalize(value);
        INTEGER(bar)[k] = mpq_sgn(value);
    }

    mpq_clear(value);
    UNPROTECT(1);
    return(bar);
}
示例#9
0
文件: QQ.cpp 项目: ChristineJost/M2
void QQ::elem_text_out(buffer &o,
                           const ring_elem ap,
                           bool p_one,
                           bool p_plus,
                           bool p_parens) const
{
  gmp_QQ a = MPQ_VAL(ap);

  char s[1000];
  char *str;

  bool is_neg = (mpq_sgn(a) == -1);
  bool is_one = (mask_mpq_cmp_si(a, 1, 1) == 0 || mask_mpq_cmp_si(a, -1, 1) == 0);

  int size = static_cast<int>(mpz_sizeinbase (mpq_numref(a), 10)
                              + mpz_sizeinbase (mpq_denref(a), 10) + 3);

  char *allocstr = (size > 1000 ? newarray_atomic(char,size) : s);

  if (!is_neg && p_plus) o << '+';
  if (is_one)
    {
      if (is_neg) o << '-';
      if (p_one) o << '1';
    }
  else
    {
      str = mpq_get_str(allocstr, 10, a);
      o << str;
    }
  if (size > 1000) deletearray(allocstr);
}
示例#10
0
文件: QQ.cpp 项目: ChristineJost/M2
bool QQ::lower_associate_divisor(ring_elem &f, const ring_elem g) const
{
  gmp_QQ a = MPQ_VAL(f);
  gmp_QQ b = MPQ_VAL(g);
  int sa = mpq_sgn(a);
  int sb = mpq_sgn(b);
  int s = (sa == 0 ? sb : sa);
  gmp_QQ result = QQ::new_elem();

  mpz_gcd(mpq_numref(result), mpq_numref(a), mpq_numref(b));
  mpz_lcm(mpq_denref(result), mpq_denref(a), mpq_denref(b));
  if (s != mpq_sgn(result))
    mpq_neg(result,result);
  f = MPQ_RINGELEM(result);
  return true; // the answer could become lower, if a newer g has a larger denom
}
示例#11
0
inline Mtbdd
readMPQAttribute(const TiXmlNode* node, const char* att)
{
    const std::string numberString = readStringAttribute(node, att);
    mpq_t gmp_value;
    mpq_init(gmp_value);
    try {
        size_t pos = 0;
        if ((pos = numberString.find('.')) != std::string::npos) {
            mpf_t f_value;
            mpf_init(f_value);
            mpf_set_str(f_value, numberString.c_str(), 10);
            mpq_set_f(gmp_value, f_value);
            mpf_clear(f_value);
        } else {
            mpq_set_str(gmp_value, numberString.c_str(), 10);
        }
        if (mpq_sgn(gmp_value) == 0) {
            mpq_clear(gmp_value);
            return mtbdd_false;
        }
        MTBDD res = mtbdd_gmp(gmp_value);
        mpq_clear(gmp_value);
        return res;
    } catch(boost::bad_lexical_cast&) {
        throw ParseError("[ERROR] String " + numberString + " is not a number");
    }
}
示例#12
0
void ssx_chuzc(SSX *ssx)
{     int m = ssx->m;
      int n = ssx->n;
      int dir = (ssx->dir == SSX_MIN ? +1 : -1);
      int *Q_col = ssx->Q_col;
      int *stat = ssx->stat;
      mpq_t *cbar = ssx->cbar;
      int j, k, s, q, q_dir;
      double best, temp;
      /* nothing is chosen so far */
      q = 0, q_dir = 0, best = 0.0;
      /* look through the list of non-basic variables */
      for (j = 1; j <= n; j++)
      {  k = Q_col[m+j]; /* x[k] = xN[j] */
         s = dir * mpq_sgn(cbar[j]);
         if ((stat[k] == SSX_NF || stat[k] == SSX_NL) && s < 0 ||
             (stat[k] == SSX_NF || stat[k] == SSX_NU) && s > 0)
         {  /* reduced cost of xN[j] indicates possible improving of
               the objective function */
            temp = fabs(mpq_get_d(cbar[j]));
            xassert(temp != 0.0);
            if (q == 0 || best < temp)
               q = j, q_dir = - s, best = temp;
         }
      }
      ssx->q = q, ssx->q_dir = q_dir;
      return;
}
示例#13
0
文件: ovm_mpq.c 项目: 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);
    }
}
示例#14
0
文件: QQ.cpp 项目: ChristineJost/M2
ring_elem QQ::preferred_associate(ring_elem f) const
{
  gmp_QQ a = MPQ_VAL(f);
  if (mpq_sgn(a) >= 0)
    return QQ::from_int(1);
  return QQ::from_int(-1);
}
示例#15
0
文件: lux.c 项目: TakeScoop/node-glpk
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;
}
示例#16
0
void lp_set_row_range(LP* lp, int row, mpq_t q) {
  mpq_t q2;
  int   var;

  mpq_init(q2);

  if(lp->row_name != NULL) {
    char buf[LP_NAME_LEN_MAX];
    buf[0] = '#'; buf[1] = 'R'; buf[2] = 0;
    strncat(buf, lp->row_name[row], LP_NAME_LEN_MAX-3);
    var = lp_add_var(lp, buf);
  }
  else
    var = lp_add_var(lp, NULL);
  lp_add_slackref(lp, row+1);
  vector_get_element(&q2, lp->b, row);
  lp->upper.is_valid[var] = TRUE;
  mpq_init(lp->upper.bound[var]);
  if (lp->row_equality[row] == LP_EQUALITY_GE) {
    if (mpq_sgn(q) < 0)
      mpq_neg(q, q);
    mpq_set(lp->lower.bound[var], q2);
    mympq_add(q2, q2, q);
    mpq_set(lp->upper.bound[var], q2);
  } else if (lp->row_equality[row] == LP_EQUALITY_LE) {
    if (mpq_sgn(q) < 0)
      mpq_neg(q, q);
    mpq_set(lp->upper.bound[var], q2);
    mympq_sub(q2, q2, q);
    mpq_set(lp->lower.bound[var], q2);
  } else if (mpq_sgn(q) > 0) {
    mpq_set(lp->lower.bound[var], q2);
    mympq_add(q2, q2, q);
    mpq_set(lp->upper.bound[var], q2);        
  } else {
    mpq_set(lp->upper.bound[var], q2);
    mympq_add(q2, q2, q);
    mpq_set(lp->lower.bound[var], q2);
  }
  lp_set_row_equality(lp, row, LP_EQUALITY_EQ);
  lp_set_coefficient(lp, mympq_minus_one, row, var);
  vector_delete_element(lp->b, row);

  mpq_clear(q2);
}
示例#17
0
文件: QQ.cpp 项目: ChristineJost/M2
void QQ::lower_content(ring_elem &c, const ring_elem g) const
{
  if (is_zero(c))
    {
      c = g;
      return;
    }
  gmp_QQ a = MPQ_VAL(c);
  gmp_QQ b = MPQ_VAL(g);
  int sa = mpq_sgn(a);
  gmp_QQ result = QQ::new_elem();

  mpz_gcd(mpq_numref(result), mpq_numref(a), mpq_numref(b));
  mpz_lcm(mpq_denref(result), mpq_denref(a), mpq_denref(b));
  if (sa != mpq_sgn(result))
    mpq_neg(result,result);
  c = MPQ_RINGELEM(result);
}
示例#18
0
void lp_add_slacks(LP* lp) {
  char buf[LP_NAME_LEN_MAX];
  int   i, j;

  for (i = 0; i < lp->vars; i ++) {
    if (is_const_var(lp, i) ||
        !vector_element_is_zero(lp->c, i) ||
        lp->A->column[i]->nonzeros != 1)
      continue;
    j = lp->A->column[i]->i[0];
    if (lp->row_equality[j] != LP_EQUALITY_LE)
      continue;
    if (mpq_sgn(*matrix_get_element_ptr(lp->A, j, i)) > 0 &&
        lp->upper.is_valid[i])
      continue;
    if (mpq_sgn(*matrix_get_element_ptr(lp->A, j, i)) < 0 &&
        lp->lower.is_valid[i])
      continue;
    lp->row_equality[j] = LP_EQUALITY_EQ;
  }

  j = 0;
  for (i = 0; i < lp->rows; i ++) {
    if (lp->row_equality[i] == LP_EQUALITY_LE)
      j ++;
  }
  matrix_resize(lp->A, lp->rows, lp->vars+j);

  for (i = 0; i < lp->rows; i ++) {
    if (lp->row_equality[i] == LP_EQUALITY_LE) {
      if(lp->row_name != NULL) {
        strncpy(buf, "#S", LP_NAME_LEN_MAX);
        strncat(buf, lp->row_name[i], LP_NAME_LEN_MAX);
        j = lp_add_var_without_A(lp, buf);
      }
      else
        j = lp_add_var_without_A(lp, NULL);
      lp_add_slackref(lp, i+1);
      lp->var_type[j] = LP_VAR_TYPE_SLACK;
      lp_set_coefficient(lp, mympq_one, i, j);
      lp->row_equality[i] = LP_EQUALITY_EQ;
    }
  }
}
示例#19
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;
}
示例#20
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 */
}
示例#21
0
static PyObject *
Pympq_sign(PyObject *self, PyObject *other)
{
    long res;
    PympqObject* tempx;

    if (Pympq_Check(other)) {
        res = mpq_sgn(Pympq_AS_MPQ(other));
    }
    else {
        if (!(tempx = Pympq_From_Number(other))) {
            TYPE_ERROR("sign() requires 'mpq' argument");
            return NULL;
        }
        else {
            res = mpq_sgn(tempx->q);
            Py_DECREF((PyObject*)tempx);
        }
    }
    return PyIntOrLong_FromLong(res);
}
示例#22
0
static void
check_nan_inf_mpq (void)
{
  mpfr_t mpfr_value, mpfr_cmp;
  mpq_t mpq_value;
  int status;

  mpfr_init2 (mpfr_value, MPFR_PREC_MIN);
  mpq_init (mpq_value);
  mpq_set_si (mpq_value, 0, 0);
  mpz_set_si (mpq_denref (mpq_value), 0);

  status = mpfr_set_q (mpfr_value, mpq_value, MPFR_RNDN);

  if ((status != 0) || (!MPFR_IS_NAN (mpfr_value)))
    {
      mpfr_init2 (mpfr_cmp, MPFR_PREC_MIN);
      mpfr_set_nan (mpfr_cmp);
      printf ("mpfr_set_q with a NAN mpq value returned a wrong value :\n"
              " waiting for ");
      mpfr_print_binary (mpfr_cmp);
      printf (" got ");
      mpfr_print_binary (mpfr_value);
      printf ("\n trinary value is %d\n", status);
      exit (1);
    }

  mpq_set_si (mpq_value, -1, 0);
  mpz_set_si (mpq_denref (mpq_value), 0);

  status = mpfr_set_q (mpfr_value, mpq_value, MPFR_RNDN);

  if ((status != 0) || (!MPFR_IS_INF (mpfr_value)) ||
      (MPFR_SIGN(mpfr_value) != mpq_sgn(mpq_value)))
    {
      mpfr_init2 (mpfr_cmp, MPFR_PREC_MIN);
      mpfr_set_inf (mpfr_cmp, -1);
      printf ("mpfr_set_q with a -INF mpq value returned a wrong value :\n"
              " waiting for ");
      mpfr_print_binary (mpfr_cmp);
      printf (" got ");
      mpfr_print_binary (mpfr_value);
      printf ("\n trinary value is %d\n", status);
      exit (1);
    }

  mpq_clear (mpq_value);
  mpfr_clear (mpfr_value);
}
示例#23
0
文件: lux.c 项目: TakeScoop/node-glpk
void lux_f_solve(LUX *lux, int tr, mpq_t x[])
{     int n = lux->n;
      LUXELM **F_row = lux->F_row;
      LUXELM **F_col = lux->F_col;
      int *P_row = lux->P_row;
      LUXELM *fik, *fkj;
      int i, j, k;
      mpq_t temp;
      mpq_init(temp);
      if (!tr)
      {  /* solve the system F*x = b */
         for (j = 1; j <= n; j++)
         {  k = P_row[j];
            if (mpq_sgn(x[k]) != 0)
            {  for (fik = F_col[k]; fik != NULL; fik = fik->c_next)
               {  mpq_mul(temp, fik->val, x[k]);
                  mpq_sub(x[fik->i], x[fik->i], temp);
               }
            }
         }
      }
      else
      {  /* solve the system F'*x = b */
         for (i = n; i >= 1; i--)
         {  k = P_row[i];
            if (mpq_sgn(x[k]) != 0)
            {  for (fkj = F_row[k]; fkj != NULL; fkj = fkj->r_next)
               {  mpq_mul(temp, fkj->val, x[k]);
                  mpq_sub(x[fkj->j], x[fkj->j], temp);
               }
            }
         }
      }
      mpq_clear(temp);
      return;
}
示例#24
0
文件: gmpy2_sign.c 项目: godbomb/gmpy
static PyObject *
GMPy_Rational_Sign(PyObject *x, CTXT_Object *context)
{
    long res;
    MPQ_Object *tempx;

    if (!(tempx = GMPy_MPQ_From_Rational(x, context))) {
        return NULL;
    }
    else {
        res = mpq_sgn(tempx->q);
        Py_DECREF((PyObject*)tempx);
        return PyIntOrLong_FromLong(res);
    }
}
示例#25
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);
}
示例#26
0
文件: misc.c 项目: aliddell/cadenza
void print_rational_number(FILE *OUT, rational_complex_number z)
/***************************************************************\
* USAGE: prints z to OUT                                        *
\***************************************************************/
{
  int base = 10;

  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;
}
示例#27
0
void convert_to_z(mpz_t z, const mpq_t q, const mpz_t prime) {
  assert(mpz_sgn(prime) != 0);
  if (mpz_cmp_ui(mpq_denref(q), 1) == 0) {
    mpz_set(z, mpq_numref(q));
    if (mpq_sgn(q) < 0) {
      mpz_mod(z, z, prime);
    }
  } else if (mpz_cmp_ui(mpq_denref(q), 0) == 0) {
    mpz_set_ui(z, 0);
  } else {
    mpz_invert(z, mpq_denref(q), prime);
    mpz_mul(z, z, mpq_numref(q));
    mpz_mod(z, z, prime);
  }
}
示例#28
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);
    }
}
示例#29
0
void lp_set_rhs_positive(LP* lp) {
  mpq_t q;
  int   i;

  mpq_init(q);

  for (i = 0; i < lp->rows; i ++) {
    get_valid_rhs(&q, lp, i);
    if (mpq_sgn(q) < 0) {
      matrix_rev_row_sgn(lp->A, i);
      vector_rev_element_sgn(lp->b, i);
    }
  }

  mpq_clear(q);
}
示例#30
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;
}