File: rewrite_complex_normal_form.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 (370 lines) | stat: -rw-r--r-- 11,254 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
    Copyright (C) 2021 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.h"
#include "ca_vec.h"

/* todo */
void ca_rewrite_complex_normal_form(ca_t res, const ca_t x, int deep, ca_ctx_t ctx);

/* todo */
void ca_set_ext(ca_t res, ca_ext_srcptr ext, ca_ctx_t ctx);

/* todo */
ulong qqbar_try_as_cyclotomic(qqbar_t zeta, fmpq_poly_t poly, const qqbar_t x);

/* todo: Re, Im, Abs, Sgn ... */

void
ca_rewrite_ext_complex_normal_form(ca_t res, ca_ext_ptr ext, int deep, ca_ctx_t ctx)
{
    switch (CA_EXT_HEAD(ext))
    {
        case CA_QQBar:

            if (qqbar_is_i(CA_EXT_QQBAR(ext)))
            {
                ca_set_ext(res, ext, ctx);
            }
            else if (qqbar_is_root_of_unity(NULL, NULL, CA_EXT_QQBAR(ext)))
            {
                ca_set_ext(res, ext, ctx);
            }
            else
            {
                fmpq_poly_t poly;
                qqbar_t zeta;
                ulong q;

                qqbar_init(zeta);
                fmpq_poly_init(poly);

                q = qqbar_try_as_cyclotomic(zeta, poly, CA_EXT_QQBAR(ext));

                if (q != 0)
                {
                    ca_set_qqbar(res, zeta, ctx);
                    ca_fmpq_poly_evaluate(res, poly, res, ctx);
                }
                else
                {
                    ca_set_ext(res, ext, ctx);
                }

                qqbar_clear(zeta);
                fmpq_poly_clear(poly);
            }
            break;

        case CA_Re:
        case CA_Im:
        case CA_Conjugate:
        case CA_Abs:
        case CA_Sign:
        case CA_Arg:
            {
                ca_t t, u;
                truth_t is_zero;

                ca_init(t, ctx);
                ca_init(u, ctx);

                if (deep)
                    ca_rewrite_complex_normal_form(t, CA_EXT_FUNC_ARGS(ext), deep, ctx);
                else
                    ca_set(t, CA_EXT_FUNC_ARGS(ext), ctx);

                switch (CA_EXT_HEAD(ext))
                {
                    case CA_Re:
                        ca_conj_deep(u, t, ctx);
                        ca_add(res, t, u, ctx);
                        ca_div_ui(res, res, 2, ctx);
                        break;

                    case CA_Im:
                        ca_conj_deep(u, t, ctx);
                        ca_sub(res, t, u, ctx);
                        ca_div_ui(res, res, 2, ctx);
                        ca_neg_i(t, ctx);
                        ca_mul(res, res, t, ctx);
                        break;

                    case CA_Conjugate:
                        ca_conj_deep(res, t, ctx);
                        break;

                    case CA_Abs:
                        ca_conj_deep(u, t, ctx);
                        ca_mul(t, t, u, ctx);
                        ca_sqrt(res, t, ctx);
                        break;

                    case CA_Sign:
                        is_zero = ca_check_is_zero(t, ctx);
                        if (is_zero == T_TRUE)
                        {
                            ca_zero(res, ctx);
                        }
                        else if (is_zero == T_FALSE)
                        {
                            ca_conj_deep(u, t, ctx);
                            ca_mul(u, t, u, ctx);
                            ca_sqrt(u, u, ctx);
                            /* todo: division by zero is impossible; optimizations here */
                            ca_div(res, t, u, ctx);
                        }
                        else
                        {
                            ca_sgn(res, t, ctx);
                        }
                        break;

                    case CA_Arg:
                        is_zero = ca_check_is_zero(t, ctx);
                        if (is_zero == T_TRUE)
                        {
                            ca_zero(res, ctx);
                        }
                        else if (is_zero == T_FALSE)
                        {
                            /* todo: better to create two logs? */
                            ca_conj_deep(u, t, ctx);
                            ca_mul(u, t, u, ctx);
                            ca_sqrt(u, u, ctx);
                            ca_div(u, t, u, ctx);
                            ca_log(u, u, ctx);
                            ca_neg_i(t, ctx);
                            ca_mul(res, t, u, ctx);
                        }
                        else
                        {
                            ca_arg(res, t, ctx);
                        }
                        break;

                    default:
                        flint_abort();
                }

                ca_clear(t, ctx);
                ca_clear(u, ctx);
            }
            break;

        /* sin(x) = (exp(ix) - 1/exp(ix))/(2i) */
        /* cos(x) = (exp(ix) + 1/exp(ix))/2 */
        /* tan(x) = 2i/(exp(2ix)+1) */
        case CA_Sin:
        case CA_Cos:
        case CA_Tan:
            {
                ca_t t, u;

                ca_init(t, ctx);
                ca_init(u, ctx);

                if (deep)
                    ca_rewrite_complex_normal_form(t, CA_EXT_FUNC_ARGS(ext), deep, ctx);
                else
                    ca_set(t, CA_EXT_FUNC_ARGS(ext), ctx);

                if (CA_EXT_HEAD(ext) == CA_Sin)
                    ca_sin_cos_exponential(res, NULL, t, ctx);
                else if (CA_EXT_HEAD(ext) == CA_Cos)
                    ca_sin_cos_exponential(NULL, res, t, ctx);
                else
                {
                    ca_sin_cos_exponential(t, u, t, ctx);
                    ca_div(res, t, u, ctx);
                }

                ca_clear(t, ctx);
                ca_clear(u, ctx);
            }
            break;

        case CA_Atan:
        case CA_Asin:
        case CA_Acos:
            {
                ca_t t;
                ca_init(t, ctx);
                if (deep)
                    ca_rewrite_complex_normal_form(t, CA_EXT_FUNC_ARGS(ext), deep, ctx);
                else
                    ca_set(t, CA_EXT_FUNC_ARGS(ext), ctx);
                if (CA_EXT_HEAD(ext) == CA_Asin)
                    ca_asin_logarithm(res, t, ctx);
                else if (CA_EXT_HEAD(ext) == CA_Acos)
                    ca_acos_logarithm(res, t, ctx);
                else
                    ca_atan_logarithm(res, t, ctx);
                ca_clear(t, ctx);
            }
            break;

        case CA_Sqrt:
        case CA_Exp:
        case CA_Log:
            if (deep)
            {
                ca_rewrite_complex_normal_form(res, CA_EXT_FUNC_ARGS(ext), deep, ctx);
                if (ca_equal_repr(res, CA_EXT_FUNC_ARGS(ext), ctx))
                {
                    ca_set_ext(res, ext, ctx);
                }
                else
                {
                    switch (CA_EXT_HEAD(ext))
                    {
                        case CA_Exp:
                            ca_exp(res, res, ctx);
                            break;
                        case CA_Log:
                            ca_log(res, res, ctx);
                            break;
                        case CA_Sqrt:
                            ca_sqrt(res, res, ctx);
                            break;
                        default:
                            flint_abort();
                    }
                }
            }
            else
            {
                ca_set_ext(res, ext, ctx);
            }
            break;

        default:
            /* todo: deep */
            ca_set_ext(res, ext, ctx);
    }

}

void
ca_rewrite_complex_normal_form(ca_t res, const ca_t x, int deep, ca_ctx_t ctx)
{
    if (CA_IS_SPECIAL(x))
    {
        if (CA_IS_SIGNED_INF(x))
        {
            ca_sgn(res, x, ctx);
            ca_rewrite_complex_normal_form(res, res, deep, ctx);
            if (!ca_is_unknown(res, ctx))
                res->field |= CA_INF;
        }
        else
        {
            ca_set(res, x, ctx);
        }
    }
    else if (CA_IS_QQ(x, ctx) || CA_IS_QQ_I(x, ctx))
    {
        ca_set(res, x, ctx);
    }
    else
    {
        ca_field_ptr K = CA_FIELD(x, ctx);

        if (CA_FIELD_IS_NF(K))
        {
            if (qqbar_is_root_of_unity(NULL, NULL, CA_FIELD_NF_QQBAR(K)))
            {
                ca_set(res, x, ctx);
            }
            else
            {
                fmpq_poly_t poly, xpoly;
                qqbar_t zeta;
                ulong q;

                qqbar_init(zeta);
                fmpq_poly_init(poly);

                q = qqbar_try_as_cyclotomic(zeta, poly, CA_FIELD_NF_QQBAR(K));

                if (q != 0)
                {
                    fmpq_poly_init(xpoly);
                    nf_elem_get_fmpq_poly(xpoly, CA_NF_ELEM(x), CA_FIELD_NF(K));
                    ca_set_qqbar(res, zeta, ctx);
                    ca_fmpq_poly_evaluate(res, poly, res, ctx);
                    ca_fmpq_poly_evaluate(res, xpoly, res, ctx);
                    fmpq_poly_clear(xpoly);
                }
                else
                {
                    ca_set(res, x, ctx);
                }

                qqbar_clear(zeta);
                fmpq_poly_clear(poly);
            }
        }
        else if (0)
        {
            ca_set(res, x, ctx);
        }
        else
        {
            slong nvars;
            int * used;
            slong i;
            ca_ptr cext;

            nvars = CA_FIELD_LENGTH(K);
            used = flint_calloc(nvars, sizeof(int));
            cext = _ca_vec_init(nvars, ctx);

            fmpz_mpoly_q_used_vars(used, CA_MPOLY_Q(x), CA_FIELD_MCTX(K, ctx));

            for (i = 0; i < nvars; i++)
            {
                if (used[i])
                {
                    ca_rewrite_ext_complex_normal_form(cext + i, CA_FIELD_EXT_ELEM(K, i), deep, ctx);
                }
            }

            ca_fmpz_mpoly_q_evaluate_no_division_by_zero(res, CA_MPOLY_Q(x), cext, CA_FIELD_MCTX(K, ctx), ctx);
            _ca_vec_clear(cext, nvars, ctx);

            /* Root of unity hack: introduce artificial root
               of unity to force simplifications. This should not be necessary;
               cases where it helps are a sign that we are not finding
               all exponential relations. */
            if (0 && !CA_IS_SPECIAL(res) && !CA_FIELD_IS_QQ(CA_FIELD(res, ctx)) && !CA_FIELD_IS_NF(CA_FIELD(res, ctx)))
            {
                ca_t t, u;
                ca_init(t, ctx);
                ca_init(u, ctx);

                ca_pi_i(t, ctx);
                ca_div_ui(t, t, 12, ctx);
                ca_exp(t, t, ctx);
                ca_add(u, res, t, ctx);
                ca_sub(u, u, t, ctx);

                if (ca_cmp_repr(u, res, ctx) < 0)
                    ca_swap(res, u, ctx);

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

            flint_free(used);
        }
    }
}