File: set_fexpr.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 (297 lines) | stat: -rw-r--r-- 8,972 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
/*
    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_ext.h"
#include "ca_vec.h"
#include "fexpr.h"
#include "fexpr_builtin.h"

#define BINARY_OP(ca_func) \
    if (nargs == 2) \
    { \
        ca_init(t, ctx); \
        fexpr_view_arg(arg, expr, 0); \
        success = _ca_set_fexpr(res, inputs, outputs, arg, ctx); \
        if (success) \
        { \
            fexpr_view_next(arg); \
            success = _ca_set_fexpr(t, inputs, outputs, arg, ctx); \
            if (success) \
                ca_func(res, res, t, ctx); \
        } \
        ca_clear(t, ctx); \
        return success; \
    } \
    return 0; \

#define UNARY_OP(ca_func) \
    if (nargs == 1) \
    { \
        fexpr_view_arg(arg, expr, 0); \
        success = _ca_set_fexpr(res, inputs, outputs, arg, ctx); \
        if (success) \
            ca_func(res, res, ctx); \
        return success; \
    } \
    return 0; \

int
_ca_set_fexpr(ca_t res, fexpr_vec_t inputs, ca_vec_t outputs, const fexpr_t expr, ca_ctx_t ctx)
{
    if (fexpr_is_integer(expr))
    {
        _ca_make_fmpq(res, ctx);
        fexpr_get_fmpz(CA_FMPQ_NUMREF(res), expr);
        fmpz_one(CA_FMPQ_DENREF(res));
        return 1;
    }

    if (fexpr_is_any_builtin_symbol(expr))
    {
        slong op = FEXPR_BUILTIN_ID(expr->data[0]);

        switch (op)
        {
            case FEXPR_Pi:
                ca_pi(res, ctx);
                return 1;
            case FEXPR_NumberI:
                ca_i(res, ctx);
                return 1;
            case FEXPR_NumberE:
                ca_one(res, ctx);
                ca_exp(res, res, ctx);
                return 1;
            case FEXPR_Euler:
                ca_euler(res, ctx);
                return 1;
            case FEXPR_GoldenRatio:
                ca_sqrt_ui(res, 5, ctx);
                ca_add_ui(res, res, 1, ctx);
                ca_div_ui(res, res, 2, ctx);
                return 1;
            case FEXPR_Infinity:
                ca_pos_inf(res, ctx);
                return 1;
            case FEXPR_UnsignedInfinity:
                ca_uinf(res, ctx);
                return 1;
            case FEXPR_Undefined:
                ca_undefined(res, ctx);
                return 1;
            case FEXPR_Unknown:
                ca_unknown(res, ctx);
                return 1;
        }

        return 0;
    }

    if (fexpr_is_symbol(expr))
    {
        slong i, num_defs;

        num_defs = inputs->length;

        /* Treat local definitions as a stack, more recent ones
           overriding older ones */
        for (i = num_defs - 1; i >= 0; i--)
        {
            if (fexpr_equal(expr, fexpr_vec_entry(inputs, i)))
            {
                ca_set(res, ca_vec_entry(outputs, i), ctx);
                return 1;
            }
        }

        return 0;
    }

    if (fexpr_is_any_builtin_call(expr))
    {
        fexpr_t func, arg;
        slong op, i, nargs;
        int success;
        ca_t t;

        nargs = fexpr_nargs(expr);
        fexpr_view_func(func, expr);
        op = FEXPR_BUILTIN_ID(func->data[0]);

        /* Parse local definitions */
        if (op == FEXPR_Where)
        {
            slong num_previous_defs;

            num_previous_defs = inputs->length;
            success = 1;

            /* Parse in reverse order; this assumes definitions are in the form
               x = f(y,z), y = g(z), z = h  which is what ca_get_fexpr currently
               generates. Should this work in both directions (or any order)?
            */

            for (i = nargs - 1; i >= 1; i--)
            {
                fexpr_t defn, symbol, value;

                fexpr_view_arg(defn, expr, i);

                if (!fexpr_is_builtin_call(defn, FEXPR_Def) || fexpr_nargs(defn) != 2)
                {
                    success = 0;
                    break;
                }

                fexpr_view_arg(symbol, defn, 0);
                fexpr_view_arg(value, defn, 1);

                success = _ca_set_fexpr(res, inputs, outputs, value, ctx);
                if (!success)
                    break;

                fexpr_vec_append(inputs, symbol);
                ca_vec_append(outputs, res, ctx);
            }

            if (success)
            {
                fexpr_view_arg(arg, expr, 0);
                success = _ca_set_fexpr(res, inputs, outputs, arg, ctx);
            }

            /* We are done with the local definitions, so erase anything new. */
            fexpr_vec_set_length(inputs, num_previous_defs);
            ca_vec_set_length(outputs, num_previous_defs, ctx);

            return success;
        }

        switch (op)
        {
            /* todo: generalize to non-algebraics; handle large decimals efficiently */
            case FEXPR_Decimal:
            case FEXPR_PolynomialRootIndexed:
            case FEXPR_PolynomialRootNearest:
            case FEXPR_AlgebraicNumberSerialized:
                {
                    qqbar_t a;
                    qqbar_init(a);
                    success = qqbar_set_fexpr(a, expr);
                    if (success)
                        ca_set_qqbar(res, a, ctx);
                    qqbar_clear(a);
                    return success;
                }

            case FEXPR_Pos: UNARY_OP(ca_set)
            case FEXPR_Neg: UNARY_OP(ca_neg)
            case FEXPR_Sub: BINARY_OP(ca_sub)
            case FEXPR_Div: BINARY_OP(ca_div)
            case FEXPR_Pow: BINARY_OP(ca_pow)
            case FEXPR_Sqrt: UNARY_OP(ca_sqrt)
            case FEXPR_Exp: UNARY_OP(ca_exp)
            case FEXPR_Log: UNARY_OP(ca_log)
            case FEXPR_Sin: UNARY_OP(ca_sin)
            case FEXPR_Cos: UNARY_OP(ca_cos)
            case FEXPR_Tan: UNARY_OP(ca_tan)
            case FEXPR_Cot: UNARY_OP(ca_cot)
            case FEXPR_Atan: UNARY_OP(ca_atan)
            case FEXPR_Acos: UNARY_OP(ca_acos)
            case FEXPR_Asin: UNARY_OP(ca_asin)
            case FEXPR_Sign: UNARY_OP(ca_sgn)
            case FEXPR_Csgn: UNARY_OP(ca_csgn)
            case FEXPR_Arg: UNARY_OP(ca_arg)
            case FEXPR_Abs: UNARY_OP(ca_abs)
            case FEXPR_Re: UNARY_OP(ca_re)
            case FEXPR_Im: UNARY_OP(ca_im)
            case FEXPR_Conjugate: UNARY_OP(ca_conj)
            case FEXPR_Floor: UNARY_OP(ca_floor)
            case FEXPR_Ceil: UNARY_OP(ca_ceil)
            case FEXPR_Gamma: UNARY_OP(ca_gamma)
            case FEXPR_Erf: UNARY_OP(ca_erf)
            case FEXPR_Erfc: UNARY_OP(ca_erfc)
            case FEXPR_Erfi: UNARY_OP(ca_erfi)

            case FEXPR_Add:
                if (nargs == 0)
                {
                    ca_zero(res, ctx);
                    return 1;
                }

                fexpr_view_arg(arg, expr, 0);
                success = _ca_set_fexpr(res, inputs, outputs, arg, ctx);

                if (success && nargs > 1)
                {
                    /* todo: divide and conquer for large nargs? */
                    ca_init(t, ctx);
                    for (i = 1; i < nargs && success; i++)
                    {
                        fexpr_view_next(arg);
                        success = _ca_set_fexpr(t, inputs, outputs, arg, ctx);
                        if (success)
                            ca_add(res, res, t, ctx);
                    }
                    ca_clear(t, ctx);
                }
                return success;

            case FEXPR_Mul:
                if (nargs == 0)
                {
                    ca_one(res, ctx);
                    return 1;
                }

                fexpr_view_arg(arg, expr, 0);
                success = _ca_set_fexpr(res, inputs, outputs, arg, ctx);

                if (success && nargs > 1)
                {
                    /* todo: divide and conquer for large nargs? */
                    ca_init(t, ctx);
                    for (i = 1; i < nargs && success; i++)
                    {
                        fexpr_view_next(arg);
                        success = _ca_set_fexpr(t, inputs, outputs, arg, ctx);
                        if (success)
                            ca_mul(res, res, t, ctx);
                    }
                    ca_clear(t, ctx);
                }
                return success;
        }
    }

    return 0;
}

int
ca_set_fexpr(ca_t res, const fexpr_t expr, ca_ctx_t ctx)
{
    int success;

    fexpr_vec_t inputs;
    ca_vec_t outputs;

    fexpr_vec_init(inputs, 0);
    ca_vec_init(outputs, 0, ctx);

    success = _ca_set_fexpr(res, inputs, outputs, expr, ctx);

    fexpr_vec_clear(inputs);
    ca_vec_clear(outputs, ctx);

    return success;
}