1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /** |
---|
5 | * ABSTRACT: numbers in an algebraic extension field K[a] / < f(a) > |
---|
6 | * Assuming that we have a coeffs object cf, then these numbers |
---|
7 | * are polynomials in the polynomial ring K[a] represented by |
---|
8 | * cf->extRing. |
---|
9 | * IMPORTANT ASSUMPTIONS: |
---|
10 | * 1.) So far we assume that cf->extRing is a valid polynomial |
---|
11 | * ring in exactly one variable, i.e., K[a], where K is allowed |
---|
12 | * to be any field (representable in SINGULAR and which may |
---|
13 | * itself be some extension field, thus allowing for extension |
---|
14 | * towers). |
---|
15 | * 2.) Moreover, this implementation assumes that |
---|
16 | * cf->extRing->qideal is not NULL but an ideal with at |
---|
17 | * least one non-zero generator which may be accessed by |
---|
18 | * cf->extRing->qideal->m[0] and which represents the minimal |
---|
19 | * polynomial f(a) of the extension variable 'a' in K[a]. |
---|
20 | * 3.) As soon as an std method for polynomial rings becomes |
---|
21 | * availabe, all reduction steps modulo f(a) should be replaced |
---|
22 | * by a call to std. Moreover, in this situation one can finally |
---|
23 | * move from K[a] / < f(a) > to |
---|
24 | * K[a_1, ..., a_s] / I, with I some zero-dimensional ideal |
---|
25 | * in K[a_1, ..., a_s] given by a lex |
---|
26 | * Gröbner basis. |
---|
27 | * The code in algext.h and algext.cc is then capable of |
---|
28 | * computing in K[a_1, ..., a_s] / I. |
---|
29 | **/ |
---|
30 | |
---|
31 | #include "config.h" |
---|
32 | #include <misc/auxiliary.h> |
---|
33 | |
---|
34 | #include <omalloc/omalloc.h> |
---|
35 | |
---|
36 | #include <reporter/reporter.h> |
---|
37 | |
---|
38 | #include <coeffs/coeffs.h> |
---|
39 | #include <coeffs/numbers.h> |
---|
40 | #include <coeffs/longrat.h> |
---|
41 | |
---|
42 | #include <polys/monomials/ring.h> |
---|
43 | #include <polys/monomials/p_polys.h> |
---|
44 | #include <polys/simpleideals.h> |
---|
45 | |
---|
46 | #include <polys/PolyEnumerator.h> |
---|
47 | |
---|
48 | #ifdef HAVE_FACTORY |
---|
49 | #include <factory/factory.h> |
---|
50 | #include <polys/clapconv.h> |
---|
51 | #include <polys/clapsing.h> |
---|
52 | #endif |
---|
53 | |
---|
54 | |
---|
55 | #include <polys/ext_fields/algext.h> |
---|
56 | #define TRANSEXT_PRIVATES 1 |
---|
57 | #include <polys/ext_fields/transext.h> |
---|
58 | |
---|
59 | #ifdef LDEBUG |
---|
60 | #define naTest(a) naDBTest(a,__FILE__,__LINE__,cf) |
---|
61 | BOOLEAN naDBTest(number a, const char *f, const int l, const coeffs r); |
---|
62 | #else |
---|
63 | #define naTest(a) ((void)(TRUE)) |
---|
64 | #endif |
---|
65 | |
---|
66 | /// Our own type! |
---|
67 | static const n_coeffType ID = n_algExt; |
---|
68 | |
---|
69 | /* polynomial ring in which our numbers live */ |
---|
70 | #define naRing cf->extRing |
---|
71 | |
---|
72 | /* coeffs object in which the coefficients of our numbers live; |
---|
73 | * methods attached to naCoeffs may be used to compute with the |
---|
74 | * coefficients of our numbers, e.g., use naCoeffs->nAdd to add |
---|
75 | * coefficients of our numbers */ |
---|
76 | #define naCoeffs cf->extRing->cf |
---|
77 | |
---|
78 | /* minimal polynomial */ |
---|
79 | #define naMinpoly naRing->qideal->m[0] |
---|
80 | |
---|
81 | /// forward declarations |
---|
82 | BOOLEAN naGreaterZero(number a, const coeffs cf); |
---|
83 | BOOLEAN naGreater(number a, number b, const coeffs cf); |
---|
84 | BOOLEAN naEqual(number a, number b, const coeffs cf); |
---|
85 | BOOLEAN naIsOne(number a, const coeffs cf); |
---|
86 | BOOLEAN naIsMOne(number a, const coeffs cf); |
---|
87 | BOOLEAN naIsZero(number a, const coeffs cf); |
---|
88 | number naInit(long i, const coeffs cf); |
---|
89 | int naInt(number &a, const coeffs cf); |
---|
90 | number naNeg(number a, const coeffs cf); |
---|
91 | number naInvers(number a, const coeffs cf); |
---|
92 | number naAdd(number a, number b, const coeffs cf); |
---|
93 | number naSub(number a, number b, const coeffs cf); |
---|
94 | number naMult(number a, number b, const coeffs cf); |
---|
95 | number naDiv(number a, number b, const coeffs cf); |
---|
96 | void naPower(number a, int exp, number *b, const coeffs cf); |
---|
97 | number naCopy(number a, const coeffs cf); |
---|
98 | void naWriteLong(number &a, const coeffs cf); |
---|
99 | void naWriteShort(number &a, const coeffs cf); |
---|
100 | number naRePart(number a, const coeffs cf); |
---|
101 | number naImPart(number a, const coeffs cf); |
---|
102 | number naGetDenom(number &a, const coeffs cf); |
---|
103 | number naGetNumerator(number &a, const coeffs cf); |
---|
104 | number naGcd(number a, number b, const coeffs cf); |
---|
105 | //number naLcm(number a, number b, const coeffs cf); |
---|
106 | int naSize(number a, const coeffs cf); |
---|
107 | void naDelete(number *a, const coeffs cf); |
---|
108 | void naCoeffWrite(const coeffs cf, BOOLEAN details); |
---|
109 | //number naIntDiv(number a, number b, const coeffs cf); |
---|
110 | const char * naRead(const char *s, number *a, const coeffs cf); |
---|
111 | |
---|
112 | static BOOLEAN naCoeffIsEqual(const coeffs cf, n_coeffType n, void * param); |
---|
113 | |
---|
114 | |
---|
115 | /// returns NULL if p == NULL, otherwise makes p monic by dividing |
---|
116 | /// by its leading coefficient (only done if this is not already 1); |
---|
117 | /// this assumes that we are over a ground field so that division |
---|
118 | /// is well-defined; |
---|
119 | /// modifies p |
---|
120 | // void p_Monic(poly p, const ring r); |
---|
121 | |
---|
122 | /// assumes that p and q are univariate polynomials in r, |
---|
123 | /// mentioning the same variable; |
---|
124 | /// assumes a global monomial ordering in r; |
---|
125 | /// assumes that not both p and q are NULL; |
---|
126 | /// returns the gcd of p and q; |
---|
127 | /// leaves p and q unmodified |
---|
128 | // poly p_Gcd(const poly p, const poly q, const ring r); |
---|
129 | |
---|
130 | /* returns NULL if p == NULL, otherwise makes p monic by dividing |
---|
131 | by its leading coefficient (only done if this is not already 1); |
---|
132 | this assumes that we are over a ground field so that division |
---|
133 | is well-defined; |
---|
134 | modifies p */ |
---|
135 | static inline void p_Monic(poly p, const ring r) |
---|
136 | { |
---|
137 | if (p == NULL) return; |
---|
138 | number n = n_Init(1, r->cf); |
---|
139 | if (p->next==NULL) { p_SetCoeff(p,n,r); return; } |
---|
140 | poly pp = p; |
---|
141 | number lc = p_GetCoeff(p, r); |
---|
142 | if (n_IsOne(lc, r->cf)) return; |
---|
143 | number lcInverse = n_Invers(lc, r->cf); |
---|
144 | p_SetCoeff(p, n, r); // destroys old leading coefficient! |
---|
145 | pIter(p); |
---|
146 | while (p != NULL) |
---|
147 | { |
---|
148 | number n = n_Mult(p_GetCoeff(p, r), lcInverse, r->cf); |
---|
149 | n_Normalize(n,r->cf); |
---|
150 | p_SetCoeff(p, n, r); // destroys old leading coefficient! |
---|
151 | pIter(p); |
---|
152 | } |
---|
153 | n_Delete(&lcInverse, r->cf); |
---|
154 | p = pp; |
---|
155 | } |
---|
156 | |
---|
157 | /// see p_Gcd; |
---|
158 | /// additional assumption: deg(p) >= deg(q); |
---|
159 | /// must destroy p and q (unless one of them is returned) |
---|
160 | static inline poly p_GcdHelper(poly &p, poly &q, const ring r) |
---|
161 | { |
---|
162 | while (q != NULL) |
---|
163 | { |
---|
164 | p_PolyDiv(p, q, FALSE, r); |
---|
165 | // swap p and q: |
---|
166 | poly& t = q; |
---|
167 | q = p; |
---|
168 | p = t; |
---|
169 | |
---|
170 | } |
---|
171 | return p; |
---|
172 | } |
---|
173 | |
---|
174 | /* assumes that p and q are univariate polynomials in r, |
---|
175 | mentioning the same variable; |
---|
176 | assumes a global monomial ordering in r; |
---|
177 | assumes that not both p and q are NULL; |
---|
178 | returns the gcd of p and q; |
---|
179 | leaves p and q unmodified */ |
---|
180 | static inline poly p_Gcd(const poly p, const poly q, const ring r) |
---|
181 | { |
---|
182 | assume((p != NULL) || (q != NULL)); |
---|
183 | |
---|
184 | poly a = p; poly b = q; |
---|
185 | if (p_Deg(a, r) < p_Deg(b, r)) { a = q; b = p; } |
---|
186 | a = p_Copy(a, r); b = p_Copy(b, r); |
---|
187 | |
---|
188 | /* We have to make p monic before we return it, so that if the |
---|
189 | gcd is a unit in the ground field, we will actually return 1. */ |
---|
190 | a = p_GcdHelper(a, b, r); |
---|
191 | p_Monic(a, r); |
---|
192 | return a; |
---|
193 | } |
---|
194 | |
---|
195 | /* see p_ExtGcd; |
---|
196 | additional assumption: deg(p) >= deg(q); |
---|
197 | must destroy p and q (unless one of them is returned) */ |
---|
198 | static inline poly p_ExtGcdHelper(poly &p, poly &pFactor, poly &q, poly &qFactor, |
---|
199 | ring r) |
---|
200 | { |
---|
201 | if (q == NULL) |
---|
202 | { |
---|
203 | qFactor = NULL; |
---|
204 | pFactor = p_ISet(1, r); |
---|
205 | p_SetCoeff(pFactor, n_Invers(p_GetCoeff(p, r), r->cf), r); |
---|
206 | p_Monic(p, r); |
---|
207 | return p; |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | poly pDivQ = p_PolyDiv(p, q, TRUE, r); |
---|
212 | poly ppFactor = NULL; poly qqFactor = NULL; |
---|
213 | poly theGcd = p_ExtGcdHelper(q, qqFactor, p, ppFactor, r); |
---|
214 | pFactor = ppFactor; |
---|
215 | qFactor = p_Add_q(qqFactor, |
---|
216 | p_Neg(p_Mult_q(pDivQ, p_Copy(ppFactor, r), r), r), |
---|
217 | r); |
---|
218 | return theGcd; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | /* assumes that p and q are univariate polynomials in r, |
---|
223 | mentioning the same variable; |
---|
224 | assumes a global monomial ordering in r; |
---|
225 | assumes that not both p and q are NULL; |
---|
226 | returns the gcd of p and q; |
---|
227 | moreover, afterwards pFactor and qFactor contain appropriate |
---|
228 | factors such that gcd(p, q) = p * pFactor + q * qFactor; |
---|
229 | leaves p and q unmodified */ |
---|
230 | poly p_ExtGcd(poly p, poly &pFactor, poly q, poly &qFactor, ring r) |
---|
231 | { |
---|
232 | assume((p != NULL) || (q != NULL)); |
---|
233 | poly a = p; poly b = q; BOOLEAN aCorrespondsToP = TRUE; |
---|
234 | if (p_Deg(a, r) < p_Deg(b, r)) |
---|
235 | { a = q; b = p; aCorrespondsToP = FALSE; } |
---|
236 | a = p_Copy(a, r); b = p_Copy(b, r); |
---|
237 | poly aFactor = NULL; poly bFactor = NULL; |
---|
238 | poly theGcd = p_ExtGcdHelper(a, aFactor, b, bFactor, r); |
---|
239 | if (aCorrespondsToP) { pFactor = aFactor; qFactor = bFactor; } |
---|
240 | else { pFactor = bFactor; qFactor = aFactor; } |
---|
241 | return theGcd; |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | #ifdef LDEBUG |
---|
247 | BOOLEAN naDBTest(number a, const char *f, const int l, const coeffs cf) |
---|
248 | { |
---|
249 | assume(getCoeffType(cf) == ID); |
---|
250 | if (a == NULL) return TRUE; |
---|
251 | p_Test((poly)a, naRing); |
---|
252 | if((((poly)a)!=naMinpoly) |
---|
253 | && p_Totaldegree((poly)a, naRing) >= p_Totaldegree(naMinpoly, naRing)) |
---|
254 | { |
---|
255 | Print("deg >= deg(minpoly) in %s:%d\n",f,l); |
---|
256 | return FALSE; |
---|
257 | } |
---|
258 | return TRUE; |
---|
259 | } |
---|
260 | #endif |
---|
261 | |
---|
262 | void heuristicReduce(poly &p, poly reducer, const coeffs cf); |
---|
263 | void definiteReduce(poly &p, poly reducer, const coeffs cf); |
---|
264 | |
---|
265 | /* returns the bottom field in this field extension tower; if the tower |
---|
266 | is flat, i.e., if there is no extension, then r itself is returned; |
---|
267 | as a side-effect, the counter 'height' is filled with the height of |
---|
268 | the extension tower (in case the tower is flat, 'height' is zero) */ |
---|
269 | static coeffs nCoeff_bottom(const coeffs r, int &height) |
---|
270 | { |
---|
271 | assume(r != NULL); |
---|
272 | coeffs cf = r; |
---|
273 | height = 0; |
---|
274 | while (nCoeff_is_Extension(cf)) |
---|
275 | { |
---|
276 | assume(cf->extRing != NULL); assume(cf->extRing->cf != NULL); |
---|
277 | cf = cf->extRing->cf; |
---|
278 | height++; |
---|
279 | } |
---|
280 | return cf; |
---|
281 | } |
---|
282 | |
---|
283 | BOOLEAN naIsZero(number a, const coeffs cf) |
---|
284 | { |
---|
285 | naTest(a); |
---|
286 | return (a == NULL); |
---|
287 | } |
---|
288 | |
---|
289 | void naDelete(number * a, const coeffs cf) |
---|
290 | { |
---|
291 | if (*a == NULL) return; |
---|
292 | if (((poly)*a)==naMinpoly) { *a=NULL;return;} |
---|
293 | poly aAsPoly = (poly)(*a); |
---|
294 | p_Delete(&aAsPoly, naRing); |
---|
295 | *a = NULL; |
---|
296 | } |
---|
297 | |
---|
298 | BOOLEAN naEqual(number a, number b, const coeffs cf) |
---|
299 | { |
---|
300 | naTest(a); naTest(b); |
---|
301 | |
---|
302 | /// simple tests |
---|
303 | if (a == b) return TRUE; |
---|
304 | if ((a == NULL) && (b != NULL)) return FALSE; |
---|
305 | if ((b == NULL) && (a != NULL)) return FALSE; |
---|
306 | |
---|
307 | /// deg test |
---|
308 | int aDeg = 0; |
---|
309 | if (a != NULL) aDeg = p_Totaldegree((poly)a, naRing); |
---|
310 | int bDeg = 0; |
---|
311 | if (b != NULL) bDeg = p_Totaldegree((poly)b, naRing); |
---|
312 | if (aDeg != bDeg) return FALSE; |
---|
313 | |
---|
314 | /// subtraction test |
---|
315 | number c = naSub(a, b, cf); |
---|
316 | BOOLEAN result = naIsZero(c, cf); |
---|
317 | naDelete(&c, cf); |
---|
318 | return result; |
---|
319 | } |
---|
320 | |
---|
321 | number naCopy(number a, const coeffs cf) |
---|
322 | { |
---|
323 | naTest(a); |
---|
324 | if (a == NULL) return NULL; |
---|
325 | if (((poly)a)==naMinpoly) return a; |
---|
326 | return (number)p_Copy((poly)a, naRing); |
---|
327 | } |
---|
328 | |
---|
329 | number naGetNumerator(number &a, const coeffs cf) |
---|
330 | { |
---|
331 | return naCopy(a, cf); |
---|
332 | } |
---|
333 | |
---|
334 | number naGetDenom(number &a, const coeffs cf) |
---|
335 | { |
---|
336 | naTest(a); |
---|
337 | return naInit(1, cf); |
---|
338 | } |
---|
339 | |
---|
340 | BOOLEAN naIsOne(number a, const coeffs cf) |
---|
341 | { |
---|
342 | naTest(a); |
---|
343 | poly aAsPoly = (poly)a; |
---|
344 | if ((a==NULL) || (!p_IsConstant(aAsPoly, naRing))) return FALSE; |
---|
345 | return n_IsOne(p_GetCoeff(aAsPoly, naRing), naCoeffs); |
---|
346 | } |
---|
347 | |
---|
348 | BOOLEAN naIsMOne(number a, const coeffs cf) |
---|
349 | { |
---|
350 | naTest(a); |
---|
351 | poly aAsPoly = (poly)a; |
---|
352 | if ((a==NULL) || (!p_IsConstant(aAsPoly, naRing))) return FALSE; |
---|
353 | return n_IsMOne(p_GetCoeff(aAsPoly, naRing), naCoeffs); |
---|
354 | } |
---|
355 | |
---|
356 | /// this is in-place, modifies a |
---|
357 | number naNeg(number a, const coeffs cf) |
---|
358 | { |
---|
359 | naTest(a); |
---|
360 | if (a != NULL) a = (number)p_Neg((poly)a, naRing); |
---|
361 | return a; |
---|
362 | } |
---|
363 | |
---|
364 | number naImPart(number a, const coeffs cf) |
---|
365 | { |
---|
366 | naTest(a); |
---|
367 | return NULL; |
---|
368 | } |
---|
369 | |
---|
370 | number naInit_bigint(number longratBigIntNumber, const coeffs src, const coeffs cf) |
---|
371 | { |
---|
372 | assume( cf != NULL ); |
---|
373 | |
---|
374 | const ring A = cf->extRing; |
---|
375 | |
---|
376 | assume( A != NULL ); |
---|
377 | |
---|
378 | const coeffs C = A->cf; |
---|
379 | |
---|
380 | assume( C != NULL ); |
---|
381 | |
---|
382 | number n = n_Init_bigint(longratBigIntNumber, src, C); |
---|
383 | |
---|
384 | if ( n_IsZero(n, C) ) |
---|
385 | { |
---|
386 | n_Delete(&n, C); |
---|
387 | return NULL; |
---|
388 | } |
---|
389 | |
---|
390 | return (number)p_NSet(n, A); |
---|
391 | } |
---|
392 | |
---|
393 | |
---|
394 | |
---|
395 | number naInit(long i, const coeffs cf) |
---|
396 | { |
---|
397 | if (i == 0) return NULL; |
---|
398 | else return (number)p_ISet(i, naRing); |
---|
399 | } |
---|
400 | |
---|
401 | int naInt(number &a, const coeffs cf) |
---|
402 | { |
---|
403 | naTest(a); |
---|
404 | poly aAsPoly = (poly)a; |
---|
405 | if(aAsPoly == NULL) |
---|
406 | return 0; |
---|
407 | if (!p_IsConstant(aAsPoly, naRing)) |
---|
408 | return 0; |
---|
409 | assume( aAsPoly != NULL ); |
---|
410 | return n_Int(p_GetCoeff(aAsPoly, naRing), naCoeffs); |
---|
411 | } |
---|
412 | |
---|
413 | /* TRUE iff (a != 0 and (b == 0 or deg(a) > deg(b) or (deg(a)==deg(b) && lc(a)>lc(b))) */ |
---|
414 | BOOLEAN naGreater(number a, number b, const coeffs cf) |
---|
415 | { |
---|
416 | naTest(a); naTest(b); |
---|
417 | if (naIsZero(a, cf)) |
---|
418 | { |
---|
419 | if (naIsZero(b, cf)) return FALSE; |
---|
420 | return !n_GreaterZero(pGetCoeff((poly)b),cf); |
---|
421 | } |
---|
422 | if (naIsZero(b, cf)) |
---|
423 | { |
---|
424 | return n_GreaterZero(pGetCoeff((poly)a),cf); |
---|
425 | } |
---|
426 | int aDeg = p_Totaldegree((poly)a, naRing); |
---|
427 | int bDeg = p_Totaldegree((poly)b, naRing); |
---|
428 | if (aDeg>bDeg) return TRUE; |
---|
429 | if (aDeg<bDeg) return FALSE; |
---|
430 | return n_Greater(pGetCoeff((poly)a),pGetCoeff((poly)b),cf); |
---|
431 | } |
---|
432 | |
---|
433 | /* TRUE iff a != 0 and (LC(a) > 0 or deg(a) > 0) */ |
---|
434 | BOOLEAN naGreaterZero(number a, const coeffs cf) |
---|
435 | { |
---|
436 | naTest(a); |
---|
437 | if (a == NULL) return FALSE; |
---|
438 | if (n_GreaterZero(p_GetCoeff((poly)a, naRing), naCoeffs)) return TRUE; |
---|
439 | if (p_Totaldegree((poly)a, naRing) > 0) return TRUE; |
---|
440 | return FALSE; |
---|
441 | } |
---|
442 | |
---|
443 | void naCoeffWrite(const coeffs cf, BOOLEAN details) |
---|
444 | { |
---|
445 | assume( cf != NULL ); |
---|
446 | |
---|
447 | const ring A = cf->extRing; |
---|
448 | |
---|
449 | assume( A != NULL ); |
---|
450 | assume( A->cf != NULL ); |
---|
451 | |
---|
452 | n_CoeffWrite(A->cf, details); |
---|
453 | |
---|
454 | // rWrite(A); |
---|
455 | |
---|
456 | const int P = rVar(A); |
---|
457 | assume( P > 0 ); |
---|
458 | |
---|
459 | Print("// %d parameter : ", P); |
---|
460 | |
---|
461 | for (int nop=0; nop < P; nop ++) |
---|
462 | Print("%s ", rRingVar(nop, A)); |
---|
463 | |
---|
464 | PrintLn(); |
---|
465 | |
---|
466 | const ideal I = A->qideal; |
---|
467 | |
---|
468 | assume( I != NULL ); |
---|
469 | assume( IDELEMS(I) == 1 ); |
---|
470 | |
---|
471 | |
---|
472 | if ( details ) |
---|
473 | { |
---|
474 | PrintS("// minpoly : ("); |
---|
475 | p_Write0( I->m[0], A); |
---|
476 | PrintS(")"); |
---|
477 | } |
---|
478 | else |
---|
479 | PrintS("// minpoly : ..."); |
---|
480 | |
---|
481 | PrintLn(); |
---|
482 | |
---|
483 | /* |
---|
484 | char *x = rRingVar(0, A); |
---|
485 | |
---|
486 | Print("// Coefficients live in the extension field K[%s]/<f(%s)>\n", x, x); |
---|
487 | Print("// with the minimal polynomial f(%s) = %s\n", x, |
---|
488 | p_String(A->qideal->m[0], A)); |
---|
489 | PrintS("// and K: "); |
---|
490 | */ |
---|
491 | } |
---|
492 | |
---|
493 | number naAdd(number a, number b, const coeffs cf) |
---|
494 | { |
---|
495 | naTest(a); naTest(b); |
---|
496 | if (a == NULL) return naCopy(b, cf); |
---|
497 | if (b == NULL) return naCopy(a, cf); |
---|
498 | poly aPlusB = p_Add_q(p_Copy((poly)a, naRing), |
---|
499 | p_Copy((poly)b, naRing), naRing); |
---|
500 | definiteReduce(aPlusB, naMinpoly, cf); |
---|
501 | return (number)aPlusB; |
---|
502 | } |
---|
503 | |
---|
504 | number naSub(number a, number b, const coeffs cf) |
---|
505 | { |
---|
506 | naTest(a); naTest(b); |
---|
507 | if (b == NULL) return naCopy(a, cf); |
---|
508 | poly minusB = p_Neg(p_Copy((poly)b, naRing), naRing); |
---|
509 | if (a == NULL) return (number)minusB; |
---|
510 | poly aMinusB = p_Add_q(p_Copy((poly)a, naRing), minusB, naRing); |
---|
511 | definiteReduce(aMinusB, naMinpoly, cf); |
---|
512 | return (number)aMinusB; |
---|
513 | } |
---|
514 | |
---|
515 | number naMult(number a, number b, const coeffs cf) |
---|
516 | { |
---|
517 | naTest(a); naTest(b); |
---|
518 | if (a == NULL) return NULL; |
---|
519 | if (b == NULL) return NULL; |
---|
520 | poly aTimesB = p_Mult_q(p_Copy((poly)a, naRing), |
---|
521 | p_Copy((poly)b, naRing), naRing); |
---|
522 | definiteReduce(aTimesB, naMinpoly, cf); |
---|
523 | return (number)aTimesB; |
---|
524 | } |
---|
525 | |
---|
526 | number naDiv(number a, number b, const coeffs cf) |
---|
527 | { |
---|
528 | naTest(a); naTest(b); |
---|
529 | if (b == NULL) WerrorS(nDivBy0); |
---|
530 | if (a == NULL) return NULL; |
---|
531 | poly bInverse = (poly)naInvers(b, cf); |
---|
532 | if(bInverse != NULL) // b is non-zero divisor! |
---|
533 | { |
---|
534 | poly aDivB = p_Mult_q(p_Copy((poly)a, naRing), bInverse, naRing); |
---|
535 | definiteReduce(aDivB, naMinpoly, cf); |
---|
536 | return (number)aDivB; |
---|
537 | } |
---|
538 | return NULL; |
---|
539 | } |
---|
540 | |
---|
541 | /* 0^0 = 0; |
---|
542 | for |exp| <= 7 compute power by a simple multiplication loop; |
---|
543 | for |exp| >= 8 compute power along binary presentation of |exp|, e.g. |
---|
544 | p^13 = p^1 * p^4 * p^8, where we utilise that |
---|
545 | p^(2^(k+1)) = p^(2^k) * p^(2^k); |
---|
546 | intermediate reduction modulo the minimal polynomial is controlled by |
---|
547 | the in-place method heuristicReduce(poly, poly, coeffs); see there. |
---|
548 | */ |
---|
549 | void naPower(number a, int exp, number *b, const coeffs cf) |
---|
550 | { |
---|
551 | naTest(a); |
---|
552 | |
---|
553 | /* special cases first */ |
---|
554 | if (a == NULL) |
---|
555 | { |
---|
556 | if (exp >= 0) *b = NULL; |
---|
557 | else WerrorS(nDivBy0); |
---|
558 | } |
---|
559 | else if (exp == 0) { *b = naInit(1, cf); return; } |
---|
560 | else if (exp == 1) { *b = naCopy(a, cf); return; } |
---|
561 | else if (exp == -1) { *b = naInvers(a, cf); return; } |
---|
562 | |
---|
563 | int expAbs = exp; if (expAbs < 0) expAbs = -expAbs; |
---|
564 | |
---|
565 | /* now compute a^expAbs */ |
---|
566 | poly pow; poly aAsPoly = (poly)a; |
---|
567 | if (expAbs <= 7) |
---|
568 | { |
---|
569 | pow = p_Copy(aAsPoly, naRing); |
---|
570 | for (int i = 2; i <= expAbs; i++) |
---|
571 | { |
---|
572 | pow = p_Mult_q(pow, p_Copy(aAsPoly, naRing), naRing); |
---|
573 | heuristicReduce(pow, naMinpoly, cf); |
---|
574 | } |
---|
575 | definiteReduce(pow, naMinpoly, cf); |
---|
576 | } |
---|
577 | else |
---|
578 | { |
---|
579 | pow = p_ISet(1, naRing); |
---|
580 | poly factor = p_Copy(aAsPoly, naRing); |
---|
581 | while (expAbs != 0) |
---|
582 | { |
---|
583 | if (expAbs & 1) |
---|
584 | { |
---|
585 | pow = p_Mult_q(pow, p_Copy(factor, naRing), naRing); |
---|
586 | heuristicReduce(pow, naMinpoly, cf); |
---|
587 | } |
---|
588 | expAbs = expAbs / 2; |
---|
589 | if (expAbs != 0) |
---|
590 | { |
---|
591 | factor = p_Mult_q(factor, p_Copy(factor, naRing), naRing); |
---|
592 | heuristicReduce(factor, naMinpoly, cf); |
---|
593 | } |
---|
594 | } |
---|
595 | p_Delete(&factor, naRing); |
---|
596 | definiteReduce(pow, naMinpoly, cf); |
---|
597 | } |
---|
598 | |
---|
599 | /* invert if original exponent was negative */ |
---|
600 | number n = (number)pow; |
---|
601 | if (exp < 0) |
---|
602 | { |
---|
603 | number m = naInvers(n, cf); |
---|
604 | naDelete(&n, cf); |
---|
605 | n = m; |
---|
606 | } |
---|
607 | *b = n; |
---|
608 | } |
---|
609 | |
---|
610 | /* may reduce p modulo the reducer by calling definiteReduce; |
---|
611 | the decision is made based on the following heuristic |
---|
612 | (which should also only be changed here in this method): |
---|
613 | if (deg(p) > 10*deg(reducer) then perform reduction; |
---|
614 | modifies p */ |
---|
615 | void heuristicReduce(poly &p, poly reducer, const coeffs cf) |
---|
616 | { |
---|
617 | #ifdef LDEBUG |
---|
618 | p_Test((poly)p, naRing); |
---|
619 | p_Test((poly)reducer, naRing); |
---|
620 | #endif |
---|
621 | if (p_Totaldegree(p, naRing) > 10 * p_Totaldegree(reducer, naRing)) |
---|
622 | definiteReduce(p, reducer, cf); |
---|
623 | } |
---|
624 | |
---|
625 | void naWriteLong(number &a, const coeffs cf) |
---|
626 | { |
---|
627 | naTest(a); |
---|
628 | if (a == NULL) |
---|
629 | StringAppendS("0"); |
---|
630 | else |
---|
631 | { |
---|
632 | poly aAsPoly = (poly)a; |
---|
633 | /* basically, just write aAsPoly using p_Write, |
---|
634 | but use brackets around the output, if a is not |
---|
635 | a constant living in naCoeffs = cf->extRing->cf */ |
---|
636 | BOOLEAN useBrackets = !(p_IsConstant(aAsPoly, naRing)); |
---|
637 | if (useBrackets) StringAppendS("("); |
---|
638 | p_String0Long(aAsPoly, naRing, naRing); |
---|
639 | if (useBrackets) StringAppendS(")"); |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | void naWriteShort(number &a, const coeffs cf) |
---|
644 | { |
---|
645 | naTest(a); |
---|
646 | if (a == NULL) |
---|
647 | StringAppendS("0"); |
---|
648 | else |
---|
649 | { |
---|
650 | poly aAsPoly = (poly)a; |
---|
651 | /* basically, just write aAsPoly using p_Write, |
---|
652 | but use brackets around the output, if a is not |
---|
653 | a constant living in naCoeffs = cf->extRing->cf */ |
---|
654 | BOOLEAN useBrackets = !(p_IsConstant(aAsPoly, naRing)); |
---|
655 | if (useBrackets) StringAppendS("("); |
---|
656 | p_String0Short(aAsPoly, naRing, naRing); |
---|
657 | if (useBrackets) StringAppendS(")"); |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | const char * naRead(const char *s, number *a, const coeffs cf) |
---|
662 | { |
---|
663 | poly aAsPoly; |
---|
664 | const char * result = p_Read(s, aAsPoly, naRing); |
---|
665 | definiteReduce(aAsPoly, naMinpoly, cf); |
---|
666 | *a = (number)aAsPoly; |
---|
667 | return result; |
---|
668 | } |
---|
669 | |
---|
670 | #if 0 |
---|
671 | /* implemented by the rule lcm(a, b) = a * b / gcd(a, b) */ |
---|
672 | number naLcm(number a, number b, const coeffs cf) |
---|
673 | { |
---|
674 | naTest(a); naTest(b); |
---|
675 | if (a == NULL) return NULL; |
---|
676 | if (b == NULL) return NULL; |
---|
677 | number theProduct = (number)p_Mult_q(p_Copy((poly)a, naRing), |
---|
678 | p_Copy((poly)b, naRing), naRing); |
---|
679 | /* note that theProduct needs not be reduced w.r.t. naMinpoly; |
---|
680 | but the final division will take care of the necessary reduction */ |
---|
681 | number theGcd = naGcd(a, b, cf); |
---|
682 | return naDiv(theProduct, theGcd, cf); |
---|
683 | } |
---|
684 | #endif |
---|
685 | |
---|
686 | /* expects *param to be castable to AlgExtInfo */ |
---|
687 | static BOOLEAN naCoeffIsEqual(const coeffs cf, n_coeffType n, void * param) |
---|
688 | { |
---|
689 | if (ID != n) return FALSE; |
---|
690 | AlgExtInfo *e = (AlgExtInfo *)param; |
---|
691 | /* for extension coefficient fields we expect the underlying |
---|
692 | polynomial rings to be IDENTICAL, i.e. the SAME OBJECT; |
---|
693 | this expectation is based on the assumption that we have properly |
---|
694 | registered cf and perform reference counting rather than creating |
---|
695 | multiple copies of the same coefficient field/domain/ring */ |
---|
696 | if (naRing == e->r) |
---|
697 | return TRUE; |
---|
698 | /* (Note that then also the minimal ideals will necessarily be |
---|
699 | the same, as they are attached to the ring.) */ |
---|
700 | |
---|
701 | // NOTE: Q(a)[x] && Q(a)[y] should better share the _same_ Q(a)... |
---|
702 | if( rEqual(naRing, e->r, TRUE) ) // also checks the equality of qideals |
---|
703 | { |
---|
704 | const ideal mi = naRing->qideal; |
---|
705 | assume( IDELEMS(mi) == 1 ); |
---|
706 | const ideal ii = e->r->qideal; |
---|
707 | assume( IDELEMS(ii) == 1 ); |
---|
708 | |
---|
709 | // TODO: the following should be extended for 2 *equal* rings... |
---|
710 | assume( p_EqualPolys(mi->m[0], ii->m[0], naRing, e->r) ); |
---|
711 | |
---|
712 | rDelete(e->r); |
---|
713 | |
---|
714 | return TRUE; |
---|
715 | } |
---|
716 | |
---|
717 | return FALSE; |
---|
718 | |
---|
719 | } |
---|
720 | |
---|
721 | int naSize(number a, const coeffs cf) |
---|
722 | { |
---|
723 | if (a == NULL) return -1; |
---|
724 | /* this has been taken from the old implementation of field extensions, |
---|
725 | where we computed the sum of the degree and the number of terms in |
---|
726 | (poly)a; so we leave it at that, for the time being; |
---|
727 | maybe, the number of terms alone is a better measure? */ |
---|
728 | poly aAsPoly = (poly)a; |
---|
729 | int theDegree = 0; int noOfTerms = 0; |
---|
730 | while (aAsPoly != NULL) |
---|
731 | { |
---|
732 | noOfTerms++; |
---|
733 | int d = p_GetExp(aAsPoly, 1, naRing); |
---|
734 | if (d > theDegree) theDegree = d; |
---|
735 | pIter(aAsPoly); |
---|
736 | } |
---|
737 | return theDegree + noOfTerms; |
---|
738 | } |
---|
739 | |
---|
740 | /* performs polynomial division and overrides p by the remainder |
---|
741 | of division of p by the reducer; |
---|
742 | modifies p */ |
---|
743 | void definiteReduce(poly &p, poly reducer, const coeffs cf) |
---|
744 | { |
---|
745 | #ifdef LDEBUG |
---|
746 | p_Test((poly)p, naRing); |
---|
747 | p_Test((poly)reducer, naRing); |
---|
748 | #endif |
---|
749 | if ((p!=NULL) && (p_GetExp(p,1,naRing)>=p_GetExp(reducer,1,naRing))) |
---|
750 | { |
---|
751 | p_PolyDiv(p, reducer, FALSE, naRing); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | void naNormalize(number &a, const coeffs cf) |
---|
756 | { |
---|
757 | poly aa=(poly)a; |
---|
758 | if (aa!=naMinpoly) |
---|
759 | definiteReduce(aa,naMinpoly,cf); |
---|
760 | a=(number)aa; |
---|
761 | } |
---|
762 | |
---|
763 | #ifdef HAVE_FACTORY |
---|
764 | number naConvFactoryNSingN( const CanonicalForm n, const coeffs cf) |
---|
765 | { |
---|
766 | if (n.isZero()) return NULL; |
---|
767 | poly p=convFactoryPSingP(n,naRing); |
---|
768 | return (number)p; |
---|
769 | } |
---|
770 | CanonicalForm naConvSingNFactoryN( number n, BOOLEAN /*setChar*/, const coeffs cf ) |
---|
771 | { |
---|
772 | naTest(n); |
---|
773 | if (n==NULL) return CanonicalForm(0); |
---|
774 | |
---|
775 | return convSingPFactoryP((poly)n,naRing); |
---|
776 | } |
---|
777 | #endif |
---|
778 | |
---|
779 | |
---|
780 | /* IMPORTANT NOTE: Since an algebraic field extension is again a field, |
---|
781 | the gcd of two elements is not very interesting. (It |
---|
782 | is actually any unit in the field, i.e., any non- |
---|
783 | zero element.) Note that the below method does not operate |
---|
784 | in this strong sense but rather computes the gcd of |
---|
785 | two given elements in the underlying polynomial ring. */ |
---|
786 | number naGcd(number a, number b, const coeffs cf) |
---|
787 | { |
---|
788 | naTest(a); naTest(b); |
---|
789 | if ((a == NULL) && (b == NULL)) WerrorS(nDivBy0); |
---|
790 | const ring R = naRing; |
---|
791 | return (number) singclap_gcd(p_Copy((poly)a, R), p_Copy((poly)b, R), R); |
---|
792 | // return (number)p_Gcd((poly)a, (poly)b, naRing); |
---|
793 | } |
---|
794 | |
---|
795 | number naInvers(number a, const coeffs cf) |
---|
796 | { |
---|
797 | naTest(a); |
---|
798 | if (a == NULL) WerrorS(nDivBy0); |
---|
799 | |
---|
800 | poly aFactor = NULL; poly mFactor = NULL; poly theGcd = NULL; |
---|
801 | // singclap_extgcd! |
---|
802 | const BOOLEAN ret = singclap_extgcd ((poly)a, naMinpoly, theGcd, aFactor, mFactor, naRing); |
---|
803 | |
---|
804 | assume( !ret ); |
---|
805 | |
---|
806 | // if( ret ) theGcd = p_ExtGcd((poly)a, aFactor, naMinpoly, mFactor, naRing); |
---|
807 | |
---|
808 | naTest((number)theGcd); naTest((number)aFactor); naTest((number)mFactor); |
---|
809 | p_Delete(&mFactor, naRing); |
---|
810 | |
---|
811 | // /* the gcd must be 1 since naMinpoly is irreducible and a != NULL: */ |
---|
812 | // assume(naIsOne((number)theGcd, cf)); |
---|
813 | |
---|
814 | if( !naIsOne((number)theGcd, cf) ) |
---|
815 | { |
---|
816 | WerrorS("zero divisor found - your minpoly is not irreducible"); |
---|
817 | p_Delete(&aFactor, naRing); aFactor = NULL; |
---|
818 | } |
---|
819 | p_Delete(&theGcd, naRing); |
---|
820 | |
---|
821 | return (number)(aFactor); |
---|
822 | } |
---|
823 | |
---|
824 | /* assumes that src = Q, dst = Q(a) */ |
---|
825 | number naMap00(number a, const coeffs src, const coeffs dst) |
---|
826 | { |
---|
827 | if (n_IsZero(a, src)) return NULL; |
---|
828 | assume(src == dst->extRing->cf); |
---|
829 | poly result = p_One(dst->extRing); |
---|
830 | p_SetCoeff(result, n_Copy(a, src), dst->extRing); |
---|
831 | return (number)result; |
---|
832 | } |
---|
833 | |
---|
834 | /* assumes that src = Z/p, dst = Q(a) */ |
---|
835 | number naMapP0(number a, const coeffs src, const coeffs dst) |
---|
836 | { |
---|
837 | if (n_IsZero(a, src)) return NULL; |
---|
838 | /* mapping via intermediate int: */ |
---|
839 | int n = n_Int(a, src); |
---|
840 | number q = n_Init(n, dst->extRing->cf); |
---|
841 | poly result = p_One(dst->extRing); |
---|
842 | p_SetCoeff(result, q, dst->extRing); |
---|
843 | return (number)result; |
---|
844 | } |
---|
845 | |
---|
846 | #if 0 |
---|
847 | /* assumes that either src = Q(a), dst = Q(a), or |
---|
848 | src = Z/p(a), dst = Z/p(a) */ |
---|
849 | number naCopyMap(number a, const coeffs src, const coeffs dst) |
---|
850 | { |
---|
851 | return naCopy(a, dst); |
---|
852 | } |
---|
853 | #endif |
---|
854 | |
---|
855 | number naCopyExt(number a, const coeffs src, const coeffs) |
---|
856 | { |
---|
857 | fraction fa=(fraction)a; |
---|
858 | return (number)p_Copy(NUM(fa),src->extRing); |
---|
859 | } |
---|
860 | |
---|
861 | /* assumes that src = Q, dst = Z/p(a) */ |
---|
862 | number naMap0P(number a, const coeffs src, const coeffs dst) |
---|
863 | { |
---|
864 | if (n_IsZero(a, src)) return NULL; |
---|
865 | int p = rChar(dst->extRing); |
---|
866 | |
---|
867 | number q = nlModP(a, src, dst->extRing->cf); |
---|
868 | |
---|
869 | poly result = p_NSet(q, dst->extRing); |
---|
870 | |
---|
871 | return (number)result; |
---|
872 | } |
---|
873 | |
---|
874 | /* assumes that src = Z/p, dst = Z/p(a) */ |
---|
875 | number naMapPP(number a, const coeffs src, const coeffs dst) |
---|
876 | { |
---|
877 | if (n_IsZero(a, src)) return NULL; |
---|
878 | assume(src == dst->extRing->cf); |
---|
879 | poly result = p_One(dst->extRing); |
---|
880 | p_SetCoeff(result, n_Copy(a, src), dst->extRing); |
---|
881 | return (number)result; |
---|
882 | } |
---|
883 | |
---|
884 | /* assumes that src = Z/u, dst = Z/p(a), where u != p */ |
---|
885 | number naMapUP(number a, const coeffs src, const coeffs dst) |
---|
886 | { |
---|
887 | if (n_IsZero(a, src)) return NULL; |
---|
888 | /* mapping via intermediate int: */ |
---|
889 | int n = n_Int(a, src); |
---|
890 | number q = n_Init(n, dst->extRing->cf); |
---|
891 | poly result = p_One(dst->extRing); |
---|
892 | p_SetCoeff(result, q, dst->extRing); |
---|
893 | return (number)result; |
---|
894 | } |
---|
895 | |
---|
896 | nMapFunc naSetMap(const coeffs src, const coeffs dst) |
---|
897 | { |
---|
898 | /* dst is expected to be an algebraic field extension */ |
---|
899 | assume(getCoeffType(dst) == ID); |
---|
900 | |
---|
901 | if( src == dst ) return ndCopyMap; |
---|
902 | |
---|
903 | int h = 0; /* the height of the extension tower given by dst */ |
---|
904 | coeffs bDst = nCoeff_bottom(dst, h); /* the bottom field in the tower dst */ |
---|
905 | coeffs bSrc = nCoeff_bottom(src, h); /* the bottom field in the tower src */ |
---|
906 | |
---|
907 | /* for the time being, we only provide maps if h = 1 and if b is Q or |
---|
908 | some field Z/pZ: */ |
---|
909 | if (h==0) |
---|
910 | { |
---|
911 | if (nCoeff_is_Q(src) && nCoeff_is_Q(bDst)) |
---|
912 | return naMap00; /// Q --> Q(a) |
---|
913 | if (nCoeff_is_Zp(src) && nCoeff_is_Q(bDst)) |
---|
914 | return naMapP0; /// Z/p --> Q(a) |
---|
915 | if (nCoeff_is_Q(src) && nCoeff_is_Zp(bDst)) |
---|
916 | return naMap0P; /// Q --> Z/p(a) |
---|
917 | if (nCoeff_is_Zp(src) && nCoeff_is_Zp(bDst)) |
---|
918 | { |
---|
919 | if (src->ch == dst->ch) return naMapPP; /// Z/p --> Z/p(a) |
---|
920 | else return naMapUP; /// Z/u --> Z/p(a) |
---|
921 | } |
---|
922 | } |
---|
923 | if (h != 1) return NULL; |
---|
924 | if ((!nCoeff_is_Zp(bDst)) && (!nCoeff_is_Q(bDst))) return NULL; |
---|
925 | if ((!nCoeff_is_Zp(bSrc)) && (!nCoeff_is_Q(bSrc))) return NULL; |
---|
926 | |
---|
927 | if (nCoeff_is_Q(bSrc) && nCoeff_is_Q(bDst)) |
---|
928 | { |
---|
929 | if (strcmp(rRingVar(0, src->extRing), |
---|
930 | rRingVar(0, dst->extRing)) == 0) |
---|
931 | { |
---|
932 | if (src->type==n_algExt) |
---|
933 | return ndCopyMap; // naCopyMap; /// Q(a) --> Q(a) |
---|
934 | else |
---|
935 | return naCopyExt; |
---|
936 | } |
---|
937 | else |
---|
938 | return NULL; /// Q(b) --> Q(a) |
---|
939 | } |
---|
940 | |
---|
941 | if (nCoeff_is_Zp(bSrc) && nCoeff_is_Zp(bDst)) |
---|
942 | { |
---|
943 | if (strcmp(rRingVar(0,src->extRing),rRingVar(0,dst->extRing))==0) |
---|
944 | { |
---|
945 | if (src->type==n_algExt) |
---|
946 | return ndCopyMap; // naCopyMap; /// Z/p(a) --> Z/p(a) |
---|
947 | else |
---|
948 | return naCopyExt; |
---|
949 | } |
---|
950 | else |
---|
951 | return NULL; /// Z/p(b) --> Z/p(a) |
---|
952 | } |
---|
953 | |
---|
954 | return NULL; /// default |
---|
955 | } |
---|
956 | |
---|
957 | static int naParDeg(number a, const coeffs cf) |
---|
958 | { |
---|
959 | if (a == NULL) return -1; |
---|
960 | poly aa=(poly)a; |
---|
961 | return cf->extRing->pFDeg(aa,cf->extRing); |
---|
962 | } |
---|
963 | |
---|
964 | /// return the specified parameter as a number in the given alg. field |
---|
965 | static number naParameter(const int iParameter, const coeffs cf) |
---|
966 | { |
---|
967 | assume(getCoeffType(cf) == ID); |
---|
968 | |
---|
969 | const ring R = cf->extRing; |
---|
970 | assume( R != NULL ); |
---|
971 | assume( 0 < iParameter && iParameter <= rVar(R) ); |
---|
972 | |
---|
973 | poly p = p_One(R); p_SetExp(p, iParameter, 1, R); p_Setm(p, R); |
---|
974 | |
---|
975 | return (number) p; |
---|
976 | } |
---|
977 | |
---|
978 | |
---|
979 | /// if m == var(i)/1 => return i, |
---|
980 | int naIsParam(number m, const coeffs cf) |
---|
981 | { |
---|
982 | assume(getCoeffType(cf) == ID); |
---|
983 | |
---|
984 | const ring R = cf->extRing; |
---|
985 | assume( R != NULL ); |
---|
986 | |
---|
987 | return p_Var( (poly)m, R ); |
---|
988 | } |
---|
989 | |
---|
990 | |
---|
991 | static void naClearContent(ICoeffsEnumerator& numberCollectionEnumerator, number& c, const coeffs cf) |
---|
992 | { |
---|
993 | assume(cf != NULL); |
---|
994 | assume(getCoeffType(cf) == ID); |
---|
995 | assume(nCoeff_is_Q_algext(cf)); // only over (Q[a]/m(a)), while the default impl. is used over Zp[a]/m(a) ! |
---|
996 | |
---|
997 | const ring R = cf->extRing; |
---|
998 | assume(R != NULL); |
---|
999 | const coeffs Q = R->cf; |
---|
1000 | assume(Q != NULL); |
---|
1001 | assume(nCoeff_is_Q(Q)); |
---|
1002 | |
---|
1003 | numberCollectionEnumerator.Reset(); |
---|
1004 | |
---|
1005 | if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial? |
---|
1006 | { |
---|
1007 | c = n_Init(1, cf); |
---|
1008 | return; |
---|
1009 | } |
---|
1010 | |
---|
1011 | naTest(numberCollectionEnumerator.Current()); |
---|
1012 | |
---|
1013 | // part 1, find a small candidate for gcd |
---|
1014 | int s1; int s=2147483647; // max. int |
---|
1015 | |
---|
1016 | const BOOLEAN lc_is_pos=naGreaterZero(numberCollectionEnumerator.Current(),cf); |
---|
1017 | |
---|
1018 | int normalcount = 0; |
---|
1019 | |
---|
1020 | poly cand1, cand; |
---|
1021 | |
---|
1022 | do |
---|
1023 | { |
---|
1024 | number& n = numberCollectionEnumerator.Current(); |
---|
1025 | naNormalize(n, cf); ++normalcount; |
---|
1026 | |
---|
1027 | naTest(n); |
---|
1028 | |
---|
1029 | cand1 = (poly)n; |
---|
1030 | |
---|
1031 | s1 = p_Deg(cand1, R); // naSize? |
---|
1032 | if (s>s1) |
---|
1033 | { |
---|
1034 | cand = cand1; |
---|
1035 | s = s1; |
---|
1036 | } |
---|
1037 | } while (numberCollectionEnumerator.MoveNext() ); |
---|
1038 | |
---|
1039 | // assume( nlGreaterZero(cand,cf) ); // cand may be a negative integer! |
---|
1040 | |
---|
1041 | cand = p_Copy(cand, R); |
---|
1042 | // part 2: compute gcd(cand,all coeffs) |
---|
1043 | |
---|
1044 | numberCollectionEnumerator.Reset(); |
---|
1045 | |
---|
1046 | int length = 0; |
---|
1047 | while (numberCollectionEnumerator.MoveNext() ) |
---|
1048 | { |
---|
1049 | number& n = numberCollectionEnumerator.Current(); |
---|
1050 | ++length; |
---|
1051 | |
---|
1052 | if( (--normalcount) <= 0) |
---|
1053 | naNormalize(n, cf); |
---|
1054 | |
---|
1055 | naTest(n); |
---|
1056 | |
---|
1057 | // p_InpGcd(cand, (poly)n, R); |
---|
1058 | |
---|
1059 | cand = singclap_gcd(cand, p_Copy((poly)n, R), R); |
---|
1060 | |
---|
1061 | // cand1 = p_Gcd(cand,(poly)n, R); p_Delete(&cand, R); cand = cand1; |
---|
1062 | |
---|
1063 | assume( naGreaterZero((number)cand, cf) ); // ??? |
---|
1064 | /* |
---|
1065 | if(p_IsConstant(cand,R)) |
---|
1066 | { |
---|
1067 | c = cand; |
---|
1068 | |
---|
1069 | if(!lc_is_pos) |
---|
1070 | { |
---|
1071 | // make the leading coeff positive |
---|
1072 | c = nlNeg(c, cf); |
---|
1073 | numberCollectionEnumerator.Reset(); |
---|
1074 | |
---|
1075 | while (numberCollectionEnumerator.MoveNext() ) |
---|
1076 | { |
---|
1077 | number& nn = numberCollectionEnumerator.Current(); |
---|
1078 | nn = nlNeg(nn, cf); |
---|
1079 | } |
---|
1080 | } |
---|
1081 | return; |
---|
1082 | } |
---|
1083 | */ |
---|
1084 | |
---|
1085 | } |
---|
1086 | |
---|
1087 | // part3: all coeffs = all coeffs / cand |
---|
1088 | if (!lc_is_pos) |
---|
1089 | cand = p_Neg(cand, R); |
---|
1090 | |
---|
1091 | c = (number)cand; naTest(c); |
---|
1092 | |
---|
1093 | poly cInverse = (poly)naInvers(c, cf); |
---|
1094 | assume(cInverse != NULL); // c is non-zero divisor!? |
---|
1095 | |
---|
1096 | |
---|
1097 | numberCollectionEnumerator.Reset(); |
---|
1098 | |
---|
1099 | |
---|
1100 | while (numberCollectionEnumerator.MoveNext() ) |
---|
1101 | { |
---|
1102 | number& n = numberCollectionEnumerator.Current(); |
---|
1103 | |
---|
1104 | assume( length > 0 ); |
---|
1105 | |
---|
1106 | if( --length > 0 ) |
---|
1107 | { |
---|
1108 | assume( cInverse != NULL ); |
---|
1109 | n = (number) p_Mult_q(p_Copy(cInverse, R), (poly)n, R); |
---|
1110 | } |
---|
1111 | else |
---|
1112 | { |
---|
1113 | n = (number) p_Mult_q(cInverse, (poly)n, R); |
---|
1114 | cInverse = NULL; |
---|
1115 | assume(length == 0); |
---|
1116 | } |
---|
1117 | |
---|
1118 | definiteReduce((poly &)n, naMinpoly, cf); |
---|
1119 | } |
---|
1120 | |
---|
1121 | assume(length == 0); |
---|
1122 | assume(cInverse == NULL); // p_Delete(&cInverse, R); |
---|
1123 | |
---|
1124 | // Quick and dirty fix for constant content clearing... !? |
---|
1125 | CRecursivePolyCoeffsEnumerator<NAConverter> itr(numberCollectionEnumerator); // recursively treat the numbers as polys! |
---|
1126 | |
---|
1127 | number cc; |
---|
1128 | |
---|
1129 | extern void nlClearContentNoPositiveLead(ICoeffsEnumerator&, number&, const coeffs); |
---|
1130 | |
---|
1131 | nlClearContentNoPositiveLead(itr, cc, Q); // TODO: get rid of (-LC) normalization!? |
---|
1132 | |
---|
1133 | // over alg. ext. of Q // takes over the input number |
---|
1134 | c = (number) p_Mult_nn( (poly)c, cc, R); |
---|
1135 | // p_Mult_q(p_NSet(cc, R), , R); |
---|
1136 | |
---|
1137 | n_Delete(&cc, Q); |
---|
1138 | |
---|
1139 | // TODO: the above is not enough! need GCD's of polynomial coeffs...! |
---|
1140 | /* |
---|
1141 | // old and wrong part of p_Content |
---|
1142 | if (rField_is_Q_a(r) && !CLEARENUMERATORS) // should not be used anymore if CLEARENUMERATORS is 1 |
---|
1143 | { |
---|
1144 | // we only need special handling for alg. ext. |
---|
1145 | if (getCoeffType(r->cf)==n_algExt) |
---|
1146 | { |
---|
1147 | number hzz = n_Init(1, r->cf->extRing->cf); |
---|
1148 | p=ph; |
---|
1149 | while (p!=NULL) |
---|
1150 | { // each monom: coeff in Q_a |
---|
1151 | poly c_n_n=(poly)pGetCoeff(p); |
---|
1152 | poly c_n=c_n_n; |
---|
1153 | while (c_n!=NULL) |
---|
1154 | { // each monom: coeff in Q |
---|
1155 | d=n_Lcm(hzz,pGetCoeff(c_n),r->cf->extRing->cf); |
---|
1156 | n_Delete(&hzz,r->cf->extRing->cf); |
---|
1157 | hzz=d; |
---|
1158 | pIter(c_n); |
---|
1159 | } |
---|
1160 | pIter(p); |
---|
1161 | } |
---|
1162 | // hzz contains the 1/lcm of all denominators in c_n_n |
---|
1163 | h=n_Invers(hzz,r->cf->extRing->cf); |
---|
1164 | n_Delete(&hzz,r->cf->extRing->cf); |
---|
1165 | n_Normalize(h,r->cf->extRing->cf); |
---|
1166 | if(!n_IsOne(h,r->cf->extRing->cf)) |
---|
1167 | { |
---|
1168 | p=ph; |
---|
1169 | while (p!=NULL) |
---|
1170 | { // each monom: coeff in Q_a |
---|
1171 | poly c_n=(poly)pGetCoeff(p); |
---|
1172 | while (c_n!=NULL) |
---|
1173 | { // each monom: coeff in Q |
---|
1174 | d=n_Mult(h,pGetCoeff(c_n),r->cf->extRing->cf); |
---|
1175 | n_Normalize(d,r->cf->extRing->cf); |
---|
1176 | n_Delete(&pGetCoeff(c_n),r->cf->extRing->cf); |
---|
1177 | pGetCoeff(c_n)=d; |
---|
1178 | pIter(c_n); |
---|
1179 | } |
---|
1180 | pIter(p); |
---|
1181 | } |
---|
1182 | } |
---|
1183 | n_Delete(&h,r->cf->extRing->cf); |
---|
1184 | } |
---|
1185 | } |
---|
1186 | */ |
---|
1187 | |
---|
1188 | |
---|
1189 | // c = n_Init(1, cf); assume(FALSE); // TODO: NOT YET IMPLEMENTED!!! |
---|
1190 | } |
---|
1191 | |
---|
1192 | |
---|
1193 | static void naClearDenominators(ICoeffsEnumerator& numberCollectionEnumerator, number& c, const coeffs cf) |
---|
1194 | { |
---|
1195 | assume(cf != NULL); |
---|
1196 | assume(getCoeffType(cf) == ID); |
---|
1197 | assume(nCoeff_is_Q_algext(cf)); // only over (Q[a]/m(a)), while the default impl. is used over Zp[a]/m(a) ! |
---|
1198 | |
---|
1199 | assume(cf->extRing != NULL); |
---|
1200 | const coeffs Q = cf->extRing->cf; |
---|
1201 | assume(Q != NULL); |
---|
1202 | assume(nCoeff_is_Q(Q)); |
---|
1203 | number n; |
---|
1204 | CRecursivePolyCoeffsEnumerator<NAConverter> itr(numberCollectionEnumerator); // recursively treat the numbers as polys! |
---|
1205 | |
---|
1206 | extern void nlClearDenominatorsNoPositiveLead(ICoeffsEnumerator&, number&, const coeffs); |
---|
1207 | |
---|
1208 | nlClearDenominatorsNoPositiveLead(itr, n, Q); // this should probably be fine... |
---|
1209 | c = (number)p_NSet(n, cf->extRing); // over alg. ext. of Q // takes over the input number |
---|
1210 | } |
---|
1211 | |
---|
1212 | |
---|
1213 | BOOLEAN naInitChar(coeffs cf, void * infoStruct) |
---|
1214 | { |
---|
1215 | assume( infoStruct != NULL ); |
---|
1216 | |
---|
1217 | AlgExtInfo *e = (AlgExtInfo *)infoStruct; |
---|
1218 | /// first check whether cf->extRing != NULL and delete old ring??? |
---|
1219 | |
---|
1220 | assume(e->r != NULL); // extRing; |
---|
1221 | assume(e->r->cf != NULL); // extRing->cf; |
---|
1222 | |
---|
1223 | assume((e->r->qideal != NULL) && // minideal has one |
---|
1224 | (IDELEMS(e->r->qideal) == 1) && // non-zero generator |
---|
1225 | (e->r->qideal->m[0] != NULL) ); // at m[0]; |
---|
1226 | |
---|
1227 | assume( cf != NULL ); |
---|
1228 | assume(getCoeffType(cf) == ID); // coeff type; |
---|
1229 | |
---|
1230 | e->r->ref ++; // increase the ref.counter for the ground poly. ring! |
---|
1231 | const ring R = e->r; // no copy! |
---|
1232 | assume( R->qideal == e->r->qideal ); |
---|
1233 | cf->extRing = R; |
---|
1234 | |
---|
1235 | /* propagate characteristic up so that it becomes |
---|
1236 | directly accessible in cf: */ |
---|
1237 | cf->ch = R->cf->ch; |
---|
1238 | |
---|
1239 | #ifdef LDEBUG |
---|
1240 | p_Test((poly)naMinpoly, naRing); |
---|
1241 | #endif |
---|
1242 | |
---|
1243 | cf->cfGreaterZero = naGreaterZero; |
---|
1244 | cf->cfGreater = naGreater; |
---|
1245 | cf->cfEqual = naEqual; |
---|
1246 | cf->cfIsZero = naIsZero; |
---|
1247 | cf->cfIsOne = naIsOne; |
---|
1248 | cf->cfIsMOne = naIsMOne; |
---|
1249 | cf->cfInit = naInit; |
---|
1250 | cf->cfInit_bigint = naInit_bigint; |
---|
1251 | cf->cfInt = naInt; |
---|
1252 | cf->cfNeg = naNeg; |
---|
1253 | cf->cfAdd = naAdd; |
---|
1254 | cf->cfSub = naSub; |
---|
1255 | cf->cfMult = naMult; |
---|
1256 | cf->cfDiv = naDiv; |
---|
1257 | cf->cfExactDiv = naDiv; |
---|
1258 | cf->cfPower = naPower; |
---|
1259 | cf->cfCopy = naCopy; |
---|
1260 | |
---|
1261 | cf->cfWriteLong = naWriteLong; |
---|
1262 | |
---|
1263 | if( rCanShortOut(naRing) ) |
---|
1264 | cf->cfWriteShort = naWriteShort; |
---|
1265 | else |
---|
1266 | cf->cfWriteShort = naWriteLong; |
---|
1267 | |
---|
1268 | cf->cfRead = naRead; |
---|
1269 | cf->cfDelete = naDelete; |
---|
1270 | cf->cfSetMap = naSetMap; |
---|
1271 | cf->cfGetDenom = naGetDenom; |
---|
1272 | cf->cfGetNumerator = naGetNumerator; |
---|
1273 | cf->cfRePart = naCopy; |
---|
1274 | cf->cfImPart = naImPart; |
---|
1275 | cf->cfCoeffWrite = naCoeffWrite; |
---|
1276 | cf->cfNormalize = naNormalize; |
---|
1277 | #ifdef LDEBUG |
---|
1278 | cf->cfDBTest = naDBTest; |
---|
1279 | #endif |
---|
1280 | cf->cfGcd = naGcd; |
---|
1281 | //cf->cfLcm = naLcm; |
---|
1282 | cf->cfSize = naSize; |
---|
1283 | cf->nCoeffIsEqual = naCoeffIsEqual; |
---|
1284 | cf->cfInvers = naInvers; |
---|
1285 | cf->cfIntDiv = naDiv; // ??? |
---|
1286 | #ifdef HAVE_FACTORY |
---|
1287 | cf->convFactoryNSingN=naConvFactoryNSingN; |
---|
1288 | cf->convSingNFactoryN=naConvSingNFactoryN; |
---|
1289 | #endif |
---|
1290 | cf->cfParDeg = naParDeg; |
---|
1291 | |
---|
1292 | cf->iNumberOfParameters = rVar(R); |
---|
1293 | cf->pParameterNames = R->names; |
---|
1294 | cf->cfParameter = naParameter; |
---|
1295 | |
---|
1296 | if( nCoeff_is_Q(R->cf) ) |
---|
1297 | { |
---|
1298 | cf->cfClearContent = naClearContent; |
---|
1299 | cf->cfClearDenominators = naClearDenominators; |
---|
1300 | } |
---|
1301 | |
---|
1302 | return FALSE; |
---|
1303 | } |
---|