File: cmpabs.c

package info (click to toggle)
calcium 0.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,756 kB
  • sloc: ansic: 62,836; python: 2,827; sh: 518; makefile: 163
file content (75 lines) | stat: -rw-r--r-- 1,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
    Copyright (C) 2020 Fredrik Johansson

    This file is part of Calcium.

    Calcium is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.  See <http://www.gnu.org/licenses/>.
*/

#include "qqbar.h"

int
qqbar_cmpabs(const qqbar_t x, const qqbar_t y)
{
    slong prec;
    acb_t z1, z2;
    arb_t z3, z4;
    int res;

    if (qqbar_sgn_im(x) == 0 && qqbar_sgn_im(y) == 0)
        return qqbar_cmpabs_re(x, y);

    if (qqbar_sgn_re(x) == 0 && qqbar_sgn_re(y) == 0)
        return qqbar_cmpabs_im(x, y);

    {
        acb_init(z1);
        acb_init(z2);
        arb_init(z3);
        arb_init(z4);

        acb_set(z1, QQBAR_ENCLOSURE(x));
        acb_set(z2, QQBAR_ENCLOSURE(y));

        res = 0;
        for (prec = QQBAR_DEFAULT_PREC / 2; ; prec *= 2)
        {
            _qqbar_enclosure_raw(z1, QQBAR_POLY(x), z1, prec);
            _qqbar_enclosure_raw(z2, QQBAR_POLY(y), z2, prec);

            acb_abs(z3, z1, prec);
            acb_abs(z4, z2, prec);

            if (!arb_overlaps(z3, z4))
            {
                res = arf_cmpabs(arb_midref(z3), arb_midref(z4));
                break;
            }

            /* Force an exact computation (may be slow) */
            if (prec >= 4 * QQBAR_DEFAULT_PREC)
            {
                qqbar_t t, u;
                qqbar_init(t);
                qqbar_init(u);
                qqbar_abs2(t, x);
                qqbar_abs2(u, y);
                res = qqbar_cmp_re(t, u);
                qqbar_clear(t);
                qqbar_clear(u);
                break;
            }
        }

        acb_clear(z1);
        acb_clear(z2);
        arb_clear(z3);
        arb_clear(z4);
    }

    return res;
}