Esempio n. 1
0
File: sqrtpos.c Progetto: isuruf/arb
void
fmprb_sqrtpos(fmprb_t z, const fmprb_t x, slong prec)
{
    if (!fmprb_is_finite(x))
    {
        if (fmpr_is_zero(fmprb_radref(x)) && fmpr_is_pos_inf(fmprb_midref(x)))
            fmprb_pos_inf(z);
        else
            fmprb_zero_pm_inf(z);
    }
    else if (fmprb_contains_nonpositive(x))
    {
        fmpr_t t;
        fmpr_init(t);
        fmpr_add(t, fmprb_midref(x), fmprb_radref(x),
            FMPRB_RAD_PREC, FMPR_RND_CEIL);
        if (fmpr_sgn(t) <= 0)
        {
            fmprb_zero(z);
        }
        else
        {
            fmpr_sqrt(t, t, FMPRB_RAD_PREC, FMPR_RND_CEIL);
            fmpr_mul_2exp_si(t, t, -1);
            fmpr_set(fmprb_midref(z), t);
            fmpr_set(fmprb_radref(z), t);
        }
        fmpr_clear(t);
    }
    else
    {
        fmprb_sqrt(z, x, prec);
    }

    fmprb_nonnegative_part(z, z, prec);
}
Esempio n. 2
0
void
fmpcb_calc_cauchy_bound(fmprb_t bound, fmpcb_calc_func_t func, void * param,
    const fmpcb_t x, const fmprb_t radius, long maxdepth, long prec)
{
    long i, n, depth, wp;

    fmprb_t pi, theta, v, s1, c1, s2, c2, st, ct;
    fmpcb_t t, u;
    fmprb_t b;

    fmprb_init(pi);
    fmprb_init(theta);
    fmprb_init(v);

    fmprb_init(s1);
    fmprb_init(c1);
    fmprb_init(s2);
    fmprb_init(c2);
    fmprb_init(st);
    fmprb_init(ct);

    fmpcb_init(t);
    fmpcb_init(u);
    fmprb_init(b);

    wp = prec + 20;

    fmprb_const_pi(pi, wp);
    fmprb_zero_pm_inf(b);

    for (depth = 0, n = 16; depth < maxdepth; n *= 2, depth++)
    {
        fmprb_zero(b);

        /* theta = 2 pi / n */
        fmprb_div_ui(theta, pi, n, wp);
        fmprb_mul_2exp_si(theta, theta, 1);

        /* sine and cosine of i*theta and (i+1)*theta */
        fmprb_zero(s1);
        fmprb_one(c1);
        fmprb_sin_cos(st, ct, theta, wp);
        fmprb_set(s2, st);
        fmprb_set(c2, ct);

        for (i = 0; i < n; i++)
        {
            /* sine and cosine of 2 pi ([i,i+1]/n) */

            /* since we use power of two subdivision points, the
               sine and cosine are monotone on each subinterval */
            fmprb_union(fmpcb_realref(t), c1, c2, wp);
            fmprb_union(fmpcb_imagref(t), s1, s2, wp);
            fmpcb_mul_fmprb(t, t, radius, wp);
            fmpcb_add(t, t, x, prec);

            /* next angle */
            fmprb_mul(v, c2, ct, wp);
            fmprb_mul(c1, s2, st, wp);
            fmprb_sub(c1, v, c1, wp);
            fmprb_mul(v, c2, st, wp);
            fmprb_mul(s1, s2, ct, wp);
            fmprb_add(s1, v, s1, wp);
            fmprb_swap(c1, c2);
            fmprb_swap(s1, s2);

            func(u, t, param, 1, prec);
            fmpcb_abs(v, u, prec);
            fmprb_add(b, b, v, prec);
        }

        fmprb_div_ui(b, b, n, prec);

        if (fmprb_is_exact(b) || fmpr_cmp(fmprb_radref(b), fmprb_midref(b)) < 0)
            break;
    }

    fmprb_set(bound, b);

    fmprb_clear(pi);
    fmprb_clear(theta);
    fmprb_clear(v);

    fmpcb_clear(t);
    fmpcb_clear(u);
    fmprb_clear(b);

    fmprb_clear(s1);
    fmprb_clear(c1);
    fmprb_clear(s2);
    fmprb_clear(c2);
    fmprb_clear(st);
    fmprb_clear(ct);
}