示例#1
0
__mpz_struct * _fmpz_new_mpz(void)
{
    if (fmpz_num_unused == 0) /* time to allocate MPZ_BLOCK more mpz_t's */
    {
        ulong i;
        if (fmpz_allocated) /* realloc mpz_t's and unused array */
        {
            fmpz_arr = (__mpz_struct *) flint_realloc(fmpz_arr, (fmpz_allocated + MPZ_BLOCK) * sizeof(__mpz_struct));
            fmpz_unused_arr = (ulong *) flint_realloc(fmpz_unused_arr, (fmpz_allocated + MPZ_BLOCK) * sizeof(ulong));
        } else /* first time alloc of mpz_t's and unused array */
        {
            fmpz_arr = (__mpz_struct *) flint_malloc(MPZ_BLOCK*sizeof(__mpz_struct)); 
            fmpz_unused_arr = (ulong *) flint_malloc(MPZ_BLOCK*sizeof(ulong));
        }
        
        /* initialise the new mpz_t's and unused array */
        for (i = 0; i < MPZ_BLOCK; i++)
        {
            mpz_init(fmpz_arr + fmpz_allocated + i);
            fmpz_unused_arr[fmpz_num_unused] = fmpz_allocated + i;
            fmpz_num_unused++;
        }
        
        fmpz_allocated += MPZ_BLOCK;   
    } 
    
    fmpz_num_unused--;
    
    return fmpz_arr + fmpz_unused_arr[fmpz_num_unused];
}
示例#2
0
文件: realloc.c 项目: goens/flint2
void fmpz_poly_factor_realloc(fmpz_poly_factor_t fac, long alloc)
{
    if (alloc == 0)             /* Clear up, reinitialise */
    {
        fmpz_poly_factor_clear(fac);
        fmpz_poly_factor_init(fac);
    }
    else if (fac->alloc)            /* Realloc */
    {
        if (fac->alloc > alloc)
        {
            long i;

            for (i = alloc; i < fac->num; i++)
                fmpz_poly_clear(fac->p + i);

            fac->p   = flint_realloc(fac->p, alloc * sizeof(fmpz_poly_struct));
            fac->exp = flint_realloc(fac->exp, alloc * sizeof(long));
            fac->alloc     = alloc;
        }
        else if (fac->alloc < alloc)
        {
            long i;

            fac->p   = flint_realloc(fac->p, alloc * sizeof(fmpz_poly_struct));
            fac->exp = flint_realloc(fac->exp, alloc * sizeof(long));

            for (i = fac->alloc; i < alloc; i++)
            {
                fmpz_poly_init(fac->p + i);
                fac->exp[i] = 0L;
            }
            fac->alloc = alloc;
        }
    }
    else                        /* Nothing allocated already so do it now */
    {
        long i;

        fac->p   = flint_malloc(alloc * sizeof(fmpz_poly_struct));
        fac->exp = flint_calloc(alloc, sizeof(long));

        for (i = 0; i < alloc; i++)
            fmpz_poly_init(fac->p + i);
        fac->num   = 0;
        fac->alloc = alloc;
    }
}
示例#3
0
void
bernoulli_cache_compute(long n)
{
    if (bernoulli_cache_num < n)
    {
        long i, new_num;
        bernoulli_rev_t iter;

        if (bernoulli_cache_num == 0)
        {
            flint_register_cleanup_function(bernoulli_cleanup);
        }

        new_num = FLINT_MAX(bernoulli_cache_num + 128, n);

        bernoulli_cache = flint_realloc(bernoulli_cache, new_num * sizeof(fmpq));
        for (i = bernoulli_cache_num; i < new_num; i++)
            fmpq_init(bernoulli_cache + i);

        i = new_num - 1;
        i -= (i % 2);
        bernoulli_rev_init(iter, i);
        for ( ; i >= bernoulli_cache_num; i -= 2)
        {
            bernoulli_rev_next(fmpq_numref(bernoulli_cache + i),
                fmpq_denref(bernoulli_cache + i), iter);
        }
        bernoulli_rev_clear(iter);

        if (new_num > 1)
            fmpq_set_si(bernoulli_cache + 1, -1, 2);

        bernoulli_cache_num = new_num;
    }
}
示例#4
0
文件: realloc.c 项目: goens/flint2
void
fmpz_poly_realloc(fmpz_poly_t poly, long alloc)
{
    if (alloc == 0)             /* Clear up, reinitialise */
    {
        fmpz_poly_clear(poly);
        fmpz_poly_init(poly);
        return;
    }

    if (poly->alloc)            /* Realloc */
    {
        fmpz_poly_truncate(poly, alloc);

        poly->coeffs = (fmpz *) flint_realloc(poly->coeffs, alloc * sizeof(fmpz));
        if (alloc > poly->alloc)
            mpn_zero((mp_ptr) (poly->coeffs + poly->alloc),
                     alloc - poly->alloc);
    }
    else                        /* Nothing allocated already so do it now */
    {
        poly->coeffs = (fmpz *) flint_calloc(alloc, sizeof(fmpz));
    }

    poly->alloc = alloc;
}
示例#5
0
文件: fit_length.c 项目: isuruf/arb
void
acb_poly_fit_length(acb_poly_t poly, slong len)
{
    slong i;

    if (len > poly->alloc)
    {
        if (len < 2 * poly->alloc)
            len = 2 * poly->alloc;

        poly->coeffs = flint_realloc(poly->coeffs,
            len * sizeof(acb_struct));

        for (i = poly->alloc; i < len; i++)
            acb_init(poly->coeffs + i);

        poly->alloc = len;
    }
}
示例#6
0
void
acb_modular_hilbert_class_poly(fmpz_poly_t res, slong D)
{
    slong i, a, b, c, ac, h, qbf_alloc, qbf_len, prec;
    slong * qbf;
    double lgh;

    if (D >= 0 || ((D & 3) > 1))
    {
        fmpz_poly_zero(res);
        return;
    }

    qbf_alloc = qbf_len = 0;
    qbf = NULL;
    b = D & 1;
    h = 0;

    /* Cohen algorithm 5.3.5 */
    do
    {
        ac = (b*b - D) / 4;
        a = FLINT_MAX(b, 1);

        do
        {
            if (ac % a == 0 && n_gcd_full(n_gcd(a, b), ac/a) == 1)
            {
                c = ac / a;

                if (qbf_len >= qbf_alloc)
                {
                    qbf_alloc = FLINT_MAX(4, FLINT_MAX(qbf_len + 1, qbf_alloc * 2));
                    qbf = flint_realloc(qbf, qbf_alloc * 3 * sizeof(slong));
                }

                if (a == b || a*a == ac || b == 0)
                {
                    qbf[3 * qbf_len + 0] = a;
                    qbf[3 * qbf_len + 1] = b;
                    qbf[3 * qbf_len + 2] = c;
                    h += 1;
                }
                else
                {
                    /* -b indicates that we have both b and -b */
                    qbf[3 * qbf_len + 0] = a;
                    qbf[3 * qbf_len + 1] = -b;
                    qbf[3 * qbf_len + 2] = c;
                    h += 2;
                }

                qbf_len++;
            }

            a++;
        }
        while (a*a <= ac);

        b += 2;
    }
    while (3*b*b <= -D);

    /* Estimate precision - see p.7 in http://hal.inria.fr/inria-00001040 */
    lgh = 0.0;
    for (i = 0; i < qbf_len; i++)
    {
        if (qbf[3 * i + 1] < 0)
            lgh += 2.0 / qbf[3 * i];
        else
            lgh += 1.0 / qbf[3 * i];
    }

    lgh = 3.141593 * sqrt(-D) * lgh;
#if 0
    lgh += 3.012 * h;
    prec = lgh * 1.442696;
    prec = prec + 10;
#else
    prec = lgh * 1.442696;     /* heuristic, but more accurate */
    prec = prec * 1.005 + 20;
#endif

    while (!_acb_modular_hilbert_class_poly(res, D, qbf, qbf_len, prec))
    {
        flint_printf("hilbert_class_poly failed at %wd bits of precision\n", prec);
        prec = prec * 1.2 + 10;
    }

    flint_free(qbf);
}