1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: numbers in a rational function field K(t_1, .., t_s) with |
---|
6 | * transcendental variables t_1, ..., t_s, where s >= 1. |
---|
7 | * Denoting the implemented coeffs object by cf, then these numbers |
---|
8 | * are represented as quotients of polynomials living in the |
---|
9 | * polynomial ring K[t_1, .., t_s] represented by cf->extring. |
---|
10 | * |
---|
11 | * An element of K(t_1, .., t_s) may have numerous representations, |
---|
12 | * due to the possibility of common polynomial factors in the |
---|
13 | * numerator and denominator. This problem is handled by a |
---|
14 | * cancellation heuristic: Each number "knows" its complexity |
---|
15 | * which is 0 if and only if common factors have definitely been |
---|
16 | * cancelled, and some positive integer otherwise. |
---|
17 | * Each arithmetic operation of two numbers with complexities c1 |
---|
18 | * and c2 will result in a number of complexity c1 + c2 + some |
---|
19 | * penalty (specific for each arithmetic operation; see constants |
---|
20 | * in the *.h file). Whenever the resulting complexity exceeds a |
---|
21 | * certain threshold (see constant in the *.h file), then the |
---|
22 | * cancellation heuristic will call 'factory' to compute the gcd |
---|
23 | * and cancel it out in the given number. (This definite cancel- |
---|
24 | * lation will also be performed at the beginning of ntWrite, |
---|
25 | * ensuring that any output is free of common factors. |
---|
26 | * For the special case of K = Q (i.e., when computing over the |
---|
27 | * rationals), this definite cancellation procedure will also take |
---|
28 | * care of nested fractions: If there are fractional coefficients |
---|
29 | * in the numerator or denominator of a number, then this number |
---|
30 | * is being replaced by a quotient of two polynomials over Z, or |
---|
31 | * - if the denominator is a constant - by a polynomial over Q. |
---|
32 | */ |
---|
33 | #define TRANSEXT_PRIVATES |
---|
34 | |
---|
35 | #include "config.h" |
---|
36 | #include <misc/auxiliary.h> |
---|
37 | |
---|
38 | #include <omalloc/omalloc.h> |
---|
39 | |
---|
40 | #include <reporter/reporter.h> |
---|
41 | |
---|
42 | #include <coeffs/coeffs.h> |
---|
43 | #include <coeffs/numbers.h> |
---|
44 | #include <coeffs/longrat.h> |
---|
45 | |
---|
46 | #include <polys/monomials/ring.h> |
---|
47 | #include <polys/monomials/p_polys.h> |
---|
48 | #include <polys/simpleideals.h> |
---|
49 | |
---|
50 | #ifdef HAVE_FACTORY |
---|
51 | #include <polys/clapsing.h> |
---|
52 | #include <polys/clapconv.h> |
---|
53 | #include <factory/factory.h> |
---|
54 | #endif |
---|
55 | |
---|
56 | #include "ext_fields/transext.h" |
---|
57 | #include "prCopy.h" |
---|
58 | |
---|
59 | /* constants for controlling the complexity of numbers */ |
---|
60 | #define ADD_COMPLEXITY 1 /**< complexity increase due to + and - */ |
---|
61 | #define MULT_COMPLEXITY 2 /**< complexity increase due to * and / */ |
---|
62 | #define BOUND_COMPLEXITY 10 /**< maximum complexity of a number */ |
---|
63 | |
---|
64 | #define NUMIS1(f) (p_IsConstant(NUM(f), cf->extRing) && \ |
---|
65 | n_IsOne(p_GetCoeff(NUM(f), cf->extRing), \ |
---|
66 | cf->extRing->cf)) |
---|
67 | /**< TRUE iff num. represents 1 */ |
---|
68 | #define COM(f) f->complexity |
---|
69 | |
---|
70 | |
---|
71 | #ifdef LDEBUG |
---|
72 | #define ntTest(a) ntDBTest(a,__FILE__,__LINE__,cf) |
---|
73 | BOOLEAN ntDBTest(number a, const char *f, const int l, const coeffs r); |
---|
74 | #else |
---|
75 | #define ntTest(a) (TRUE) |
---|
76 | #endif |
---|
77 | |
---|
78 | /// Our own type! |
---|
79 | static const n_coeffType ID = n_transExt; |
---|
80 | |
---|
81 | /* polynomial ring in which the numerators and denominators of our |
---|
82 | numbers live */ |
---|
83 | #define ntRing cf->extRing |
---|
84 | |
---|
85 | /* coeffs object in which the coefficients of our numbers live; |
---|
86 | * methods attached to ntCoeffs may be used to compute with the |
---|
87 | * coefficients of our numbers, e.g., use ntCoeffs->nAdd to add |
---|
88 | * coefficients of our numbers */ |
---|
89 | #define ntCoeffs cf->extRing->cf |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | omBin fractionObjectBin = omGetSpecBin(sizeof(fractionObject)); |
---|
94 | |
---|
95 | /// forward declarations |
---|
96 | BOOLEAN ntGreaterZero(number a, const coeffs cf); |
---|
97 | BOOLEAN ntGreater(number a, number b, const coeffs cf); |
---|
98 | BOOLEAN ntEqual(number a, number b, const coeffs cf); |
---|
99 | BOOLEAN ntIsOne(number a, const coeffs cf); |
---|
100 | BOOLEAN ntIsMOne(number a, const coeffs cf); |
---|
101 | BOOLEAN ntIsZero(number a, const coeffs cf); |
---|
102 | number ntInit(long i, const coeffs cf); |
---|
103 | int ntInt(number &a, const coeffs cf); |
---|
104 | number ntNeg(number a, const coeffs cf); |
---|
105 | number ntInvers(number a, const coeffs cf); |
---|
106 | number ntAdd(number a, number b, const coeffs cf); |
---|
107 | number ntSub(number a, number b, const coeffs cf); |
---|
108 | number ntMult(number a, number b, const coeffs cf); |
---|
109 | number ntDiv(number a, number b, const coeffs cf); |
---|
110 | void ntPower(number a, int exp, number *b, const coeffs cf); |
---|
111 | number ntCopy(number a, const coeffs cf); |
---|
112 | void ntWrite(number &a, const coeffs cf); |
---|
113 | number ntRePart(number a, const coeffs cf); |
---|
114 | number ntImPart(number a, const coeffs cf); |
---|
115 | number ntGetDenom(number &a, const coeffs cf); |
---|
116 | number ntGetNumerator(number &a, const coeffs cf); |
---|
117 | number ntGcd(number a, number b, const coeffs cf); |
---|
118 | number ntLcm(number a, number b, const coeffs cf); |
---|
119 | int ntSize(number a, const coeffs cf); |
---|
120 | void ntDelete(number * a, const coeffs cf); |
---|
121 | void ntCoeffWrite(const coeffs cf, BOOLEAN details); |
---|
122 | number ntIntDiv(number a, number b, const coeffs cf); |
---|
123 | const char * ntRead(const char *s, number *a, const coeffs cf); |
---|
124 | static BOOLEAN ntCoeffIsEqual(const coeffs cf, n_coeffType n, void * param); |
---|
125 | |
---|
126 | void heuristicGcdCancellation(number a, const coeffs cf); |
---|
127 | void definiteGcdCancellation(number a, const coeffs cf, |
---|
128 | BOOLEAN simpleTestsHaveAlreadyBeenPerformed); |
---|
129 | void handleNestedFractionsOverQ(fraction f, const coeffs cf); |
---|
130 | |
---|
131 | #ifdef LDEBUG |
---|
132 | BOOLEAN ntDBTest(number a, const char *f, const int l, const coeffs cf) |
---|
133 | { |
---|
134 | assume(getCoeffType(cf) == ID); |
---|
135 | fraction t = (fraction)a; |
---|
136 | if (IS0(t)) return TRUE; |
---|
137 | assume(NUM(t) != NULL); /**< t != 0 ==> numerator(t) != 0 */ |
---|
138 | p_Test(NUM(t), ntRing); |
---|
139 | if (!DENIS1(t)) |
---|
140 | { |
---|
141 | p_Test(DEN(t), ntRing); |
---|
142 | if(p_IsConstant(DEN(t),ntRing) && (n_IsOne(pGetCoeff(DEN(t)),ntRing->cf))) |
---|
143 | { |
---|
144 | Print("?/1 in %s:%d\n",f,l); |
---|
145 | return FALSE; |
---|
146 | } |
---|
147 | } |
---|
148 | return TRUE; |
---|
149 | } |
---|
150 | #endif |
---|
151 | |
---|
152 | /* returns the bottom field in this field extension tower; if the tower |
---|
153 | is flat, i.e., if there is no extension, then r itself is returned; |
---|
154 | as a side-effect, the counter 'height' is filled with the height of |
---|
155 | the extension tower (in case the tower is flat, 'height' is zero) */ |
---|
156 | static coeffs nCoeff_bottom(const coeffs r, int &height) |
---|
157 | { |
---|
158 | assume(r != NULL); |
---|
159 | coeffs cf = r; |
---|
160 | height = 0; |
---|
161 | while (nCoeff_is_Extension(cf)) |
---|
162 | { |
---|
163 | assume(cf->extRing != NULL); assume(cf->extRing->cf != NULL); |
---|
164 | cf = cf->extRing->cf; |
---|
165 | height++; |
---|
166 | } |
---|
167 | return cf; |
---|
168 | } |
---|
169 | |
---|
170 | BOOLEAN ntIsZero(number a, const coeffs cf) |
---|
171 | { |
---|
172 | ntTest(a); |
---|
173 | return (IS0(a)); |
---|
174 | } |
---|
175 | |
---|
176 | void ntDelete(number * a, const coeffs cf) |
---|
177 | { |
---|
178 | fraction f = (fraction)(*a); |
---|
179 | if (IS0(f)) return; |
---|
180 | p_Delete(&NUM(f), ntRing); |
---|
181 | if (!DENIS1(f)) p_Delete(&DEN(f), ntRing); |
---|
182 | omFreeBin((ADDRESS)f, fractionObjectBin); |
---|
183 | *a = NULL; |
---|
184 | } |
---|
185 | |
---|
186 | BOOLEAN ntEqual(number a, number b, const coeffs cf) |
---|
187 | { |
---|
188 | ntTest(a); ntTest(b); |
---|
189 | |
---|
190 | /// simple tests |
---|
191 | if (a == b) return TRUE; |
---|
192 | if ((IS0(a)) && (!IS0(b))) return FALSE; |
---|
193 | if ((IS0(b)) && (!IS0(a))) return FALSE; |
---|
194 | |
---|
195 | /// cheap test if gcd's have been cancelled in both numbers |
---|
196 | fraction fa = (fraction)a; |
---|
197 | fraction fb = (fraction)b; |
---|
198 | if ((COM(fa) == 1) && (COM(fb) == 1)) |
---|
199 | { |
---|
200 | poly f = p_Add_q(p_Copy(NUM(fa), ntRing), |
---|
201 | p_Neg(p_Copy(NUM(fb), ntRing), ntRing), |
---|
202 | ntRing); |
---|
203 | if (f != NULL) { p_Delete(&f, ntRing); return FALSE; } |
---|
204 | if (DENIS1(fa) && DENIS1(fb)) return TRUE; |
---|
205 | if (DENIS1(fa) && !DENIS1(fb)) return FALSE; |
---|
206 | if (!DENIS1(fa) && DENIS1(fb)) return FALSE; |
---|
207 | f = p_Add_q(p_Copy(DEN(fa), ntRing), |
---|
208 | p_Neg(p_Copy(DEN(fb), ntRing), ntRing), |
---|
209 | ntRing); |
---|
210 | if (f != NULL) { p_Delete(&f, ntRing); return FALSE; } |
---|
211 | return TRUE; |
---|
212 | } |
---|
213 | |
---|
214 | /* default: the more expensive multiplication test |
---|
215 | a/b = c/d <==> a*d = b*c */ |
---|
216 | poly f = p_Copy(NUM(fa), ntRing); |
---|
217 | if (!DENIS1(fb)) f = p_Mult_q(f, p_Copy(DEN(fb), ntRing), ntRing); |
---|
218 | poly g = p_Copy(NUM(fb), ntRing); |
---|
219 | if (!DENIS1(fa)) g = p_Mult_q(g, p_Copy(DEN(fa), ntRing), ntRing); |
---|
220 | poly h = p_Add_q(f, p_Neg(g, ntRing), ntRing); |
---|
221 | if (h == NULL) return TRUE; |
---|
222 | else |
---|
223 | { |
---|
224 | p_Delete(&h, ntRing); |
---|
225 | return FALSE; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | number ntCopy(number a, const coeffs cf) |
---|
230 | { |
---|
231 | ntTest(a); |
---|
232 | if (IS0(a)) return NULL; |
---|
233 | fraction f = (fraction)a; |
---|
234 | poly g = p_Copy(NUM(f), ntRing); |
---|
235 | poly h = NULL; if (!DENIS1(f)) h = p_Copy(DEN(f), ntRing); |
---|
236 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
237 | NUM(result) = g; |
---|
238 | DEN(result) = h; |
---|
239 | COM(result) = COM(f); |
---|
240 | return (number)result; |
---|
241 | } |
---|
242 | |
---|
243 | number ntGetNumerator(number &a, const coeffs cf) |
---|
244 | { |
---|
245 | ntTest(a); |
---|
246 | definiteGcdCancellation(a, cf, FALSE); |
---|
247 | if (IS0(a)) return NULL; |
---|
248 | fraction f = (fraction)a; |
---|
249 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
250 | BOOLEAN denis1= DENIS1 (f); |
---|
251 | if (getCoeffType (ntCoeffs) == n_Q && !denis1) |
---|
252 | handleNestedFractionsOverQ (f, cf); |
---|
253 | NUM (result)= p_Copy (NUM (f), ntRing); |
---|
254 | DEN (result) = NULL; |
---|
255 | COM (result) = 0; |
---|
256 | if (getCoeffType (ntCoeffs) == n_Q && denis1) |
---|
257 | { |
---|
258 | if (!p_IsConstant (NUM (result), ntRing) && pNext (NUM(result)) != NULL) |
---|
259 | p_Cleardenom (NUM(result), ntRing); |
---|
260 | else |
---|
261 | { |
---|
262 | number g= p_GetAllDenom (NUM (result), ntRing); |
---|
263 | NUM (result)= p_Mult_nn (NUM (result), g, ntRing); |
---|
264 | } |
---|
265 | } |
---|
266 | return (number)result; |
---|
267 | } |
---|
268 | |
---|
269 | number ntGetDenom(number &a, const coeffs cf) |
---|
270 | { |
---|
271 | ntTest(a); |
---|
272 | definiteGcdCancellation(a, cf, FALSE); |
---|
273 | fraction f = (fraction)a; |
---|
274 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
275 | number g; |
---|
276 | if (IS0(f) || (DENIS1 (f) && getCoeffType (ntCoeffs) != n_Q)) |
---|
277 | { |
---|
278 | NUM (result)= p_One(ntRing); |
---|
279 | DEN (result)= NULL; |
---|
280 | COM (result)= 0; |
---|
281 | } |
---|
282 | else if (DENIS1 (f)) |
---|
283 | { |
---|
284 | poly num= p_Copy (NUM (f), ntRing); |
---|
285 | if (!p_IsConstant (num, ntRing) && pNext(num) != NULL) |
---|
286 | p_Cleardenom_n (num, ntRing, g); |
---|
287 | else |
---|
288 | g= p_GetAllDenom (num, ntRing); |
---|
289 | result= (fraction) ntSetMap (ntRing->cf, cf) (g, ntRing->cf, cf); |
---|
290 | } |
---|
291 | else |
---|
292 | { |
---|
293 | if (getCoeffType (ntCoeffs) == n_Q) |
---|
294 | handleNestedFractionsOverQ (f, cf); |
---|
295 | NUM (result)= p_Copy (DEN (f), ntRing); |
---|
296 | DEN (result) = NULL; |
---|
297 | COM (result) = 0; |
---|
298 | } |
---|
299 | return (number)result; |
---|
300 | } |
---|
301 | |
---|
302 | BOOLEAN ntIsOne(number a, const coeffs cf) |
---|
303 | { |
---|
304 | ntTest(a); |
---|
305 | definiteGcdCancellation(a, cf, FALSE); |
---|
306 | fraction f = (fraction)a; |
---|
307 | return (f!=NULL) && DENIS1(f) && NUMIS1(f); |
---|
308 | } |
---|
309 | |
---|
310 | BOOLEAN ntIsMOne(number a, const coeffs cf) |
---|
311 | { |
---|
312 | ntTest(a); |
---|
313 | definiteGcdCancellation(a, cf, FALSE); |
---|
314 | fraction f = (fraction)a; |
---|
315 | if ((f==NULL) || (!DENIS1(f))) return FALSE; |
---|
316 | poly g = NUM(f); |
---|
317 | if (!p_IsConstant(g, ntRing)) return FALSE; |
---|
318 | return n_IsMOne(p_GetCoeff(g, ntRing), ntCoeffs); |
---|
319 | } |
---|
320 | |
---|
321 | /// this is in-place, modifies a |
---|
322 | number ntNeg(number a, const coeffs cf) |
---|
323 | { |
---|
324 | ntTest(a); |
---|
325 | if (!IS0(a)) |
---|
326 | { |
---|
327 | fraction f = (fraction)a; |
---|
328 | NUM(f) = p_Neg(NUM(f), ntRing); |
---|
329 | } |
---|
330 | return a; |
---|
331 | } |
---|
332 | |
---|
333 | number ntImPart(number a, const coeffs cf) |
---|
334 | { |
---|
335 | ntTest(a); |
---|
336 | return NULL; |
---|
337 | } |
---|
338 | |
---|
339 | number ntInit_bigint(number longratBigIntNumber, const coeffs src, const coeffs cf) |
---|
340 | { |
---|
341 | assume( cf != NULL ); |
---|
342 | |
---|
343 | const ring A = cf->extRing; |
---|
344 | |
---|
345 | assume( A != NULL ); |
---|
346 | |
---|
347 | const coeffs C = A->cf; |
---|
348 | |
---|
349 | assume( C != NULL ); |
---|
350 | |
---|
351 | number n = n_Init_bigint(longratBigIntNumber, src, C); |
---|
352 | |
---|
353 | if ( n_IsZero(n, C) ) |
---|
354 | { |
---|
355 | n_Delete(&n, C); |
---|
356 | return NULL; |
---|
357 | } |
---|
358 | |
---|
359 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
360 | |
---|
361 | NUM(result) = p_NSet(n, A); |
---|
362 | DEN(result) = NULL; |
---|
363 | COM(result) = 0; |
---|
364 | return (number)result; |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | number ntInit(long i, const coeffs cf) |
---|
369 | { |
---|
370 | if (i == 0) return NULL; |
---|
371 | else |
---|
372 | { |
---|
373 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
374 | NUM(result) = p_ISet(i, ntRing); |
---|
375 | //DEN(result) = NULL; // done by omAlloc0Bin |
---|
376 | //COM(result) = 0; // done by omAlloc0Bin |
---|
377 | return (number)result; |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | number ntInit(poly p, const coeffs cf) |
---|
382 | { |
---|
383 | if (p == 0) return NULL; |
---|
384 | else |
---|
385 | { |
---|
386 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
387 | NUM(result) = p; |
---|
388 | DEN(result) = NULL; |
---|
389 | COM(result) = 0; |
---|
390 | return (number)result; |
---|
391 | } |
---|
392 | } |
---|
393 | |
---|
394 | int ntInt(number &a, const coeffs cf) |
---|
395 | { |
---|
396 | ntTest(a); |
---|
397 | if (IS0(a)) return 0; |
---|
398 | definiteGcdCancellation(a, cf, FALSE); |
---|
399 | fraction f = (fraction)a; |
---|
400 | if (!DENIS1(f)) return 0; |
---|
401 | if (!p_IsConstant(NUM(f), ntRing)) return 0; |
---|
402 | return n_Int(p_GetCoeff(NUM(f), ntRing), ntCoeffs); |
---|
403 | } |
---|
404 | |
---|
405 | /* This method will only consider the numerators of a and b, without |
---|
406 | cancelling gcd's before. |
---|
407 | Moreover it may return TRUE only if one or both numerators |
---|
408 | are zero or if their degrees are equal. Then TRUE is returned iff |
---|
409 | coeff(numerator(a)) > coeff(numerator(b)); |
---|
410 | In all other cases, FALSE will be returned. */ |
---|
411 | BOOLEAN ntGreater(number a, number b, const coeffs cf) |
---|
412 | { |
---|
413 | ntTest(a); ntTest(b); |
---|
414 | number aNumCoeff = NULL; int aNumDeg = 0; |
---|
415 | number bNumCoeff = NULL; int bNumDeg = 0; |
---|
416 | if (!IS0(a)) |
---|
417 | { |
---|
418 | fraction fa = (fraction)a; |
---|
419 | aNumDeg = p_Totaldegree(NUM(fa), ntRing); |
---|
420 | aNumCoeff = p_GetCoeff(NUM(fa), ntRing); |
---|
421 | } |
---|
422 | if (!IS0(b)) |
---|
423 | { |
---|
424 | fraction fb = (fraction)b; |
---|
425 | bNumDeg = p_Totaldegree(NUM(fb), ntRing); |
---|
426 | bNumCoeff = p_GetCoeff(NUM(fb), ntRing); |
---|
427 | } |
---|
428 | if (aNumDeg != bNumDeg) return FALSE; |
---|
429 | else return n_Greater(aNumCoeff, bNumCoeff, ntCoeffs); |
---|
430 | } |
---|
431 | |
---|
432 | /* this method will only consider the numerator of a, without cancelling |
---|
433 | the gcd before; |
---|
434 | returns TRUE iff the leading coefficient of the numerator of a is > 0 |
---|
435 | or the leading term of the numerator of a is not a |
---|
436 | constant */ |
---|
437 | BOOLEAN ntGreaterZero(number a, const coeffs cf) |
---|
438 | { |
---|
439 | ntTest(a); |
---|
440 | if (IS0(a)) return FALSE; |
---|
441 | fraction f = (fraction)a; |
---|
442 | poly g = NUM(f); |
---|
443 | return (n_GreaterZero(p_GetCoeff(g, ntRing), ntCoeffs) || |
---|
444 | (!p_LmIsConstant(g, ntRing))); |
---|
445 | } |
---|
446 | |
---|
447 | void ntCoeffWrite(const coeffs cf, BOOLEAN details) |
---|
448 | { |
---|
449 | assume( cf != NULL ); |
---|
450 | |
---|
451 | const ring A = cf->extRing; |
---|
452 | |
---|
453 | assume( A != NULL ); |
---|
454 | assume( A->cf != NULL ); |
---|
455 | |
---|
456 | n_CoeffWrite(A->cf, details); |
---|
457 | |
---|
458 | // rWrite(A); |
---|
459 | |
---|
460 | const int P = rVar(A); |
---|
461 | assume( P > 0 ); |
---|
462 | |
---|
463 | Print("// %d parameter : ", P); |
---|
464 | |
---|
465 | for (int nop=0; nop < P; nop ++) |
---|
466 | Print("%s ", rRingVar(nop, A)); |
---|
467 | |
---|
468 | assume( A->minideal == NULL ); |
---|
469 | |
---|
470 | PrintS("\n// minpoly : 0\n"); |
---|
471 | |
---|
472 | /* |
---|
473 | PrintS("// Coefficients live in the rational function field\n"); |
---|
474 | Print("// K("); |
---|
475 | for (int i = 0; i < rVar(ntRing); i++) |
---|
476 | { |
---|
477 | if (i > 0) PrintS(" "); |
---|
478 | Print("%s", rRingVar(i, ntRing)); |
---|
479 | } |
---|
480 | PrintS(") with\n"); |
---|
481 | PrintS("// K: "); n_CoeffWrite(cf->extRing->cf); |
---|
482 | */ |
---|
483 | } |
---|
484 | |
---|
485 | number ntAdd(number a, number b, const coeffs cf) |
---|
486 | { |
---|
487 | ntTest(a); ntTest(b); |
---|
488 | if (IS0(a)) return ntCopy(b, cf); |
---|
489 | if (IS0(b)) return ntCopy(a, cf); |
---|
490 | |
---|
491 | fraction fa = (fraction)a; |
---|
492 | fraction fb = (fraction)b; |
---|
493 | |
---|
494 | poly g = p_Copy(NUM(fa), ntRing); |
---|
495 | if (!DENIS1(fb)) g = p_Mult_q(g, p_Copy(DEN(fb), ntRing), ntRing); |
---|
496 | poly h = p_Copy(NUM(fb), ntRing); |
---|
497 | if (!DENIS1(fa)) h = p_Mult_q(h, p_Copy(DEN(fa), ntRing), ntRing); |
---|
498 | g = p_Add_q(g, h, ntRing); |
---|
499 | |
---|
500 | if (g == NULL) return NULL; |
---|
501 | |
---|
502 | poly f; |
---|
503 | if (DENIS1(fa) && DENIS1(fb)) f = NULL; |
---|
504 | else if (!DENIS1(fa) && DENIS1(fb)) f = p_Copy(DEN(fa), ntRing); |
---|
505 | else if (DENIS1(fa) && !DENIS1(fb)) f = p_Copy(DEN(fb), ntRing); |
---|
506 | else /* both denom's are != 1 */ f = p_Mult_q(p_Copy(DEN(fa), ntRing), |
---|
507 | p_Copy(DEN(fb), ntRing), |
---|
508 | ntRing); |
---|
509 | |
---|
510 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
511 | NUM(result) = g; |
---|
512 | DEN(result) = f; |
---|
513 | COM(result) = COM(fa) + COM(fb) + ADD_COMPLEXITY; |
---|
514 | heuristicGcdCancellation((number)result, cf); |
---|
515 | return (number)result; |
---|
516 | } |
---|
517 | |
---|
518 | number ntSub(number a, number b, const coeffs cf) |
---|
519 | { |
---|
520 | ntTest(a); ntTest(b); |
---|
521 | if (IS0(a)) return ntNeg(ntCopy(b, cf), cf); |
---|
522 | if (IS0(b)) return ntCopy(a, cf); |
---|
523 | |
---|
524 | fraction fa = (fraction)a; |
---|
525 | fraction fb = (fraction)b; |
---|
526 | |
---|
527 | poly g = p_Copy(NUM(fa), ntRing); |
---|
528 | if (!DENIS1(fb)) g = p_Mult_q(g, p_Copy(DEN(fb), ntRing), ntRing); |
---|
529 | poly h = p_Copy(NUM(fb), ntRing); |
---|
530 | if (!DENIS1(fa)) h = p_Mult_q(h, p_Copy(DEN(fa), ntRing), ntRing); |
---|
531 | g = p_Add_q(g, p_Neg(h, ntRing), ntRing); |
---|
532 | |
---|
533 | if (g == NULL) return NULL; |
---|
534 | |
---|
535 | poly f; |
---|
536 | if (DENIS1(fa) && DENIS1(fb)) f = NULL; |
---|
537 | else if (!DENIS1(fa) && DENIS1(fb)) f = p_Copy(DEN(fa), ntRing); |
---|
538 | else if (DENIS1(fa) && !DENIS1(fb)) f = p_Copy(DEN(fb), ntRing); |
---|
539 | else /* both den's are != 1 */ f = p_Mult_q(p_Copy(DEN(fa), ntRing), |
---|
540 | p_Copy(DEN(fb), ntRing), |
---|
541 | ntRing); |
---|
542 | |
---|
543 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
544 | NUM(result) = g; |
---|
545 | DEN(result) = f; |
---|
546 | COM(result) = COM(fa) + COM(fb) + ADD_COMPLEXITY; |
---|
547 | heuristicGcdCancellation((number)result, cf); |
---|
548 | return (number)result; |
---|
549 | } |
---|
550 | |
---|
551 | number ntMult(number a, number b, const coeffs cf) |
---|
552 | { |
---|
553 | ntTest(a); ntTest(b); |
---|
554 | if (IS0(a) || IS0(b)) return NULL; |
---|
555 | |
---|
556 | fraction fa = (fraction)a; |
---|
557 | fraction fb = (fraction)b; |
---|
558 | |
---|
559 | poly g = p_Copy(NUM(fa), ntRing); |
---|
560 | poly h = p_Copy(NUM(fb), ntRing); |
---|
561 | g = p_Mult_q(g, h, ntRing); |
---|
562 | |
---|
563 | if (g == NULL) return NULL; /* may happen due to zero divisors */ |
---|
564 | |
---|
565 | poly f; |
---|
566 | if (DENIS1(fa) && DENIS1(fb)) f = NULL; |
---|
567 | else if (!DENIS1(fa) && DENIS1(fb)) f = p_Copy(DEN(fa), ntRing); |
---|
568 | else if (DENIS1(fa) && !DENIS1(fb)) f = p_Copy(DEN(fb), ntRing); |
---|
569 | else /* both den's are != 1 */ f = p_Mult_q(p_Copy(DEN(fa), ntRing), |
---|
570 | p_Copy(DEN(fb), ntRing), |
---|
571 | ntRing); |
---|
572 | |
---|
573 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
574 | NUM(result) = g; |
---|
575 | DEN(result) = f; |
---|
576 | COM(result) = COM(fa) + COM(fb) + MULT_COMPLEXITY; |
---|
577 | heuristicGcdCancellation((number)result, cf); |
---|
578 | return (number)result; |
---|
579 | } |
---|
580 | |
---|
581 | number ntDiv(number a, number b, const coeffs cf) |
---|
582 | { |
---|
583 | ntTest(a); ntTest(b); |
---|
584 | if (IS0(a)) return NULL; |
---|
585 | if (IS0(b)) WerrorS(nDivBy0); |
---|
586 | |
---|
587 | fraction fa = (fraction)a; |
---|
588 | fraction fb = (fraction)b; |
---|
589 | |
---|
590 | poly g = p_Copy(NUM(fa), ntRing); |
---|
591 | if (!DENIS1(fb)) g = p_Mult_q(g, p_Copy(DEN(fb), ntRing), ntRing); |
---|
592 | |
---|
593 | if (g == NULL) return NULL; /* may happen due to zero divisors */ |
---|
594 | |
---|
595 | poly f = p_Copy(NUM(fb), ntRing); |
---|
596 | if (!DENIS1(fa)) f = p_Mult_q(f, p_Copy(DEN(fa), ntRing), ntRing); |
---|
597 | |
---|
598 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
599 | NUM(result) = g; |
---|
600 | if (!p_IsConstant(f,ntRing) || !n_IsOne(pGetCoeff(f),ntRing->cf)) |
---|
601 | DEN(result) = f; |
---|
602 | COM(result) = COM(fa) + COM(fb) + MULT_COMPLEXITY; |
---|
603 | heuristicGcdCancellation((number)result, cf); |
---|
604 | return (number)result; |
---|
605 | } |
---|
606 | |
---|
607 | /* 0^0 = 0; |
---|
608 | for |exp| <= 7 compute power by a simple multiplication loop; |
---|
609 | for |exp| >= 8 compute power along binary presentation of |exp|, e.g. |
---|
610 | p^13 = p^1 * p^4 * p^8, where we utilise that |
---|
611 | p^(2^(k+1)) = p^(2^k) * p^(2^k); |
---|
612 | intermediate cancellation is controlled by the in-place method |
---|
613 | heuristicGcdCancellation; see there. |
---|
614 | */ |
---|
615 | void ntPower(number a, int exp, number *b, const coeffs cf) |
---|
616 | { |
---|
617 | ntTest(a); |
---|
618 | |
---|
619 | /* special cases first */ |
---|
620 | if (IS0(a)) |
---|
621 | { |
---|
622 | if (exp >= 0) *b = NULL; |
---|
623 | else WerrorS(nDivBy0); |
---|
624 | } |
---|
625 | else if (exp == 0) { *b = ntInit(1, cf); return;} |
---|
626 | else if (exp == 1) { *b = ntCopy(a, cf); return;} |
---|
627 | else if (exp == -1) { *b = ntInvers(a, cf); return;} |
---|
628 | |
---|
629 | int expAbs = exp; if (expAbs < 0) expAbs = -expAbs; |
---|
630 | |
---|
631 | /* now compute a^expAbs */ |
---|
632 | number pow; number t; |
---|
633 | if (expAbs <= 7) |
---|
634 | { |
---|
635 | pow = ntCopy(a, cf); |
---|
636 | for (int i = 2; i <= expAbs; i++) |
---|
637 | { |
---|
638 | t = ntMult(pow, a, cf); |
---|
639 | ntDelete(&pow, cf); |
---|
640 | pow = t; |
---|
641 | heuristicGcdCancellation(pow, cf); |
---|
642 | } |
---|
643 | } |
---|
644 | else |
---|
645 | { |
---|
646 | pow = ntInit(1, cf); |
---|
647 | number factor = ntCopy(a, cf); |
---|
648 | while (expAbs != 0) |
---|
649 | { |
---|
650 | if (expAbs & 1) |
---|
651 | { |
---|
652 | t = ntMult(pow, factor, cf); |
---|
653 | ntDelete(&pow, cf); |
---|
654 | pow = t; |
---|
655 | heuristicGcdCancellation(pow, cf); |
---|
656 | } |
---|
657 | expAbs = expAbs / 2; |
---|
658 | if (expAbs != 0) |
---|
659 | { |
---|
660 | t = ntMult(factor, factor, cf); |
---|
661 | ntDelete(&factor, cf); |
---|
662 | factor = t; |
---|
663 | heuristicGcdCancellation(factor, cf); |
---|
664 | } |
---|
665 | } |
---|
666 | ntDelete(&factor, cf); |
---|
667 | } |
---|
668 | |
---|
669 | /* invert if original exponent was negative */ |
---|
670 | if (exp < 0) |
---|
671 | { |
---|
672 | t = ntInvers(pow, cf); |
---|
673 | ntDelete(&pow, cf); |
---|
674 | pow = t; |
---|
675 | } |
---|
676 | *b = pow; |
---|
677 | } |
---|
678 | |
---|
679 | /* assumes that cf represents the rationals, i.e. Q, and will only |
---|
680 | be called in that case; |
---|
681 | assumes furthermore that f != NULL and that the denominator of f != 1; |
---|
682 | generally speaking, this method removes denominators in the rational |
---|
683 | coefficients of the numerator and denominator of 'a'; |
---|
684 | more concretely, the following normalizations will be performed, |
---|
685 | where t^alpha denotes a monomial in the transcendental variables t_k |
---|
686 | (1) if 'a' is of the form |
---|
687 | (sum_alpha a_alpha/b_alpha * t^alpha) |
---|
688 | ------------------------------------- |
---|
689 | (sum_beta c_beta/d_beta * t^beta) |
---|
690 | with integers a_alpha, b_alpha, c_beta, d_beta, then both the |
---|
691 | numerator and the denominator will be multiplied by the LCM of |
---|
692 | the b_alpha's and the d_beta's (if this LCM is != 1), |
---|
693 | (2) if 'a' is - e.g. after having performed step (1) - of the form |
---|
694 | (sum_alpha a_alpha * t^alpha) |
---|
695 | ----------------------------- |
---|
696 | (sum_beta c_beta * t^beta) |
---|
697 | with integers a_alpha, c_beta, and with a non-constant denominator, |
---|
698 | then both the numerator and the denominator will be divided by the |
---|
699 | GCD of the a_alpha's and the c_beta's (if this GCD is != 1), |
---|
700 | (3) if 'a' is - e.g. after having performed steps (1) and (2) - of the |
---|
701 | form |
---|
702 | (sum_alpha a_alpha * t^alpha) |
---|
703 | ----------------------------- |
---|
704 | c |
---|
705 | with integers a_alpha, and c != 1, then 'a' will be replaced by |
---|
706 | (sum_alpha a_alpha/c * t^alpha); |
---|
707 | this procedure does not alter COM(f) (this has to be done by the |
---|
708 | calling procedure); |
---|
709 | modifies f */ |
---|
710 | void handleNestedFractionsOverQ(fraction f, const coeffs cf) |
---|
711 | { |
---|
712 | assume(nCoeff_is_Q(ntCoeffs)); |
---|
713 | assume(!IS0(f)); |
---|
714 | assume(!DENIS1(f)); |
---|
715 | |
---|
716 | if (!p_IsConstant(DEN(f), ntRing)) |
---|
717 | { /* step (1); see documentation of this procedure above */ |
---|
718 | p_Normalize(NUM(f), ntRing); |
---|
719 | p_Normalize(DEN(f), ntRing); |
---|
720 | number lcmOfDenominators = n_Init(1, ntCoeffs); |
---|
721 | number c; number tmp; |
---|
722 | poly p = NUM(f); |
---|
723 | /* careful when using n_Lcm!!! It computes the lcm of the numerator |
---|
724 | of the 1st argument and the denominator of the 2nd!!! */ |
---|
725 | while (p != NULL) |
---|
726 | { |
---|
727 | c = p_GetCoeff(p, ntRing); |
---|
728 | tmp = n_Lcm(lcmOfDenominators, c, ntCoeffs); |
---|
729 | n_Delete(&lcmOfDenominators, ntCoeffs); |
---|
730 | lcmOfDenominators = tmp; |
---|
731 | pIter(p); |
---|
732 | } |
---|
733 | p = DEN(f); |
---|
734 | while (p != NULL) |
---|
735 | { |
---|
736 | c = p_GetCoeff(p, ntRing); |
---|
737 | tmp = n_Lcm(lcmOfDenominators, c, ntCoeffs); |
---|
738 | n_Delete(&lcmOfDenominators, ntCoeffs); |
---|
739 | lcmOfDenominators = tmp; |
---|
740 | pIter(p); |
---|
741 | } |
---|
742 | if (!n_IsOne(lcmOfDenominators, ntCoeffs)) |
---|
743 | { /* multiply NUM(f) and DEN(f) with lcmOfDenominators */ |
---|
744 | NUM(f) = p_Mult_nn(NUM(f), lcmOfDenominators, ntRing); |
---|
745 | p_Normalize(NUM(f), ntRing); |
---|
746 | DEN(f) = p_Mult_nn(DEN(f), lcmOfDenominators, ntRing); |
---|
747 | p_Normalize(DEN(f), ntRing); |
---|
748 | } |
---|
749 | n_Delete(&lcmOfDenominators, ntCoeffs); |
---|
750 | if (!p_IsConstant(DEN(f), ntRing)) |
---|
751 | { /* step (2); see documentation of this procedure above */ |
---|
752 | p = NUM(f); |
---|
753 | number gcdOfCoefficients = n_Copy(p_GetCoeff(p, ntRing), ntCoeffs); |
---|
754 | pIter(p); |
---|
755 | while ((p != NULL) && (!n_IsOne(gcdOfCoefficients, ntCoeffs))) |
---|
756 | { |
---|
757 | c = p_GetCoeff(p, ntRing); |
---|
758 | tmp = n_Gcd(c, gcdOfCoefficients, ntCoeffs); |
---|
759 | n_Delete(&gcdOfCoefficients, ntCoeffs); |
---|
760 | gcdOfCoefficients = tmp; |
---|
761 | pIter(p); |
---|
762 | } |
---|
763 | p = DEN(f); |
---|
764 | while ((p != NULL) && (!n_IsOne(gcdOfCoefficients, ntCoeffs))) |
---|
765 | { |
---|
766 | c = p_GetCoeff(p, ntRing); |
---|
767 | tmp = n_Gcd(c, gcdOfCoefficients, ntCoeffs); |
---|
768 | n_Delete(&gcdOfCoefficients, ntCoeffs); |
---|
769 | gcdOfCoefficients = tmp; |
---|
770 | pIter(p); |
---|
771 | } |
---|
772 | if (!n_IsOne(gcdOfCoefficients, ntCoeffs)) |
---|
773 | { /* divide NUM(f) and DEN(f) by gcdOfCoefficients */ |
---|
774 | number inverseOfGcdOfCoefficients = n_Invers(gcdOfCoefficients, |
---|
775 | ntCoeffs); |
---|
776 | NUM(f) = p_Mult_nn(NUM(f), inverseOfGcdOfCoefficients, ntRing); |
---|
777 | p_Normalize(NUM(f), ntRing); |
---|
778 | DEN(f) = p_Mult_nn(DEN(f), inverseOfGcdOfCoefficients, ntRing); |
---|
779 | p_Normalize(DEN(f), ntRing); |
---|
780 | n_Delete(&inverseOfGcdOfCoefficients, ntCoeffs); |
---|
781 | } |
---|
782 | n_Delete(&gcdOfCoefficients, ntCoeffs); |
---|
783 | } |
---|
784 | } |
---|
785 | if (p_IsConstant(DEN(f), ntRing) && |
---|
786 | (!n_IsOne(p_GetCoeff(DEN(f), ntRing), ntCoeffs))) |
---|
787 | { /* step (3); see documentation of this procedure above */ |
---|
788 | number inverseOfDen = n_Invers(p_GetCoeff(DEN(f), ntRing), ntCoeffs); |
---|
789 | NUM(f) = p_Mult_nn(NUM(f), inverseOfDen, ntRing); |
---|
790 | n_Delete(&inverseOfDen, ntCoeffs); |
---|
791 | p_Delete(&DEN(f), ntRing); |
---|
792 | DEN(f) = NULL; |
---|
793 | } |
---|
794 | |
---|
795 | /* Now, due to the above computations, DEN(f) may have become the |
---|
796 | 1-polynomial which needs to be represented by NULL: */ |
---|
797 | if ((DEN(f) != NULL) && |
---|
798 | p_IsConstant(DEN(f), ntRing) && |
---|
799 | n_IsOne(p_GetCoeff(DEN(f), ntRing), ntCoeffs)) |
---|
800 | { |
---|
801 | p_Delete(&DEN(f), ntRing); DEN(f) = NULL; |
---|
802 | } |
---|
803 | } |
---|
804 | |
---|
805 | /* modifies a */ |
---|
806 | void heuristicGcdCancellation(number a, const coeffs cf) |
---|
807 | { |
---|
808 | ntTest(a); |
---|
809 | if (IS0(a)) return; |
---|
810 | |
---|
811 | fraction f = (fraction)a; |
---|
812 | if (DENIS1(f) || NUMIS1(f)) { COM(f) = 0; return; } |
---|
813 | |
---|
814 | /* check whether NUM(f) = DEN(f), and - if so - replace 'a' by 1 */ |
---|
815 | if (p_EqualPolys(NUM(f), DEN(f), ntRing)) |
---|
816 | { /* numerator and denominator are both != 1 */ |
---|
817 | p_Delete(&NUM(f), ntRing); NUM(f) = p_ISet(1, ntRing); |
---|
818 | p_Delete(&DEN(f), ntRing); DEN(f) = NULL; |
---|
819 | COM(f) = 0; |
---|
820 | return; |
---|
821 | } |
---|
822 | |
---|
823 | if (COM(f) <= BOUND_COMPLEXITY) return; |
---|
824 | else definiteGcdCancellation(a, cf, TRUE); |
---|
825 | } |
---|
826 | |
---|
827 | /* modifies a */ |
---|
828 | void definiteGcdCancellation(number a, const coeffs cf, |
---|
829 | BOOLEAN simpleTestsHaveAlreadyBeenPerformed) |
---|
830 | { |
---|
831 | ntTest(a); |
---|
832 | |
---|
833 | fraction f = (fraction)a; |
---|
834 | |
---|
835 | if (!simpleTestsHaveAlreadyBeenPerformed) |
---|
836 | { |
---|
837 | if (IS0(a)) return; |
---|
838 | if (DENIS1(f) || NUMIS1(f)) { COM(f) = 0; return; } |
---|
839 | |
---|
840 | /* check whether NUM(f) = DEN(f), and - if so - replace 'a' by 1 */ |
---|
841 | if (p_EqualPolys(NUM(f), DEN(f), ntRing)) |
---|
842 | { /* numerator and denominator are both != 1 */ |
---|
843 | p_Delete(&NUM(f), ntRing); NUM(f) = p_ISet(1, ntRing); |
---|
844 | p_Delete(&DEN(f), ntRing); DEN(f) = NULL; |
---|
845 | COM(f) = 0; |
---|
846 | return; |
---|
847 | } |
---|
848 | } |
---|
849 | |
---|
850 | #ifdef HAVE_FACTORY |
---|
851 | /* singclap_gcd destroys its arguments; we hence need copies: */ |
---|
852 | poly pNum = p_Copy(NUM(f), ntRing); |
---|
853 | poly pDen = p_Copy(DEN(f), ntRing); |
---|
854 | |
---|
855 | /* Note that, over Q, singclap_gcd will remove the denominators in all |
---|
856 | rational coefficients of pNum and pDen, before starting to compute |
---|
857 | the gcd. Thus, we do not need to ensure that the coefficients of |
---|
858 | pNum and pDen live in Z; they may well be elements of Q\Z. */ |
---|
859 | poly pGcd = singclap_gcd(pNum, pDen, cf->extRing); |
---|
860 | if (p_IsConstant(pGcd, ntRing) && |
---|
861 | n_IsOne(p_GetCoeff(pGcd, ntRing), ntCoeffs)) |
---|
862 | { /* gcd = 1; nothing to cancel; |
---|
863 | Suppose the given rational function field is over Q. Although the |
---|
864 | gcd is 1, we may have produced fractional coefficients in NUM(f), |
---|
865 | DEN(f), or both, due to previous arithmetics. The next call will |
---|
866 | remove those nested fractions, in case there are any. */ |
---|
867 | if (nCoeff_is_Q(ntCoeffs)) handleNestedFractionsOverQ(f, cf); |
---|
868 | } |
---|
869 | else |
---|
870 | { /* We divide both NUM(f) and DEN(f) by the gcd which is known |
---|
871 | to be != 1. */ |
---|
872 | poly newNum = singclap_pdivide(NUM(f), pGcd, ntRing); |
---|
873 | p_Delete(&NUM(f), ntRing); |
---|
874 | NUM(f) = newNum; |
---|
875 | poly newDen = singclap_pdivide(DEN(f), pGcd, ntRing); |
---|
876 | p_Delete(&DEN(f), ntRing); |
---|
877 | DEN(f) = newDen; |
---|
878 | if (p_IsConstant(DEN(f), ntRing) && |
---|
879 | n_IsOne(p_GetCoeff(DEN(f), ntRing), ntCoeffs)) |
---|
880 | { |
---|
881 | /* DEN(f) = 1 needs to be represented by NULL! */ |
---|
882 | p_Delete(&DEN(f), ntRing); |
---|
883 | newDen = NULL; |
---|
884 | } |
---|
885 | else |
---|
886 | { /* Note that over Q, by cancelling the gcd, we may have produced |
---|
887 | fractional coefficients in NUM(f), DEN(f), or both. The next |
---|
888 | call will remove those nested fractions, in case there are |
---|
889 | any. */ |
---|
890 | if (nCoeff_is_Q(ntCoeffs)) handleNestedFractionsOverQ(f, cf); |
---|
891 | } |
---|
892 | } |
---|
893 | COM(f) = 0; |
---|
894 | p_Delete(&pGcd, ntRing); |
---|
895 | #endif /* HAVE_FACTORY */ |
---|
896 | } |
---|
897 | |
---|
898 | /* modifies a */ |
---|
899 | void ntWrite(number &a, const coeffs cf) |
---|
900 | { |
---|
901 | ntTest(a); |
---|
902 | definiteGcdCancellation(a, cf, FALSE); |
---|
903 | if (IS0(a)) |
---|
904 | StringAppendS("0"); |
---|
905 | else |
---|
906 | { |
---|
907 | fraction f = (fraction)a; |
---|
908 | // stole logic from napWrite from kernel/longtrans.cc of legacy singular |
---|
909 | BOOLEAN omitBrackets = p_IsConstant(NUM(f), ntRing); |
---|
910 | if (!omitBrackets) StringAppendS("("); |
---|
911 | p_String0(NUM(f), ntRing, ntRing); |
---|
912 | if (!omitBrackets) StringAppendS(")"); |
---|
913 | if (!DENIS1(f)) |
---|
914 | { |
---|
915 | StringAppendS("/"); |
---|
916 | omitBrackets = p_IsConstant(DEN(f), ntRing); |
---|
917 | if (!omitBrackets) StringAppendS("("); |
---|
918 | p_String0(DEN(f), ntRing, ntRing); |
---|
919 | if (!omitBrackets) StringAppendS(")"); |
---|
920 | } |
---|
921 | } |
---|
922 | } |
---|
923 | |
---|
924 | const char * ntRead(const char *s, number *a, const coeffs cf) |
---|
925 | { |
---|
926 | poly p; |
---|
927 | const char * result = p_Read(s, p, ntRing); |
---|
928 | if (p == NULL) { *a = NULL; return result; } |
---|
929 | else |
---|
930 | { |
---|
931 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
932 | NUM(f) = p; |
---|
933 | DEN(f) = NULL; |
---|
934 | COM(f) = 0; |
---|
935 | *a = (number)f; |
---|
936 | return result; |
---|
937 | } |
---|
938 | } |
---|
939 | |
---|
940 | void ntNormalize (number &a, const coeffs cf) |
---|
941 | { |
---|
942 | definiteGcdCancellation(a, cf, FALSE); |
---|
943 | } |
---|
944 | |
---|
945 | /* expects *param to be castable to TransExtInfo */ |
---|
946 | static BOOLEAN ntCoeffIsEqual(const coeffs cf, n_coeffType n, void * param) |
---|
947 | { |
---|
948 | if (ID != n) return FALSE; |
---|
949 | TransExtInfo *e = (TransExtInfo *)param; |
---|
950 | /* for rational function fields we expect the underlying |
---|
951 | polynomial rings to be IDENTICAL, i.e. the SAME OBJECT; |
---|
952 | this expectation is based on the assumption that we have properly |
---|
953 | registered cf and perform reference counting rather than creating |
---|
954 | multiple copies of the same coefficient field/domain/ring */ |
---|
955 | if (ntRing == e->r) |
---|
956 | return TRUE; |
---|
957 | |
---|
958 | // NOTE: Q(a)[x] && Q(a)[y] should better share the _same_ Q(a)... |
---|
959 | if( rEqual(ntRing, e->r, TRUE) ) |
---|
960 | { |
---|
961 | rDelete(e->r); |
---|
962 | return TRUE; |
---|
963 | } |
---|
964 | |
---|
965 | return FALSE; |
---|
966 | } |
---|
967 | |
---|
968 | number ntLcm(number a, number b, const coeffs cf) |
---|
969 | { |
---|
970 | ntTest(a); ntTest(b); |
---|
971 | fraction fb = (fraction)b; |
---|
972 | if ((b==NULL)||(DEN(fb)==NULL)) return ntCopy(a,cf); |
---|
973 | #ifdef HAVE_FACTORY |
---|
974 | fraction fa = (fraction)a; |
---|
975 | /* singclap_gcd destroys its arguments; we hence need copies: */ |
---|
976 | poly pa = p_Copy(NUM(fa), ntRing); |
---|
977 | poly pb = p_Copy(DEN(fb), ntRing); |
---|
978 | |
---|
979 | /* Note that, over Q, singclap_gcd will remove the denominators in all |
---|
980 | rational coefficients of pa and pb, before starting to compute |
---|
981 | the gcd. Thus, we do not need to ensure that the coefficients of |
---|
982 | pa and pb live in Z; they may well be elements of Q\Z. */ |
---|
983 | poly pGcd = singclap_gcd(pa, pb, cf->extRing); |
---|
984 | if (p_IsConstant(pGcd, ntRing) && |
---|
985 | n_IsOne(p_GetCoeff(pGcd, ntRing), ntCoeffs)) |
---|
986 | { /* gcd = 1; return pa*pb*/ |
---|
987 | p_Delete(&pGcd,ntRing); |
---|
988 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
989 | NUM(result) = pp_Mult_qq(NUM(fa),DEN(fb),ntRing); |
---|
990 | return (number)result; |
---|
991 | } |
---|
992 | else |
---|
993 | { /* return pa*pb/gcd */ |
---|
994 | poly newNum = singclap_pdivide(NUM(fa), pGcd, ntRing); |
---|
995 | p_Delete(&pGcd,ntRing); |
---|
996 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
997 | NUM(result) = p_Mult_q(p_Copy(DEN(fb),ntRing),newNum,ntRing); |
---|
998 | return (number)result; |
---|
999 | } |
---|
1000 | #else |
---|
1001 | Print("// factory needed: transext.cc:ntLcm\n"); |
---|
1002 | return NULL; |
---|
1003 | #endif /* HAVE_FACTORY */ |
---|
1004 | return NULL; |
---|
1005 | } |
---|
1006 | |
---|
1007 | number ntGcd(number a, number b, const coeffs cf) |
---|
1008 | { |
---|
1009 | ntTest(a); ntTest(b); |
---|
1010 | if (a==NULL) return ntCopy(b,cf); |
---|
1011 | if (b==NULL) return ntCopy(a,cf); |
---|
1012 | #ifdef HAVE_FACTORY |
---|
1013 | fraction fa = (fraction)a; |
---|
1014 | fraction fb = (fraction)b; |
---|
1015 | /* singclap_gcd destroys its arguments; we hence need copies: */ |
---|
1016 | poly pa = p_Copy(NUM(fa), ntRing); |
---|
1017 | poly pb = p_Copy(NUM(fb), ntRing); |
---|
1018 | |
---|
1019 | /* Note that, over Q, singclap_gcd will remove the denominators in all |
---|
1020 | rational coefficients of pa and pb, before starting to compute |
---|
1021 | the gcd. Thus, we do not need to ensure that the coefficients of |
---|
1022 | pa and pb live in Z; they may well be elements of Q\Z. */ |
---|
1023 | poly pGcd = singclap_gcd(pa, pb, cf->extRing); |
---|
1024 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1025 | NUM(result) = pGcd; |
---|
1026 | return (number)result; |
---|
1027 | #else |
---|
1028 | Print("// factory needed: transext.cc:ntGcd\n"); |
---|
1029 | return NULL; |
---|
1030 | #endif /* HAVE_FACTORY */ |
---|
1031 | } |
---|
1032 | |
---|
1033 | int ntSize(number a, const coeffs cf) |
---|
1034 | { |
---|
1035 | ntTest(a); |
---|
1036 | if (IS0(a)) return -1; |
---|
1037 | /* this has been taken from the old implementation of field extensions, |
---|
1038 | where we computed the sum of the degrees and the numbers of terms in |
---|
1039 | the numerator and denominator of a; so we leave it at that, for the |
---|
1040 | time being */ |
---|
1041 | fraction f = (fraction)a; |
---|
1042 | poly p = NUM(f); |
---|
1043 | int noOfTerms = 0; |
---|
1044 | int numDegree = 0; |
---|
1045 | while (p != NULL) |
---|
1046 | { |
---|
1047 | noOfTerms++; |
---|
1048 | int d = 0; |
---|
1049 | for (int i = 1; i <= rVar(ntRing); i++) |
---|
1050 | d += p_GetExp(p, i, ntRing); |
---|
1051 | if (d > numDegree) numDegree = d; |
---|
1052 | pIter(p); |
---|
1053 | } |
---|
1054 | int denDegree = 0; |
---|
1055 | if (!DENIS1(f)) |
---|
1056 | { |
---|
1057 | p = DEN(f); |
---|
1058 | while (p != NULL) |
---|
1059 | { |
---|
1060 | noOfTerms++; |
---|
1061 | int d = 0; |
---|
1062 | for (int i = 1; i <= rVar(ntRing); i++) |
---|
1063 | d += p_GetExp(p, i, ntRing); |
---|
1064 | if (d > denDegree) denDegree = d; |
---|
1065 | pIter(p); |
---|
1066 | } |
---|
1067 | } |
---|
1068 | return numDegree + denDegree + noOfTerms; |
---|
1069 | } |
---|
1070 | |
---|
1071 | number ntInvers(number a, const coeffs cf) |
---|
1072 | { |
---|
1073 | ntTest(a); |
---|
1074 | if (IS0(a)) WerrorS(nDivBy0); |
---|
1075 | fraction f = (fraction)a; |
---|
1076 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1077 | poly g; |
---|
1078 | if (DENIS1(f)) g = p_One(ntRing); |
---|
1079 | else g = p_Copy(DEN(f), ntRing); |
---|
1080 | NUM(result) = g; |
---|
1081 | DEN(result) = p_Copy(NUM(f), ntRing); |
---|
1082 | COM(result) = COM(f); |
---|
1083 | return (number)result; |
---|
1084 | } |
---|
1085 | |
---|
1086 | /* assumes that src = Q, dst = Q(t_1, ..., t_s) */ |
---|
1087 | number ntMap00(number a, const coeffs src, const coeffs dst) |
---|
1088 | { |
---|
1089 | if (n_IsZero(a, src)) return NULL; |
---|
1090 | assume(src == dst->extRing->cf); |
---|
1091 | poly p = p_Init(dst->extRing); |
---|
1092 | number na=n_Copy(a, src); |
---|
1093 | n_Normalize(na, src); |
---|
1094 | p_SetCoeff0(p, na, dst->extRing); |
---|
1095 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1096 | NUM(f) = p; DEN(f) = NULL; COM(f) = 0; |
---|
1097 | return (number)f; |
---|
1098 | } |
---|
1099 | |
---|
1100 | /* assumes that src = Z/p, dst = Q(t_1, ..., t_s) */ |
---|
1101 | number ntMapP0(number a, const coeffs src, const coeffs dst) |
---|
1102 | { |
---|
1103 | if (n_IsZero(a, src)) return NULL; |
---|
1104 | /* mapping via intermediate int: */ |
---|
1105 | int n = n_Int(a, src); |
---|
1106 | number q = n_Init(n, dst->extRing->cf); |
---|
1107 | poly p; |
---|
1108 | if (n_IsZero(q, dst->extRing->cf)) |
---|
1109 | { |
---|
1110 | n_Delete(&q, dst->extRing->cf); |
---|
1111 | return NULL; |
---|
1112 | } |
---|
1113 | p = p_One(dst->extRing); |
---|
1114 | p_SetCoeff(p, q, dst->extRing); |
---|
1115 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1116 | NUM(f) = p; DEN(f) = NULL; COM(f) = 0; |
---|
1117 | return (number)f; |
---|
1118 | } |
---|
1119 | |
---|
1120 | /* assumes that either src = Q(t_1, ..., t_s), dst = Q(t_1, ..., t_s), or |
---|
1121 | src = Z/p(t_1, ..., t_s), dst = Z/p(t_1, ..., t_s) */ |
---|
1122 | number ntCopyMap(number a, const coeffs cf, const coeffs dst) |
---|
1123 | { |
---|
1124 | // if (n_IsZero(a, cf)) return NULL; |
---|
1125 | |
---|
1126 | ntTest(a); |
---|
1127 | |
---|
1128 | if (IS0(a)) return NULL; |
---|
1129 | |
---|
1130 | const ring rSrc = cf->extRing; |
---|
1131 | const ring rDst = dst->extRing; |
---|
1132 | |
---|
1133 | if( rSrc == rDst ) |
---|
1134 | return ntCopy(a, dst); // USUALLY WRONG! |
---|
1135 | |
---|
1136 | fraction f = (fraction)a; |
---|
1137 | poly g = prCopyR(NUM(f), rSrc, rDst); |
---|
1138 | |
---|
1139 | poly h = NULL; |
---|
1140 | |
---|
1141 | if (!DENIS1(f)) |
---|
1142 | h = prCopyR(DEN(f), rSrc, rDst); |
---|
1143 | |
---|
1144 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1145 | |
---|
1146 | NUM(result) = g; |
---|
1147 | DEN(result) = h; |
---|
1148 | COM(result) = COM(f); |
---|
1149 | return (number)result; |
---|
1150 | } |
---|
1151 | |
---|
1152 | number ntCopyAlg(number a, const coeffs cf, const coeffs dst) |
---|
1153 | { |
---|
1154 | if (n_IsZero(a, cf)) return NULL; |
---|
1155 | |
---|
1156 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1157 | // DEN(f) = NULL; COM(f) = 0; |
---|
1158 | NUM(f) = prCopyR((poly)a, cf->extRing, dst->extRing); |
---|
1159 | return (number)f; |
---|
1160 | } |
---|
1161 | |
---|
1162 | /* assumes that src = Q, dst = Z/p(t_1, ..., t_s) */ |
---|
1163 | number ntMap0P(number a, const coeffs src, const coeffs dst) |
---|
1164 | { |
---|
1165 | if (n_IsZero(a, src)) return NULL; |
---|
1166 | int p = rChar(dst->extRing); |
---|
1167 | number q = nlModP(a, src, dst->extRing->cf); |
---|
1168 | |
---|
1169 | if (n_IsZero(q, dst->extRing->cf)) |
---|
1170 | { |
---|
1171 | n_Delete(&q, dst->extRing->cf); |
---|
1172 | return NULL; |
---|
1173 | } |
---|
1174 | |
---|
1175 | poly g = p_NSet(q, dst->extRing); |
---|
1176 | |
---|
1177 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1178 | NUM(f) = g; // DEN(f) = NULL; COM(f) = 0; |
---|
1179 | return (number)f; |
---|
1180 | } |
---|
1181 | |
---|
1182 | /* assumes that src = Z/p, dst = Z/p(t_1, ..., t_s) */ |
---|
1183 | number ntMapPP(number a, const coeffs src, const coeffs dst) |
---|
1184 | { |
---|
1185 | if (n_IsZero(a, src)) return NULL; |
---|
1186 | assume(src == dst->extRing->cf); |
---|
1187 | poly p = p_One(dst->extRing); |
---|
1188 | p_SetCoeff(p, n_Copy(a, src), dst->extRing); |
---|
1189 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1190 | NUM(f) = p; DEN(f) = NULL; COM(f) = 0; |
---|
1191 | return (number)f; |
---|
1192 | } |
---|
1193 | |
---|
1194 | /* assumes that src = Z/u, dst = Z/p(t_1, ..., t_s), where u != p */ |
---|
1195 | number ntMapUP(number a, const coeffs src, const coeffs dst) |
---|
1196 | { |
---|
1197 | if (n_IsZero(a, src)) return NULL; |
---|
1198 | /* mapping via intermediate int: */ |
---|
1199 | int n = n_Int(a, src); |
---|
1200 | number q = n_Init(n, dst->extRing->cf); |
---|
1201 | poly p; |
---|
1202 | if (n_IsZero(q, dst->extRing->cf)) |
---|
1203 | { |
---|
1204 | n_Delete(&q, dst->extRing->cf); |
---|
1205 | return NULL; |
---|
1206 | } |
---|
1207 | p = p_One(dst->extRing); |
---|
1208 | p_SetCoeff(p, q, dst->extRing); |
---|
1209 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1210 | NUM(f) = p; DEN(f) = NULL; COM(f) = 0; |
---|
1211 | return (number)f; |
---|
1212 | } |
---|
1213 | |
---|
1214 | nMapFunc ntSetMap(const coeffs src, const coeffs dst) |
---|
1215 | { |
---|
1216 | /* dst is expected to be a rational function field */ |
---|
1217 | assume(getCoeffType(dst) == ID); |
---|
1218 | |
---|
1219 | int h = 0; /* the height of the extension tower given by dst */ |
---|
1220 | coeffs bDst = nCoeff_bottom(dst, h); /* the bottom field in the tower dst */ |
---|
1221 | coeffs bSrc = nCoeff_bottom(src, h); /* the bottom field in the tower src */ |
---|
1222 | |
---|
1223 | /* for the time being, we only provide maps if h = 1 and if b is Q or |
---|
1224 | some field Z/pZ: */ |
---|
1225 | if (h==0) |
---|
1226 | { |
---|
1227 | if (nCoeff_is_Q(src) && nCoeff_is_Q(bDst)) |
---|
1228 | return ntMap00; /// Q --> Q(T) |
---|
1229 | if (nCoeff_is_Zp(src) && nCoeff_is_Q(bDst)) |
---|
1230 | return ntMapP0; /// Z/p --> Q(T) |
---|
1231 | if (nCoeff_is_Q(src) && nCoeff_is_Zp(bDst)) |
---|
1232 | return ntMap0P; /// Q --> Z/p(T) |
---|
1233 | if (nCoeff_is_Zp(src) && nCoeff_is_Zp(bDst)) |
---|
1234 | { |
---|
1235 | if (src->ch == dst->ch) return ntMapPP; /// Z/p --> Z/p(T) |
---|
1236 | else return ntMapUP; /// Z/u --> Z/p(T) |
---|
1237 | } |
---|
1238 | } |
---|
1239 | if (h != 1) return NULL; |
---|
1240 | if ((!nCoeff_is_Zp(bDst)) && (!nCoeff_is_Q(bDst))) return NULL; |
---|
1241 | |
---|
1242 | /* Let T denote the sequence of transcendental extension variables, i.e., |
---|
1243 | K[t_1, ..., t_s] =: K[T]; |
---|
1244 | Let moreover, for any such sequence T, T' denote any subsequence of T |
---|
1245 | of the form t_1, ..., t_w with w <= s. */ |
---|
1246 | |
---|
1247 | if ((!nCoeff_is_Zp(bSrc)) && (!nCoeff_is_Q(bSrc))) return NULL; |
---|
1248 | |
---|
1249 | if (nCoeff_is_Q(bSrc) && nCoeff_is_Q(bDst)) |
---|
1250 | { |
---|
1251 | if (rVar(src->extRing) > rVar(dst->extRing)) |
---|
1252 | return NULL; |
---|
1253 | |
---|
1254 | for (int i = 0; i < rVar(src->extRing); i++) |
---|
1255 | if (strcmp(rRingVar(i, src->extRing), rRingVar(i, dst->extRing)) != 0) |
---|
1256 | return NULL; |
---|
1257 | |
---|
1258 | if (src->type==n_transExt) |
---|
1259 | return ntCopyMap; /// Q(T') --> Q(T) |
---|
1260 | else |
---|
1261 | return ntCopyAlg; |
---|
1262 | } |
---|
1263 | |
---|
1264 | if (nCoeff_is_Zp(bSrc) && nCoeff_is_Zp(bDst)) |
---|
1265 | { |
---|
1266 | if (rVar(src->extRing) > rVar(dst->extRing)) |
---|
1267 | return NULL; |
---|
1268 | |
---|
1269 | for (int i = 0; i < rVar(src->extRing); i++) |
---|
1270 | if (strcmp(rRingVar(i, src->extRing), rRingVar(i, dst->extRing)) != 0) |
---|
1271 | return NULL; |
---|
1272 | |
---|
1273 | if (src->type==n_transExt) |
---|
1274 | return ntCopyMap; /// Z/p(T') --> Z/p(T) |
---|
1275 | else |
---|
1276 | return ntCopyAlg; |
---|
1277 | } |
---|
1278 | |
---|
1279 | return NULL; /// default |
---|
1280 | } |
---|
1281 | #if 0 |
---|
1282 | nMapFunc ntSetMap_T(const coeffs src, const coeffs dst) |
---|
1283 | { |
---|
1284 | nMapFunc n=ntSetMap(src,dst); |
---|
1285 | if (n==ntCopyAlg) printf("n=ntCopyAlg\n"); |
---|
1286 | else if (n==ntCopyMap) printf("n=ntCopyMap\n"); |
---|
1287 | else if (n==ntMapUP) printf("n=ntMapUP\n"); |
---|
1288 | else if (n==ntMap0P) printf("n=ntMap0P\n"); |
---|
1289 | else if (n==ntMapP0) printf("n=ntMapP0\n"); |
---|
1290 | else if (n==ntMap00) printf("n=ntMap00\n"); |
---|
1291 | else if (n==NULL) printf("n=NULL\n"); |
---|
1292 | else printf("n=?\n"); |
---|
1293 | return n; |
---|
1294 | } |
---|
1295 | #endif |
---|
1296 | |
---|
1297 | void ntKillChar(coeffs cf) |
---|
1298 | { |
---|
1299 | if ((--cf->extRing->ref) == 0) |
---|
1300 | rDelete(cf->extRing); |
---|
1301 | } |
---|
1302 | #ifdef HAVE_FACTORY |
---|
1303 | number ntConvFactoryNSingN( const CanonicalForm n, const coeffs cf) |
---|
1304 | { |
---|
1305 | if (n.isZero()) return NULL; |
---|
1306 | poly p=convFactoryPSingP(n,ntRing); |
---|
1307 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1308 | NUM(result) = p; |
---|
1309 | //DEN(result) = NULL; // done by omAlloc0Bin |
---|
1310 | //COM(result) = 0; // done by omAlloc0Bin |
---|
1311 | return (number)result; |
---|
1312 | } |
---|
1313 | CanonicalForm ntConvSingNFactoryN( number n, BOOLEAN setChar, const coeffs cf ) |
---|
1314 | { |
---|
1315 | ntTest(n); |
---|
1316 | if (IS0(n)) return CanonicalForm(0); |
---|
1317 | |
---|
1318 | fraction f = (fraction)n; |
---|
1319 | return convSingPFactoryP(NUM(f),ntRing); |
---|
1320 | } |
---|
1321 | #endif |
---|
1322 | |
---|
1323 | int ntParDeg(number a, const coeffs cf) |
---|
1324 | { |
---|
1325 | if (IS0(a)) return -1; |
---|
1326 | fraction fa = (fraction)a; |
---|
1327 | return cf->extRing->pFDeg(NUM(fa),cf->extRing); |
---|
1328 | } |
---|
1329 | |
---|
1330 | BOOLEAN ntInitChar(coeffs cf, void * infoStruct) |
---|
1331 | { |
---|
1332 | |
---|
1333 | assume( infoStruct != NULL ); |
---|
1334 | |
---|
1335 | TransExtInfo *e = (TransExtInfo *)infoStruct; |
---|
1336 | |
---|
1337 | assume( e->r != NULL); // extRing; |
---|
1338 | assume( e->r->cf != NULL); // extRing->cf; |
---|
1339 | assume( e->r->minideal == NULL ); |
---|
1340 | |
---|
1341 | assume( cf != NULL ); |
---|
1342 | assume(getCoeffType(cf) == ID); // coeff type; |
---|
1343 | |
---|
1344 | cf->extRing = e->r; |
---|
1345 | cf->extRing->ref ++; // increase the ref.counter for the ground poly. ring! |
---|
1346 | cf->factoryVarOffset = cf->extRing->cf->factoryVarOffset+rVar(cf->extRing); |
---|
1347 | |
---|
1348 | /* propagate characteristic up so that it becomes |
---|
1349 | directly accessible in cf: */ |
---|
1350 | cf->ch = cf->extRing->cf->ch; |
---|
1351 | |
---|
1352 | cf->cfGreaterZero = ntGreaterZero; |
---|
1353 | cf->cfGreater = ntGreater; |
---|
1354 | cf->cfEqual = ntEqual; |
---|
1355 | cf->cfIsZero = ntIsZero; |
---|
1356 | cf->cfIsOne = ntIsOne; |
---|
1357 | cf->cfIsMOne = ntIsMOne; |
---|
1358 | cf->cfInit = ntInit; |
---|
1359 | cf->cfInit_bigint = ntInit_bigint; |
---|
1360 | cf->cfInt = ntInt; |
---|
1361 | cf->cfNeg = ntNeg; |
---|
1362 | cf->cfAdd = ntAdd; |
---|
1363 | cf->cfSub = ntSub; |
---|
1364 | cf->cfMult = ntMult; |
---|
1365 | cf->cfDiv = ntDiv; |
---|
1366 | cf->cfExactDiv = ntDiv; |
---|
1367 | cf->cfPower = ntPower; |
---|
1368 | cf->cfCopy = ntCopy; |
---|
1369 | cf->cfWrite = ntWrite; |
---|
1370 | cf->cfRead = ntRead; |
---|
1371 | cf->cfNormalize = ntNormalize; |
---|
1372 | cf->cfDelete = ntDelete; |
---|
1373 | cf->cfSetMap = ntSetMap; |
---|
1374 | cf->cfGetDenom = ntGetDenom; |
---|
1375 | cf->cfGetNumerator = ntGetNumerator; |
---|
1376 | cf->cfRePart = ntCopy; |
---|
1377 | cf->cfImPart = ntImPart; |
---|
1378 | cf->cfCoeffWrite = ntCoeffWrite; |
---|
1379 | #ifdef LDEBUG |
---|
1380 | cf->cfDBTest = ntDBTest; |
---|
1381 | #endif |
---|
1382 | cf->cfGcd = ntGcd; |
---|
1383 | cf->cfLcm = ntLcm; |
---|
1384 | cf->cfSize = ntSize; |
---|
1385 | cf->nCoeffIsEqual = ntCoeffIsEqual; |
---|
1386 | cf->cfInvers = ntInvers; |
---|
1387 | cf->cfIntDiv = ntDiv; |
---|
1388 | cf->cfKillChar = ntKillChar; |
---|
1389 | |
---|
1390 | #ifndef HAVE_FACTORY |
---|
1391 | PrintS("// Warning: The 'factory' module is not available.\n"); |
---|
1392 | PrintS("// Hence gcd's cannot be cancelled in any\n"); |
---|
1393 | PrintS("// computed fraction!\n"); |
---|
1394 | #else |
---|
1395 | cf->convFactoryNSingN =ntConvFactoryNSingN; |
---|
1396 | cf->convSingNFactoryN =ntConvSingNFactoryN; |
---|
1397 | #endif |
---|
1398 | cf->cfParDeg = ntParDeg; |
---|
1399 | |
---|
1400 | return FALSE; |
---|
1401 | } |
---|
1402 | |
---|
1403 | |
---|
1404 | number ntParam(const short iParameter, const coeffs cf) |
---|
1405 | { |
---|
1406 | assume(getCoeffType(cf) == ID); |
---|
1407 | |
---|
1408 | const ring R = cf->extRing; |
---|
1409 | assume( R != NULL ); |
---|
1410 | assume( 0 < iParameter && iParameter <= rVar(R) ); |
---|
1411 | |
---|
1412 | poly p = p_One(R); p_SetExp(p, iParameter, 1, R); p_Setm(p, R); |
---|
1413 | |
---|
1414 | // return (number) p; |
---|
1415 | |
---|
1416 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
1417 | NUM(f) = p; |
---|
1418 | DEN(f) = NULL; |
---|
1419 | COM(f) = 0; |
---|
1420 | |
---|
1421 | return (number)f; |
---|
1422 | } |
---|
1423 | |
---|
1424 | |
---|
1425 | /// if m == var(i)/1 => return i, |
---|
1426 | int ntIsParam(number m, const coeffs cf) |
---|
1427 | { |
---|
1428 | assume(getCoeffType(cf) == ID); |
---|
1429 | |
---|
1430 | const ring R = cf->extRing; |
---|
1431 | assume( R != NULL ); |
---|
1432 | |
---|
1433 | fraction f = (fraction)m; |
---|
1434 | |
---|
1435 | if( DEN(f) != NULL ) |
---|
1436 | return 0; |
---|
1437 | |
---|
1438 | return p_Var( NUM(f), R ); |
---|
1439 | } |
---|