Beispiel #1
0
void w3j_sqrt_sq(mpq_t r, long j1, long j2, long j3, long m1, long m2, long m3)
{
  mpz_t n,d,h;
  mpz_init(n);
  mpz_init(d);
  mpz_init(h);

  binomialCoefficient(n, 2*j1,j1-j2+j3);
  binomialCoefficient(h, 2*j2,j1+j2-j3);
  mpz_mul(n,n,h);
  binomialCoefficient(h, 2*j3,-j1+j2+j3);
  mpz_mul(n,n,h);;

  mpq_set_z(r,n);

  binomialCoefficient(d, 2*j1,j1+m1);
  binomialCoefficient(h, 2*j2,j2+m2);
  mpz_mul(d,d,h);
  binomialCoefficient(h, 2*j3,j3+m3);
  mpz_mul(d,d,h);

  mpq_set_den(r,d);
  mpq_canonicalize(r);

  mpz_clear(d); mpz_clear(h); mpz_clear(n);
}
Beispiel #2
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);
}
Beispiel #3
0
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);
    }
}
Beispiel #4
0
gint random_integer_with_probability(mpq_t *p, gint n)
{
    mpz_t *den;
    den = (mpz_t *)g_malloc(sizeof(mpz_t) * n);
    int i;
    for(i = 0; i < n; ++i)
    {
        mpz_init(den[i]);
        mpq_get_den(den[i], p[i]);
    }
    
    mpz_t l;
    mpz_init(l);
    lcm(l, den, n);
    
    mpq_t nlcm;
    mpq_init(nlcm);
    mpq_set_z(nlcm, l);
    
    int lc =  mpz_get_ui(l);
    gint x = g_random_int_range(0, lc);
    mpq_t y;
    mpq_init(y);
    mpq_set_si(y, x, 1);
    
    mpq_t p_i;
    mpq_init(p_i);
    
    int j = n - 1;
    
    for(i = 0; i < n - 1; i++)
    {
        mpq_mul(p_i, nlcm, p[i]);
        
        if(mpq_cmp(y, p_i) < 0)
        {
            j = i;
            break;
        }
        
        mpq_sub(y, y, p_i);
    }
    
    for(i = 0; i < n; ++i)
    {
        mpz_clear(den[i]);
    }
    g_free(den);
    
    mpz_clear(l);
    mpq_clear(nlcm);
    mpq_clear(y);
    mpq_clear(p_i);
    
    return j;
}
Beispiel #5
0
static int _compo(CRATIONAL *a, void *o, bool invert)
{
    if (GB.Is(o, CLASS_BigInt))
    {
        mpq_set_z(_tmp.n, ((CBIGINT *)o)->n);
        return mpq_cmp(a->n, _tmp.n);
    }
    else
        return -2;
}
Beispiel #6
0
static CRATIONAL *_mulo(CRATIONAL *a, void *o, bool invert)
{
    if (GB.Is(o, CLASS_BigInt))
    {
        mpq_set_z(_tmp.n, ((CBIGINT *)o)->n);
        return RATIONAL_make(a, &_tmp, mpq_mul);
    }
    else
        return NULL;
}
Beispiel #7
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;
}
Beispiel #8
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 */
}
Beispiel #9
0
void mpz_mat_to_mpq_mat(mpq_mat_t res, mpz_mat_t mat){

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

   long i;
   for (i = 0; i < mat->r*mat->c; i++)
      mpq_set_z(res->entries[i], mat->entries[i]);

   return;
}
Beispiel #10
0
static CRATIONAL *_divo(CRATIONAL *a, void *o, bool invert)
{
    if (GB.Is(o, CLASS_BigInt))
    {
        mpq_set_z(_tmp.n, ((CBIGINT *)o)->n);
        if (invert)
            return _div(&_tmp, a, FALSE);
        else
            return _div(a, &_tmp, FALSE);
    }
    else
        return NULL;
}
Beispiel #11
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;
}
Beispiel #12
0
void
ovm_q_complex(oregister_t *l, oregister_t *r)
{
    switch (r->t) {
	case t_void:
	    break;
	case t_word:
	    l->t = t_cqq;
	    mpq_set_si(oqi(l), r->v.w, 1);
	    check_cqq(l);
	    break;
	case t_float:
	    l->t = t_cdd;
	    real(l->v.dd) = mpq_get_d(oqr(l));
	    imag(l->v.dd) = r->v.d;
	    check_cdd(l);
	    break;
	case t_mpz:
	    l->t = t_cqq;
	    mpq_set_z(oqi(l), ozr(r));
	    check_cqq(l);
	    break;
	case t_mpq:
	    l->t = t_cqq;
	    mpq_set(oqi(l), oqr(r));
	    check_cqq(l);
	    break;
	case t_mpr:
	    l->t = t_mpc;
	    mpfr_set_q(orr(l), oqr(l), thr_rnd);
	    mpfr_set(ori(l), orr(r), thr_rnd);
	    check_mpc(l);
	    break;
	default:
	    ovm_raise(except_not_a_real_number);
    }
}
Beispiel #13
0
static bool _convert(CRATIONAL *a, GB_TYPE type, GB_VALUE *conv)
{
    if (a)
    {
        switch (type)
        {
        case GB_T_FLOAT:
            conv->_float.value = mpq_get_d(a->n);
            return FALSE;

        case GB_T_SINGLE:
            conv->_single.value = mpq_get_d(a->n);
            return FALSE;

        case GB_T_INTEGER:
        case GB_T_SHORT:
        case GB_T_BYTE:
            conv->_integer.value = (int)mpq_get_d(a->n);
            return FALSE;

        case GB_T_LONG:
            conv->_long.value = (int64_t)mpq_get_d(a->n);
            return FALSE;

        case GB_T_STRING:
        case GB_T_CSTRING:
            conv->_string.value.addr = RATIONAL_to_string(a->n, 10); //, type == GB_T_CSTRING);
            conv->_string.value.start = 0;
            conv->_string.value.len = GB.StringLength(conv->_string.value.addr);
            return FALSE;

        default:

            if (type == CLASS_BigInt)
            {
                mpz_t n;

                mpz_init(n);
                mpz_tdiv_q(n, mpq_numref(a->n), mpq_denref(a->n));

                conv->_object.value = BIGINT_create(n);

                return FALSE;
            }

            return TRUE;
        }
    }
    else
    {
        mpq_t n;

        switch(type)
        {
        case GB_T_FLOAT:
            mpq_init_set_d(n, conv->_float.value);
            conv->_object.value = RATIONAL_create(n);
            return FALSE;

        case GB_T_SINGLE:
            mpq_init_set_d(n, conv->_single.value);
            conv->_object.value = RATIONAL_create(n);
            return FALSE;

        case GB_T_INTEGER:
        case GB_T_SHORT:
        case GB_T_BYTE:
            mpq_init_set_si(n, (long)conv->_integer.value);
            conv->_object.value = RATIONAL_create(n);
            return FALSE;

        case GB_T_LONG:
            mpq_init_set_si(n, (long)conv->_long.value);
            conv->_object.value = RATIONAL_create(n);
            return FALSE;

        case GB_T_STRING:
        case GB_T_CSTRING:
            conv->_object.value = RATIONAL_from_string(GB.ToZeroString(&conv->_string), 10);
            return conv->_object.value == NULL;

        default:

            if (type == CLASS_BigInt)
            {
                mpq_init(n);
                mpq_set_z(n, ((CBIGINT *)conv->_object.value)->n);
                conv->_object.value = RATIONAL_create(n);
                return FALSE;
            }

            return TRUE;
        }
    }
}
Beispiel #14
0
static PyObject *
GMPy_Real_DivMod_2(PyObject *x, PyObject *y, CTXT_Object *context)
{
    MPFR_Object *tempx = NULL, *tempy = NULL, *quo = NULL, *rem = NULL;
    PyObject *result = NULL;

    CHECK_CONTEXT(context);

    if (!(result = PyTuple_New(2)) ||
        !(rem = GMPy_MPFR_New(0, context)) ||
        !(quo = GMPy_MPFR_New(0, context))) {

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

    if (IS_REAL(x) && IS_REAL(y)) {
        if (!(tempx = GMPy_MPFR_From_Real(x, 1, context)) ||
            !(tempy = GMPy_MPFR_From_Real(y, 1, context))) {

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

        if (mpfr_zero_p(tempy->f)) {
            context->ctx.divzero = 1;
            if (context->ctx.traps & TRAP_DIVZERO) {
                GMPY_DIVZERO("divmod() division by zero");
                goto error;
            }
        }

        if (mpfr_nan_p(tempx->f) || mpfr_nan_p(tempy->f) || mpfr_inf_p(tempx->f)) {
            context->ctx.invalid = 1;
            if (context->ctx.traps & TRAP_INVALID) {
                GMPY_INVALID("divmod() invalid operation");
                goto error;
            }
            else {
                mpfr_set_nan(quo->f);
                mpfr_set_nan(rem->f);
            }
        }
        else if (mpfr_inf_p(tempy->f)) {
            context->ctx.invalid = 1;
            if (context->ctx.traps & TRAP_INVALID) {
                GMPY_INVALID("divmod() invalid operation");
                goto error;
            }
            if (mpfr_zero_p(tempx->f)) {
                mpfr_set_zero(quo->f, mpfr_sgn(tempy->f));
                mpfr_set_zero(rem->f, mpfr_sgn(tempy->f));
            }
            else if ((mpfr_signbit(tempx->f)) != (mpfr_signbit(tempy->f))) {
                mpfr_set_si(quo->f, -1, MPFR_RNDN);
                mpfr_set_inf(rem->f, mpfr_sgn(tempy->f));
            }
            else {
                mpfr_set_si(quo->f, 0, MPFR_RNDN);
                rem->rc = mpfr_set(rem->f, tempx->f, MPFR_RNDN);
            }
        }
        else {
            MPQ_Object *mpqx = NULL, *mpqy = NULL, *temp_rem = NULL;
            MPZ_Object *temp_quo = NULL;

            if (!(mpqx = GMPy_MPQ_From_MPFR(tempx, context)) ||
                !(mpqy = GMPy_MPQ_From_MPFR(tempy, context))) {

                /* LCOV_EXCL_START */
                Py_XDECREF((PyObject*)mpqx);
                Py_XDECREF((PyObject*)mpqy);
                goto error;
                /* LCOV_EXCL_STOP */
            }

            if (!(temp_rem = GMPy_MPQ_New(context)) ||
                !(temp_quo = GMPy_MPZ_New(context))) {

                /* LCOV_EXCL_START */
                Py_XDECREF((PyObject*)temp_rem);
                Py_XDECREF((PyObject*)temp_quo);
                Py_XDECREF((PyObject*)mpqx);
                Py_XDECREF((PyObject*)mpqy);
                goto error;
                /* LCOV_EXCL_STOP */
            }

            Py_DECREF((PyObject*)tempx);
            Py_DECREF((PyObject*)tempy);

            mpq_div(temp_rem->q, mpqx->q, mpqy->q);
            mpz_fdiv_q(temp_quo->z, mpq_numref(temp_rem->q), mpq_denref(temp_rem->q));
            /* Need to calculate x - quo * y. */
            mpq_set_z(temp_rem->q, temp_quo->z);
            mpq_mul(temp_rem->q, temp_rem->q, mpqy->q);
            mpq_sub(temp_rem->q, mpqx->q, temp_rem->q);

            Py_DECREF((PyObject*)mpqx);
            Py_DECREF((PyObject*)mpqy);

            quo->rc = mpfr_set_z(quo->f, temp_quo->z, MPFR_RNDD);
            rem->rc = mpfr_set_q(rem->f, temp_rem->q, MPFR_RNDN);

            Py_DECREF((PyObject*)temp_rem);
            Py_DECREF((PyObject*)temp_quo);

            GMPY_MPFR_CHECK_RANGE(quo, context);
            GMPY_MPFR_CHECK_RANGE(rem, context);
            GMPY_MPFR_SUBNORMALIZE(quo, context);
            GMPY_MPFR_SUBNORMALIZE(rem, context);

            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_Real_DivMod_2().");
  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 */
}