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
/*
Copyright (C) 2021 Fredrik Johansson
This file is part of FLINT.
FLINT 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 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "fmpz_mpoly_q.h"
#include "fexpr.h"
#include "fexpr_builtin.h"
int
fexpr_get_fmpz_mpoly_q(fmpz_mpoly_q_t res, const fexpr_t expr, const fexpr_vec_t vars, const fmpz_mpoly_ctx_t ctx)
{
if (fexpr_is_integer(expr))
{
fmpz_t c;
fmpz_init(c);
fexpr_get_fmpz(c, expr);
fmpz_mpoly_q_set_fmpz(res, c, ctx);
fmpz_clear(c);
return 1;
}
else
{
slong i, nargs;
ulong op_head;
fexpr_t func, arg;
int success;
if (fexpr_is_arithmetic_operation(expr))
{
fmpz_mpoly_q_t A, B;
nargs = fexpr_nargs(expr);
if (nargs == 0)
{
/* not implemented */
return 0;
}
success = 1;
fmpz_mpoly_q_init(A, ctx);
fmpz_mpoly_q_init(B, ctx);
fexpr_view_func(func, expr);
op_head = func->data[0];
fexpr_view_arg(arg, expr, 0);
success = fexpr_get_fmpz_mpoly_q(res, arg, vars, ctx);
if (!success)
goto cleanup1;
/* todo: verify nargs == 1 */
if (op_head == FEXPR_SYMBOL_Neg)
{
fmpz_mpoly_q_neg(res, res, ctx);
goto cleanup1;
}
for (i = 1; i < nargs; i++)
{
fexpr_view_next(arg);
success = fexpr_get_fmpz_mpoly_q(A, arg, vars, ctx);
if (!success)
goto cleanup1;
if (op_head == FEXPR_SYMBOL_Add)
fmpz_mpoly_q_add(B, res, A, ctx);
else if (op_head == FEXPR_SYMBOL_Sub)
fmpz_mpoly_q_sub(B, res, A, ctx);
else if (op_head == FEXPR_SYMBOL_Mul)
fmpz_mpoly_q_mul(B, res, A, ctx);
else if (op_head == FEXPR_SYMBOL_Div)
{
/* formal division by zero */
if (fmpz_mpoly_q_is_zero(A, ctx))
success = 0;
else
fmpz_mpoly_q_div(B, res, A, ctx);
}
fmpz_mpoly_q_swap(res, B, ctx);
}
cleanup1:
fmpz_mpoly_q_clear(A, ctx);
fmpz_mpoly_q_clear(B, ctx);
return success;
}
if (fexpr_is_builtin_call(expr, FEXPR_Pow) && (fexpr_nargs(expr) == 2))
{
fexpr_t base, exp;
int success;
fexpr_view_arg(base, expr, 0);
fexpr_view_arg(exp, expr, 1);
if (fexpr_is_integer(exp))
{
fmpz_t c;
success = fexpr_get_fmpz_mpoly_q(res, base, vars, ctx);
if (!success)
return 0;
fmpz_init(c);
fexpr_get_fmpz(c, exp);
if (fmpz_sgn(c) < 0)
{
if (fmpz_mpoly_q_is_zero(res, ctx))
{
success = 0;
goto cleanup2;
}
fmpz_neg(c, c);
fmpz_mpoly_q_inv(res, res, ctx);
}
success = (fmpz_mpoly_pow_fmpz(fmpz_mpoly_q_numref(res), fmpz_mpoly_q_numref(res), c, ctx) &&
fmpz_mpoly_pow_fmpz(fmpz_mpoly_q_denref(res), fmpz_mpoly_q_denref(res), c, ctx));
cleanup2:
fmpz_clear(c);
return success;
}
}
for (i = 0; i < vars->length; i++)
{
if (fexpr_equal(expr, fexpr_vec_entry(vars, i)))
{
fmpz_mpoly_q_gen(res, i, ctx);
return 1;
}
}
return 0;
}
}