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