Ejemplo n.º 1
0
void _fmpq_poly_set_length(fmpq_poly_t poly, long len)
{
    long i;
    for (i = len; i < poly->length; i++)
        _fmpz_demote(poly->coeffs + i);
    poly->length = len;
}
Ejemplo n.º 2
0
void fmpq_poly_clear(fmpq_poly_t poly)
{
    if (poly->coeffs)
    {
        slong i;
        for (i = 0; i < poly->alloc; i++)
            _fmpz_demote(poly->coeffs + i);
        flint_free(poly->coeffs);
    }
    fmpz_clear(poly->den);
}
Ejemplo n.º 3
0
Archivo: set.c Proyecto: goens/flint2
void
fmpz_set(fmpz_t f, const fmpz_t g)
{
    if (f == g)
        return;                 /* aliased inputs */

    if (!COEFF_IS_MPZ(*g))      /* g is small */
    {
        _fmpz_demote(f);
        *f = *g;
    }
    else                        /* g is large */
    {
        __mpz_struct *mpz_ptr = _fmpz_promote(f);
        mpz_set(mpz_ptr, COEFF_TO_PTR(*g));
    }
}
Ejemplo n.º 4
0
void
mag_set_d_lower(mag_t z, double c)
{
    if (c < 0.0)
        c = -c;

    if (c == 0.0 || (c != c))
    {
        mag_zero(z);
    }
    else if (c == D_INF)
    {
        mag_inf(z);
    }
    else
    {
        _fmpz_demote(MAG_EXPREF(z));
        MAG_SET_D_2EXP_LOWER(MAG_MAN(z), MAG_EXP(z), c, 0);
    }
}
Ejemplo n.º 5
0
void
mag_rfac_ui(mag_t z, ulong n)
{
    if (n < MAG_FAC_TABLE_NUM)
    {
        _fmpz_demote(MAG_EXPREF(z));
        MAG_EXP(z) = mag_rfac_tab[n * 2];
        MAG_MAN(z) = mag_rfac_tab[n * 2 + 1];
    }
    else
    {
        double x = n;

        x = ceil((((x+0.5)*mag_d_log_lower_bound(x) - x) * 1.4426950408889634074) * -0.9999999);

        /* x + 1 could round down for huge x, but this doesn't matter
           as long as the value was perturbed up above */
        fmpz_set_d(MAG_EXPREF(z), x + 1);
        MAG_MAN(z) = MAG_ONE_HALF;
    }
}
Ejemplo n.º 6
0
void
fmpz_randtest_unsigned(fmpz_t f, flint_rand_t state, mp_bitcnt_t bits)
{
    ulong m;

    m    = n_randlimb(state);
    bits = n_randint(state, bits + 1);

    if (bits <= FLINT_BITS - 2)
    {
        _fmpz_demote(f);
        if (m & UWORD(3))
            *f = n_randtest_bits(state, bits);
        else
        {
            m >>= 2;
            if (bits == 0)
                *f = 0;
            else if (bits < FLINT_BITS - 2)
                *f = m & UWORD(1);
            else
                *f = COEFF_MAX;
        }
    }
Ejemplo n.º 7
0
void fmpz_powm_ui(fmpz_t f, const fmpz_t g, ulong e, const fmpz_t m)
{
    if (fmpz_sgn(m) <= 0)
    {
        printf("Exception (fmpz_powm_ui).  Modulus is less than 1.\n");
        abort();
    }

    if (fmpz_is_one(m))
    {
        fmpz_zero(f);
    }
    else if (e == 0)
    {
        fmpz_one(f);
    }
    else  /* e != 0, m > 0 */
    {
        fmpz g2 = *g;
        fmpz m2 = *m;

        if (!COEFF_IS_MPZ(m2))  /* m is small */
        {
            if (!COEFF_IS_MPZ(g2))  /* g is small */
            {
                mp_limb_t minv = n_preinvert_limb(m2);

                _fmpz_demote(f);

                if (g2 >= 0)
                {
                    g2 = n_mod2_preinv(g2, m2, minv);
                    *f = n_powmod2_preinv(g2, e, m2, minv);
                }
                else
                {
                    g2 = n_mod2_preinv(-g2, m2, minv);
                    *f = n_powmod2_preinv(g2, e, m2, minv);
                    if ((e & 1UL))
                        *f = n_negmod(*f, m2);
                }
            }
            else  /* g is large */
            {
                __mpz_struct *ptr = _fmpz_promote(f);
                mpz_t m3;

                mpz_init_set_ui(m3, m2);
                mpz_powm_ui(ptr, COEFF_TO_PTR(g2), e, m3);
                mpz_clear(m3);

                _fmpz_demote_val(f);
            }
        }
        else  /* m is large */
        {
            if (!COEFF_IS_MPZ(g2))  /* g is small */
            {
                __mpz_struct *ptr = _fmpz_promote(f);
                mpz_t g3;

                mpz_init_set_si(g3, g2);
                mpz_powm_ui(ptr, g3, e, COEFF_TO_PTR(m2));
                mpz_clear(g3);

                _fmpz_demote_val(f);
            }
            else  /* g is large */
            {
               __mpz_struct *ptr = _fmpz_promote(f);

                mpz_powm_ui(ptr, COEFF_TO_PTR(g2), e, COEFF_TO_PTR(m2));

                _fmpz_demote_val(f);
            }
        }
    }
}