-
Notifications
You must be signed in to change notification settings - Fork 1
/
polvar.c
306 lines (267 loc) · 8.46 KB
/
polvar.c
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
/* This file is part of the MAYLIB libray.
Copyright 2007-2018 Patrick Pelissier
This Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with th Library; see the file COPYING.LESSER.txt.
If not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "may-impl.h"
/* Add the identifiers found in x view as a polynomial to list
with their maximum estimated exponent */
static void
add_polvar (may_list_t list, may_t x, may_t expo)
{
unsigned long i, n;
MAY_ASSERT (MAY_TYPE (expo) == MAY_INT_T);
switch (MAY_TYPE (x)) {
case MAY_INT_T:
case MAY_RAT_T:
case MAY_REAL_T:
case MAY_COMPLEX_T:
break;
case MAY_FACTOR_T:
add_polvar (list, MAY_AT (x, 1), expo);
break;
case MAY_SUM_T:
case MAY_PRODUCT_T:
n = MAY_NODE_SIZE(x);
MAY_ASSERT (n >= 2);
for (i = 0; i < n; i++)
add_polvar (list, MAY_AT (x, i), expo);
break;
case MAY_POW_T:
if (MAY_LIKELY (MAY_TYPE (MAY_AT (x, 1)) == MAY_INT_T
&& mpz_sgn (MAY_INT (MAY_AT (x, 1))) > 0)) {
expo = may_mul (expo, MAY_AT (x, 1));
add_polvar (list, MAY_AT (x, 0), expo);
break;
} /* else fall down to default */
/* Falls through. */
default:
/* Check if x is already in list */
n = may_list_get_size (list);
MAY_ASSERT ((n%2) == 0);
for (i = 0; i < n; i+=2)
if (may_identical (x, may_list_at (list, i)) == 0) {
if (may_num_cmp (may_list_at (list, i+1), expo) < 0)
may_list_set_at (list, i+1, expo);
return ;
}
/* Add x in the list */
may_list_push_back (list, x);
may_list_push_back (list, expo);
break;
}
}
/* Add in 'list' the identifiers found in x view as a polynomial
iff there are found in 'org' too (with their maximal extimated exponent) */
static void
remove_polvar (may_list_t list, may_list_t org, may_t x, may_t expo)
{
unsigned long i, n;
MAY_ASSERT (MAY_TYPE (expo) == MAY_INT_T);
switch (MAY_TYPE (x)) {
case MAY_INT_T:
case MAY_RAT_T:
case MAY_REAL_T:
case MAY_COMPLEX_T:
break;
case MAY_FACTOR_T:
remove_polvar (list, org, MAY_AT (x, 1), expo);
break;
case MAY_SUM_T:
case MAY_PRODUCT_T:
n = MAY_NODE_SIZE(x);
MAY_ASSERT (n >= 2);
for (i = 0; i < n; i++)
remove_polvar (list, org, MAY_AT (x, i), expo);
break;
case MAY_POW_T:
if (MAY_LIKELY (MAY_TYPE (MAY_AT (x, 1)) == MAY_INT_T
&& mpz_sgn (MAY_INT (MAY_AT (x, 1))) > 0)) {
expo = may_mul (expo, MAY_AT (x, 1));
remove_polvar (list, org, MAY_AT (x, 0), expo);
break;
} /* else fall down to default */
/* Falls through. */
default:
/* Add x in list if it is found in org */
n = may_list_get_size (org);
MAY_ASSERT ((n%2) == 0);
for (i = 0; i < n; i+=2)
if (may_identical (x, may_list_at (org, i)) == 0) {
/* Keep the max between expo and previous one */
if (may_num_cmp (may_list_at (org,i+1), expo) > 0)
expo = may_list_at (org,i+1);
/* Check if x is already in list */
n = may_list_get_size (list);
MAY_ASSERT ((n%2) == 0);
for (i = 0; i < n; i+=2)
if (may_identical (x, may_list_at (list, i)) == 0) {
if (may_num_cmp (may_list_at (list, i+1), expo) < 0)
may_list_set_at (list, i+1, expo);
return ;
}
/* Add x in list */
may_list_push_back (list, x);
may_list_push_back (list, expo);
break;
}
break;
}
}
/* Find the commun var with the minimal estimated exponent
from all items of tab */
may_t
may_find_one_polvar (unsigned long n, const may_t tab[])
{
may_list_t list, list2;
may_t x, min;
unsigned long i;
MAY_ASSERT (n >= 1);
MAY_LOG_FUNC (("n=%lu, tab[0]='%Y'", n, tab[0]));
/* First pass, detect if there is a purenumerical.
If so, we can't find a commun var. */
for (i = 0; i < n; i ++)
if (MAY_UNLIKELY (MAY_PURENUM_P (tab[i])))
return NULL;
MAY_RECORD ();
may_list_init (list, 0);
/* Second pass: search for all commun vars */
add_polvar (list, tab[0], MAY_ONE);
may_list_init (list2, may_list_get_size (list));
for (i = 1; i < n && may_list_get_size (list) > 0; i++) {
may_list_resize (list2, 0);
remove_polvar (list2, list, tab[i], MAY_ONE);
may_list_swap (list, list2);
}
/* If no comun var was found, return NULL */
n = may_list_get_size (list);
if (MAY_UNLIKELY (n == 0))
MAY_RET (NULL);
/* Find the common var with the minimal exponent */
x = may_list_at (list, 0);
min = may_list_at (list, 1);
for (i = 0; i < n; i+=2) {
may_t y = may_list_at (list, i);
if (MAY_UNLIKELY (MAY_TYPE (y) == MAY_STRING_T
&& (MAY_TYPE (x) != MAY_STRING_T
|| may_num_cmp (may_list_at (list, i+1), min) < 0))){
x = y;
min = may_list_at (list, i+1);
}
}
MAY_LOG_VAR(x);
MAY_RET (x);
}
/***************************************************************************/
/* Return the list of the variables which are, at least, not used
by one element of the tab. */
may_t
may_find_unused_polvar (unsigned long n, const may_t tab[])
{
may_list_t used, common, unused;
unsigned long i, j, nused, ncommon;
MAY_ASSERT (n >= 1);
MAY_LOG_FUNC (("n=%d", (int)n));
MAY_RECORD ();
may_list_init (used, 0);
/* Search for all used and commun vars */
add_polvar (used, tab[0], MAY_ONE);
may_list_init (common, may_list_get_size (used));
may_list_init (unused, may_list_get_size (used));
may_list_set (common, used);
for (i = 1; i < n; i++) {
add_polvar (used, tab[i], MAY_ONE);
if (may_list_get_size (common) > 0) {
may_list_resize (unused, 0);
remove_polvar (unused, common, tab[i], MAY_ONE);
may_list_set (common, unused);
}
}
/* Save in 'unused' all variables present in 'used' but not in 'commun' */
nused = may_list_get_size (used);
ncommon = may_list_get_size (common);
may_list_resize (unused, 0);
for (i = 0; i < nused; i+=2) {
may_t t = may_list_at (used, i);
for (j = 0; j < ncommon; j+=2)
if (may_identical (t, may_list_at (common, j)) == 0)
break;
if (j == ncommon)
may_list_push_back_single (unused, t);
}
if (MAY_UNLIKELY (may_list_get_size (unused) == 0))
MAY_RET (NULL);
else
MAY_RET_EVAL (may_list_quit (unused));
}
/***************************************************************************/
/* Add the identifiers found in x view as a rational function to list */
static void
add_ratvar (may_list_t list, may_t x, may_indets_e flags)
{
unsigned long i, n;
if ((flags & MAY_INDETS_NUM) == 0 && MAY_NUM_P (x))
return;
switch (MAY_TYPE (x)) {
case MAY_INT_T:
case MAY_RAT_T:
case MAY_REAL_T:
case MAY_COMPLEX_T:
break;
case MAY_FACTOR_T:
add_ratvar (list, MAY_AT (x, 1), flags);
break;
case MAY_SUM_T:
case MAY_PRODUCT_T:
n = MAY_NODE_SIZE(x);
MAY_ASSERT (n >= 2);
for (i = 0; i < n; i++)
add_ratvar (list, MAY_AT (x, i), flags);
break;
case MAY_FUNC_T:
may_list_push_back_single (list, x);
if ((flags & MAY_INDETS_RECUR) != 0)
add_ratvar (list, MAY_AT (x, 1), flags);
break;
case MAY_POW_T:
if (MAY_LIKELY (MAY_TYPE (MAY_AT (x, 1)) == MAY_INT_T)) {
add_ratvar (list, MAY_AT (x, 0), flags);
break;
} /* else fall down to default */
/* Falls through. */
default:
may_list_push_back_single (list, x);
if (MAY_UNLIKELY ((flags & MAY_INDETS_RECUR) != 0 && MAY_NODE_P (x))) {
n = MAY_NODE_SIZE(x);
for (i = 0; i < n; i++)
add_ratvar (list, MAY_AT (x, i), flags);
}
break;
}
}
static int
cmp_ratvar (const may_t *a, const may_t *b) {
return MAY_TYPE (*a) - MAY_TYPE (*b);
}
may_t
may_indets (may_t x, may_indets_e flags)
{
may_list_t list;
MAY_LOG_FUNC (("x=%Y flags=%d", x, (int)flags));
may_mark();
may_list_init (list, 0);
add_ratvar (list, x, flags);
may_list_sort (list, cmp_ratvar);
may_t y = may_list_quit (list);
return may_keep (may_eval (y));
}