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