[6ccdd3a] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
[2c7f28] | 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 |
---|
[06df101] | 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 |
---|
[6d92ca] | 23 | * and cancel it out in the given number. |
---|
[06df101] | 24 | * For the special case of K = Q (i.e., when computing over the |
---|
| 25 | * rationals), this definite cancellation procedure will also take |
---|
| 26 | * care of nested fractions: If there are fractional coefficients |
---|
| 27 | * in the numerator or denominator of a number, then this number |
---|
| 28 | * is being replaced by a quotient of two polynomials over Z, or |
---|
| 29 | * - if the denominator is a constant - by a polynomial over Q. |
---|
[dc79bd] | 30 | * |
---|
| 31 | * TODO: the description above needs a major update!!! |
---|
[6ccdd3a] | 32 | */ |
---|
[1f414c8] | 33 | #define TRANSEXT_PRIVATES |
---|
[6ccdd3a] | 34 | |
---|
| 35 | #include <misc/auxiliary.h> |
---|
| 36 | |
---|
| 37 | #include <omalloc/omalloc.h> |
---|
[810491] | 38 | #include <factory/factory.h> |
---|
[6ccdd3a] | 39 | |
---|
| 40 | #include <reporter/reporter.h> |
---|
| 41 | |
---|
| 42 | #include <coeffs/coeffs.h> |
---|
| 43 | #include <coeffs/numbers.h> |
---|
[810491] | 44 | |
---|
[6ccdd3a] | 45 | #include <coeffs/longrat.h> |
---|
| 46 | |
---|
| 47 | #include <polys/monomials/ring.h> |
---|
| 48 | #include <polys/monomials/p_polys.h> |
---|
| 49 | #include <polys/simpleideals.h> |
---|
| 50 | |
---|
[e5d267] | 51 | #include <polys/clapsing.h> |
---|
[fc4977] | 52 | #include <polys/clapconv.h> |
---|
[e5d267] | 53 | |
---|
[dc79bd] | 54 | #include <polys/prCopy.h> |
---|
[810491] | 55 | #include "transext.h" |
---|
| 56 | #include "algext.h" |
---|
[dc79bd] | 57 | |
---|
| 58 | #include <polys/PolyEnumerator.h> |
---|
| 59 | |
---|
[1f414c8] | 60 | |
---|
| 61 | /* constants for controlling the complexity of numbers */ |
---|
| 62 | #define ADD_COMPLEXITY 1 /**< complexity increase due to + and - */ |
---|
| 63 | #define MULT_COMPLEXITY 2 /**< complexity increase due to * and / */ |
---|
[12f6ce0] | 64 | #define DIFF_COMPLEXITY 2 /**< complexity increase due to * and / */ |
---|
[1f414c8] | 65 | #define BOUND_COMPLEXITY 10 /**< maximum complexity of a number */ |
---|
| 66 | |
---|
[dc79bd] | 67 | /// TRUE iff num. represents 1 |
---|
| 68 | #define NUMIS1(f) (p_IsOne(NUM(f), cf->extRing)) |
---|
| 69 | |
---|
[1f414c8] | 70 | #define COM(f) f->complexity |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | #ifdef LDEBUG |
---|
| 74 | BOOLEAN ntDBTest(number a, const char *f, const int l, const coeffs r); |
---|
| 75 | #endif |
---|
| 76 | |
---|
[fc2acf] | 77 | #define ntTest(a) n_Test(a, cf) |
---|
| 78 | |
---|
[1f414c8] | 79 | /* polynomial ring in which the numerators and denominators of our |
---|
| 80 | numbers live */ |
---|
| 81 | #define ntRing cf->extRing |
---|
| 82 | |
---|
| 83 | /* coeffs object in which the coefficients of our numbers live; |
---|
| 84 | * methods attached to ntCoeffs may be used to compute with the |
---|
| 85 | * coefficients of our numbers, e.g., use ntCoeffs->nAdd to add |
---|
| 86 | * coefficients of our numbers */ |
---|
| 87 | #define ntCoeffs cf->extRing->cf |
---|
| 88 | |
---|
[6ccdd3a] | 89 | |
---|
[fc4977] | 90 | omBin fractionObjectBin = omGetSpecBin(sizeof(fractionObject)); |
---|
[de90c01] | 91 | |
---|
[6ccdd3a] | 92 | /// forward declarations |
---|
[c14846c] | 93 | BOOLEAN ntGreaterZero(number a, const coeffs cf); |
---|
[6ccdd3a] | 94 | BOOLEAN ntGreater(number a, number b, const coeffs cf); |
---|
| 95 | BOOLEAN ntEqual(number a, number b, const coeffs cf); |
---|
| 96 | BOOLEAN ntIsOne(number a, const coeffs cf); |
---|
| 97 | BOOLEAN ntIsMOne(number a, const coeffs cf); |
---|
| 98 | BOOLEAN ntIsZero(number a, const coeffs cf); |
---|
[2f3764] | 99 | number ntInit(long i, const coeffs cf); |
---|
[777f8b] | 100 | long ntInt(number &a, const coeffs cf); |
---|
[6ccdd3a] | 101 | number ntNeg(number a, const coeffs cf); |
---|
| 102 | number ntInvers(number a, const coeffs cf); |
---|
| 103 | number ntAdd(number a, number b, const coeffs cf); |
---|
| 104 | number ntSub(number a, number b, const coeffs cf); |
---|
| 105 | number ntMult(number a, number b, const coeffs cf); |
---|
| 106 | number ntDiv(number a, number b, const coeffs cf); |
---|
| 107 | void ntPower(number a, int exp, number *b, const coeffs cf); |
---|
| 108 | number ntCopy(number a, const coeffs cf); |
---|
[6d92ca] | 109 | void ntWriteLong(number a, const coeffs cf); |
---|
| 110 | void ntWriteShort(number a, const coeffs cf); |
---|
[6ccdd3a] | 111 | number ntRePart(number a, const coeffs cf); |
---|
| 112 | number ntImPart(number a, const coeffs cf); |
---|
| 113 | number ntGetDenom(number &a, const coeffs cf); |
---|
| 114 | number ntGetNumerator(number &a, const coeffs cf); |
---|
| 115 | number ntGcd(number a, number b, const coeffs cf); |
---|
[88cceb] | 116 | number ntNormalizeHelper(number a, number b, const coeffs cf); |
---|
[2c7f28] | 117 | int ntSize(number a, const coeffs cf); |
---|
[6ccdd3a] | 118 | void ntDelete(number * a, const coeffs cf); |
---|
[03f7b5] | 119 | void ntCoeffWrite(const coeffs cf, BOOLEAN details); |
---|
[6ccdd3a] | 120 | const char * ntRead(const char *s, number *a, const coeffs cf); |
---|
| 121 | static BOOLEAN ntCoeffIsEqual(const coeffs cf, n_coeffType n, void * param); |
---|
| 122 | |
---|
[2c7f28] | 123 | void heuristicGcdCancellation(number a, const coeffs cf); |
---|
[010f3b] | 124 | void definiteGcdCancellation(number a, const coeffs cf, |
---|
[06df101] | 125 | BOOLEAN simpleTestsHaveAlreadyBeenPerformed); |
---|
| 126 | void handleNestedFractionsOverQ(fraction f, const coeffs cf); |
---|
[2c7f28] | 127 | |
---|
[fbdfd4] | 128 | /* test routine, usualy disabled * |
---|
| 129 | * if want to activate it, activate also the calls to check_N * |
---|
| 130 | * |
---|
| 131 | void check_normalized(number t,const coeffs cf, const char *f, int l) |
---|
| 132 | { |
---|
| 133 | if (IS0(t)) return; |
---|
| 134 | if(rField_is_Q(ntRing)) |
---|
| 135 | { |
---|
| 136 | poly pp=NUM(t); |
---|
| 137 | while(pp!=NULL) |
---|
| 138 | { |
---|
| 139 | if (((SR_HDL(pGetCoeff(pp)) & SR_INT)==0)&&(SR_HDL(pGetCoeff(pp))!=NULL)) |
---|
| 140 | { |
---|
| 141 | if (pGetCoeff(pp)->s==0) |
---|
| 142 | { |
---|
| 143 | Print("NUM not normalized in %s:%d\n",f,l); |
---|
| 144 | p_Normalize(pp,ntRing); |
---|
| 145 | } |
---|
| 146 | else if (pGetCoeff(pp)->s==1) |
---|
| 147 | Print("NUM is rational in %s:%d\n",f,l); |
---|
| 148 | } |
---|
| 149 | pIter(pp); |
---|
| 150 | } |
---|
| 151 | pp=DEN(t); |
---|
| 152 | while(pp!=NULL) |
---|
| 153 | { |
---|
| 154 | if (((SR_HDL(pGetCoeff(pp)) & SR_INT)==0)&&(SR_HDL(pGetCoeff(pp))!=NULL)) |
---|
| 155 | { |
---|
| 156 | if (pGetCoeff(pp)->s==0) |
---|
| 157 | { |
---|
| 158 | Print("NUM not normalized in %s:%d\n",f,l); |
---|
| 159 | p_Normalize(pp,ntRing); |
---|
| 160 | } |
---|
| 161 | else if (pGetCoeff(pp)->s==1) |
---|
| 162 | Print("DEN is rational in %s:%d\n",f,l); |
---|
| 163 | } |
---|
| 164 | pIter(pp); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | #define check_N(A,B) check_normalized(A,B,__FILE__,__LINE__) |
---|
| 169 | */ |
---|
| 170 | |
---|
[6ccdd3a] | 171 | #ifdef LDEBUG |
---|
| 172 | BOOLEAN ntDBTest(number a, const char *f, const int l, const coeffs cf) |
---|
| 173 | { |
---|
[6d0515d] | 174 | assume(getCoeffType(cf) == n_transExt); |
---|
[dc79bd] | 175 | |
---|
| 176 | if (IS0(a)) return TRUE; |
---|
| 177 | |
---|
| 178 | const fraction t = (fraction)a; |
---|
| 179 | |
---|
[fbdfd4] | 180 | //check_N(a,cf); |
---|
[dc79bd] | 181 | const poly num = NUM(t); |
---|
[fc2acf] | 182 | assume(num != NULL); ///< t != 0 ==> numerator(t) != 0 |
---|
[fea494] | 183 | |
---|
| 184 | p_Test(num, ntRing); |
---|
[dc79bd] | 185 | |
---|
[fc2acf] | 186 | if (getCoeffType(ntCoeffs)==n_Q) |
---|
| 187 | for( poly p = num; p != NULL; pIter(p) ) |
---|
| 188 | if (! nlIsInteger( p_GetCoeff(p, ntRing), ntCoeffs) ) |
---|
| 189 | { |
---|
[ba52f58] | 190 | Print("ERROR in %s:%d: non-integer Q coeff in num. poly\n",f,l); |
---|
| 191 | Print("TERM: "); p_wrp(p, ntRing); PrintLn(); |
---|
[e9478b] | 192 | return FALSE; |
---|
[fc2acf] | 193 | } |
---|
| 194 | |
---|
[dc79bd] | 195 | const poly den = DEN(t); |
---|
[a355723] | 196 | |
---|
[dc79bd] | 197 | if (den != NULL) // !DENIS1(f) |
---|
[1374bc] | 198 | { |
---|
[eb55f8a] | 199 | p_Test(den, ntRing); |
---|
[fc2acf] | 200 | |
---|
| 201 | if (getCoeffType(ntCoeffs)==n_Q) |
---|
| 202 | for( poly p = den; p != NULL; pIter(p) ) |
---|
| 203 | if (! nlIsInteger( p_GetCoeff(p, ntRing), ntCoeffs) ) |
---|
| 204 | { |
---|
[e9478b] | 205 | Print("ERROR in %s:%d: non-integer Q coeff in den. poly\n",f,l); |
---|
[ba52f58] | 206 | Print("TERM: "); p_wrp(p, ntRing); PrintLn(); |
---|
[e9478b] | 207 | return FALSE; |
---|
[fc2acf] | 208 | } |
---|
| 209 | |
---|
| 210 | if (getCoeffType(ntCoeffs)==n_Zp) |
---|
| 211 | { |
---|
| 212 | if( p_IsConstant(den, ntRing) ) |
---|
| 213 | { |
---|
[ba52f58] | 214 | Print("ERROR in %s:%d: constant den. poly / Zp\n",f,l); |
---|
[f9b0bd] | 215 | PrintS("NUM: "); p_Write(num, ntRing); |
---|
| 216 | PrintS("DEN: "); p_Write(den, ntRing); |
---|
[fc2acf] | 217 | return FALSE; |
---|
| 218 | } |
---|
[e9478b] | 219 | |
---|
[fc2acf] | 220 | if( !n_IsOne(pGetCoeff(den), ntCoeffs) ) |
---|
[e9478b] | 221 | { |
---|
| 222 | Print("ERROR in %s:%d: non-monic den. poly / Zp\n",f,l); |
---|
[f9b0bd] | 223 | PrintS("NUM: "); p_Write(num, ntRing); |
---|
| 224 | PrintS("DEN: "); p_Write(den, ntRing); |
---|
[fc2acf] | 225 | return FALSE; |
---|
| 226 | } |
---|
| 227 | } |
---|
[e9478b] | 228 | |
---|
[4132ee] | 229 | if (COM(t)==0) |
---|
[fc2acf] | 230 | { |
---|
[4132ee] | 231 | poly gcd = singclap_gcd_r( num, den, ntRing ); |
---|
[b06842] | 232 | if(gcd!=NULL) |
---|
[4132ee] | 233 | { |
---|
[b06842] | 234 | if((gcd!=NULL) && !p_IsOne(gcd, ntRing) ) |
---|
| 235 | { |
---|
| 236 | Print("ERROR in %s:%d: 1 != GCD between num. & den. poly\n",f,l); |
---|
[f9b0bd] | 237 | PrintS("GCD: "); p_Write(gcd, ntRing); |
---|
| 238 | PrintS("NUM: "); p_Write(num, ntRing); |
---|
| 239 | PrintS("DEN: "); p_Write(den, ntRing); |
---|
[b06842] | 240 | return FALSE; |
---|
| 241 | } |
---|
| 242 | p_Delete( &gcd, ntRing ); |
---|
[4132ee] | 243 | } |
---|
[fc2acf] | 244 | } |
---|
| 245 | return TRUE; |
---|
| 246 | |
---|
[e5c9e5] | 247 | if(p_IsConstant(den, ntRing) && (n_IsOne(pGetCoeff(den), ntCoeffs))) |
---|
[1374bc] | 248 | { |
---|
| 249 | Print("?/1 in %s:%d\n",f,l); |
---|
[dc79bd] | 250 | return FALSE; |
---|
| 251 | } |
---|
[e5c9e5] | 252 | if( !n_GreaterZero(pGetCoeff(den), ntCoeffs) ) |
---|
[dc79bd] | 253 | { |
---|
| 254 | Print("negative sign of DEN. of a fraction in %s:%d\n",f,l); |
---|
| 255 | return FALSE; |
---|
[1374bc] | 256 | } |
---|
[dc79bd] | 257 | // test that den is over integers!? |
---|
[2a4231] | 258 | } |
---|
| 259 | else |
---|
[fc2acf] | 260 | { |
---|
| 261 | return TRUE; |
---|
| 262 | |
---|
| 263 | // num != NULL // den == NULL |
---|
[dc79bd] | 264 | // if( COM(t) != 0 ) |
---|
| 265 | // { |
---|
[a355723] | 266 | // Print("?//NULL with non-zero complexity: %d in %s:%d\n", COM(t), f, l); |
---|
[dc79bd] | 267 | // return FALSE; |
---|
| 268 | // } |
---|
| 269 | // test that nume is over integers!? |
---|
[1374bc] | 270 | } |
---|
[a44bcf] | 271 | if (getCoeffType(ntCoeffs)==n_Q) |
---|
| 272 | { |
---|
| 273 | poly p=num; // !=NULL |
---|
| 274 | do |
---|
| 275 | { |
---|
| 276 | number n=pGetCoeff(p); |
---|
| 277 | n_Test(n,ntCoeffs); |
---|
[2e7132] | 278 | if ((!(SR_HDL(n) & SR_INT))&&(n->s==0)) |
---|
| 279 | /* not normalized, just do for the following test*/ |
---|
| 280 | { |
---|
| 281 | n_Normalize(pGetCoeff(p),ntCoeffs); |
---|
| 282 | n=pGetCoeff(p); |
---|
| 283 | } |
---|
[a44bcf] | 284 | if (!(SR_HDL(n) & SR_INT)) |
---|
| 285 | { |
---|
| 286 | if (n->s<2) |
---|
| 287 | Print("rational coeff in num: %s:%d\n",f,l); |
---|
| 288 | } |
---|
| 289 | pIter(p); |
---|
| 290 | } while(p!=NULL); |
---|
| 291 | p=den; |
---|
| 292 | while(p!=NULL) |
---|
| 293 | { |
---|
| 294 | number n=pGetCoeff(p); |
---|
| 295 | if (!(SR_HDL(n) & SR_INT)) |
---|
| 296 | { |
---|
| 297 | if (n->s!=3) |
---|
| 298 | Print("rational coeff in den.:%s:%d\n",f,l); |
---|
| 299 | } |
---|
| 300 | pIter(p); |
---|
| 301 | } |
---|
| 302 | } |
---|
[6ccdd3a] | 303 | return TRUE; |
---|
| 304 | } |
---|
| 305 | #endif |
---|
| 306 | |
---|
| 307 | /* returns the bottom field in this field extension tower; if the tower |
---|
| 308 | is flat, i.e., if there is no extension, then r itself is returned; |
---|
| 309 | as a side-effect, the counter 'height' is filled with the height of |
---|
| 310 | the extension tower (in case the tower is flat, 'height' is zero) */ |
---|
| 311 | static coeffs nCoeff_bottom(const coeffs r, int &height) |
---|
| 312 | { |
---|
| 313 | assume(r != NULL); |
---|
| 314 | coeffs cf = r; |
---|
| 315 | height = 0; |
---|
| 316 | while (nCoeff_is_Extension(cf)) |
---|
| 317 | { |
---|
| 318 | assume(cf->extRing != NULL); assume(cf->extRing->cf != NULL); |
---|
| 319 | cf = cf->extRing->cf; |
---|
| 320 | height++; |
---|
| 321 | } |
---|
| 322 | return cf; |
---|
| 323 | } |
---|
| 324 | |
---|
[2c7f28] | 325 | BOOLEAN ntIsZero(number a, const coeffs cf) |
---|
[6ccdd3a] | 326 | { |
---|
[fbdfd4] | 327 | //check_N(a,cf); |
---|
[dc79bd] | 328 | ntTest(a); // !!! |
---|
[e5d267] | 329 | return (IS0(a)); |
---|
[6ccdd3a] | 330 | } |
---|
| 331 | |
---|
[2c7f28] | 332 | void ntDelete(number * a, const coeffs cf) |
---|
[6ccdd3a] | 333 | { |
---|
[fbdfd4] | 334 | //check_N(*a,cf); |
---|
[dc79bd] | 335 | ntTest(*a); // !!! |
---|
[e9478b] | 336 | |
---|
[2c7f28] | 337 | fraction f = (fraction)(*a); |
---|
[e5d267] | 338 | if (IS0(f)) return; |
---|
| 339 | p_Delete(&NUM(f), ntRing); |
---|
| 340 | if (!DENIS1(f)) p_Delete(&DEN(f), ntRing); |
---|
[2c7f28] | 341 | omFreeBin((ADDRESS)f, fractionObjectBin); |
---|
[6ccdd3a] | 342 | *a = NULL; |
---|
| 343 | } |
---|
| 344 | |
---|
[2c7f28] | 345 | BOOLEAN ntEqual(number a, number b, const coeffs cf) |
---|
[6ccdd3a] | 346 | { |
---|
[fbdfd4] | 347 | //check_N(a,cf); |
---|
| 348 | //check_N(b,cf); |
---|
[dc79bd] | 349 | ntTest(a); |
---|
| 350 | ntTest(b); |
---|
[c14846c] | 351 | |
---|
[6ccdd3a] | 352 | /// simple tests |
---|
| 353 | if (a == b) return TRUE; |
---|
[e5d267] | 354 | if ((IS0(a)) && (!IS0(b))) return FALSE; |
---|
| 355 | if ((IS0(b)) && (!IS0(a))) return FALSE; |
---|
[c14846c] | 356 | |
---|
| 357 | /// cheap test if gcd's have been cancelled in both numbers |
---|
[2c7f28] | 358 | fraction fa = (fraction)a; |
---|
| 359 | fraction fb = (fraction)b; |
---|
[e5d267] | 360 | if ((COM(fa) == 1) && (COM(fb) == 1)) |
---|
[2c7f28] | 361 | { |
---|
[e5d267] | 362 | poly f = p_Add_q(p_Copy(NUM(fa), ntRing), |
---|
| 363 | p_Neg(p_Copy(NUM(fb), ntRing), ntRing), |
---|
[2c7f28] | 364 | ntRing); |
---|
| 365 | if (f != NULL) { p_Delete(&f, ntRing); return FALSE; } |
---|
[e5d267] | 366 | if (DENIS1(fa) && DENIS1(fb)) return TRUE; |
---|
| 367 | if (DENIS1(fa) && !DENIS1(fb)) return FALSE; |
---|
| 368 | if (!DENIS1(fa) && DENIS1(fb)) return FALSE; |
---|
| 369 | f = p_Add_q(p_Copy(DEN(fa), ntRing), |
---|
| 370 | p_Neg(p_Copy(DEN(fb), ntRing), ntRing), |
---|
[2c7f28] | 371 | ntRing); |
---|
| 372 | if (f != NULL) { p_Delete(&f, ntRing); return FALSE; } |
---|
| 373 | return TRUE; |
---|
| 374 | } |
---|
[c14846c] | 375 | |
---|
[2c7f28] | 376 | /* default: the more expensive multiplication test |
---|
| 377 | a/b = c/d <==> a*d = b*c */ |
---|
[e5d267] | 378 | poly f = p_Copy(NUM(fa), ntRing); |
---|
| 379 | if (!DENIS1(fb)) f = p_Mult_q(f, p_Copy(DEN(fb), ntRing), ntRing); |
---|
| 380 | poly g = p_Copy(NUM(fb), ntRing); |
---|
| 381 | if (!DENIS1(fa)) g = p_Mult_q(g, p_Copy(DEN(fa), ntRing), ntRing); |
---|
[2c7f28] | 382 | poly h = p_Add_q(f, p_Neg(g, ntRing), ntRing); |
---|
| 383 | if (h == NULL) return TRUE; |
---|
| 384 | else |
---|
| 385 | { |
---|
| 386 | p_Delete(&h, ntRing); |
---|
| 387 | return FALSE; |
---|
| 388 | } |
---|
[6ccdd3a] | 389 | } |
---|
| 390 | |
---|
[2c7f28] | 391 | number ntCopy(number a, const coeffs cf) |
---|
[6ccdd3a] | 392 | { |
---|
[fbdfd4] | 393 | //check_N(a,cf); |
---|
[dc79bd] | 394 | ntTest(a); // !!! |
---|
[e5d267] | 395 | if (IS0(a)) return NULL; |
---|
[2c7f28] | 396 | fraction f = (fraction)a; |
---|
[2a4231] | 397 | poly g = NUM(f); |
---|
| 398 | poly h = NULL; |
---|
| 399 | h =DEN(f); |
---|
| 400 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 401 | NUM(result) = p_Copy(g,cf->extRing); |
---|
| 402 | DEN(result) = p_Copy(h,cf->extRing); |
---|
[e5d267] | 403 | COM(result) = COM(f); |
---|
[dc79bd] | 404 | ntTest((number)result); |
---|
[2c7f28] | 405 | return (number)result; |
---|
[6ccdd3a] | 406 | } |
---|
| 407 | |
---|
[dc79bd] | 408 | /// TODO: normalization of a!? |
---|
[2c7f28] | 409 | number ntGetNumerator(number &a, const coeffs cf) |
---|
[6ccdd3a] | 410 | { |
---|
[fbdfd4] | 411 | //check_N(a,cf); |
---|
[2c7f28] | 412 | ntTest(a); |
---|
[e5d267] | 413 | if (IS0(a)) return NULL; |
---|
[dc79bd] | 414 | |
---|
[9d68fd] | 415 | definiteGcdCancellation(a, cf, FALSE); |
---|
| 416 | |
---|
[2c7f28] | 417 | fraction f = (fraction)a; |
---|
| 418 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[dc79bd] | 419 | |
---|
| 420 | const BOOLEAN denis1= DENIS1 (f); |
---|
| 421 | |
---|
[28216f] | 422 | if (getCoeffType (ntCoeffs) == n_Q && !denis1) |
---|
| 423 | handleNestedFractionsOverQ (f, cf); |
---|
[dc79bd] | 424 | |
---|
[28216f] | 425 | if (getCoeffType (ntCoeffs) == n_Q && denis1) |
---|
| 426 | { |
---|
[dc79bd] | 427 | assume( DEN (f) == NULL ); |
---|
[a355723] | 428 | |
---|
[dc79bd] | 429 | number g; |
---|
| 430 | // TODO/NOTE: the following should not be necessary (due to |
---|
[a355723] | 431 | // Hannes!) as NUM (f) should be over Z!!! |
---|
[dc79bd] | 432 | CPolyCoeffsEnumerator itr(NUM(f)); |
---|
| 433 | |
---|
| 434 | |
---|
[e5c9e5] | 435 | n_ClearDenominators(itr, g, ntCoeffs); |
---|
[dc79bd] | 436 | |
---|
[e5c9e5] | 437 | if( !n_GreaterZero(g, ntCoeffs) ) |
---|
[28216f] | 438 | { |
---|
[2a4231] | 439 | NUM (f) = p_Neg(NUM (f), ntRing); |
---|
[ec89bb4] | 440 | g = n_InpNeg(g, ntCoeffs); |
---|
[28216f] | 441 | } |
---|
[dc79bd] | 442 | |
---|
| 443 | // g should be a positive integer now! |
---|
[e5c9e5] | 444 | assume( n_GreaterZero(g, ntCoeffs) ); |
---|
[a355723] | 445 | |
---|
[e5c9e5] | 446 | if( !n_IsOne(g, ntCoeffs) ) |
---|
[dc79bd] | 447 | { |
---|
[2a4231] | 448 | DEN (f) = p_NSet(g, ntRing); |
---|
[dc79bd] | 449 | COM (f) ++; |
---|
[a355723] | 450 | assume( DEN (f) != NULL ); |
---|
[dc79bd] | 451 | } |
---|
| 452 | else |
---|
[e5c9e5] | 453 | n_Delete(&g, ntCoeffs); |
---|
[dc79bd] | 454 | |
---|
| 455 | ntTest(a); |
---|
[28216f] | 456 | } |
---|
[dc79bd] | 457 | |
---|
| 458 | // Call ntNormalize instead of above?!? |
---|
[a355723] | 459 | |
---|
[dc79bd] | 460 | NUM (result) = p_Copy (NUM (f), ntRing); // ??? |
---|
[061ea67] | 461 | //DEN (result) = NULL; // done by ..Alloc0.. |
---|
| 462 | //COM (result) = 0; // done by ..Alloc0.. |
---|
[a355723] | 463 | |
---|
[dc79bd] | 464 | ntTest((number)result); |
---|
[fbdfd4] | 465 | //check_N((number)result,cf); |
---|
[2c7f28] | 466 | return (number)result; |
---|
[6ccdd3a] | 467 | } |
---|
| 468 | |
---|
[dc79bd] | 469 | /// TODO: normalization of a!? |
---|
[2c7f28] | 470 | number ntGetDenom(number &a, const coeffs cf) |
---|
[6ccdd3a] | 471 | { |
---|
[fbdfd4] | 472 | //check_N(a,cf); |
---|
[2c7f28] | 473 | ntTest(a); |
---|
[dc79bd] | 474 | |
---|
[2c7f28] | 475 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[061ea67] | 476 | //DEN (result)= NULL; // done by ..Alloc0.. |
---|
| 477 | //COM (result)= 0; // done by ..Alloc0.. |
---|
[dc79bd] | 478 | |
---|
[fbdfd4] | 479 | if (IS0(a)) |
---|
[6b32a6b] | 480 | { |
---|
[9501be] | 481 | NUM (result) = p_One(ntRing); |
---|
[6b32a6b] | 482 | return (number)result; |
---|
[9501be] | 483 | } |
---|
[fbdfd4] | 484 | |
---|
[9501be] | 485 | definiteGcdCancellation(a, cf, FALSE); |
---|
[fbdfd4] | 486 | |
---|
[9501be] | 487 | fraction f = (fraction)a; |
---|
[fbdfd4] | 488 | |
---|
[9501be] | 489 | assume( !IS0(f) ); |
---|
| 490 | |
---|
[dc79bd] | 491 | const BOOLEAN denis1 = DENIS1 (f); |
---|
[fbdfd4] | 492 | |
---|
[9501be] | 493 | if( denis1 && (getCoeffType (ntCoeffs) != n_Q) ) // */1 or 0 |
---|
[28216f] | 494 | { |
---|
| 495 | NUM (result)= p_One(ntRing); |
---|
[dc79bd] | 496 | ntTest((number)result); |
---|
| 497 | return (number)result; |
---|
[28216f] | 498 | } |
---|
[dc79bd] | 499 | |
---|
| 500 | if (!denis1) // */* / Q |
---|
[28216f] | 501 | { |
---|
[dc79bd] | 502 | assume( DEN (f) != NULL ); |
---|
| 503 | |
---|
[28216f] | 504 | if (getCoeffType (ntCoeffs) == n_Q) |
---|
| 505 | handleNestedFractionsOverQ (f, cf); |
---|
[dc79bd] | 506 | |
---|
| 507 | ntTest(a); |
---|
| 508 | |
---|
| 509 | if( DEN (f) != NULL ) // is it ?? // 1 now??? |
---|
| 510 | { |
---|
| 511 | assume( !p_IsOne(DEN (f), ntRing) ); |
---|
| 512 | |
---|
| 513 | NUM (result) = p_Copy (DEN (f), ntRing); |
---|
| 514 | ntTest((number)result); |
---|
| 515 | return (number)result; |
---|
| 516 | } |
---|
| 517 | // NUM (result) = p_One(ntRing); // NOTE: just in order to be sure... |
---|
| 518 | } |
---|
[a355723] | 519 | |
---|
[dc79bd] | 520 | // */1 / Q |
---|
| 521 | assume( getCoeffType (ntCoeffs) == n_Q ); |
---|
[a355723] | 522 | assume( DEN (f) == NULL ); |
---|
| 523 | |
---|
| 524 | number g; |
---|
[dc79bd] | 525 | // poly num= p_Copy (NUM (f), ntRing); // ??? |
---|
| 526 | |
---|
| 527 | |
---|
| 528 | // TODO/NOTE: the following should not be necessary (due to |
---|
| 529 | // Hannes!) as NUM (f) should be over Z!!! |
---|
| 530 | CPolyCoeffsEnumerator itr(NUM(f)); |
---|
[a355723] | 531 | |
---|
[e5c9e5] | 532 | n_ClearDenominators(itr, g, ntCoeffs); // may return -1 :((( |
---|
[a355723] | 533 | |
---|
[e5c9e5] | 534 | if( !n_GreaterZero(g, ntCoeffs) ) |
---|
[dc79bd] | 535 | { |
---|
| 536 | // NUM (f) = p_Neg(NUM (f), ntRing); // Ugly :((( |
---|
[ec89bb4] | 537 | // g = n_InpNeg(g, ntCoeffs); |
---|
[dc79bd] | 538 | NUM (f) = p_Neg(NUM (f), ntRing); // Ugly :((( |
---|
[ec89bb4] | 539 | g = n_InpNeg(g, ntCoeffs); |
---|
[dc79bd] | 540 | } |
---|
| 541 | |
---|
| 542 | // g should be a positive integer now! |
---|
[e5c9e5] | 543 | assume( n_GreaterZero(g, ntCoeffs) ); |
---|
[dc79bd] | 544 | |
---|
[e5c9e5] | 545 | if( !n_IsOne(g, ntCoeffs) ) |
---|
[dc79bd] | 546 | { |
---|
[e5c9e5] | 547 | assume( n_GreaterZero(g, ntCoeffs) ); |
---|
| 548 | assume( !n_IsOne(g, ntCoeffs) ); |
---|
[a355723] | 549 | |
---|
[dc79bd] | 550 | DEN (f) = p_NSet(g, ntRing); // update COM(f)??? |
---|
| 551 | assume( DEN (f) != NULL ); |
---|
| 552 | COM (f) ++; |
---|
[a355723] | 553 | |
---|
[28216f] | 554 | NUM (result)= p_Copy (DEN (f), ntRing); |
---|
| 555 | } |
---|
[dc79bd] | 556 | else |
---|
| 557 | { // common denom == 1? |
---|
| 558 | NUM (result)= p_NSet(g, ntRing); // p_Copy (DEN (f), ntRing); |
---|
[e5c9e5] | 559 | // n_Delete(&g, ntCoeffs); |
---|
[a355723] | 560 | } |
---|
| 561 | |
---|
[dc79bd] | 562 | // if (!p_IsConstant (num, ntRing) && pNext(num) != NULL) |
---|
| 563 | // else |
---|
| 564 | // g= p_GetAllDenom (num, ntRing); |
---|
[e5c9e5] | 565 | // result= (fraction) ntSetMap (ntCoeffs, cf) (g, ntCoeffs, cf); |
---|
[dc79bd] | 566 | |
---|
| 567 | ntTest((number)result); |
---|
[fbdfd4] | 568 | //check_N((number)result,cf); |
---|
[2c7f28] | 569 | return (number)result; |
---|
[6ccdd3a] | 570 | } |
---|
| 571 | |
---|
[2c7f28] | 572 | BOOLEAN ntIsOne(number a, const coeffs cf) |
---|
[6ccdd3a] | 573 | { |
---|
[fbdfd4] | 574 | //check_N(a,cf); |
---|
[a355723] | 575 | ntTest(a); // !!! |
---|
[010f3b] | 576 | definiteGcdCancellation(a, cf, FALSE); |
---|
[2c7f28] | 577 | fraction f = (fraction)a; |
---|
[31c731] | 578 | return (f!=NULL) && DENIS1(f) && NUMIS1(f); |
---|
[6ccdd3a] | 579 | } |
---|
| 580 | |
---|
[2c7f28] | 581 | BOOLEAN ntIsMOne(number a, const coeffs cf) |
---|
[6ccdd3a] | 582 | { |
---|
[fbdfd4] | 583 | //check_N(a,cf); |
---|
[2c7f28] | 584 | ntTest(a); |
---|
[010f3b] | 585 | definiteGcdCancellation(a, cf, FALSE); |
---|
[2c7f28] | 586 | fraction f = (fraction)a; |
---|
[31c731] | 587 | if ((f==NULL) || (!DENIS1(f))) return FALSE; |
---|
[e5d267] | 588 | poly g = NUM(f); |
---|
[2c7f28] | 589 | if (!p_IsConstant(g, ntRing)) return FALSE; |
---|
| 590 | return n_IsMOne(p_GetCoeff(g, ntRing), ntCoeffs); |
---|
[6ccdd3a] | 591 | } |
---|
| 592 | |
---|
| 593 | /// this is in-place, modifies a |
---|
[2c7f28] | 594 | number ntNeg(number a, const coeffs cf) |
---|
[6ccdd3a] | 595 | { |
---|
[fbdfd4] | 596 | //check_N(a,cf); |
---|
[2c7f28] | 597 | ntTest(a); |
---|
[e5d267] | 598 | if (!IS0(a)) |
---|
[2c7f28] | 599 | { |
---|
| 600 | fraction f = (fraction)a; |
---|
[e5d267] | 601 | NUM(f) = p_Neg(NUM(f), ntRing); |
---|
[2c7f28] | 602 | } |
---|
[dc79bd] | 603 | ntTest(a); |
---|
[6ccdd3a] | 604 | return a; |
---|
| 605 | } |
---|
| 606 | |
---|
[2c7f28] | 607 | number ntImPart(number a, const coeffs cf) |
---|
[6ccdd3a] | 608 | { |
---|
[2c7f28] | 609 | ntTest(a); |
---|
[6ccdd3a] | 610 | return NULL; |
---|
| 611 | } |
---|
| 612 | |
---|
[2f3764] | 613 | number ntInit(long i, const coeffs cf) |
---|
[6ccdd3a] | 614 | { |
---|
[6ac003] | 615 | if (i != 0) |
---|
[2c7f28] | 616 | { |
---|
[6ac003] | 617 | poly p=p_ISet(i, ntRing); |
---|
| 618 | if (p!=NULL) |
---|
| 619 | { |
---|
| 620 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 621 | NUM(result) = p; |
---|
| 622 | //DEN(result) = NULL; // done by omAlloc0Bin |
---|
| 623 | //COM(result) = 0; // done by omAlloc0Bin |
---|
| 624 | ntTest((number)result); |
---|
[fbdfd4] | 625 | //check_N((number)result,cf); |
---|
[6ac003] | 626 | return (number)result; |
---|
| 627 | } |
---|
[2c7f28] | 628 | } |
---|
[6ac003] | 629 | return NULL; |
---|
[6ccdd3a] | 630 | } |
---|
| 631 | |
---|
[dc79bd] | 632 | |
---|
| 633 | /// takes over p! |
---|
[b38d70] | 634 | number ntInit(poly p, const coeffs cf) |
---|
| 635 | { |
---|
[6ac003] | 636 | if (p == NULL) return NULL; |
---|
[a355723] | 637 | |
---|
[fc2acf] | 638 | p_Test( p, ntRing); |
---|
[e5c9e5] | 639 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 640 | |
---|
| 641 | if (nCoeff_is_Q(ntCoeffs)) |
---|
| 642 | { |
---|
[dc79bd] | 643 | number g; |
---|
[fbdfd4] | 644 | // the following is necessary because |
---|
| 645 | // NUM (f) should be over Z, |
---|
| 646 | // while p may be over Q |
---|
[dc79bd] | 647 | CPolyCoeffsEnumerator itr(p); |
---|
| 648 | |
---|
[e5c9e5] | 649 | n_ClearDenominators(itr, g, ntCoeffs); |
---|
[dc79bd] | 650 | |
---|
[e5c9e5] | 651 | if( !n_GreaterZero(g, ntCoeffs) ) |
---|
[dc79bd] | 652 | { |
---|
[e5c9e5] | 653 | p = p_Neg(p, ntRing); |
---|
[ec89bb4] | 654 | g = n_InpNeg(g, ntCoeffs); |
---|
[dc79bd] | 655 | } |
---|
| 656 | |
---|
| 657 | // g should be a positive integer now! |
---|
[e5c9e5] | 658 | assume( n_GreaterZero(g, ntCoeffs) ); |
---|
[a355723] | 659 | |
---|
[e5c9e5] | 660 | if( !n_IsOne(g, ntCoeffs) ) |
---|
[dc79bd] | 661 | { |
---|
[a355723] | 662 | DEN (f) = p_NSet(g, ntRing); |
---|
[fbdfd4] | 663 | p_Normalize(DEN(f), ntRing); |
---|
[a355723] | 664 | assume( DEN (f) != NULL ); |
---|
[dc79bd] | 665 | } |
---|
| 666 | else |
---|
| 667 | { |
---|
[e5c9e5] | 668 | //DEN(f) = NULL; // done by omAlloc0 |
---|
| 669 | n_Delete(&g, ntCoeffs); |
---|
[a355723] | 670 | } |
---|
[e5c9e5] | 671 | } |
---|
[a355723] | 672 | |
---|
[fbdfd4] | 673 | p_Normalize(p, ntRing); |
---|
[e5c9e5] | 674 | NUM(f) = p; |
---|
[061ea67] | 675 | //COM(f) = 0; // done by omAlloc0 |
---|
[dc79bd] | 676 | |
---|
[fbdfd4] | 677 | //check_N((number)f,cf); |
---|
[e5c9e5] | 678 | ntTest((number)f); |
---|
| 679 | return (number)f; |
---|
[b38d70] | 680 | } |
---|
| 681 | |
---|
[777f8b] | 682 | long ntInt(number &a, const coeffs cf) |
---|
[6ccdd3a] | 683 | { |
---|
[fbdfd4] | 684 | //check_N(a,cf); |
---|
[2c7f28] | 685 | ntTest(a); |
---|
[e5d267] | 686 | if (IS0(a)) return 0; |
---|
[010f3b] | 687 | definiteGcdCancellation(a, cf, FALSE); |
---|
[2c7f28] | 688 | fraction f = (fraction)a; |
---|
[e5d267] | 689 | if (!DENIS1(f)) return 0; |
---|
[a355723] | 690 | |
---|
[1090a98] | 691 | const poly aAsPoly = NUM(f); |
---|
| 692 | |
---|
| 693 | if(aAsPoly == NULL) |
---|
| 694 | return 0; |
---|
[a355723] | 695 | |
---|
[1090a98] | 696 | if (!p_IsConstant(aAsPoly, ntRing)) |
---|
| 697 | return 0; |
---|
| 698 | |
---|
| 699 | assume( aAsPoly != NULL ); |
---|
[a355723] | 700 | |
---|
[1090a98] | 701 | return n_Int(p_GetCoeff(aAsPoly, ntRing), ntCoeffs); |
---|
[2c7f28] | 702 | } |
---|
| 703 | |
---|
| 704 | /* This method will only consider the numerators of a and b, without |
---|
| 705 | cancelling gcd's before. |
---|
| 706 | Moreover it may return TRUE only if one or both numerators |
---|
| 707 | are zero or if their degrees are equal. Then TRUE is returned iff |
---|
| 708 | coeff(numerator(a)) > coeff(numerator(b)); |
---|
| 709 | In all other cases, FALSE will be returned. */ |
---|
| 710 | BOOLEAN ntGreater(number a, number b, const coeffs cf) |
---|
| 711 | { |
---|
[fbdfd4] | 712 | //check_N(a,cf); |
---|
| 713 | //check_N(b,cf); |
---|
[dc79bd] | 714 | ntTest(a); |
---|
| 715 | ntTest(b); |
---|
[c5fbc4] | 716 | number aNumCoeff = NULL; int aNumDeg = 0; |
---|
| 717 | number aDenCoeff = NULL; int aDenDeg = 0; |
---|
| 718 | number bNumCoeff = NULL; int bNumDeg = 0; |
---|
| 719 | number bDenCoeff = NULL; int bDenDeg = 0; |
---|
[e5d267] | 720 | if (!IS0(a)) |
---|
[2c7f28] | 721 | { |
---|
| 722 | fraction fa = (fraction)a; |
---|
[e5d267] | 723 | aNumDeg = p_Totaldegree(NUM(fa), ntRing); |
---|
| 724 | aNumCoeff = p_GetCoeff(NUM(fa), ntRing); |
---|
[fd60be] | 725 | if (DEN(fa)!=NULL) |
---|
| 726 | { |
---|
| 727 | aDenDeg = p_Totaldegree(DEN(fa), ntRing); |
---|
| 728 | aDenCoeff=p_GetCoeff(DEN(fa),ntRing); |
---|
| 729 | } |
---|
[2c7f28] | 730 | } |
---|
[95c826] | 731 | else return !(ntGreaterZero (b,cf)); |
---|
[e5d267] | 732 | if (!IS0(b)) |
---|
[2c7f28] | 733 | { |
---|
| 734 | fraction fb = (fraction)b; |
---|
[e5d267] | 735 | bNumDeg = p_Totaldegree(NUM(fb), ntRing); |
---|
| 736 | bNumCoeff = p_GetCoeff(NUM(fb), ntRing); |
---|
[fd60be] | 737 | if (DEN(fb)!=NULL) |
---|
| 738 | { |
---|
| 739 | bDenDeg = p_Totaldegree(DEN(fb), ntRing); |
---|
| 740 | bDenCoeff=p_GetCoeff(DEN(fb),ntRing); |
---|
| 741 | } |
---|
[2c7f28] | 742 | } |
---|
[95c826] | 743 | else return ntGreaterZero(a,cf); |
---|
[fd60be] | 744 | if (aNumDeg-aDenDeg > bNumDeg-bDenDeg) return TRUE; |
---|
| 745 | if (aNumDeg-aDenDeg < bNumDeg-bDenDeg) return FALSE; |
---|
| 746 | number aa; |
---|
| 747 | number bb; |
---|
[e5c9e5] | 748 | if (bDenCoeff==NULL) aa=n_Copy(aNumCoeff,ntCoeffs); |
---|
| 749 | else aa=n_Mult(aNumCoeff,bDenCoeff,ntCoeffs); |
---|
| 750 | if (aDenCoeff==NULL) bb=n_Copy(bNumCoeff,ntCoeffs); |
---|
| 751 | else bb=n_Mult(bNumCoeff,aDenCoeff,ntCoeffs); |
---|
[fd60be] | 752 | BOOLEAN rr= n_Greater(aa, bb, ntCoeffs); |
---|
[e5c9e5] | 753 | n_Delete(&aa,ntCoeffs); |
---|
| 754 | n_Delete(&bb,ntCoeffs); |
---|
[fd60be] | 755 | return rr; |
---|
[6ccdd3a] | 756 | } |
---|
| 757 | |
---|
[2c7f28] | 758 | /* this method will only consider the numerator of a, without cancelling |
---|
| 759 | the gcd before; |
---|
| 760 | returns TRUE iff the leading coefficient of the numerator of a is > 0 |
---|
| 761 | or the leading term of the numerator of a is not a |
---|
| 762 | constant */ |
---|
| 763 | BOOLEAN ntGreaterZero(number a, const coeffs cf) |
---|
[6ccdd3a] | 764 | { |
---|
[fbdfd4] | 765 | //check_N(a,cf); |
---|
[2c7f28] | 766 | ntTest(a); |
---|
[e5d267] | 767 | if (IS0(a)) return FALSE; |
---|
[2c7f28] | 768 | fraction f = (fraction)a; |
---|
[e5d267] | 769 | poly g = NUM(f); |
---|
[7964f9c] | 770 | return (!p_LmIsConstant(g,ntRing)|| n_GreaterZero(pGetCoeff(g), ntCoeffs)); |
---|
[6ccdd3a] | 771 | } |
---|
| 772 | |
---|
[03f7b5] | 773 | void ntCoeffWrite(const coeffs cf, BOOLEAN details) |
---|
[6ccdd3a] | 774 | { |
---|
[a55ef0] | 775 | assume( cf != NULL ); |
---|
| 776 | |
---|
| 777 | const ring A = cf->extRing; |
---|
| 778 | |
---|
| 779 | assume( A != NULL ); |
---|
| 780 | assume( A->cf != NULL ); |
---|
| 781 | |
---|
[03f7b5] | 782 | n_CoeffWrite(A->cf, details); |
---|
[a55ef0] | 783 | |
---|
| 784 | // rWrite(A); |
---|
| 785 | |
---|
| 786 | const int P = rVar(A); |
---|
| 787 | assume( P > 0 ); |
---|
[c14846c] | 788 | |
---|
[8d1432e] | 789 | PrintS("("); |
---|
[c14846c] | 790 | |
---|
[a55ef0] | 791 | for (int nop=0; nop < P; nop ++) |
---|
[8d1432e] | 792 | { |
---|
| 793 | Print("%s", rRingVar(nop, A)); |
---|
| 794 | if (nop!=P-1) PrintS(", "); |
---|
| 795 | } |
---|
[a55ef0] | 796 | |
---|
[8d1432e] | 797 | PrintS(")"); |
---|
[c14846c] | 798 | |
---|
[8d1432e] | 799 | assume( A->qideal == NULL ); |
---|
[a55ef0] | 800 | |
---|
| 801 | /* |
---|
[2c7f28] | 802 | PrintS("// Coefficients live in the rational function field\n"); |
---|
| 803 | Print("// K("); |
---|
| 804 | for (int i = 0; i < rVar(ntRing); i++) |
---|
| 805 | { |
---|
[a55ef0] | 806 | if (i > 0) PrintS(" "); |
---|
[2c7f28] | 807 | Print("%s", rRingVar(i, ntRing)); |
---|
| 808 | } |
---|
| 809 | PrintS(") with\n"); |
---|
| 810 | PrintS("// K: "); n_CoeffWrite(cf->extRing->cf); |
---|
[a55ef0] | 811 | */ |
---|
[6ccdd3a] | 812 | } |
---|
| 813 | |
---|
[12f6ce0] | 814 | number ntDiff(number a, number d, const coeffs cf) |
---|
| 815 | { |
---|
[fbdfd4] | 816 | //check_N(a,cf); |
---|
| 817 | //check_N(d,cf); |
---|
[12f6ce0] | 818 | ntTest(a); |
---|
| 819 | ntTest(d); |
---|
| 820 | |
---|
[061ea67] | 821 | if (IS0(d)) |
---|
[0b8402] | 822 | { |
---|
| 823 | WerrorS("ringvar expected"); |
---|
| 824 | return NULL; |
---|
| 825 | } |
---|
[96847c] | 826 | fraction t = (fraction) d; |
---|
[12f6ce0] | 827 | if (!DENIS1(t)) |
---|
| 828 | { |
---|
| 829 | WerrorS("expected differentiation by a variable"); |
---|
[96847c] | 830 | return NULL; |
---|
[12f6ce0] | 831 | } |
---|
| 832 | int k=p_Var(NUM(t),ntRing); |
---|
| 833 | if (k==0) |
---|
| 834 | { |
---|
| 835 | WerrorS("expected differentiation by a variable"); |
---|
[96847c] | 836 | return NULL; |
---|
[12f6ce0] | 837 | } |
---|
| 838 | |
---|
| 839 | if (IS0(a)) return ntCopy(a, cf); |
---|
| 840 | |
---|
| 841 | fraction fa = (fraction)a; |
---|
[061ea67] | 842 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[fbdfd4] | 843 | if (DENIS1(fa)) |
---|
| 844 | { |
---|
[1e8006] | 845 | NUM(result) = p_Diff(NUM(fa),k,ntRing); |
---|
[061ea67] | 846 | //DEN(result) = NULL; // done by ..Alloc0.. |
---|
[14086c2] | 847 | if (NUM(result)==NULL) |
---|
[7a09e1d] | 848 | { |
---|
[404949] | 849 | omFreeBin((ADDRESS)result, fractionObjectBin); |
---|
[7a09e1d] | 850 | return(NULL); |
---|
| 851 | } |
---|
[12f6ce0] | 852 | COM(result) = COM(fa); |
---|
[fbdfd4] | 853 | //check_N((number)result,cf); |
---|
[fc2acf] | 854 | ntTest((number)result); |
---|
[12f6ce0] | 855 | return (number)result; |
---|
| 856 | } |
---|
| 857 | |
---|
[1e8006] | 858 | poly fg = p_Mult_q(p_Copy(DEN(fa),ntRing),p_Diff(NUM(fa),k,ntRing),ntRing); |
---|
| 859 | poly gf = p_Mult_q(p_Copy(NUM(fa),ntRing),p_Diff(DEN(fa),k,ntRing),ntRing); |
---|
| 860 | NUM(result) = p_Sub(fg,gf,ntRing); |
---|
[5d258e9] | 861 | if (NUM(result)==NULL) return(NULL); |
---|
[1e8006] | 862 | DEN(result) = pp_Mult_qq(DEN(fa), DEN(fa), ntRing); |
---|
| 863 | COM(result) = COM(fa) + COM(fa) + DIFF_COMPLEXITY; |
---|
[12f6ce0] | 864 | heuristicGcdCancellation((number)result, cf); |
---|
| 865 | |
---|
[fbdfd4] | 866 | //check_N((number)result,cf); |
---|
[fc2acf] | 867 | ntTest((number)result); |
---|
[12f6ce0] | 868 | return (number)result; |
---|
| 869 | } |
---|
| 870 | |
---|
| 871 | |
---|
[2c7f28] | 872 | number ntAdd(number a, number b, const coeffs cf) |
---|
[6ccdd3a] | 873 | { |
---|
[fbdfd4] | 874 | //check_N(a,cf); |
---|
| 875 | //check_N(b,cf); |
---|
[dc79bd] | 876 | ntTest(a); |
---|
| 877 | ntTest(b); |
---|
[e5d267] | 878 | if (IS0(a)) return ntCopy(b, cf); |
---|
| 879 | if (IS0(b)) return ntCopy(a, cf); |
---|
[c14846c] | 880 | |
---|
[2c7f28] | 881 | fraction fa = (fraction)a; |
---|
| 882 | fraction fb = (fraction)b; |
---|
[c14846c] | 883 | |
---|
[e5d267] | 884 | poly g = p_Copy(NUM(fa), ntRing); |
---|
| 885 | if (!DENIS1(fb)) g = p_Mult_q(g, p_Copy(DEN(fb), ntRing), ntRing); |
---|
| 886 | poly h = p_Copy(NUM(fb), ntRing); |
---|
| 887 | if (!DENIS1(fa)) h = p_Mult_q(h, p_Copy(DEN(fa), ntRing), ntRing); |
---|
[de90c01] | 888 | g = p_Add_q(g, h, ntRing); |
---|
[c14846c] | 889 | |
---|
[de90c01] | 890 | if (g == NULL) return NULL; |
---|
[c14846c] | 891 | |
---|
[2c7f28] | 892 | poly f; |
---|
[e5d267] | 893 | if (DENIS1(fa) && DENIS1(fb)) f = NULL; |
---|
| 894 | else if (!DENIS1(fa) && DENIS1(fb)) f = p_Copy(DEN(fa), ntRing); |
---|
| 895 | else if (DENIS1(fa) && !DENIS1(fb)) f = p_Copy(DEN(fb), ntRing); |
---|
| 896 | else /* both denom's are != 1 */ f = p_Mult_q(p_Copy(DEN(fa), ntRing), |
---|
| 897 | p_Copy(DEN(fb), ntRing), |
---|
[2c7f28] | 898 | ntRing); |
---|
[c14846c] | 899 | |
---|
[061ea67] | 900 | fraction result = (fraction)omAllocBin(fractionObjectBin); |
---|
[e5d267] | 901 | NUM(result) = g; |
---|
| 902 | DEN(result) = f; |
---|
| 903 | COM(result) = COM(fa) + COM(fb) + ADD_COMPLEXITY; |
---|
[2c7f28] | 904 | heuristicGcdCancellation((number)result, cf); |
---|
[dc79bd] | 905 | |
---|
| 906 | // ntTest((number)result); |
---|
[a355723] | 907 | |
---|
[fbdfd4] | 908 | //check_N((number)result,cf); |
---|
[fc2acf] | 909 | ntTest((number)result); |
---|
[2c7f28] | 910 | return (number)result; |
---|
[6ccdd3a] | 911 | } |
---|
| 912 | |
---|
[2c7f28] | 913 | number ntSub(number a, number b, const coeffs cf) |
---|
[6ccdd3a] | 914 | { |
---|
[fbdfd4] | 915 | //check_N(a,cf); |
---|
| 916 | //check_N(b,cf); |
---|
[dc79bd] | 917 | ntTest(a); |
---|
| 918 | ntTest(b); |
---|
[e5d267] | 919 | if (IS0(a)) return ntNeg(ntCopy(b, cf), cf); |
---|
| 920 | if (IS0(b)) return ntCopy(a, cf); |
---|
[c14846c] | 921 | |
---|
[2c7f28] | 922 | fraction fa = (fraction)a; |
---|
| 923 | fraction fb = (fraction)b; |
---|
[c14846c] | 924 | |
---|
[e5d267] | 925 | poly g = p_Copy(NUM(fa), ntRing); |
---|
| 926 | if (!DENIS1(fb)) g = p_Mult_q(g, p_Copy(DEN(fb), ntRing), ntRing); |
---|
| 927 | poly h = p_Copy(NUM(fb), ntRing); |
---|
| 928 | if (!DENIS1(fa)) h = p_Mult_q(h, p_Copy(DEN(fa), ntRing), ntRing); |
---|
[de90c01] | 929 | g = p_Add_q(g, p_Neg(h, ntRing), ntRing); |
---|
[c14846c] | 930 | |
---|
[de90c01] | 931 | if (g == NULL) return NULL; |
---|
[c14846c] | 932 | |
---|
[2c7f28] | 933 | poly f; |
---|
[e5d267] | 934 | if (DENIS1(fa) && DENIS1(fb)) f = NULL; |
---|
| 935 | else if (!DENIS1(fa) && DENIS1(fb)) f = p_Copy(DEN(fa), ntRing); |
---|
| 936 | else if (DENIS1(fa) && !DENIS1(fb)) f = p_Copy(DEN(fb), ntRing); |
---|
| 937 | else /* both den's are != 1 */ f = p_Mult_q(p_Copy(DEN(fa), ntRing), |
---|
| 938 | p_Copy(DEN(fb), ntRing), |
---|
[2c7f28] | 939 | ntRing); |
---|
[c14846c] | 940 | |
---|
[061ea67] | 941 | fraction result = (fraction)omAllocBin(fractionObjectBin); |
---|
[e5d267] | 942 | NUM(result) = g; |
---|
| 943 | DEN(result) = f; |
---|
| 944 | COM(result) = COM(fa) + COM(fb) + ADD_COMPLEXITY; |
---|
[2c7f28] | 945 | heuristicGcdCancellation((number)result, cf); |
---|
[dc79bd] | 946 | // ntTest((number)result); |
---|
[fbdfd4] | 947 | //check_N((number)result,cf); |
---|
[fc2acf] | 948 | ntTest((number)result); |
---|
[2c7f28] | 949 | return (number)result; |
---|
[6ccdd3a] | 950 | } |
---|
| 951 | |
---|
[2c7f28] | 952 | number ntMult(number a, number b, const coeffs cf) |
---|
[6ccdd3a] | 953 | { |
---|
[fbdfd4] | 954 | //check_N(a,cf); |
---|
| 955 | //check_N(b,cf); |
---|
[dc79bd] | 956 | ntTest(a); // !!!? |
---|
| 957 | ntTest(b); // !!!? |
---|
[a355723] | 958 | |
---|
[e5d267] | 959 | if (IS0(a) || IS0(b)) return NULL; |
---|
[c14846c] | 960 | |
---|
[2c7f28] | 961 | fraction fa = (fraction)a; |
---|
| 962 | fraction fb = (fraction)b; |
---|
[c14846c] | 963 | |
---|
[dc79bd] | 964 | const poly g = pp_Mult_qq(NUM(fa), NUM(fb), ntRing); |
---|
[c14846c] | 965 | |
---|
[dc79bd] | 966 | if (g == NULL) return NULL; // may happen due to zero divisors??? |
---|
[c14846c] | 967 | |
---|
[061ea67] | 968 | fraction result = (fraction)omAllocBin(fractionObjectBin); |
---|
[dc79bd] | 969 | |
---|
[e5d267] | 970 | NUM(result) = g; |
---|
[dc79bd] | 971 | |
---|
| 972 | const poly da = DEN(fa); |
---|
| 973 | const poly db = DEN(fb); |
---|
| 974 | |
---|
[a355723] | 975 | |
---|
[fbdfd4] | 976 | //check_N((number)result,cf); |
---|
[dc79bd] | 977 | if (db == NULL) |
---|
| 978 | { |
---|
| 979 | // b = ? // NULL |
---|
[a355723] | 980 | |
---|
[dc79bd] | 981 | if(da == NULL) |
---|
| 982 | { // both fa && fb are ?? // NULL! |
---|
| 983 | assume (da == NULL && db == NULL); |
---|
| 984 | DEN(result) = NULL; |
---|
| 985 | COM(result) = 0; |
---|
| 986 | } |
---|
[a355723] | 987 | else |
---|
[dc79bd] | 988 | { |
---|
| 989 | assume (da != NULL && db == NULL); |
---|
| 990 | DEN(result) = p_Copy(da, ntRing); |
---|
| 991 | COM(result) = COM(fa) + MULT_COMPLEXITY; |
---|
| 992 | heuristicGcdCancellation((number)result, cf); |
---|
[fbdfd4] | 993 | //check_N((number)result,cf); |
---|
[dc79bd] | 994 | } |
---|
[fbdfd4] | 995 | } |
---|
| 996 | else |
---|
[dc79bd] | 997 | { // b = ?? / ?? |
---|
| 998 | if (da == NULL) |
---|
| 999 | { // a == ? // NULL |
---|
| 1000 | assume( db != NULL && da == NULL); |
---|
| 1001 | DEN(result) = p_Copy(db, ntRing); |
---|
| 1002 | COM(result) = COM(fb) + MULT_COMPLEXITY; |
---|
| 1003 | heuristicGcdCancellation((number)result, cf); |
---|
[fbdfd4] | 1004 | //check_N((number)result,cf); |
---|
[dc79bd] | 1005 | } |
---|
| 1006 | else /* both den's are != 1 */ |
---|
| 1007 | { |
---|
| 1008 | assume (da != NULL && db != NULL); |
---|
| 1009 | DEN(result) = pp_Mult_qq(da, db, ntRing); |
---|
| 1010 | COM(result) = COM(fa) + COM(fb) + MULT_COMPLEXITY; |
---|
| 1011 | heuristicGcdCancellation((number)result, cf); |
---|
[fbdfd4] | 1012 | //check_N((number)result,cf); |
---|
[dc79bd] | 1013 | } |
---|
| 1014 | } |
---|
| 1015 | |
---|
| 1016 | // ntTest((number)result); |
---|
[a355723] | 1017 | |
---|
[fbdfd4] | 1018 | //check_N((number)result,cf); |
---|
[fc2acf] | 1019 | ntTest((number)result); |
---|
[2c7f28] | 1020 | return (number)result; |
---|
[6ccdd3a] | 1021 | } |
---|
| 1022 | |
---|
[f3b4359] | 1023 | static void ntNormalizeDen(fraction result, const ring R) |
---|
| 1024 | { |
---|
| 1025 | if ((nCoeff_has_simple_inverse(R->cf)) |
---|
| 1026 | && (result!=NULL) |
---|
| 1027 | && (DEN(result)!=NULL)) |
---|
| 1028 | { |
---|
| 1029 | poly n=DEN(result); |
---|
| 1030 | if (!n_IsOne(pGetCoeff(n),R->cf)) |
---|
| 1031 | { |
---|
| 1032 | number inv=n_Invers(pGetCoeff(n),R->cf); |
---|
| 1033 | DEN(result)=p_Mult_nn(n,inv,R); |
---|
| 1034 | NUM(result)=p_Mult_nn(NUM(result),inv,R); |
---|
| 1035 | n_Delete(&inv,R->cf); |
---|
| 1036 | if (p_IsOne(DEN(result), R)) |
---|
| 1037 | { |
---|
| 1038 | n=DEN(result); |
---|
| 1039 | DEN(result)=NULL; |
---|
| 1040 | COM(result) = 0; |
---|
| 1041 | p_Delete(&n,R); |
---|
| 1042 | } |
---|
| 1043 | } |
---|
| 1044 | } |
---|
| 1045 | } |
---|
| 1046 | |
---|
[2c7f28] | 1047 | number ntDiv(number a, number b, const coeffs cf) |
---|
[6ccdd3a] | 1048 | { |
---|
[fbdfd4] | 1049 | //check_N(a,cf); |
---|
| 1050 | //check_N(b,cf); |
---|
[dc79bd] | 1051 | ntTest(a); |
---|
| 1052 | ntTest(b); |
---|
[e5d267] | 1053 | if (IS0(a)) return NULL; |
---|
| 1054 | if (IS0(b)) WerrorS(nDivBy0); |
---|
[c14846c] | 1055 | |
---|
[2c7f28] | 1056 | fraction fa = (fraction)a; |
---|
| 1057 | fraction fb = (fraction)b; |
---|
[c14846c] | 1058 | |
---|
[e5d267] | 1059 | poly g = p_Copy(NUM(fa), ntRing); |
---|
| 1060 | if (!DENIS1(fb)) g = p_Mult_q(g, p_Copy(DEN(fb), ntRing), ntRing); |
---|
[c14846c] | 1061 | |
---|
[de90c01] | 1062 | if (g == NULL) return NULL; /* may happen due to zero divisors */ |
---|
[c14846c] | 1063 | |
---|
[e5d267] | 1064 | poly f = p_Copy(NUM(fb), ntRing); |
---|
| 1065 | if (!DENIS1(fa)) f = p_Mult_q(f, p_Copy(DEN(fa), ntRing), ntRing); |
---|
[c14846c] | 1066 | |
---|
[2c7f28] | 1067 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[e5d267] | 1068 | NUM(result) = g; |
---|
[e5c9e5] | 1069 | if (!n_GreaterZero(pGetCoeff(f),ntCoeffs)) |
---|
[644b31] | 1070 | { |
---|
| 1071 | g=p_Neg(g,ntRing); |
---|
| 1072 | f=p_Neg(f,ntRing); |
---|
| 1073 | NUM(result) = g; |
---|
| 1074 | } |
---|
[e5c9e5] | 1075 | if (!p_IsConstant(f,ntRing) || !n_IsOne(pGetCoeff(f),ntCoeffs)) |
---|
[644b31] | 1076 | { |
---|
[1374bc] | 1077 | DEN(result) = f; |
---|
[644b31] | 1078 | } |
---|
[e5d267] | 1079 | COM(result) = COM(fa) + COM(fb) + MULT_COMPLEXITY; |
---|
[423198] | 1080 | // definiteGcdCancellation((number)result, cf,FALSE); |
---|
[2c7f28] | 1081 | heuristicGcdCancellation((number)result, cf); |
---|
[dc79bd] | 1082 | // ntTest((number)result); |
---|
[fbdfd4] | 1083 | //check_N((number)result,cf); |
---|
[f3b4359] | 1084 | ntNormalizeDen(result,ntRing); |
---|
[fc2acf] | 1085 | ntTest((number)result); |
---|
[2c7f28] | 1086 | return (number)result; |
---|
[6ccdd3a] | 1087 | } |
---|
| 1088 | |
---|
| 1089 | /* 0^0 = 0; |
---|
| 1090 | for |exp| <= 7 compute power by a simple multiplication loop; |
---|
| 1091 | for |exp| >= 8 compute power along binary presentation of |exp|, e.g. |
---|
| 1092 | p^13 = p^1 * p^4 * p^8, where we utilise that |
---|
[2c7f28] | 1093 | p^(2^(k+1)) = p^(2^k) * p^(2^k); |
---|
| 1094 | intermediate cancellation is controlled by the in-place method |
---|
| 1095 | heuristicGcdCancellation; see there. |
---|
[6ccdd3a] | 1096 | */ |
---|
[2c7f28] | 1097 | void ntPower(number a, int exp, number *b, const coeffs cf) |
---|
[6ccdd3a] | 1098 | { |
---|
[2c7f28] | 1099 | ntTest(a); |
---|
[c14846c] | 1100 | |
---|
[6ccdd3a] | 1101 | /* special cases first */ |
---|
[e5d267] | 1102 | if (IS0(a)) |
---|
[6ccdd3a] | 1103 | { |
---|
| 1104 | if (exp >= 0) *b = NULL; |
---|
| 1105 | else WerrorS(nDivBy0); |
---|
| 1106 | } |
---|
[35e86e] | 1107 | else if (exp == 0) { *b = ntInit(1, cf); return;} |
---|
| 1108 | else if (exp == 1) { *b = ntCopy(a, cf); return;} |
---|
| 1109 | else if (exp == -1) { *b = ntInvers(a, cf); return;} |
---|
[c14846c] | 1110 | |
---|
[6ccdd3a] | 1111 | int expAbs = exp; if (expAbs < 0) expAbs = -expAbs; |
---|
[c14846c] | 1112 | |
---|
[2c7f28] | 1113 | /* now compute a^expAbs */ |
---|
| 1114 | number pow; number t; |
---|
[6ccdd3a] | 1115 | if (expAbs <= 7) |
---|
| 1116 | { |
---|
[2c7f28] | 1117 | pow = ntCopy(a, cf); |
---|
[6ccdd3a] | 1118 | for (int i = 2; i <= expAbs; i++) |
---|
| 1119 | { |
---|
[2c7f28] | 1120 | t = ntMult(pow, a, cf); |
---|
| 1121 | ntDelete(&pow, cf); |
---|
| 1122 | pow = t; |
---|
| 1123 | heuristicGcdCancellation(pow, cf); |
---|
[6ccdd3a] | 1124 | } |
---|
| 1125 | } |
---|
| 1126 | else |
---|
| 1127 | { |
---|
[2c7f28] | 1128 | pow = ntInit(1, cf); |
---|
| 1129 | number factor = ntCopy(a, cf); |
---|
[6ccdd3a] | 1130 | while (expAbs != 0) |
---|
| 1131 | { |
---|
| 1132 | if (expAbs & 1) |
---|
| 1133 | { |
---|
[2c7f28] | 1134 | t = ntMult(pow, factor, cf); |
---|
| 1135 | ntDelete(&pow, cf); |
---|
| 1136 | pow = t; |
---|
| 1137 | heuristicGcdCancellation(pow, cf); |
---|
[6ccdd3a] | 1138 | } |
---|
| 1139 | expAbs = expAbs / 2; |
---|
| 1140 | if (expAbs != 0) |
---|
| 1141 | { |
---|
[2c7f28] | 1142 | t = ntMult(factor, factor, cf); |
---|
| 1143 | ntDelete(&factor, cf); |
---|
| 1144 | factor = t; |
---|
| 1145 | heuristicGcdCancellation(factor, cf); |
---|
[6ccdd3a] | 1146 | } |
---|
| 1147 | } |
---|
[2c7f28] | 1148 | ntDelete(&factor, cf); |
---|
[6ccdd3a] | 1149 | } |
---|
[c14846c] | 1150 | |
---|
[6ccdd3a] | 1151 | /* invert if original exponent was negative */ |
---|
| 1152 | if (exp < 0) |
---|
| 1153 | { |
---|
[2c7f28] | 1154 | t = ntInvers(pow, cf); |
---|
| 1155 | ntDelete(&pow, cf); |
---|
| 1156 | pow = t; |
---|
[6ccdd3a] | 1157 | } |
---|
[2c7f28] | 1158 | *b = pow; |
---|
[dc79bd] | 1159 | ntTest(*b); |
---|
[fbdfd4] | 1160 | //check_N(*b,cf); |
---|
[6ccdd3a] | 1161 | } |
---|
| 1162 | |
---|
[06df101] | 1163 | /* assumes that cf represents the rationals, i.e. Q, and will only |
---|
| 1164 | be called in that case; |
---|
| 1165 | assumes furthermore that f != NULL and that the denominator of f != 1; |
---|
| 1166 | generally speaking, this method removes denominators in the rational |
---|
| 1167 | coefficients of the numerator and denominator of 'a'; |
---|
| 1168 | more concretely, the following normalizations will be performed, |
---|
| 1169 | where t^alpha denotes a monomial in the transcendental variables t_k |
---|
| 1170 | (1) if 'a' is of the form |
---|
| 1171 | (sum_alpha a_alpha/b_alpha * t^alpha) |
---|
| 1172 | ------------------------------------- |
---|
| 1173 | (sum_beta c_beta/d_beta * t^beta) |
---|
| 1174 | with integers a_alpha, b_alpha, c_beta, d_beta, then both the |
---|
| 1175 | numerator and the denominator will be multiplied by the LCM of |
---|
| 1176 | the b_alpha's and the d_beta's (if this LCM is != 1), |
---|
| 1177 | (2) if 'a' is - e.g. after having performed step (1) - of the form |
---|
| 1178 | (sum_alpha a_alpha * t^alpha) |
---|
| 1179 | ----------------------------- |
---|
| 1180 | (sum_beta c_beta * t^beta) |
---|
| 1181 | with integers a_alpha, c_beta, and with a non-constant denominator, |
---|
| 1182 | then both the numerator and the denominator will be divided by the |
---|
| 1183 | GCD of the a_alpha's and the c_beta's (if this GCD is != 1), |
---|
| 1184 | this procedure does not alter COM(f) (this has to be done by the |
---|
| 1185 | calling procedure); |
---|
| 1186 | modifies f */ |
---|
| 1187 | void handleNestedFractionsOverQ(fraction f, const coeffs cf) |
---|
| 1188 | { |
---|
| 1189 | assume(nCoeff_is_Q(ntCoeffs)); |
---|
| 1190 | assume(!IS0(f)); |
---|
| 1191 | assume(!DENIS1(f)); |
---|
[c14846c] | 1192 | |
---|
[06df101] | 1193 | { /* step (1); see documentation of this procedure above */ |
---|
| 1194 | number lcmOfDenominators = n_Init(1, ntCoeffs); |
---|
| 1195 | number c; number tmp; |
---|
| 1196 | poly p = NUM(f); |
---|
[88cceb] | 1197 | /* careful when using n_NormalizeHelper!!! It computes the lcm of the numerator |
---|
[06df101] | 1198 | of the 1st argument and the denominator of the 2nd!!! */ |
---|
| 1199 | while (p != NULL) |
---|
| 1200 | { |
---|
| 1201 | c = p_GetCoeff(p, ntRing); |
---|
[88cceb] | 1202 | tmp = n_NormalizeHelper(lcmOfDenominators, c, ntCoeffs); |
---|
[06df101] | 1203 | n_Delete(&lcmOfDenominators, ntCoeffs); |
---|
| 1204 | lcmOfDenominators = tmp; |
---|
| 1205 | pIter(p); |
---|
| 1206 | } |
---|
| 1207 | p = DEN(f); |
---|
| 1208 | while (p != NULL) |
---|
| 1209 | { |
---|
| 1210 | c = p_GetCoeff(p, ntRing); |
---|
[88cceb] | 1211 | tmp = n_NormalizeHelper(lcmOfDenominators, c, ntCoeffs); |
---|
[06df101] | 1212 | n_Delete(&lcmOfDenominators, ntCoeffs); |
---|
| 1213 | lcmOfDenominators = tmp; |
---|
| 1214 | pIter(p); |
---|
| 1215 | } |
---|
| 1216 | if (!n_IsOne(lcmOfDenominators, ntCoeffs)) |
---|
| 1217 | { /* multiply NUM(f) and DEN(f) with lcmOfDenominators */ |
---|
| 1218 | NUM(f) = p_Mult_nn(NUM(f), lcmOfDenominators, ntRing); |
---|
[d12f186] | 1219 | p_Normalize(NUM(f), ntRing); |
---|
[06df101] | 1220 | DEN(f) = p_Mult_nn(DEN(f), lcmOfDenominators, ntRing); |
---|
[d12f186] | 1221 | p_Normalize(DEN(f), ntRing); |
---|
[06df101] | 1222 | } |
---|
| 1223 | n_Delete(&lcmOfDenominators, ntCoeffs); |
---|
[2e7132] | 1224 | if (DEN(f)!=NULL) |
---|
[06df101] | 1225 | { /* step (2); see documentation of this procedure above */ |
---|
| 1226 | p = NUM(f); |
---|
| 1227 | number gcdOfCoefficients = n_Copy(p_GetCoeff(p, ntRing), ntCoeffs); |
---|
| 1228 | pIter(p); |
---|
| 1229 | while ((p != NULL) && (!n_IsOne(gcdOfCoefficients, ntCoeffs))) |
---|
| 1230 | { |
---|
| 1231 | c = p_GetCoeff(p, ntRing); |
---|
[2206753] | 1232 | tmp = n_Gcd(c, gcdOfCoefficients, ntCoeffs); |
---|
[06df101] | 1233 | n_Delete(&gcdOfCoefficients, ntCoeffs); |
---|
| 1234 | gcdOfCoefficients = tmp; |
---|
| 1235 | pIter(p); |
---|
| 1236 | } |
---|
| 1237 | p = DEN(f); |
---|
| 1238 | while ((p != NULL) && (!n_IsOne(gcdOfCoefficients, ntCoeffs))) |
---|
| 1239 | { |
---|
| 1240 | c = p_GetCoeff(p, ntRing); |
---|
[2206753] | 1241 | tmp = n_Gcd(c, gcdOfCoefficients, ntCoeffs); |
---|
[06df101] | 1242 | n_Delete(&gcdOfCoefficients, ntCoeffs); |
---|
| 1243 | gcdOfCoefficients = tmp; |
---|
| 1244 | pIter(p); |
---|
| 1245 | } |
---|
| 1246 | if (!n_IsOne(gcdOfCoefficients, ntCoeffs)) |
---|
| 1247 | { /* divide NUM(f) and DEN(f) by gcdOfCoefficients */ |
---|
| 1248 | number inverseOfGcdOfCoefficients = n_Invers(gcdOfCoefficients, |
---|
| 1249 | ntCoeffs); |
---|
| 1250 | NUM(f) = p_Mult_nn(NUM(f), inverseOfGcdOfCoefficients, ntRing); |
---|
[d12f186] | 1251 | p_Normalize(NUM(f), ntRing); |
---|
[06df101] | 1252 | DEN(f) = p_Mult_nn(DEN(f), inverseOfGcdOfCoefficients, ntRing); |
---|
[d12f186] | 1253 | p_Normalize(DEN(f), ntRing); |
---|
[06df101] | 1254 | n_Delete(&inverseOfGcdOfCoefficients, ntCoeffs); |
---|
| 1255 | } |
---|
| 1256 | n_Delete(&gcdOfCoefficients, ntCoeffs); |
---|
| 1257 | } |
---|
| 1258 | } |
---|
[c14846c] | 1259 | |
---|
[06df101] | 1260 | /* Now, due to the above computations, DEN(f) may have become the |
---|
| 1261 | 1-polynomial which needs to be represented by NULL: */ |
---|
| 1262 | if ((DEN(f) != NULL) && |
---|
| 1263 | p_IsConstant(DEN(f), ntRing) && |
---|
| 1264 | n_IsOne(p_GetCoeff(DEN(f), ntRing), ntCoeffs)) |
---|
| 1265 | { |
---|
| 1266 | p_Delete(&DEN(f), ntRing); DEN(f) = NULL; |
---|
| 1267 | } |
---|
[a355723] | 1268 | |
---|
[dc79bd] | 1269 | if( DEN(f) != NULL ) |
---|
| 1270 | if( !n_GreaterZero(pGetCoeff(DEN(f)), ntCoeffs) ) |
---|
| 1271 | { |
---|
| 1272 | NUM(f) = p_Neg(NUM(f), ntRing); |
---|
| 1273 | DEN(f) = p_Neg(DEN(f), ntRing); |
---|
[a355723] | 1274 | } |
---|
| 1275 | |
---|
[dc79bd] | 1276 | ntTest((number)f); // TODO! |
---|
[06df101] | 1277 | } |
---|
| 1278 | |
---|
[2c7f28] | 1279 | /* modifies a */ |
---|
[423198] | 1280 | /* this is an intermediate simplification routine - not a comple "normalize" */ |
---|
[2c7f28] | 1281 | void heuristicGcdCancellation(number a, const coeffs cf) |
---|
[6ccdd3a] | 1282 | { |
---|
[e5d267] | 1283 | if (IS0(a)) return; |
---|
[c14846c] | 1284 | |
---|
[2c7f28] | 1285 | fraction f = (fraction)a; |
---|
[fbdfd4] | 1286 | p_Normalize(NUM(f),ntRing); |
---|
[5a4e17] | 1287 | if (DENIS1(f) || NUMIS1(f)) { COM(f) = 0; return; } |
---|
| 1288 | |
---|
[dc79bd] | 1289 | assume( DEN(f) != NULL ); |
---|
[fbdfd4] | 1290 | p_Normalize(DEN(f),ntRing); |
---|
[c14846c] | 1291 | |
---|
[e5d267] | 1292 | /* check whether NUM(f) = DEN(f), and - if so - replace 'a' by 1 */ |
---|
| 1293 | if (p_EqualPolys(NUM(f), DEN(f), ntRing)) |
---|
| 1294 | { /* numerator and denominator are both != 1 */ |
---|
| 1295 | p_Delete(&NUM(f), ntRing); NUM(f) = p_ISet(1, ntRing); |
---|
| 1296 | p_Delete(&DEN(f), ntRing); DEN(f) = NULL; |
---|
| 1297 | COM(f) = 0; |
---|
[603ac7a] | 1298 | } |
---|
| 1299 | else |
---|
[dc79bd] | 1300 | { |
---|
[a355723] | 1301 | if (COM(f) > BOUND_COMPLEXITY) |
---|
[dc79bd] | 1302 | definiteGcdCancellation(a, cf, TRUE); |
---|
[c14846c] | 1303 | |
---|
[7964f9c] | 1304 | // TODO: check if it is enough to put the following into definiteGcdCancellation?! |
---|
| 1305 | if( DEN(f) != NULL ) |
---|
[dc79bd] | 1306 | { |
---|
[7964f9c] | 1307 | if( !n_GreaterZero(pGetCoeff(DEN(f)), ntCoeffs) ) |
---|
| 1308 | { |
---|
| 1309 | NUM(f) = p_Neg(NUM(f), ntRing); |
---|
| 1310 | DEN(f) = p_Neg(DEN(f), ntRing); |
---|
| 1311 | } |
---|
| 1312 | if (ntCoeffs->has_simple_Inverse) |
---|
| 1313 | { |
---|
| 1314 | if (!n_IsOne(pGetCoeff(DEN(f)),ntCoeffs)) |
---|
| 1315 | { |
---|
| 1316 | number inv=n_Invers(pGetCoeff(DEN(f)),ntCoeffs); |
---|
| 1317 | DEN(f)=p_Mult_nn(DEN(f),inv,ntRing); |
---|
| 1318 | NUM(f)=p_Mult_nn(NUM(f),inv,ntRing); |
---|
| 1319 | } |
---|
| 1320 | if(p_LmIsConstant(DEN(f),ntRing)) |
---|
| 1321 | { |
---|
| 1322 | p_Delete(&DEN(f),ntRing); |
---|
| 1323 | COM(f)=0; |
---|
| 1324 | } |
---|
| 1325 | } |
---|
[423198] | 1326 | if ((DEN(f)!=NULL) |
---|
| 1327 | && (pNext(DEN(f))==NULL)) |
---|
| 1328 | { |
---|
| 1329 | poly den_f=DEN(f); |
---|
| 1330 | poly h=NUM(f); |
---|
| 1331 | loop |
---|
| 1332 | { |
---|
| 1333 | if (h==NULL) |
---|
| 1334 | { |
---|
| 1335 | h=NUM(f); |
---|
| 1336 | do |
---|
| 1337 | { |
---|
| 1338 | p_ExpVectorDiff(h,h,den_f,ntRing); |
---|
| 1339 | pIter(h); |
---|
| 1340 | } while(h!=NULL); |
---|
[4132ee] | 1341 | p_ExpVectorDiff(den_f,den_f,den_f,ntRing); |
---|
[423198] | 1342 | break; |
---|
| 1343 | } |
---|
| 1344 | int i=0; |
---|
| 1345 | do |
---|
| 1346 | { |
---|
| 1347 | i++; |
---|
| 1348 | if (p_GetExp(den_f,i,ntRing) > p_GetExp(h,i,ntRing)) return; |
---|
| 1349 | } while(i<ntRing->N); |
---|
| 1350 | pIter(h); |
---|
| 1351 | } |
---|
| 1352 | } |
---|
[a355723] | 1353 | } |
---|
[dc79bd] | 1354 | } |
---|
[992bde] | 1355 | if ((DEN(f)!=NULL) |
---|
| 1356 | && (pNext(DEN(f))==NULL) |
---|
| 1357 | && (p_LmIsConstantComp(DEN(f),ntRing)) |
---|
| 1358 | && (n_IsOne(pGetCoeff(DEN(f)),ntCoeffs))) |
---|
| 1359 | { |
---|
| 1360 | p_Delete(&DEN(f),ntRing); |
---|
| 1361 | COM(f)=0; |
---|
| 1362 | } |
---|
[6ccdd3a] | 1363 | } |
---|
| 1364 | |
---|
[dc79bd] | 1365 | /// modifies a |
---|
[010f3b] | 1366 | void definiteGcdCancellation(number a, const coeffs cf, |
---|
[06df101] | 1367 | BOOLEAN simpleTestsHaveAlreadyBeenPerformed) |
---|
[6ccdd3a] | 1368 | { |
---|
[fc2acf] | 1369 | // ntTest(a); // !!!! |
---|
[c14846c] | 1370 | |
---|
[2c7f28] | 1371 | fraction f = (fraction)a; |
---|
[c14846c] | 1372 | |
---|
[603ac7a] | 1373 | if (IS0(a)) return; |
---|
[fc2acf] | 1374 | if (DENIS1(f) || NUMIS1(f)) { COM(f) = 0; ntTest(a); return; } |
---|
[06df101] | 1375 | if (!simpleTestsHaveAlreadyBeenPerformed) |
---|
[2c7f28] | 1376 | { |
---|
[c14846c] | 1377 | |
---|
[e5d267] | 1378 | /* check whether NUM(f) = DEN(f), and - if so - replace 'a' by 1 */ |
---|
| 1379 | if (p_EqualPolys(NUM(f), DEN(f), ntRing)) |
---|
| 1380 | { /* numerator and denominator are both != 1 */ |
---|
| 1381 | p_Delete(&NUM(f), ntRing); NUM(f) = p_ISet(1, ntRing); |
---|
| 1382 | p_Delete(&DEN(f), ntRing); DEN(f) = NULL; |
---|
| 1383 | COM(f) = 0; |
---|
[2a4231] | 1384 | ntTest(a); |
---|
[010f3b] | 1385 | return; |
---|
| 1386 | } |
---|
[2c7f28] | 1387 | } |
---|
[14a0ca] | 1388 | /*if (rField_is_Q(ntRing)) |
---|
[5a4e17] | 1389 | { |
---|
| 1390 | number c=n_Copy(pGetCoeff(NUM(f)),ntCoeffs); |
---|
| 1391 | poly p=pNext(NUM(f)); |
---|
| 1392 | while((p!=NULL)&&(!n_IsOne(c,ntCoeffs))) |
---|
| 1393 | { |
---|
| 1394 | number cc=n_Gcd(c,pGetCoeff(p),ntCoeffs); |
---|
| 1395 | n_Delete(&c,ntCoeffs); |
---|
| 1396 | c=cc; |
---|
| 1397 | pIter(p); |
---|
| 1398 | }; |
---|
| 1399 | p=DEN(f); |
---|
| 1400 | while((p!=NULL)&&(!n_IsOne(c,ntCoeffs))) |
---|
| 1401 | { |
---|
| 1402 | number cc=n_Gcd(c,pGetCoeff(p),ntCoeffs); |
---|
| 1403 | n_Delete(&c,ntCoeffs); |
---|
| 1404 | c=cc; |
---|
| 1405 | pIter(p); |
---|
| 1406 | }; |
---|
| 1407 | if(!n_IsOne(c,ntCoeffs)) |
---|
| 1408 | { |
---|
| 1409 | p=NUM(f); |
---|
| 1410 | do |
---|
| 1411 | { |
---|
| 1412 | number cc=n_Div(pGetCoeff(p),c,ntCoeffs); |
---|
| 1413 | n_Normalize(cc,ntCoeffs); |
---|
| 1414 | p_SetCoeff(p,cc,ntRing); |
---|
| 1415 | pIter(p); |
---|
| 1416 | } while(p!=NULL); |
---|
| 1417 | p=DEN(f); |
---|
| 1418 | do |
---|
| 1419 | { |
---|
| 1420 | number cc=n_Div(pGetCoeff(p),c,ntCoeffs); |
---|
| 1421 | n_Normalize(cc,ntCoeffs); |
---|
| 1422 | p_SetCoeff(p,cc,ntRing); |
---|
| 1423 | pIter(p); |
---|
| 1424 | } while(p!=NULL); |
---|
| 1425 | n_Delete(&c,ntCoeffs); |
---|
| 1426 | if(pNext(DEN(f))==NULL) |
---|
| 1427 | { |
---|
| 1428 | if (p_IsOne(DEN(f),ntRing)) |
---|
| 1429 | { |
---|
| 1430 | p_LmDelete(&DEN(f),ntRing); |
---|
| 1431 | COM(f)=0; |
---|
| 1432 | return; |
---|
| 1433 | } |
---|
| 1434 | else |
---|
| 1435 | { |
---|
| 1436 | return; |
---|
| 1437 | } |
---|
| 1438 | } |
---|
| 1439 | } |
---|
[14a0ca] | 1440 | }*/ |
---|
[c14846c] | 1441 | |
---|
[5a4e17] | 1442 | /* here we assume: NUM(f), DEN(f) !=NULL, in Z_a reqp. Z/p_a */ |
---|
[2de7b0] | 1443 | poly pGcd = singclap_gcd_and_divide(NUM(f), DEN(f), ntRing); |
---|
[f3f0ad] | 1444 | //PrintS("gcd= ");p_wrp(pGcd,ntRing);PrintLn(); |
---|
[5a4e17] | 1445 | if (p_IsConstant(pGcd, ntRing) |
---|
[14a0ca] | 1446 | && n_IsOne(p_GetCoeff(pGcd, ntRing), ntCoeffs) |
---|
[5a4e17] | 1447 | ) |
---|
[06df101] | 1448 | { /* gcd = 1; nothing to cancel; |
---|
| 1449 | Suppose the given rational function field is over Q. Although the |
---|
| 1450 | gcd is 1, we may have produced fractional coefficients in NUM(f), |
---|
| 1451 | DEN(f), or both, due to previous arithmetics. The next call will |
---|
| 1452 | remove those nested fractions, in case there are any. */ |
---|
[4254a7f] | 1453 | if (nCoeff_is_Zp(ntCoeffs)) |
---|
[9053f02] | 1454 | { |
---|
| 1455 | NUM (f) = p_Div_nn (NUM (f), p_GetCoeff (DEN(f),ntRing), ntRing); |
---|
[4254a7f] | 1456 | if (p_IsConstant (DEN (f), ntRing)) |
---|
| 1457 | { |
---|
| 1458 | p_Delete(&DEN (f), ntRing); |
---|
| 1459 | DEN (f) = NULL; |
---|
| 1460 | } |
---|
| 1461 | else |
---|
| 1462 | { |
---|
| 1463 | p_Norm (DEN (f),ntRing); |
---|
| 1464 | } |
---|
[9053f02] | 1465 | } else if (nCoeff_is_Q(ntCoeffs)) handleNestedFractionsOverQ(f, cf); |
---|
[e5d267] | 1466 | } |
---|
| 1467 | else |
---|
[06df101] | 1468 | { /* We divide both NUM(f) and DEN(f) by the gcd which is known |
---|
| 1469 | to be != 1. */ |
---|
| 1470 | if (p_IsConstant(DEN(f), ntRing) && |
---|
[4132ee] | 1471 | n_IsOne(p_GetCoeff(DEN(f), ntRing), ntCoeffs)) |
---|
[06df101] | 1472 | { |
---|
| 1473 | /* DEN(f) = 1 needs to be represented by NULL! */ |
---|
[e5d267] | 1474 | p_Delete(&DEN(f), ntRing); |
---|
[2de7b0] | 1475 | DEN(f) = NULL; |
---|
[06df101] | 1476 | } |
---|
| 1477 | else |
---|
[57f6787] | 1478 | { |
---|
[4254a7f] | 1479 | if (nCoeff_is_Zp(ntCoeffs)) |
---|
[2de7b0] | 1480 | { |
---|
| 1481 | NUM (f) = p_Div_nn (NUM (f), p_GetCoeff (DEN(f),ntRing), ntRing); |
---|
[4254a7f] | 1482 | if (p_IsConstant (DEN (f), ntRing)) |
---|
| 1483 | { |
---|
| 1484 | p_Delete(&DEN (f), ntRing); |
---|
| 1485 | DEN (f) = NULL; |
---|
| 1486 | } |
---|
| 1487 | else |
---|
| 1488 | { |
---|
| 1489 | p_Norm (DEN (f),ntRing); |
---|
| 1490 | } |
---|
[57f6787] | 1491 | } |
---|
[06df101] | 1492 | } |
---|
[e5d267] | 1493 | } |
---|
| 1494 | p_Delete(&pGcd, ntRing); |
---|
[4132ee] | 1495 | COM(f) = 0; |
---|
[dc79bd] | 1496 | |
---|
| 1497 | if( DEN(f) != NULL ) |
---|
[4132ee] | 1498 | { |
---|
[dc79bd] | 1499 | if( !n_GreaterZero(pGetCoeff(DEN(f)), ntCoeffs) ) |
---|
| 1500 | { |
---|
| 1501 | NUM(f) = p_Neg(NUM(f), ntRing); |
---|
| 1502 | DEN(f) = p_Neg(DEN(f), ntRing); |
---|
[f3a5b8] | 1503 | if (p_IsConstant(DEN(f), ntRing) && |
---|
| 1504 | n_IsOne(p_GetCoeff(DEN(f), ntRing), ntCoeffs)) |
---|
| 1505 | { |
---|
| 1506 | /* DEN(f) = 1 needs to be represented by NULL! */ |
---|
| 1507 | p_Delete(&DEN(f), ntRing); |
---|
| 1508 | DEN (f) = NULL; |
---|
| 1509 | } |
---|
[a355723] | 1510 | } |
---|
[4132ee] | 1511 | } |
---|
[dc79bd] | 1512 | ntTest(a); // !!!! |
---|
[2c7f28] | 1513 | } |
---|
| 1514 | |
---|
[6d92ca] | 1515 | void ntWriteLong(number a, const coeffs cf) |
---|
[ce1f78] | 1516 | { |
---|
| 1517 | ntTest(a); |
---|
| 1518 | if (IS0(a)) |
---|
| 1519 | StringAppendS("0"); |
---|
| 1520 | else |
---|
| 1521 | { |
---|
| 1522 | fraction f = (fraction)a; |
---|
| 1523 | // stole logic from napWrite from kernel/longtrans.cc of legacy singular |
---|
| 1524 | BOOLEAN omitBrackets = p_IsConstant(NUM(f), ntRing); |
---|
| 1525 | if (!omitBrackets) StringAppendS("("); |
---|
| 1526 | p_String0Long(NUM(f), ntRing, ntRing); |
---|
| 1527 | if (!omitBrackets) StringAppendS(")"); |
---|
| 1528 | if (!DENIS1(f)) |
---|
| 1529 | { |
---|
| 1530 | StringAppendS("/"); |
---|
| 1531 | omitBrackets = p_IsConstant(DEN(f), ntRing); |
---|
| 1532 | if (!omitBrackets) StringAppendS("("); |
---|
| 1533 | p_String0Long(DEN(f), ntRing, ntRing); |
---|
| 1534 | if (!omitBrackets) StringAppendS(")"); |
---|
[a355723] | 1535 | } |
---|
[ce1f78] | 1536 | } |
---|
[dc79bd] | 1537 | ntTest(a); // !!!! |
---|
[ce1f78] | 1538 | } |
---|
| 1539 | |
---|
[6d92ca] | 1540 | void ntWriteShort(number a, const coeffs cf) |
---|
[2c7f28] | 1541 | { |
---|
| 1542 | ntTest(a); |
---|
[e5d267] | 1543 | if (IS0(a)) |
---|
[6ccdd3a] | 1544 | StringAppendS("0"); |
---|
| 1545 | else |
---|
| 1546 | { |
---|
[2c7f28] | 1547 | fraction f = (fraction)a; |
---|
[a5071b9] | 1548 | // stole logic from napWrite from kernel/longtrans.cc of legacy singular |
---|
[a0a9f0] | 1549 | BOOLEAN omitBrackets = p_IsConstant(NUM(f), ntRing); |
---|
[a5071b9] | 1550 | if (!omitBrackets) StringAppendS("("); |
---|
[ce1f78] | 1551 | p_String0Short(NUM(f), ntRing, ntRing); |
---|
[a5071b9] | 1552 | if (!omitBrackets) StringAppendS(")"); |
---|
[e5d267] | 1553 | if (!DENIS1(f)) |
---|
[2c7f28] | 1554 | { |
---|
| 1555 | StringAppendS("/"); |
---|
[a0a9f0] | 1556 | omitBrackets = p_IsConstant(DEN(f), ntRing); |
---|
| 1557 | if (!omitBrackets) StringAppendS("("); |
---|
[ce1f78] | 1558 | p_String0Short(DEN(f), ntRing, ntRing); |
---|
[a0a9f0] | 1559 | if (!omitBrackets) StringAppendS(")"); |
---|
[2c7f28] | 1560 | } |
---|
[6ccdd3a] | 1561 | } |
---|
[dc79bd] | 1562 | ntTest(a); |
---|
[6ccdd3a] | 1563 | } |
---|
| 1564 | |
---|
[2c7f28] | 1565 | const char * ntRead(const char *s, number *a, const coeffs cf) |
---|
[6ccdd3a] | 1566 | { |
---|
[2c7f28] | 1567 | poly p; |
---|
| 1568 | const char * result = p_Read(s, p, ntRing); |
---|
[dc79bd] | 1569 | if (p == NULL) *a = NULL; |
---|
| 1570 | else *a = ntInit(p, cf); |
---|
[fc2acf] | 1571 | ntTest(*a); |
---|
[dc79bd] | 1572 | return result; |
---|
[6ccdd3a] | 1573 | } |
---|
| 1574 | |
---|
[237b4dd] | 1575 | void ntNormalize (number &a, const coeffs cf) |
---|
| 1576 | { |
---|
[805db88] | 1577 | if ( /*(*/ a!=NULL /*)*/ ) |
---|
[6ad7e6] | 1578 | { |
---|
[f3f0ad] | 1579 | //PrintS("num=");p_wrp(NUM(a),ntRing); |
---|
| 1580 | //PrintS(" den=");p_wrp(DEN(a),ntRing);PrintLn(); |
---|
[6ad7e6] | 1581 | definiteGcdCancellation(a, cf, FALSE); |
---|
[2a4231] | 1582 | if ((DEN((fraction)a)!=NULL) |
---|
| 1583 | &&(!n_GreaterZero(pGetCoeff(DEN((fraction)a)),ntCoeffs))) |
---|
[6ad7e6] | 1584 | { |
---|
[2a4231] | 1585 | NUM((fraction)a)=p_Neg(NUM((fraction)a),ntRing); |
---|
| 1586 | DEN((fraction)a)=p_Neg(DEN((fraction)a),ntRing); |
---|
[6ad7e6] | 1587 | } |
---|
| 1588 | } |
---|
[f3b4359] | 1589 | ntNormalizeDen((fraction)a,ntRing); |
---|
[dc79bd] | 1590 | ntTest(a); // !!!! |
---|
[237b4dd] | 1591 | } |
---|
| 1592 | |
---|
[2c7f28] | 1593 | /* expects *param to be castable to TransExtInfo */ |
---|
| 1594 | static BOOLEAN ntCoeffIsEqual(const coeffs cf, n_coeffType n, void * param) |
---|
[6ccdd3a] | 1595 | { |
---|
[6d0515d] | 1596 | if (n_transExt != n) return FALSE; |
---|
[2c7f28] | 1597 | TransExtInfo *e = (TransExtInfo *)param; |
---|
| 1598 | /* for rational function fields we expect the underlying |
---|
| 1599 | polynomial rings to be IDENTICAL, i.e. the SAME OBJECT; |
---|
[6ccdd3a] | 1600 | this expectation is based on the assumption that we have properly |
---|
| 1601 | registered cf and perform reference counting rather than creating |
---|
| 1602 | multiple copies of the same coefficient field/domain/ring */ |
---|
[4d94c97] | 1603 | if (ntRing == e->r) |
---|
| 1604 | return TRUE; |
---|
| 1605 | |
---|
| 1606 | // NOTE: Q(a)[x] && Q(a)[y] should better share the _same_ Q(a)... |
---|
| 1607 | if( rEqual(ntRing, e->r, TRUE) ) |
---|
| 1608 | { |
---|
| 1609 | rDelete(e->r); |
---|
| 1610 | return TRUE; |
---|
| 1611 | } |
---|
| 1612 | |
---|
[a355723] | 1613 | return FALSE; |
---|
[6ccdd3a] | 1614 | } |
---|
| 1615 | |
---|
[88cceb] | 1616 | number ntNormalizeHelper(number a, number b, const coeffs cf) |
---|
[6ccdd3a] | 1617 | { |
---|
[dc79bd] | 1618 | ntTest(a); |
---|
| 1619 | ntTest(b); |
---|
[1577ebd] | 1620 | fraction fb = (fraction)b; |
---|
| 1621 | if ((b==NULL)||(DEN(fb)==NULL)) return ntCopy(a,cf); |
---|
| 1622 | fraction fa = (fraction)a; |
---|
| 1623 | /* singclap_gcd destroys its arguments; we hence need copies: */ |
---|
| 1624 | poly pa = p_Copy(NUM(fa), ntRing); |
---|
| 1625 | poly pb = p_Copy(DEN(fb), ntRing); |
---|
[c14846c] | 1626 | |
---|
[0635d51] | 1627 | poly pGcd; |
---|
| 1628 | if (nCoeff_is_Q(ntCoeffs)) |
---|
| 1629 | { |
---|
| 1630 | if (p_IsConstant(pa,ntRing) && p_IsConstant(pb,ntRing)) |
---|
| 1631 | { |
---|
| 1632 | pGcd = pa; |
---|
[2206753] | 1633 | p_SetCoeff (pGcd, n_Gcd (pGetCoeff(pGcd), pGetCoeff(pb), ntCoeffs), ntRing); |
---|
[0635d51] | 1634 | } |
---|
| 1635 | else |
---|
| 1636 | { |
---|
| 1637 | number contentpa, contentpb, tmp; |
---|
| 1638 | |
---|
| 1639 | contentpb= p_GetCoeff(pb, ntRing); |
---|
| 1640 | pIter(pb); |
---|
| 1641 | while (pb != NULL) |
---|
| 1642 | { |
---|
[1a3437] | 1643 | tmp = n_SubringGcd(contentpb, p_GetCoeff(pb, ntRing) , ntCoeffs); |
---|
[0635d51] | 1644 | n_Delete(&contentpb, ntCoeffs); |
---|
| 1645 | contentpb = tmp; |
---|
| 1646 | pIter(pb); |
---|
| 1647 | } |
---|
| 1648 | |
---|
| 1649 | contentpa= p_GetCoeff(pa, ntRing); |
---|
| 1650 | pIter(pa); |
---|
| 1651 | while (pa != NULL) |
---|
| 1652 | { |
---|
[1a3437] | 1653 | tmp = n_SubringGcd(contentpa, p_GetCoeff(pa, ntRing), ntCoeffs); |
---|
[0635d51] | 1654 | n_Delete(&contentpa, ntCoeffs); |
---|
| 1655 | contentpa = tmp; |
---|
| 1656 | pIter(pa); |
---|
| 1657 | } |
---|
| 1658 | |
---|
[1a3437] | 1659 | tmp= n_SubringGcd (contentpb, contentpa, ntCoeffs); |
---|
[0635d51] | 1660 | n_Delete(&contentpa, ntCoeffs); |
---|
| 1661 | n_Delete(&contentpb, ntCoeffs); |
---|
| 1662 | contentpa= tmp; |
---|
| 1663 | p_Delete(&pb, ntRing); |
---|
| 1664 | p_Delete(&pa, ntRing); |
---|
| 1665 | |
---|
| 1666 | /* singclap_gcd destroys its arguments; we hence need copies: */ |
---|
| 1667 | pGcd = singclap_gcd(p_Copy(NUM(fa),ntRing), p_Copy(DEN(fb),ntRing), ntRing); |
---|
| 1668 | pGcd= p_Mult_nn (pGcd, contentpa, ntRing); |
---|
| 1669 | n_Delete(&contentpa, ntCoeffs); |
---|
| 1670 | } |
---|
| 1671 | } |
---|
| 1672 | else |
---|
| 1673 | pGcd = singclap_gcd(pa, pb, cf->extRing); |
---|
| 1674 | |
---|
[1577ebd] | 1675 | /* Note that, over Q, singclap_gcd will remove the denominators in all |
---|
| 1676 | rational coefficients of pa and pb, before starting to compute |
---|
| 1677 | the gcd. Thus, we do not need to ensure that the coefficients of |
---|
| 1678 | pa and pb live in Z; they may well be elements of Q\Z. */ |
---|
[0635d51] | 1679 | |
---|
[1577ebd] | 1680 | if (p_IsConstant(pGcd, ntRing) && |
---|
| 1681 | n_IsOne(p_GetCoeff(pGcd, ntRing), ntCoeffs)) |
---|
| 1682 | { /* gcd = 1; return pa*pb*/ |
---|
| 1683 | p_Delete(&pGcd,ntRing); |
---|
| 1684 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 1685 | NUM(result) = pp_Mult_qq(NUM(fa),DEN(fb),ntRing); |
---|
[dc79bd] | 1686 | |
---|
| 1687 | ntTest((number)result); // !!!! |
---|
[a355723] | 1688 | |
---|
[1577ebd] | 1689 | return (number)result; |
---|
| 1690 | } |
---|
[a355723] | 1691 | |
---|
[dc79bd] | 1692 | |
---|
| 1693 | /* return pa*pb/gcd */ |
---|
[1577ebd] | 1694 | poly newNum = singclap_pdivide(NUM(fa), pGcd, ntRing); |
---|
[331fd0] | 1695 | p_Delete(&pGcd,ntRing); |
---|
[1577ebd] | 1696 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 1697 | NUM(result) = p_Mult_q(p_Copy(DEN(fb),ntRing),newNum,ntRing); |
---|
[dc79bd] | 1698 | ntTest((number)result); // !!!! |
---|
[1577ebd] | 1699 | return (number)result; |
---|
[a355723] | 1700 | |
---|
[fc2acf] | 1701 | return NULL; |
---|
[6ccdd3a] | 1702 | } |
---|
| 1703 | |
---|
[2c7f28] | 1704 | number ntGcd(number a, number b, const coeffs cf) |
---|
[6ccdd3a] | 1705 | { |
---|
[dc79bd] | 1706 | ntTest(a); |
---|
| 1707 | ntTest(b); |
---|
[1577ebd] | 1708 | if (a==NULL) return ntCopy(b,cf); |
---|
| 1709 | if (b==NULL) return ntCopy(a,cf); |
---|
| 1710 | fraction fa = (fraction)a; |
---|
| 1711 | fraction fb = (fraction)b; |
---|
[0635d51] | 1712 | |
---|
[1577ebd] | 1713 | poly pa = p_Copy(NUM(fa), ntRing); |
---|
| 1714 | poly pb = p_Copy(NUM(fb), ntRing); |
---|
[c14846c] | 1715 | |
---|
[0635d51] | 1716 | poly pGcd; |
---|
| 1717 | if (nCoeff_is_Q(ntCoeffs)) |
---|
| 1718 | { |
---|
| 1719 | if (p_IsConstant(pa,ntRing) && p_IsConstant(pb,ntRing)) |
---|
| 1720 | { |
---|
| 1721 | pGcd = pa; |
---|
[1a3437] | 1722 | p_SetCoeff (pGcd, n_SubringGcd (pGetCoeff(pGcd), pGetCoeff(pb), ntCoeffs), ntRing); |
---|
[0635d51] | 1723 | } |
---|
| 1724 | else |
---|
| 1725 | { |
---|
| 1726 | number contentpa, contentpb, tmp; |
---|
| 1727 | |
---|
| 1728 | contentpb= p_GetCoeff(pb, ntRing); |
---|
| 1729 | pIter(pb); |
---|
| 1730 | while (pb != NULL) |
---|
| 1731 | { |
---|
[1a3437] | 1732 | tmp = n_SubringGcd(contentpb, p_GetCoeff(pb, ntRing) , ntCoeffs); |
---|
[0635d51] | 1733 | n_Delete(&contentpb, ntCoeffs); |
---|
| 1734 | contentpb = tmp; |
---|
| 1735 | pIter(pb); |
---|
| 1736 | } |
---|
| 1737 | |
---|
| 1738 | contentpa= p_GetCoeff(pa, ntRing); |
---|
| 1739 | pIter(pa); |
---|
| 1740 | while (pa != NULL) |
---|
| 1741 | { |
---|
[1a3437] | 1742 | tmp = n_SubringGcd(contentpa, p_GetCoeff(pa, ntRing), ntCoeffs); |
---|
[0635d51] | 1743 | n_Delete(&contentpa, ntCoeffs); |
---|
| 1744 | contentpa = tmp; |
---|
| 1745 | pIter(pa); |
---|
| 1746 | } |
---|
| 1747 | |
---|
[1a3437] | 1748 | tmp= n_SubringGcd (contentpb, contentpa, ntCoeffs); |
---|
[0635d51] | 1749 | n_Delete(&contentpa, ntCoeffs); |
---|
| 1750 | n_Delete(&contentpb, ntCoeffs); |
---|
| 1751 | contentpa= tmp; |
---|
| 1752 | p_Delete(&pb, ntRing); |
---|
| 1753 | p_Delete(&pa, ntRing); |
---|
| 1754 | |
---|
| 1755 | /* singclap_gcd destroys its arguments; we hence need copies: */ |
---|
| 1756 | pGcd = singclap_gcd(p_Copy(NUM(fa),ntRing), p_Copy(NUM(fb),ntRing), ntRing); |
---|
| 1757 | pGcd= p_Mult_nn (pGcd, contentpa, ntRing); |
---|
| 1758 | n_Delete(&contentpa, ntCoeffs); |
---|
| 1759 | } |
---|
| 1760 | } |
---|
| 1761 | else |
---|
| 1762 | pGcd = singclap_gcd(pa, pb, cf->extRing); |
---|
[1577ebd] | 1763 | /* Note that, over Q, singclap_gcd will remove the denominators in all |
---|
| 1764 | rational coefficients of pa and pb, before starting to compute |
---|
| 1765 | the gcd. Thus, we do not need to ensure that the coefficients of |
---|
| 1766 | pa and pb live in Z; they may well be elements of Q\Z. */ |
---|
[0635d51] | 1767 | |
---|
[1577ebd] | 1768 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 1769 | NUM(result) = pGcd; |
---|
[dc79bd] | 1770 | ntTest((number)result); // !!!! |
---|
[1577ebd] | 1771 | return (number)result; |
---|
[6ccdd3a] | 1772 | } |
---|
[1a3437] | 1773 | //number ntGcd_dummy(number a, number b, const coeffs cf) |
---|
| 1774 | //{ |
---|
| 1775 | // extern char my_yylinebuf[80]; |
---|
| 1776 | // Print("ntGcd in >>%s<<\n",my_yylinebuf); |
---|
| 1777 | // return ntGcd(a,b,cf); |
---|
| 1778 | //} |
---|
[6ccdd3a] | 1779 | |
---|
[2c7f28] | 1780 | int ntSize(number a, const coeffs cf) |
---|
[6ccdd3a] | 1781 | { |
---|
[2c7f28] | 1782 | ntTest(a); |
---|
[e5d267] | 1783 | if (IS0(a)) return -1; |
---|
[2c7f28] | 1784 | /* this has been taken from the old implementation of field extensions, |
---|
| 1785 | where we computed the sum of the degrees and the numbers of terms in |
---|
| 1786 | the numerator and denominator of a; so we leave it at that, for the |
---|
| 1787 | time being */ |
---|
| 1788 | fraction f = (fraction)a; |
---|
[e5d267] | 1789 | poly p = NUM(f); |
---|
[2c7f28] | 1790 | int noOfTerms = 0; |
---|
| 1791 | int numDegree = 0; |
---|
[0e32c7] | 1792 | if (p!=NULL) |
---|
[2c7f28] | 1793 | { |
---|
[0e32c7] | 1794 | numDegree = p_Totaldegree(p,ntRing); |
---|
| 1795 | noOfTerms = pLength(p); |
---|
[2c7f28] | 1796 | } |
---|
| 1797 | int denDegree = 0; |
---|
[e5d267] | 1798 | if (!DENIS1(f)) |
---|
[2c7f28] | 1799 | { |
---|
[0e32c7] | 1800 | denDegree = p_Totaldegree(DEN(f),ntRing); |
---|
| 1801 | noOfTerms += pLength(DEN(f)); |
---|
[2c7f28] | 1802 | } |
---|
[dc79bd] | 1803 | ntTest(a); // !!!! |
---|
[2c7f28] | 1804 | return numDegree + denDegree + noOfTerms; |
---|
[6ccdd3a] | 1805 | } |
---|
| 1806 | |
---|
[2c7f28] | 1807 | number ntInvers(number a, const coeffs cf) |
---|
[6ccdd3a] | 1808 | { |
---|
[fbdfd4] | 1809 | //check_N(a,cf); |
---|
[2c7f28] | 1810 | ntTest(a); |
---|
[dc79bd] | 1811 | if (IS0(a)) |
---|
[a355723] | 1812 | { |
---|
[dc79bd] | 1813 | WerrorS(nDivBy0); |
---|
| 1814 | return NULL; |
---|
[a355723] | 1815 | } |
---|
[2c7f28] | 1816 | fraction f = (fraction)a; |
---|
[dc79bd] | 1817 | assume( f != NULL ); |
---|
| 1818 | |
---|
[2c7f28] | 1819 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[dc79bd] | 1820 | |
---|
| 1821 | assume( NUM(f) != NULL ); |
---|
| 1822 | const poly den = DEN(f); |
---|
[a355723] | 1823 | |
---|
[dc79bd] | 1824 | if (den == NULL) |
---|
| 1825 | NUM(result) = p_One(ntRing); |
---|
| 1826 | else |
---|
| 1827 | NUM(result) = p_Copy(den, ntRing); |
---|
| 1828 | |
---|
| 1829 | if( !NUMIS1(f) ) |
---|
| 1830 | { |
---|
[a355723] | 1831 | poly num_f=NUM(f); |
---|
[e5c9e5] | 1832 | BOOLEAN neg= !n_GreaterZero(pGetCoeff(num_f),ntCoeffs); |
---|
[a355723] | 1833 | if (neg) |
---|
| 1834 | { |
---|
| 1835 | num_f=p_Neg(p_Copy(num_f, ntRing), ntRing); |
---|
| 1836 | NUM(result)=p_Neg(NUM(result), ntRing); |
---|
| 1837 | } |
---|
| 1838 | else |
---|
| 1839 | { |
---|
| 1840 | num_f=p_Copy(num_f, ntRing); |
---|
| 1841 | } |
---|
| 1842 | DEN(result) = num_f; |
---|
[dc79bd] | 1843 | COM(result) = COM(f); |
---|
[a355723] | 1844 | if (neg) |
---|
| 1845 | { |
---|
| 1846 | if (p_IsOne(num_f, ntRing)) |
---|
| 1847 | { |
---|
[152ec74] | 1848 | DEN(result)=NULL; |
---|
| 1849 | //COM(result) = 0; |
---|
[a355723] | 1850 | p_Delete(&num_f,ntRing); |
---|
| 1851 | } |
---|
| 1852 | } |
---|
[dc79bd] | 1853 | } |
---|
[061ea67] | 1854 | //else// Alloc0 |
---|
| 1855 | //{ |
---|
| 1856 | // DEN(result) = NULL; |
---|
| 1857 | // COM(result) = 0; |
---|
| 1858 | //} |
---|
[f3b4359] | 1859 | ntNormalizeDen(result,ntRing); |
---|
[dc79bd] | 1860 | ntTest((number)result); // !!!! |
---|
[fbdfd4] | 1861 | //check_N((number)result,cf); |
---|
[2c7f28] | 1862 | return (number)result; |
---|
[6ccdd3a] | 1863 | } |
---|
| 1864 | |
---|
[6a1aa7] | 1865 | /* assumes that src = Q or Z, dst = Q(t_1, ..., t_s) */ |
---|
[2c7f28] | 1866 | number ntMap00(number a, const coeffs src, const coeffs dst) |
---|
[6ccdd3a] | 1867 | { |
---|
[eb55f8a] | 1868 | n_Test(a, src); |
---|
[e9478b] | 1869 | |
---|
[fc2acf] | 1870 | if (n_IsZero(a, src)) return NULL; |
---|
[6a1aa7] | 1871 | assume(src->rep == dst->extRing->cf->rep); |
---|
[0c516e] | 1872 | if ((SR_HDL(a) & SR_INT) || (a->s==3)) |
---|
[9d68fd] | 1873 | { |
---|
| 1874 | number res=ntInit(p_NSet(n_Copy(a, src), dst->extRing), dst); |
---|
[fc2acf] | 1875 | n_Test(res, dst); |
---|
[9d68fd] | 1876 | return res; |
---|
| 1877 | } |
---|
[2206753] | 1878 | number nn=n_GetDenom(a,src); |
---|
| 1879 | number zz=n_GetNumerator(a,src); |
---|
[0c516e] | 1880 | number res=ntInit(p_NSet(zz,dst->extRing), dst); |
---|
| 1881 | fraction ff=(fraction)res; |
---|
[9d68fd] | 1882 | if (n_IsOne(nn,src)) DEN(ff)=NULL; |
---|
| 1883 | else DEN(ff)=p_NSet(nn,dst->extRing); |
---|
[e9478b] | 1884 | |
---|
[9d68fd] | 1885 | n_Test((number)ff,dst); |
---|
[fbdfd4] | 1886 | //check_N((number)ff,dst); |
---|
[0c516e] | 1887 | return (number)ff; |
---|
[6ccdd3a] | 1888 | } |
---|
| 1889 | |
---|
[6a1aa7] | 1890 | number ntMapZ0(number a, const coeffs src, const coeffs dst) |
---|
| 1891 | { |
---|
[eb55f8a] | 1892 | n_Test(a, src); |
---|
[fc2acf] | 1893 | if (n_IsZero(a, src)) return NULL; |
---|
[6a1aa7] | 1894 | nMapFunc nMap=n_SetMap(src,dst->extRing->cf); |
---|
| 1895 | poly p=p_NSet(nMap(a, src,dst->extRing->cf), dst->extRing); |
---|
| 1896 | if (n_IsZero(pGetCoeff(p),dst->extRing->cf)) |
---|
| 1897 | p_Delete(&p,dst->extRing); |
---|
| 1898 | number res=ntInit(p, dst); |
---|
| 1899 | n_Test(res,dst); |
---|
| 1900 | return res; |
---|
| 1901 | } |
---|
| 1902 | |
---|
[2c7f28] | 1903 | /* assumes that src = Z/p, dst = Q(t_1, ..., t_s) */ |
---|
| 1904 | number ntMapP0(number a, const coeffs src, const coeffs dst) |
---|
[6ccdd3a] | 1905 | { |
---|
[eb55f8a] | 1906 | n_Test(a, src); |
---|
[fc2acf] | 1907 | if (n_IsZero(a, src)) return NULL; |
---|
[6ccdd3a] | 1908 | /* mapping via intermediate int: */ |
---|
| 1909 | int n = n_Int(a, src); |
---|
| 1910 | number q = n_Init(n, dst->extRing->cf); |
---|
[2c7f28] | 1911 | if (n_IsZero(q, dst->extRing->cf)) |
---|
| 1912 | { |
---|
| 1913 | n_Delete(&q, dst->extRing->cf); |
---|
| 1914 | return NULL; |
---|
| 1915 | } |
---|
[dc79bd] | 1916 | return ntInit(p_NSet(q, dst->extRing), dst); |
---|
[6ccdd3a] | 1917 | } |
---|
| 1918 | |
---|
[14086c2] | 1919 | /* assumes that either src = K(t_1, ..., t_s), dst = K(t_1, ..., t_s) */ |
---|
[b38d70] | 1920 | number ntCopyMap(number a, const coeffs cf, const coeffs dst) |
---|
[6ccdd3a] | 1921 | { |
---|
[b38d70] | 1922 | ntTest(a); |
---|
| 1923 | if (IS0(a)) return NULL; |
---|
[a355723] | 1924 | |
---|
[b38d70] | 1925 | const ring rSrc = cf->extRing; |
---|
| 1926 | const ring rDst = dst->extRing; |
---|
[a355723] | 1927 | |
---|
[b38d70] | 1928 | if( rSrc == rDst ) |
---|
| 1929 | return ntCopy(a, dst); // USUALLY WRONG! |
---|
[a355723] | 1930 | |
---|
[b38d70] | 1931 | fraction f = (fraction)a; |
---|
| 1932 | poly g = prCopyR(NUM(f), rSrc, rDst); |
---|
[a355723] | 1933 | |
---|
[b38d70] | 1934 | poly h = NULL; |
---|
[a355723] | 1935 | |
---|
[b38d70] | 1936 | if (!DENIS1(f)) |
---|
| 1937 | h = prCopyR(DEN(f), rSrc, rDst); |
---|
[a355723] | 1938 | |
---|
[061ea67] | 1939 | fraction result = (fraction)omAllocBin(fractionObjectBin); |
---|
[a355723] | 1940 | |
---|
[b38d70] | 1941 | NUM(result) = g; |
---|
| 1942 | DEN(result) = h; |
---|
| 1943 | COM(result) = COM(f); |
---|
[fbdfd4] | 1944 | //check_N((number)result,dst); |
---|
[eb55f8a] | 1945 | n_Test((number)result, dst); |
---|
[a355723] | 1946 | return (number)result; |
---|
[6ccdd3a] | 1947 | } |
---|
| 1948 | |
---|
[14086c2] | 1949 | number ntGenMap(number a, const coeffs cf, const coeffs dst) |
---|
| 1950 | { |
---|
| 1951 | ntTest(a); |
---|
| 1952 | if (IS0(a)) return NULL; |
---|
| 1953 | |
---|
| 1954 | const ring rSrc = cf->extRing; |
---|
| 1955 | const ring rDst = dst->extRing; |
---|
| 1956 | |
---|
| 1957 | const nMapFunc nMap=n_SetMap(rSrc->cf,rDst->cf); |
---|
| 1958 | fraction f = (fraction)a; |
---|
| 1959 | poly g = prMapR(NUM(f), nMap, rSrc, rDst); |
---|
[2a4231] | 1960 | /* g may contain summands with coeff 0 */ |
---|
| 1961 | poly hh=g; |
---|
| 1962 | poly prev=NULL; |
---|
| 1963 | while(hh!=NULL) |
---|
| 1964 | { |
---|
| 1965 | if (n_IsZero(pGetCoeff(hh),rDst->cf)) |
---|
| 1966 | { |
---|
| 1967 | if (prev==NULL) |
---|
| 1968 | { |
---|
| 1969 | g=p_LmFreeAndNext(g,rDst); |
---|
| 1970 | hh=g; |
---|
| 1971 | } |
---|
| 1972 | else |
---|
| 1973 | { |
---|
| 1974 | prev->next=p_LmFreeAndNext(prev->next,rDst); |
---|
| 1975 | hh=prev->next; |
---|
| 1976 | } |
---|
| 1977 | } |
---|
| 1978 | else |
---|
| 1979 | { |
---|
| 1980 | prev=hh; |
---|
| 1981 | pIter(hh); |
---|
| 1982 | } |
---|
| 1983 | } |
---|
| 1984 | if (g==NULL) return NULL; |
---|
[14086c2] | 1985 | |
---|
| 1986 | poly h = NULL; |
---|
| 1987 | |
---|
| 1988 | if (!DENIS1(f)) |
---|
[2a4231] | 1989 | { |
---|
[14086c2] | 1990 | h = prMapR(DEN(f), nMap, rSrc, rDst); |
---|
[2a4231] | 1991 | /* h may contain summands with coeff 0 */ |
---|
| 1992 | hh=h; |
---|
| 1993 | prev=NULL; |
---|
| 1994 | while(hh!=NULL) |
---|
| 1995 | { |
---|
| 1996 | if (n_IsZero(pGetCoeff(hh),rDst->cf)) |
---|
| 1997 | { |
---|
| 1998 | if (prev==NULL) |
---|
| 1999 | { |
---|
| 2000 | h=p_LmFreeAndNext(h,rDst); |
---|
| 2001 | hh=h; |
---|
| 2002 | } |
---|
| 2003 | else |
---|
| 2004 | { |
---|
| 2005 | prev->next=p_LmFreeAndNext(prev->next,rDst); |
---|
| 2006 | hh=prev->next; |
---|
| 2007 | } |
---|
| 2008 | } |
---|
| 2009 | else |
---|
| 2010 | { |
---|
| 2011 | prev=hh; |
---|
| 2012 | pIter(hh); |
---|
| 2013 | } |
---|
| 2014 | } |
---|
| 2015 | if (h==NULL) WerrorS("mapping to */0"); |
---|
| 2016 | } |
---|
[14086c2] | 2017 | |
---|
| 2018 | fraction result = (fraction)omAllocBin(fractionObjectBin); |
---|
| 2019 | |
---|
| 2020 | NUM(result) = g; |
---|
| 2021 | DEN(result) = h; |
---|
| 2022 | COM(result) = COM(f); |
---|
| 2023 | //check_N((number)result,dst); |
---|
[eb55f8a] | 2024 | n_Test((number)result, dst); |
---|
[14086c2] | 2025 | return (number)result; |
---|
| 2026 | } |
---|
| 2027 | |
---|
[b38d70] | 2028 | number ntCopyAlg(number a, const coeffs cf, const coeffs dst) |
---|
[31c731] | 2029 | { |
---|
[eb55f8a] | 2030 | n_Test(a, cf) ; |
---|
[b38d70] | 2031 | if (n_IsZero(a, cf)) return NULL; |
---|
[fbdfd4] | 2032 | return ntInit(prCopyR((poly)a, cf->extRing, dst->extRing),dst); |
---|
[31c731] | 2033 | } |
---|
| 2034 | |
---|
[14086c2] | 2035 | number ntGenAlg(number a, const coeffs cf, const coeffs dst) |
---|
| 2036 | { |
---|
[eb55f8a] | 2037 | n_Test(a, cf) ; |
---|
[14086c2] | 2038 | if (n_IsZero(a, cf)) return NULL; |
---|
| 2039 | |
---|
| 2040 | const nMapFunc nMap=n_SetMap(cf->extRing->cf,dst->extRing->cf); |
---|
| 2041 | return ntInit(prMapR((poly)a, nMap, cf->extRing, dst->extRing),dst); |
---|
| 2042 | } |
---|
| 2043 | |
---|
[2c7f28] | 2044 | /* assumes that src = Q, dst = Z/p(t_1, ..., t_s) */ |
---|
| 2045 | number ntMap0P(number a, const coeffs src, const coeffs dst) |
---|
[6ccdd3a] | 2046 | { |
---|
[eb55f8a] | 2047 | n_Test(a, src) ; |
---|
[2c7f28] | 2048 | if (n_IsZero(a, src)) return NULL; |
---|
[6909cfb] | 2049 | // int p = rChar(dst->extRing); |
---|
[2206753] | 2050 | |
---|
| 2051 | number q = nlModP(a, src, dst->extRing->cf); // FIXME? TODO? // extern number nlModP(number q, const coeffs Q, const coeffs Zp); // Map q \in QQ \to Zp |
---|
[79020f] | 2052 | |
---|
[2c7f28] | 2053 | if (n_IsZero(q, dst->extRing->cf)) |
---|
| 2054 | { |
---|
| 2055 | n_Delete(&q, dst->extRing->cf); |
---|
| 2056 | return NULL; |
---|
| 2057 | } |
---|
[a355723] | 2058 | |
---|
[79020f] | 2059 | poly g = p_NSet(q, dst->extRing); |
---|
| 2060 | |
---|
[2c7f28] | 2061 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[31c731] | 2062 | NUM(f) = g; // DEN(f) = NULL; COM(f) = 0; |
---|
[eb55f8a] | 2063 | n_Test((number)f, dst); |
---|
[fbdfd4] | 2064 | //check_N((number)f,dst); |
---|
[2c7f28] | 2065 | return (number)f; |
---|
[6ccdd3a] | 2066 | } |
---|
| 2067 | |
---|
[2c7f28] | 2068 | /* assumes that src = Z/p, dst = Z/p(t_1, ..., t_s) */ |
---|
| 2069 | number ntMapPP(number a, const coeffs src, const coeffs dst) |
---|
[6ccdd3a] | 2070 | { |
---|
[eb55f8a] | 2071 | n_Test(a, src) ; |
---|
[2c7f28] | 2072 | if (n_IsZero(a, src)) return NULL; |
---|
[6ccdd3a] | 2073 | assume(src == dst->extRing->cf); |
---|
[2c7f28] | 2074 | poly p = p_One(dst->extRing); |
---|
[a0acbc] | 2075 | p_SetCoeff(p, n_Copy(a, src), dst->extRing); |
---|
[2c7f28] | 2076 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[061ea67] | 2077 | NUM(f) = p; // DEN(f) = NULL; COM(f) = 0; |
---|
[eb55f8a] | 2078 | n_Test((number)f, dst); |
---|
[fbdfd4] | 2079 | //check_N((number)f,dst); |
---|
[2c7f28] | 2080 | return (number)f; |
---|
[6ccdd3a] | 2081 | } |
---|
| 2082 | |
---|
[2c7f28] | 2083 | /* assumes that src = Z/u, dst = Z/p(t_1, ..., t_s), where u != p */ |
---|
| 2084 | number ntMapUP(number a, const coeffs src, const coeffs dst) |
---|
[6ccdd3a] | 2085 | { |
---|
[eb55f8a] | 2086 | n_Test(a, src) ; |
---|
[2c7f28] | 2087 | if (n_IsZero(a, src)) return NULL; |
---|
[6ccdd3a] | 2088 | /* mapping via intermediate int: */ |
---|
| 2089 | int n = n_Int(a, src); |
---|
| 2090 | number q = n_Init(n, dst->extRing->cf); |
---|
[2c7f28] | 2091 | poly p; |
---|
| 2092 | if (n_IsZero(q, dst->extRing->cf)) |
---|
| 2093 | { |
---|
| 2094 | n_Delete(&q, dst->extRing->cf); |
---|
| 2095 | return NULL; |
---|
| 2096 | } |
---|
| 2097 | p = p_One(dst->extRing); |
---|
| 2098 | p_SetCoeff(p, q, dst->extRing); |
---|
| 2099 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
[061ea67] | 2100 | NUM(f) = p; // DEN(f) = NULL; COM(f) = 0; |
---|
[eb55f8a] | 2101 | n_Test((number)f, dst); |
---|
[fbdfd4] | 2102 | //check_N((number)f,dst); |
---|
[2c7f28] | 2103 | return (number)f; |
---|
[6ccdd3a] | 2104 | } |
---|
| 2105 | |
---|
[2c7f28] | 2106 | nMapFunc ntSetMap(const coeffs src, const coeffs dst) |
---|
[6ccdd3a] | 2107 | { |
---|
[2c7f28] | 2108 | /* dst is expected to be a rational function field */ |
---|
[6d0515d] | 2109 | assume(getCoeffType(dst) == n_transExt); |
---|
[c14846c] | 2110 | |
---|
[da0565b] | 2111 | if( src == dst ) return ndCopyMap; |
---|
| 2112 | |
---|
[6ccdd3a] | 2113 | int h = 0; /* the height of the extension tower given by dst */ |
---|
| 2114 | coeffs bDst = nCoeff_bottom(dst, h); /* the bottom field in the tower dst */ |
---|
[331fd0] | 2115 | coeffs bSrc = nCoeff_bottom(src, h); /* the bottom field in the tower src */ |
---|
[c14846c] | 2116 | |
---|
[6ccdd3a] | 2117 | /* for the time being, we only provide maps if h = 1 and if b is Q or |
---|
| 2118 | some field Z/pZ: */ |
---|
[31c731] | 2119 | if (h==0) |
---|
| 2120 | { |
---|
[6a1aa7] | 2121 | if ((src->rep==n_rep_gap_rat) && nCoeff_is_Q(bDst)) |
---|
| 2122 | return ntMap00; /// Q or Z --> Q(T) |
---|
| 2123 | if (src->rep==n_rep_gap_gmp) |
---|
| 2124 | return ntMapZ0; /// Z --> K(T) |
---|
[31c731] | 2125 | if (nCoeff_is_Zp(src) && nCoeff_is_Q(bDst)) |
---|
| 2126 | return ntMapP0; /// Z/p --> Q(T) |
---|
[20f29bb] | 2127 | if (nCoeff_is_Q_or_BI(src) && nCoeff_is_Zp(bDst)) |
---|
[31c731] | 2128 | return ntMap0P; /// Q --> Z/p(T) |
---|
| 2129 | if (nCoeff_is_Zp(src) && nCoeff_is_Zp(bDst)) |
---|
| 2130 | { |
---|
| 2131 | if (src->ch == dst->ch) return ntMapPP; /// Z/p --> Z/p(T) |
---|
| 2132 | else return ntMapUP; /// Z/u --> Z/p(T) |
---|
| 2133 | } |
---|
| 2134 | } |
---|
[6ccdd3a] | 2135 | if (h != 1) return NULL; |
---|
[14086c2] | 2136 | //if ((!nCoeff_is_Zp(bDst)) && (!nCoeff_is_Q(bDst))) return NULL; |
---|
[c14846c] | 2137 | |
---|
[2c7f28] | 2138 | /* Let T denote the sequence of transcendental extension variables, i.e., |
---|
| 2139 | K[t_1, ..., t_s] =: K[T]; |
---|
| 2140 | Let moreover, for any such sequence T, T' denote any subsequence of T |
---|
| 2141 | of the form t_1, ..., t_w with w <= s. */ |
---|
[c14846c] | 2142 | |
---|
[14086c2] | 2143 | if (rVar(src->extRing) > rVar(dst->extRing)) |
---|
| 2144 | return NULL; |
---|
[c14846c] | 2145 | |
---|
[14086c2] | 2146 | for (int i = 0; i < rVar(src->extRing); i++) |
---|
| 2147 | if (strcmp(rRingVar(i, src->extRing), rRingVar(i, dst->extRing)) != 0) |
---|
[b38d70] | 2148 | return NULL; |
---|
[a355723] | 2149 | |
---|
[14086c2] | 2150 | if (src->type==n_transExt) |
---|
| 2151 | { |
---|
| 2152 | if (src->extRing->cf==dst->extRing->cf) |
---|
| 2153 | return ntCopyMap; /// K(T') --> K(T) |
---|
| 2154 | else |
---|
| 2155 | return ntGenMap; /// K(T') --> K'(T) |
---|
[6ccdd3a] | 2156 | } |
---|
[14086c2] | 2157 | else |
---|
[6ccdd3a] | 2158 | { |
---|
[14086c2] | 2159 | if (src->extRing->cf==dst->extRing->cf) |
---|
| 2160 | return ntCopyAlg; /// K(T') --> K(T) |
---|
| 2161 | else |
---|
| 2162 | return ntGenAlg; /// K(T') --> K'(T) |
---|
[6ccdd3a] | 2163 | } |
---|
[c14846c] | 2164 | |
---|
| 2165 | return NULL; /// default |
---|
[6ccdd3a] | 2166 | } |
---|
[c8e030] | 2167 | #if 0 |
---|
| 2168 | nMapFunc ntSetMap_T(const coeffs src, const coeffs dst) |
---|
| 2169 | { |
---|
| 2170 | nMapFunc n=ntSetMap(src,dst); |
---|
| 2171 | if (n==ntCopyAlg) printf("n=ntCopyAlg\n"); |
---|
| 2172 | else if (n==ntCopyMap) printf("n=ntCopyMap\n"); |
---|
| 2173 | else if (n==ntMapUP) printf("n=ntMapUP\n"); |
---|
| 2174 | else if (n==ntMap0P) printf("n=ntMap0P\n"); |
---|
| 2175 | else if (n==ntMapP0) printf("n=ntMapP0\n"); |
---|
| 2176 | else if (n==ntMap00) printf("n=ntMap00\n"); |
---|
| 2177 | else if (n==NULL) printf("n=NULL\n"); |
---|
| 2178 | else printf("n=?\n"); |
---|
| 2179 | return n; |
---|
| 2180 | } |
---|
| 2181 | #endif |
---|
[6ccdd3a] | 2182 | |
---|
[31c731] | 2183 | void ntKillChar(coeffs cf) |
---|
| 2184 | { |
---|
[a195ed] | 2185 | if ((--cf->extRing->ref) == 0) |
---|
| 2186 | rDelete(cf->extRing); |
---|
| 2187 | } |
---|
[fc4977] | 2188 | number ntConvFactoryNSingN( const CanonicalForm n, const coeffs cf) |
---|
| 2189 | { |
---|
| 2190 | if (n.isZero()) return NULL; |
---|
| 2191 | poly p=convFactoryPSingP(n,ntRing); |
---|
[fbdfd4] | 2192 | p_Normalize(p,ntRing); |
---|
[fc4977] | 2193 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 2194 | NUM(result) = p; |
---|
| 2195 | //DEN(result) = NULL; // done by omAlloc0Bin |
---|
| 2196 | //COM(result) = 0; // done by omAlloc0Bin |
---|
[dc79bd] | 2197 | ntTest((number)result); |
---|
[fc4977] | 2198 | return (number)result; |
---|
| 2199 | } |
---|
[2e4ec14] | 2200 | CanonicalForm ntConvSingNFactoryN( number n, BOOLEAN /*setChar*/, const coeffs cf ) |
---|
[fc4977] | 2201 | { |
---|
| 2202 | ntTest(n); |
---|
| 2203 | if (IS0(n)) return CanonicalForm(0); |
---|
| 2204 | |
---|
| 2205 | fraction f = (fraction)n; |
---|
| 2206 | return convSingPFactoryP(NUM(f),ntRing); |
---|
| 2207 | } |
---|
[a195ed] | 2208 | |
---|
[da5d77] | 2209 | static int ntParDeg(number a, const coeffs cf) |
---|
[48a41a] | 2210 | { |
---|
[dc79bd] | 2211 | ntTest(a); |
---|
[48a41a] | 2212 | if (IS0(a)) return -1; |
---|
| 2213 | fraction fa = (fraction)a; |
---|
| 2214 | return cf->extRing->pFDeg(NUM(fa),cf->extRing); |
---|
| 2215 | } |
---|
| 2216 | |
---|
[7fee876] | 2217 | /// return the specified parameter as a number in the given trans.ext. |
---|
| 2218 | static number ntParameter(const int iParameter, const coeffs cf) |
---|
| 2219 | { |
---|
[6d0515d] | 2220 | assume(getCoeffType(cf) == n_transExt); |
---|
[7fee876] | 2221 | |
---|
| 2222 | const ring R = cf->extRing; |
---|
| 2223 | assume( R != NULL ); |
---|
| 2224 | assume( 0 < iParameter && iParameter <= rVar(R) ); |
---|
| 2225 | |
---|
| 2226 | poly p = p_One(R); p_SetExp(p, iParameter, 1, R); p_Setm(p, R); |
---|
[9d68fd] | 2227 | p_Test(p,R); |
---|
[7fee876] | 2228 | |
---|
| 2229 | fraction f = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 2230 | NUM(f) = p; |
---|
[061ea67] | 2231 | //DEN(f) = NULL; |
---|
| 2232 | //COM(f) = 0; |
---|
[7fee876] | 2233 | |
---|
[dc79bd] | 2234 | ntTest((number)f); |
---|
| 2235 | |
---|
[7fee876] | 2236 | return (number)f; |
---|
| 2237 | } |
---|
| 2238 | |
---|
| 2239 | /// if m == var(i)/1 => return i, |
---|
| 2240 | int ntIsParam(number m, const coeffs cf) |
---|
| 2241 | { |
---|
[dc79bd] | 2242 | ntTest(m); |
---|
[6d0515d] | 2243 | assume(getCoeffType(cf) == n_transExt); |
---|
[7fee876] | 2244 | |
---|
| 2245 | const ring R = cf->extRing; |
---|
| 2246 | assume( R != NULL ); |
---|
| 2247 | |
---|
| 2248 | fraction f = (fraction)m; |
---|
| 2249 | |
---|
| 2250 | if( DEN(f) != NULL ) |
---|
| 2251 | return 0; |
---|
| 2252 | |
---|
| 2253 | return p_Var( NUM(f), R ); |
---|
| 2254 | } |
---|
| 2255 | |
---|
[dc79bd] | 2256 | struct NTNumConverter |
---|
| 2257 | { |
---|
| 2258 | static inline poly convert(const number& n) |
---|
| 2259 | { |
---|
| 2260 | // suitable for trans. ext. numbers that are fractions of polys |
---|
| 2261 | return NUM((fraction)n); // return the numerator |
---|
| 2262 | } |
---|
| 2263 | }; |
---|
| 2264 | |
---|
| 2265 | |
---|
| 2266 | static void ntClearContent(ICoeffsEnumerator& numberCollectionEnumerator, number& c, const coeffs cf) |
---|
[de88371] | 2267 | { |
---|
| 2268 | assume(cf != NULL); |
---|
[6d0515d] | 2269 | assume(getCoeffType(cf) == n_transExt); |
---|
[dc79bd] | 2270 | // all coeffs are given by fractions of polynomails over integers!!! |
---|
| 2271 | // without denominators!!! |
---|
| 2272 | |
---|
| 2273 | const ring R = cf->extRing; |
---|
| 2274 | assume(R != NULL); |
---|
[a355723] | 2275 | const coeffs Q = R->cf; |
---|
| 2276 | assume(Q != NULL); |
---|
| 2277 | assume(nCoeff_is_Q(Q)); |
---|
| 2278 | |
---|
[dc79bd] | 2279 | |
---|
| 2280 | numberCollectionEnumerator.Reset(); |
---|
| 2281 | |
---|
| 2282 | if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial? |
---|
| 2283 | { |
---|
| 2284 | c = ntInit(1, cf); |
---|
| 2285 | return; |
---|
| 2286 | } |
---|
| 2287 | |
---|
| 2288 | // all coeffs are given by integers after returning from this routine |
---|
| 2289 | |
---|
| 2290 | // part 1, collect product of all denominators /gcds |
---|
| 2291 | poly cand = NULL; |
---|
| 2292 | |
---|
| 2293 | do |
---|
| 2294 | { |
---|
| 2295 | number &n = numberCollectionEnumerator.Current(); |
---|
| 2296 | |
---|
| 2297 | ntNormalize(n, cf); |
---|
| 2298 | |
---|
| 2299 | fraction f = (fraction)n; |
---|
| 2300 | |
---|
| 2301 | assume( f != NULL ); |
---|
| 2302 | |
---|
| 2303 | const poly den = DEN(f); |
---|
| 2304 | |
---|
| 2305 | assume( den == NULL ); // ?? / 1 ? |
---|
| 2306 | |
---|
| 2307 | const poly num = NUM(f); |
---|
| 2308 | |
---|
| 2309 | if( cand == NULL ) |
---|
| 2310 | cand = p_Copy(num, R); |
---|
| 2311 | else |
---|
| 2312 | cand = singclap_gcd(cand, p_Copy(num, R), R); // gcd(cand, num) |
---|
| 2313 | |
---|
| 2314 | if( p_IsConstant(cand, R) ) |
---|
| 2315 | break; |
---|
| 2316 | } |
---|
| 2317 | while( numberCollectionEnumerator.MoveNext() ) ; |
---|
| 2318 | |
---|
[a355723] | 2319 | |
---|
[dc79bd] | 2320 | // part2: all coeffs = all coeffs * cand |
---|
| 2321 | if( cand != NULL ) |
---|
| 2322 | { |
---|
| 2323 | if( !p_IsConstant(cand, R) ) |
---|
| 2324 | { |
---|
[a355723] | 2325 | c = ntInit(cand, cf); |
---|
[dc79bd] | 2326 | numberCollectionEnumerator.Reset(); |
---|
| 2327 | while (numberCollectionEnumerator.MoveNext() ) |
---|
| 2328 | { |
---|
| 2329 | number &n = numberCollectionEnumerator.Current(); |
---|
| 2330 | const number t = ntDiv(n, c, cf); // TODO: rewrite!? |
---|
| 2331 | ntDelete(&n, cf); |
---|
| 2332 | n = t; |
---|
| 2333 | } |
---|
| 2334 | } // else NUM (result) = p_One(R); |
---|
| 2335 | else { p_Delete(&cand, R); cand = NULL; } |
---|
| 2336 | } |
---|
[a355723] | 2337 | |
---|
[dc79bd] | 2338 | // Quick and dirty fix for constant content clearing: consider numerators??? |
---|
| 2339 | CRecursivePolyCoeffsEnumerator<NTNumConverter> itr(numberCollectionEnumerator); // recursively treat the NUM(numbers) as polys! |
---|
| 2340 | number cc; |
---|
[a355723] | 2341 | |
---|
[558f3cc] | 2342 | n_ClearContent(itr, cc, Q); |
---|
[dc79bd] | 2343 | number g = ntInit(p_NSet(cc, R), cf); |
---|
[a355723] | 2344 | |
---|
[dc79bd] | 2345 | if( cand != NULL ) |
---|
| 2346 | { |
---|
| 2347 | number gg = ntMult(g, c, cf); |
---|
[a355723] | 2348 | ntDelete(&g, cf); |
---|
[dc79bd] | 2349 | ntDelete(&c, cf); c = gg; |
---|
[a355723] | 2350 | } else |
---|
[dc79bd] | 2351 | c = g; |
---|
| 2352 | ntTest(c); |
---|
[de88371] | 2353 | } |
---|
| 2354 | |
---|
[dc79bd] | 2355 | static void ntClearDenominators(ICoeffsEnumerator& numberCollectionEnumerator, number& c, const coeffs cf) |
---|
[de88371] | 2356 | { |
---|
| 2357 | assume(cf != NULL); |
---|
[6d0515d] | 2358 | assume(getCoeffType(cf) == n_transExt); // both over Q(a) and Zp(a)! |
---|
[dc79bd] | 2359 | // all coeffs are given by fractions of polynomails over integers!!! |
---|
| 2360 | |
---|
| 2361 | numberCollectionEnumerator.Reset(); |
---|
| 2362 | |
---|
| 2363 | if( !numberCollectionEnumerator.MoveNext() ) // empty zero polynomial? |
---|
| 2364 | { |
---|
| 2365 | c = ntInit(1, cf); |
---|
| 2366 | return; |
---|
| 2367 | } |
---|
| 2368 | |
---|
| 2369 | // all coeffs are given by integers after returning from this routine |
---|
| 2370 | |
---|
| 2371 | // part 1, collect product of all denominators /gcds |
---|
| 2372 | poly cand = NULL; |
---|
| 2373 | |
---|
| 2374 | const ring R = cf->extRing; |
---|
[a355723] | 2375 | assume(R != NULL); |
---|
[dc79bd] | 2376 | |
---|
[a355723] | 2377 | const coeffs Q = R->cf; |
---|
| 2378 | assume(Q != NULL); |
---|
| 2379 | // assume(nCoeff_is_Q(Q)); |
---|
[dc79bd] | 2380 | |
---|
| 2381 | do |
---|
| 2382 | { |
---|
| 2383 | number &n = numberCollectionEnumerator.Current(); |
---|
[a355723] | 2384 | |
---|
[dc79bd] | 2385 | ntNormalize(n, cf); |
---|
| 2386 | |
---|
[e56eb1a] | 2387 | fraction f = (fraction)ntGetDenom (n, cf); |
---|
[dc79bd] | 2388 | |
---|
| 2389 | assume( f != NULL ); |
---|
| 2390 | |
---|
[e56eb1a] | 2391 | const poly den = NUM(f); |
---|
[dc79bd] | 2392 | |
---|
| 2393 | if( den == NULL ) // ?? / 1 ? |
---|
| 2394 | continue; |
---|
| 2395 | |
---|
| 2396 | if( cand == NULL ) |
---|
| 2397 | cand = p_Copy(den, R); |
---|
| 2398 | else |
---|
| 2399 | { |
---|
| 2400 | // cand === LCM( cand, den )!!!! |
---|
| 2401 | // NOTE: maybe it's better to make the product and clearcontent afterwards!? |
---|
| 2402 | // TODO: move the following to factory? |
---|
| 2403 | poly gcd = singclap_gcd(p_Copy(cand, R), p_Copy(den, R), R); // gcd(cand, den) is monic no mater leading coeffs! :(((( |
---|
[e56eb1a] | 2404 | if (nCoeff_is_Q (Q)) |
---|
| 2405 | { |
---|
[1a3437] | 2406 | number LcGcd= n_SubringGcd (p_GetCoeff (cand, R), p_GetCoeff(den, R), Q); |
---|
[e56eb1a] | 2407 | gcd = p_Mult_nn(gcd, LcGcd, R); |
---|
[20c99b] | 2408 | n_Delete(&LcGcd,Q); |
---|
[e56eb1a] | 2409 | } |
---|
[dc79bd] | 2410 | // assume( n_IsOne(pGetCoeff(gcd), Q) ); // TODO: this may be wrong... |
---|
[a355723] | 2411 | cand = p_Mult_q(cand, p_Copy(den, R), R); // cand *= den |
---|
[dc79bd] | 2412 | const poly t = singclap_pdivide( cand, gcd, R ); // cand' * den / gcd(cand', den) |
---|
| 2413 | p_Delete(&cand, R); |
---|
| 2414 | p_Delete(&gcd, R); |
---|
| 2415 | cand = t; |
---|
| 2416 | } |
---|
| 2417 | } |
---|
| 2418 | while( numberCollectionEnumerator.MoveNext() ); |
---|
| 2419 | |
---|
| 2420 | if( cand == NULL ) |
---|
| 2421 | { |
---|
| 2422 | c = ntInit(1, cf); |
---|
| 2423 | return; |
---|
[a355723] | 2424 | } |
---|
[dc79bd] | 2425 | |
---|
[a355723] | 2426 | c = ntInit(cand, cf); |
---|
[dc79bd] | 2427 | |
---|
| 2428 | numberCollectionEnumerator.Reset(); |
---|
[a355723] | 2429 | |
---|
[dc79bd] | 2430 | number d = NULL; |
---|
| 2431 | |
---|
| 2432 | while (numberCollectionEnumerator.MoveNext() ) |
---|
| 2433 | { |
---|
| 2434 | number &n = numberCollectionEnumerator.Current(); |
---|
| 2435 | number t = ntMult(n, c, cf); // TODO: rewrite!? |
---|
| 2436 | ntDelete(&n, cf); |
---|
| 2437 | |
---|
| 2438 | ntNormalize(t, cf); // TODO: needed? |
---|
| 2439 | n = t; |
---|
[a355723] | 2440 | |
---|
[dc79bd] | 2441 | fraction f = (fraction)t; |
---|
| 2442 | assume( f != NULL ); |
---|
[de88371] | 2443 | |
---|
[dc79bd] | 2444 | const poly den = DEN(f); |
---|
| 2445 | |
---|
| 2446 | if( den != NULL ) // ?? / ?? ? |
---|
| 2447 | { |
---|
| 2448 | assume( p_IsConstant(den, R) ); |
---|
| 2449 | assume( pNext(den) == NULL ); |
---|
[a355723] | 2450 | |
---|
[dc79bd] | 2451 | if( d == NULL ) |
---|
[a355723] | 2452 | d = n_Copy(pGetCoeff(den), Q); |
---|
[dc79bd] | 2453 | else |
---|
| 2454 | { |
---|
[88cceb] | 2455 | number g = n_NormalizeHelper(d, pGetCoeff(den), Q); |
---|
[dc79bd] | 2456 | n_Delete(&d, Q); d = g; |
---|
| 2457 | } |
---|
| 2458 | } |
---|
| 2459 | } |
---|
[a355723] | 2460 | |
---|
[dc79bd] | 2461 | if( d != NULL ) |
---|
| 2462 | { |
---|
| 2463 | numberCollectionEnumerator.Reset(); |
---|
| 2464 | while (numberCollectionEnumerator.MoveNext() ) |
---|
| 2465 | { |
---|
| 2466 | number &n = numberCollectionEnumerator.Current(); |
---|
| 2467 | fraction f = (fraction)n; |
---|
| 2468 | |
---|
| 2469 | assume( f != NULL ); |
---|
| 2470 | |
---|
| 2471 | const poly den = DEN(f); |
---|
| 2472 | |
---|
| 2473 | if( den == NULL ) // ?? / 1 ? |
---|
| 2474 | NUM(f) = p_Mult_nn(NUM(f), d, R); |
---|
| 2475 | else |
---|
| 2476 | { |
---|
| 2477 | assume( p_IsConstant(den, R) ); |
---|
| 2478 | assume( pNext(den) == NULL ); |
---|
[a355723] | 2479 | |
---|
| 2480 | number ddd = n_Div(d, pGetCoeff(den), Q); // but be an integer now!!! |
---|
[dc79bd] | 2481 | NUM(f) = p_Mult_nn(NUM(f), ddd, R); |
---|
[a355723] | 2482 | n_Delete(&ddd, Q); |
---|
| 2483 | |
---|
| 2484 | p_Delete(&DEN(f), R); |
---|
| 2485 | DEN(f) = NULL; // TODO: check if this is needed!? |
---|
| 2486 | } |
---|
| 2487 | |
---|
[dc79bd] | 2488 | assume( DEN(f) == NULL ); |
---|
| 2489 | } |
---|
[a355723] | 2490 | |
---|
[6105f4f] | 2491 | NUM((fraction)c) = p_Mult_nn(NUM((fraction)c), d, R); |
---|
[dc79bd] | 2492 | n_Delete(&d, Q); |
---|
| 2493 | } |
---|
[a355723] | 2494 | |
---|
| 2495 | |
---|
[dc79bd] | 2496 | ntTest(c); |
---|
[de88371] | 2497 | } |
---|
| 2498 | |
---|
[44494ec] | 2499 | number ntChineseRemainder(number *x, number *q,int rl, BOOLEAN /*sym*/,CFArray &inv_cache,const coeffs cf) |
---|
[e79e26] | 2500 | { |
---|
| 2501 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 2502 | |
---|
| 2503 | poly *P=(poly*)omAlloc(rl*sizeof(poly*)); |
---|
| 2504 | number *X=(number *)omAlloc(rl*sizeof(number)); |
---|
| 2505 | |
---|
[fc2acf] | 2506 | int i; |
---|
| 2507 | |
---|
[e79e26] | 2508 | for(i=0;i<rl;i++) P[i]=p_Copy(NUM((fraction)(x[i])),cf->extRing); |
---|
[a1f17b] | 2509 | NUM(result)=p_ChineseRemainder(P,X,q,rl,inv_cache,cf->extRing); |
---|
[e79e26] | 2510 | |
---|
| 2511 | for(i=0;i<rl;i++) |
---|
| 2512 | { |
---|
| 2513 | P[i]=p_Copy(DEN((fraction)(x[i])),cf->extRing); |
---|
| 2514 | if (P[i]==NULL) P[i]=p_One(cf->extRing); |
---|
| 2515 | } |
---|
[a1f17b] | 2516 | DEN(result)=p_ChineseRemainder(P,X,q,rl,inv_cache,cf->extRing); |
---|
[e79e26] | 2517 | |
---|
| 2518 | omFreeSize(X,rl*sizeof(number)); |
---|
| 2519 | omFreeSize(P,rl*sizeof(poly*)); |
---|
[5c8e18f] | 2520 | if (p_IsConstant(DEN(result), ntRing) |
---|
| 2521 | && n_IsOne(pGetCoeff(DEN(result)), ntCoeffs)) |
---|
| 2522 | { |
---|
| 2523 | p_Delete(&DEN(result),ntRing); |
---|
| 2524 | } |
---|
[fc2acf] | 2525 | ntTest((number)result); |
---|
[e79e26] | 2526 | return ((number)result); |
---|
| 2527 | } |
---|
| 2528 | |
---|
[616d685] | 2529 | number ntFarey(number p, number n, const coeffs cf) |
---|
| 2530 | { |
---|
| 2531 | // n is really a bigint |
---|
| 2532 | fraction result = (fraction)omAlloc0Bin(fractionObjectBin); |
---|
| 2533 | NUM(result)=p_Farey(p_Copy(NUM((fraction)p),cf->extRing),n,cf->extRing); |
---|
| 2534 | DEN(result)=p_Farey(p_Copy(DEN((fraction)p),cf->extRing),n,cf->extRing); |
---|
[fc2acf] | 2535 | ntTest((number)result); |
---|
[616d685] | 2536 | return ((number)result); |
---|
| 2537 | } |
---|
| 2538 | |
---|
[2c7f28] | 2539 | BOOLEAN ntInitChar(coeffs cf, void * infoStruct) |
---|
[a55ef0] | 2540 | { |
---|
| 2541 | |
---|
| 2542 | assume( infoStruct != NULL ); |
---|
[c14846c] | 2543 | |
---|
[2c7f28] | 2544 | TransExtInfo *e = (TransExtInfo *)infoStruct; |
---|
[c14846c] | 2545 | |
---|
[ec5ec8] | 2546 | assume( e->r != NULL); // extRing; |
---|
| 2547 | assume( e->r->cf != NULL); // extRing->cf; |
---|
[dd668f] | 2548 | assume( e->r->qideal == NULL ); |
---|
[2c7f28] | 2549 | |
---|
[ec5ec8] | 2550 | assume( cf != NULL ); |
---|
[6d0515d] | 2551 | assume(getCoeffType(cf) == n_transExt); // coeff type; |
---|
[ec5ec8] | 2552 | |
---|
[7fee876] | 2553 | ring R = e->r; |
---|
| 2554 | assume(R != NULL); |
---|
[a355723] | 2555 | |
---|
[7fee876] | 2556 | R->ref ++; // increase the ref.counter for the ground poly. ring! |
---|
[ec5ec8] | 2557 | |
---|
[7fee876] | 2558 | cf->extRing = R; |
---|
[6ccdd3a] | 2559 | /* propagate characteristic up so that it becomes |
---|
| 2560 | directly accessible in cf: */ |
---|
[7fee876] | 2561 | cf->ch = R->cf->ch; |
---|
[36a8e9] | 2562 | |
---|
| 2563 | cf->is_field=TRUE; |
---|
| 2564 | cf->is_domain=TRUE; |
---|
[6a1aa7] | 2565 | cf->rep=n_rep_rat_fct; |
---|
[36a8e9] | 2566 | |
---|
[7fee876] | 2567 | cf->factoryVarOffset = R->cf->factoryVarOffset + rVar(R); |
---|
[75f460] | 2568 | |
---|
[2206753] | 2569 | cf->cfCoeffString = naCoeffString; // FIXME? TODO? // extern char* naCoeffString(const coeffs r); |
---|
[b3c736] | 2570 | cf->cfCoeffName = naCoeffName; // FIXME? TODO? // extern char* naCoeffString(const coeffs r); |
---|
[a355723] | 2571 | |
---|
[2c7f28] | 2572 | cf->cfGreaterZero = ntGreaterZero; |
---|
| 2573 | cf->cfGreater = ntGreater; |
---|
| 2574 | cf->cfEqual = ntEqual; |
---|
| 2575 | cf->cfIsZero = ntIsZero; |
---|
| 2576 | cf->cfIsOne = ntIsOne; |
---|
| 2577 | cf->cfIsMOne = ntIsMOne; |
---|
| 2578 | cf->cfInit = ntInit; |
---|
[616d685] | 2579 | cf->cfFarey = ntFarey; |
---|
[e79e26] | 2580 | cf->cfChineseRemainder = ntChineseRemainder; |
---|
[2c7f28] | 2581 | cf->cfInt = ntInt; |
---|
[ec89bb4] | 2582 | cf->cfInpNeg = ntNeg; |
---|
[2c7f28] | 2583 | cf->cfAdd = ntAdd; |
---|
| 2584 | cf->cfSub = ntSub; |
---|
| 2585 | cf->cfMult = ntMult; |
---|
| 2586 | cf->cfDiv = ntDiv; |
---|
| 2587 | cf->cfExactDiv = ntDiv; |
---|
| 2588 | cf->cfPower = ntPower; |
---|
| 2589 | cf->cfCopy = ntCopy; |
---|
[ce1f78] | 2590 | cf->cfWriteLong = ntWriteLong; |
---|
[2c7f28] | 2591 | cf->cfRead = ntRead; |
---|
[237b4dd] | 2592 | cf->cfNormalize = ntNormalize; |
---|
[2c7f28] | 2593 | cf->cfDelete = ntDelete; |
---|
| 2594 | cf->cfSetMap = ntSetMap; |
---|
| 2595 | cf->cfGetDenom = ntGetDenom; |
---|
| 2596 | cf->cfGetNumerator = ntGetNumerator; |
---|
| 2597 | cf->cfRePart = ntCopy; |
---|
| 2598 | cf->cfImPart = ntImPart; |
---|
| 2599 | cf->cfCoeffWrite = ntCoeffWrite; |
---|
[dbcf787] | 2600 | #ifdef LDEBUG |
---|
[2c7f28] | 2601 | cf->cfDBTest = ntDBTest; |
---|
[dbcf787] | 2602 | #endif |
---|
[1a3437] | 2603 | //cf->cfGcd = ntGcd_dummy; |
---|
| 2604 | cf->cfSubringGcd = ntGcd; |
---|
[88cceb] | 2605 | cf->cfNormalizeHelper = ntNormalizeHelper; |
---|
[2c7f28] | 2606 | cf->cfSize = ntSize; |
---|
| 2607 | cf->nCoeffIsEqual = ntCoeffIsEqual; |
---|
| 2608 | cf->cfInvers = ntInvers; |
---|
[a195ed] | 2609 | cf->cfKillChar = ntKillChar; |
---|
[c14846c] | 2610 | |
---|
[ce1f78] | 2611 | if( rCanShortOut(ntRing) ) |
---|
| 2612 | cf->cfWriteShort = ntWriteShort; |
---|
| 2613 | else |
---|
| 2614 | cf->cfWriteShort = ntWriteLong; |
---|
| 2615 | |
---|
[fc4977] | 2616 | cf->convFactoryNSingN =ntConvFactoryNSingN; |
---|
| 2617 | cf->convSingNFactoryN =ntConvSingNFactoryN; |
---|
[48a41a] | 2618 | cf->cfParDeg = ntParDeg; |
---|
[c14846c] | 2619 | |
---|
[7fee876] | 2620 | cf->iNumberOfParameters = rVar(R); |
---|
[6352ad] | 2621 | cf->pParameterNames = (const char**)R->names; |
---|
[7fee876] | 2622 | cf->cfParameter = ntParameter; |
---|
[ecf4a6a] | 2623 | cf->has_simple_Inverse= FALSE; |
---|
[1ab8c4] | 2624 | /* cf->has_simple_Alloc= FALSE; */ |
---|
[ecf4a6a] | 2625 | |
---|
[6637ee] | 2626 | |
---|
[de88371] | 2627 | if( nCoeff_is_Q(R->cf) ) |
---|
| 2628 | cf->cfClearContent = ntClearContent; |
---|
| 2629 | |
---|
| 2630 | cf->cfClearDenominators = ntClearDenominators; |
---|
[6637ee] | 2631 | |
---|
[7fee876] | 2632 | return FALSE; |
---|
[e82417] | 2633 | } |
---|
[35564a5] | 2634 | |
---|
| 2635 | template class CRecursivePolyCoeffsEnumerator<NTNumConverter>; |
---|
| 2636 | template class IEnumerator<snumber*>; |
---|