File: roots.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 (304 lines) | stat: -rw-r--r-- 7,083 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
    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 "ca_poly.h"

void
_ca_poly_roots_quadratic(ca_t r1, ca_t r2, const ca_t a, const ca_t b, const ca_t c, ca_ctx_t ctx)
{
    ca_t d, t;

    ca_init(d, ctx);
    ca_init(t, ctx);

    ca_mul(t, a, c, ctx);
    ca_mul_ui(t, t, 4, ctx);
    ca_sqr(d, b, ctx);
    ca_sub(d, d, t, ctx);
    ca_sqrt(d, d, ctx);

    ca_inv(t, a, ctx);
    ca_div_ui(t, t, 2, ctx);

    ca_sub(r1, d, b, ctx);
    ca_add(r2, b, d, ctx);
    ca_neg(r2, r2, ctx);
    ca_mul(r1, r1, t, ctx);
    ca_mul(r2, r2, t, ctx);

    ca_clear(d, ctx);
    ca_clear(t, ctx);
}

/* exp(2 pi i * (1 / 3)) */
/* todo: make this faster to construct */
void
ca_omega(ca_t res, ca_ctx_t ctx)
{
    ca_pi_i(res, ctx);
    ca_mul_ui(res, res, 2, ctx);
    ca_div_ui(res, res, 3, ctx);
    ca_exp(res, res, ctx);
}

/* Solves a cubic equation using the cubic formula.
   Assumes (does not check) that a is invertible. */
int
_ca_poly_roots_cubic(ca_t r1, ca_t r2, ca_t r3, const ca_t a, const ca_t b, const ca_t c, const ca_t d, ca_ctx_t ctx)
{
    ca_t D0, D1, C, w1, w2, t;
    int success;

    ca_init(D0, ctx);
    ca_init(D1, ctx);
    ca_init(C, ctx);
    ca_init(w1, ctx);
    ca_init(w2, ctx);
    ca_init(t, ctx);

    /* D0 = b^2 - 3*a*c */
    ca_sqr(D0, b, ctx);
    ca_mul(t, a, c, ctx);
    ca_mul_ui(t, t, 3, ctx);
    ca_sub(D0, D0, t, ctx);

    /* D1 = b*(2*b^2 - 9*a*c) + 27*a^2*d */
    ca_sqr(D1, b, ctx);
    ca_mul_ui(D1, D1, 2, ctx);
    ca_mul(t, a, c, ctx);
    ca_mul_ui(t, t, 9, ctx);
    ca_sub(D1, D1, t, ctx);
    ca_mul(D1, D1, b, ctx);
    ca_sqr(t, a, ctx);
    ca_mul(t, t, d, ctx);
    ca_mul_ui(t, t, 27, ctx);
    ca_add(D1, D1, t, ctx);

    ca_sqr(C, D1, ctx);
    ca_sqr(t, D0, ctx);
    ca_mul(t, t, D0, ctx);
    ca_mul_ui(t, t, 4, ctx);
    ca_sub(C, C, t, ctx);
    ca_sqrt(C, C, ctx);

    /* C = (D1 + sqrt(D1^2 - 4*D0^3)) / 2  or
           (D1 - sqrt(D1^2 - 4*D0^3)) / 2,
            whichever is nonzero */
    success = 1;
    ca_add(t, D1, C, ctx);
    if (ca_check_is_zero(t, ctx) == T_FALSE)
    {
        ca_swap(C, t, ctx);
    }
    else if (ca_check_is_zero(t, ctx) != T_FALSE)
    {
        ca_sub(t, D1, C, ctx);
        if (ca_check_is_zero(t, ctx) == T_FALSE)
            ca_swap(C, t, ctx);
        else
            success = 0;
    }

    if (success)
    {
        ca_div_ui(C, C, 2, ctx);

        /* C = C^(1/3) */
        ca_set_ui(t, 1, ctx);
        ca_div_ui(t, t, 3, ctx);
        ca_pow(C, C, t, ctx);

        /* w1 = exp(2 pi i * (1 / 3)) */
        /* w2 = exp(2 pi i * (2 / 3)) = w1^2 */
        ca_omega(w1, ctx);
        ca_sqr(w2, w1, ctx);

        /* r1 = (b + C + D0 / C) / (-3*a) */
        /* r2 = (b + w1*C + w2 * D0 / C) / (-3*a) */
        /* r3 = (b + w2*C + w1 * D0 / C) / (-3*a) */

        ca_div(r1, D0, C, ctx);
        ca_mul(r2, r1, w2, ctx);
        ca_mul(r3, r1, w1, ctx);

        ca_add(r1, r1, C, ctx);
        ca_mul(t, w1, C, ctx);
        ca_add(r2, r2, t, ctx);
        ca_mul(t, w2, C, ctx);
        ca_add(r3, r3, t, ctx);

        ca_add(r1, r1, b, ctx);
        ca_add(r2, r2, b, ctx);
        ca_add(r3, r3, b, ctx);

        ca_mul_si(t, a, -3, ctx);
        ca_inv(t, t, ctx);

        ca_mul(r1, r1, t, ctx);
        ca_mul(r2, r2, t, ctx);
        ca_mul(r3, r3, t, ctx);
    }
    else
    {
        ca_unknown(r1, ctx);
        ca_unknown(r2, ctx);
        ca_unknown(r3, ctx);
    }

    ca_clear(D0, ctx);
    ca_clear(D1, ctx);
    ca_clear(C, ctx);
    ca_clear(w1, ctx);
    ca_clear(w2, ctx);
    ca_clear(t, ctx);

    return success;
}


int
_ca_poly_roots(ca_ptr roots, ca_srcptr poly, slong len, ca_ctx_t ctx)
{
    slong deg;
    truth_t leading_zero;

    if (len == 0)
        return 0;

    deg = len - 1;

    leading_zero = ca_check_is_zero(poly + deg, ctx);

    if (leading_zero != T_FALSE)
        return 0;

    while (deg > 1 && ca_check_is_zero(poly, ctx) == T_TRUE)
    {
        ca_zero(roots, ctx);
        roots++;
        poly++;
        len--;
        deg--;
    }

    if (deg == 0)
        return 1;

    if (deg == 1)
    {
        ca_div(roots, poly, poly + 1, ctx);
        ca_neg(roots, roots, ctx);
        return 1;
    }

    if (_ca_vec_is_fmpq_vec(poly, len, ctx))
    {
        fmpz_poly_t f;
        qqbar_ptr r;
        slong i;

        f->coeffs = _fmpz_vec_init(len);
        f->length = f->alloc = len;
        r = _qqbar_vec_init(len - 1);

        if (_ca_vec_fmpq_vec_is_fmpz_vec(poly, len, ctx))
        {
            for (i = 0; i < len; i++)
                fmpz_set(f->coeffs + i, CA_FMPQ_NUMREF(poly + i));
        }
        else
        {
            fmpz_t t;
            fmpz_init(t);
            fmpz_one(t);
            for (i = 0; i < len; i++)
                fmpz_lcm(t, t, CA_FMPQ_DENREF(poly + i));

            for (i = 0; i < len; i++)
            {
                fmpz_divexact(f->coeffs + i, t, CA_FMPQ_DENREF(poly + i));
                fmpz_mul(f->coeffs + i, f->coeffs + i, CA_FMPQ_NUMREF(poly + i));
            }

            fmpz_clear(t);
        }

        qqbar_roots_fmpz_poly(r, f, 0);

        for (i = 0; i < deg; i++)
            ca_set_qqbar(roots + i, r + i, ctx);

        _fmpz_vec_clear(f->coeffs, len);
        _qqbar_vec_clear(r, len - 1);

        return 1;
    }

    if (deg == 2)
    {
        _ca_poly_roots_quadratic(roots, roots + 1, poly + 2, poly + 1, poly + 0, ctx);
        return 1;
    }

    if (deg == 3)
    {
        return _ca_poly_roots_cubic(roots, roots + 1, roots + 2,
            poly + 3, poly + 2, poly + 1, poly + 0, ctx);
    }

    return 0;
}

int
ca_poly_roots(ca_vec_t roots, ulong * exp, const ca_poly_t poly, ca_ctx_t ctx)
{
    ca_poly_vec_t fac;
    ca_t c;
    slong i, j, num_roots, factor_deg;
    int success;
    ulong * fac_exp;

    if (poly->length == 0)
        return 0;

    ca_poly_vec_init(fac, 0, ctx);
    ca_init(c, ctx);
    fac_exp = flint_malloc(sizeof(ulong) * poly->length);
    success = ca_poly_factor_squarefree(c, fac, fac_exp, poly, ctx);

    if (success)
    {
        num_roots = 0;
        for (i = 0; i < fac->length; i++)
            num_roots += (fac->entries + i)->length - 1;

        ca_vec_set_length(roots, num_roots, ctx);

        num_roots = 0;
        for (i = 0; success && i < fac->length; i++)
        {
            factor_deg = (fac->entries + i)->length - 1;

            success = _ca_poly_roots(roots->entries + num_roots, (fac->entries + i)->coeffs, (fac->entries + i)->length, ctx);

            for (j = 0; j < factor_deg; j++)
                exp[num_roots + j] = fac_exp[i];

            num_roots += factor_deg;
        }
    }

    ca_poly_vec_clear(fac, ctx);
    ca_clear(c, ctx);
    flint_free(fac_exp);

    return success;
}