[fba6f18] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* $Id$ */ |
---|
| 5 | /* |
---|
[6ccdd3a] | 6 | * ABSTRACT: numbers in an algebraic extension field K[a] / < f(a) > |
---|
[fba6f18] | 7 | * Assuming that we have a coeffs object cf, then these numbers |
---|
| 8 | * are polynomials in the polynomial ring K[a] represented by |
---|
[6ccdd3a] | 9 | * cf->extRing. |
---|
[fba6f18] | 10 | * IMPORTANT ASSUMPTIONS: |
---|
[6ccdd3a] | 11 | * 1.) So far we assume that cf->extRing is a valid polynomial |
---|
[fba6f18] | 12 | * ring in exactly one variable, i.e., K[a], where K is allowed |
---|
| 13 | * to be any field (representable in SINGULAR and which may |
---|
| 14 | * itself be some extension field, thus allowing for extension |
---|
| 15 | * towers). |
---|
| 16 | * 2.) Moreover, this implementation assumes that |
---|
[6ccdd3a] | 17 | * cf->extRing->minideal is not NULL but an ideal with at |
---|
[fba6f18] | 18 | * least one non-zero generator which may be accessed by |
---|
[6ccdd3a] | 19 | * cf->extRing->minideal->m[0] and which represents the minimal |
---|
| 20 | * polynomial f(a) of the extension variable 'a' in K[a]. |
---|
| 21 | * 3.) As soon as an std method for polynomial rings becomes |
---|
| 22 | * availabe, all reduction steps modulo f(a) should be replaced |
---|
| 23 | * by a call to std. Moreover, in this situation one can finally |
---|
| 24 | * move from K[a] / < f(a) > to |
---|
| 25 | * K[a_1, ..., a_s] / I, with I some zero-dimensional ideal |
---|
| 26 | * in K[a_1, ..., a_s] given by a lex |
---|
| 27 | * Gröbner basis. |
---|
| 28 | * The code in algext.h and algext.cc is then capable of |
---|
| 29 | * computing in K[a_1, ..., a_s] / I. |
---|
[fba6f18] | 30 | */ |
---|
| 31 | |
---|
| 32 | #include "config.h" |
---|
| 33 | #include <misc/auxiliary.h> |
---|
| 34 | |
---|
| 35 | #include <omalloc/omalloc.h> |
---|
| 36 | |
---|
| 37 | #include <reporter/reporter.h> |
---|
| 38 | |
---|
| 39 | #include <coeffs/coeffs.h> |
---|
| 40 | #include <coeffs/numbers.h> |
---|
[0fb5991] | 41 | #include <coeffs/longrat.h> |
---|
[fba6f18] | 42 | |
---|
| 43 | #include <polys/monomials/ring.h> |
---|
| 44 | #include <polys/monomials/p_polys.h> |
---|
| 45 | #include <polys/simpleideals.h> |
---|
| 46 | |
---|
| 47 | #include <polys/ext_fields/algext.h> |
---|
| 48 | |
---|
[73a9ffb] | 49 | /// our type has been defined as a macro in algext.h |
---|
| 50 | /// and is accessible by 'naID' |
---|
| 51 | |
---|
[fba6f18] | 52 | /// forward declarations |
---|
| 53 | BOOLEAN naGreaterZero(number a, const coeffs cf); |
---|
| 54 | BOOLEAN naGreater(number a, number b, const coeffs cf); |
---|
| 55 | BOOLEAN naEqual(number a, number b, const coeffs cf); |
---|
| 56 | BOOLEAN naIsOne(number a, const coeffs cf); |
---|
| 57 | BOOLEAN naIsMOne(number a, const coeffs cf); |
---|
| 58 | BOOLEAN naIsZero(number a, const coeffs cf); |
---|
| 59 | number naInit(int i, const coeffs cf); |
---|
| 60 | int naInt(number &a, const coeffs cf); |
---|
| 61 | number naNeg(number a, const coeffs cf); |
---|
| 62 | number naInvers(number a, const coeffs cf); |
---|
| 63 | number naPar(int i, const coeffs cf); |
---|
| 64 | number naAdd(number a, number b, const coeffs cf); |
---|
| 65 | number naSub(number a, number b, const coeffs cf); |
---|
| 66 | number naMult(number a, number b, const coeffs cf); |
---|
| 67 | number naDiv(number a, number b, const coeffs cf); |
---|
| 68 | void naPower(number a, int exp, number *b, const coeffs cf); |
---|
| 69 | number naCopy(number a, const coeffs cf); |
---|
| 70 | void naWrite(number &a, const coeffs cf); |
---|
| 71 | number naRePart(number a, const coeffs cf); |
---|
| 72 | number naImPart(number a, const coeffs cf); |
---|
[0fb5991] | 73 | number naGetDenom(number &a, const coeffs cf); |
---|
| 74 | number naGetNumerator(number &a, const coeffs cf); |
---|
[ba2359] | 75 | number naGcd(number a, number b, const coeffs cf); |
---|
| 76 | number naLcm(number a, number b, const coeffs cf); |
---|
| 77 | int naSize(number a, const coeffs cf); |
---|
[fba6f18] | 78 | void naDelete(number *a, const coeffs cf); |
---|
| 79 | void naCoeffWrite(const coeffs cf); |
---|
[ba2359] | 80 | number naIntDiv(number a, number b, const coeffs cf); |
---|
[fba6f18] | 81 | const char * naRead(const char *s, number *a, const coeffs cf); |
---|
| 82 | static BOOLEAN naCoeffIsEqual(const coeffs cf, n_coeffType n, void * param); |
---|
| 83 | |
---|
| 84 | #ifdef LDEBUG |
---|
[ba2359] | 85 | BOOLEAN naDBTest(number a, const char *f, const int l, const coeffs cf) |
---|
[fba6f18] | 86 | { |
---|
[ba2359] | 87 | assume(getCoeffType(cf) == naID); |
---|
[f0b01f] | 88 | if (a == NULL) return TRUE; |
---|
[c28ecf] | 89 | p_Test((poly)a, naRing); |
---|
[2c7f28] | 90 | assume(p_Totaldegree((poly)a, naRing) <= p_Totaldegree(naMinpoly, naRing)); |
---|
[fba6f18] | 91 | return TRUE; |
---|
| 92 | } |
---|
| 93 | #endif |
---|
| 94 | |
---|
[4a2260e] | 95 | void heuristicReduce(poly &p, poly reducer, const coeffs cf); |
---|
| 96 | void definiteReduce(poly &p, poly reducer, const coeffs cf); |
---|
[ba2359] | 97 | |
---|
[488808e] | 98 | /* returns the bottom field in this field extension tower; if the tower |
---|
| 99 | is flat, i.e., if there is no extension, then r itself is returned; |
---|
| 100 | as a side-effect, the counter 'height' is filled with the height of |
---|
| 101 | the extension tower (in case the tower is flat, 'height' is zero) */ |
---|
| 102 | static coeffs nCoeff_bottom(const coeffs r, int &height) |
---|
| 103 | { |
---|
| 104 | assume(r != NULL); |
---|
| 105 | coeffs cf = r; |
---|
| 106 | height = 0; |
---|
| 107 | while (nCoeff_is_Extension(cf)) |
---|
| 108 | { |
---|
[6ccdd3a] | 109 | assume(cf->extRing != NULL); assume(cf->extRing->cf != NULL); |
---|
| 110 | cf = cf->extRing->cf; |
---|
[488808e] | 111 | height++; |
---|
| 112 | } |
---|
| 113 | return cf; |
---|
| 114 | } |
---|
| 115 | |
---|
[fba6f18] | 116 | BOOLEAN naIsZero(number a, const coeffs cf) |
---|
| 117 | { |
---|
| 118 | naTest(a); |
---|
| 119 | return (a == NULL); |
---|
| 120 | } |
---|
| 121 | |
---|
[c28ecf] | 122 | void naDelete(number * a, const coeffs cf) |
---|
[fba6f18] | 123 | { |
---|
[c28ecf] | 124 | if (*a == NULL) return; |
---|
| 125 | poly aAsPoly = (poly)(*a); |
---|
| 126 | p_Delete(&aAsPoly, naRing); |
---|
[fba6f18] | 127 | *a = NULL; |
---|
| 128 | } |
---|
| 129 | |
---|
[2c7f28] | 130 | BOOLEAN naEqual(number a, number b, const coeffs cf) |
---|
[fba6f18] | 131 | { |
---|
| 132 | naTest(a); naTest(b); |
---|
| 133 | |
---|
| 134 | /// simple tests |
---|
| 135 | if (a == b) return TRUE; |
---|
| 136 | if ((a == NULL) && (b != NULL)) return FALSE; |
---|
| 137 | if ((b == NULL) && (a != NULL)) return FALSE; |
---|
| 138 | |
---|
| 139 | /// deg test |
---|
| 140 | int aDeg = 0; |
---|
[2c7f28] | 141 | if (a != NULL) aDeg = p_Totaldegree((poly)a, naRing); |
---|
[fba6f18] | 142 | int bDeg = 0; |
---|
[2c7f28] | 143 | if (b != NULL) bDeg = p_Totaldegree((poly)b, naRing); |
---|
[fba6f18] | 144 | if (aDeg != bDeg) return FALSE; |
---|
| 145 | |
---|
| 146 | /// subtraction test |
---|
| 147 | number c = naSub(a, b, cf); |
---|
| 148 | BOOLEAN result = naIsZero(c, cf); |
---|
| 149 | naDelete(&c, naCoeffs); |
---|
| 150 | return result; |
---|
| 151 | } |
---|
| 152 | |
---|
[0fb5991] | 153 | number naCopy(number a, const coeffs cf) |
---|
[fba6f18] | 154 | { |
---|
| 155 | naTest(a); |
---|
[ba2359] | 156 | if (a == NULL) return NULL; |
---|
[fba6f18] | 157 | return (number)p_Copy((poly)a, naRing); |
---|
| 158 | } |
---|
| 159 | |
---|
[0fb5991] | 160 | number naGetNumerator(number &a, const coeffs cf) |
---|
| 161 | { |
---|
| 162 | return naCopy(a, cf); |
---|
| 163 | } |
---|
| 164 | |
---|
[fba6f18] | 165 | number naGetDenom(number &a, const coeffs cf) |
---|
| 166 | { |
---|
| 167 | naTest(a); |
---|
| 168 | return naInit(1, cf); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | BOOLEAN naIsOne(number a, const coeffs cf) |
---|
| 172 | { |
---|
| 173 | naTest(a); |
---|
[2c7f28] | 174 | poly aAsPoly = (poly)a; |
---|
| 175 | if (!p_IsConstant(aAsPoly, naRing)) return FALSE; |
---|
| 176 | return n_IsOne(p_GetCoeff(aAsPoly, naRing), naCoeffs); |
---|
[fba6f18] | 177 | } |
---|
| 178 | |
---|
| 179 | BOOLEAN naIsMOne(number a, const coeffs cf) |
---|
| 180 | { |
---|
| 181 | naTest(a); |
---|
[2c7f28] | 182 | poly aAsPoly = (poly)a; |
---|
| 183 | if (!p_IsConstant(aAsPoly, naRing)) return FALSE; |
---|
| 184 | return n_IsMOne(p_GetCoeff(aAsPoly, naRing), naCoeffs); |
---|
[fba6f18] | 185 | } |
---|
| 186 | |
---|
| 187 | /// this is in-place, modifies a |
---|
| 188 | number naNeg(number a, const coeffs cf) |
---|
| 189 | { |
---|
| 190 | naTest(a); |
---|
| 191 | if (a != NULL) a = (number)p_Neg((poly)a, naRing); |
---|
| 192 | return a; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | number naImPart(number a, const coeffs cf) |
---|
| 196 | { |
---|
| 197 | naTest(a); |
---|
| 198 | return NULL; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | number naInit(int i, const coeffs cf) |
---|
| 202 | { |
---|
| 203 | if (i == 0) return NULL; |
---|
| 204 | else return (number)p_ISet(i, naRing); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | int naInt(number &a, const coeffs cf) |
---|
| 208 | { |
---|
| 209 | naTest(a); |
---|
[2c7f28] | 210 | poly aAsPoly = (poly)a; |
---|
| 211 | if (!p_IsConstant(aAsPoly, naRing)) return 0; |
---|
| 212 | return n_Int(p_GetCoeff(aAsPoly, naRing), naCoeffs); |
---|
[fba6f18] | 213 | } |
---|
| 214 | |
---|
| 215 | /* TRUE iff (a != 0 and (b == 0 or deg(a) > deg(b))) */ |
---|
| 216 | BOOLEAN naGreater(number a, number b, const coeffs cf) |
---|
| 217 | { |
---|
[2c7f28] | 218 | naTest(a); naTest(b); |
---|
[fba6f18] | 219 | if (naIsZero(a, cf)) return FALSE; |
---|
| 220 | if (naIsZero(b, cf)) return TRUE; |
---|
| 221 | int aDeg = 0; |
---|
[2c7f28] | 222 | if (a != NULL) aDeg = p_Totaldegree((poly)a, naRing); |
---|
[fba6f18] | 223 | int bDeg = 0; |
---|
[2c7f28] | 224 | if (b != NULL) bDeg = p_Totaldegree((poly)b, naRing); |
---|
[fba6f18] | 225 | return (aDeg > bDeg); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /* TRUE iff a != 0 and (LC(a) > 0 or deg(a) > 0) */ |
---|
| 229 | BOOLEAN naGreaterZero(number a, const coeffs cf) |
---|
| 230 | { |
---|
| 231 | naTest(a); |
---|
| 232 | if (a == NULL) return FALSE; |
---|
| 233 | if (n_GreaterZero(p_GetCoeff((poly)a, naRing), naCoeffs)) return TRUE; |
---|
[2c7f28] | 234 | if (p_Totaldegree((poly)a, naRing) > 0) return TRUE; |
---|
[fba6f18] | 235 | return FALSE; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | void naCoeffWrite(const coeffs cf) |
---|
| 239 | { |
---|
| 240 | char *x = rRingVar(0, naRing); |
---|
| 241 | Print("// Coefficients live in the extension field K[%s]/<f(%s)>\n", x, x); |
---|
| 242 | Print("// with the minimal polynomial f(%s) = %s\n", x, |
---|
| 243 | p_String(naMinpoly, naRing)); |
---|
[6ccdd3a] | 244 | PrintS("// and K: "); n_CoeffWrite(cf->extRing->cf); |
---|
[fba6f18] | 245 | } |
---|
| 246 | |
---|
[ba2359] | 247 | number naPar(int i, const coeffs cf) |
---|
| 248 | { |
---|
| 249 | assume(i == 1); // there is only one parameter in this extension field |
---|
| 250 | poly p = p_ISet(1, naRing); // p = 1 |
---|
| 251 | p_SetExp(p, 1, 1, naRing); // p = the sole extension variable |
---|
| 252 | p_Setm(p, naRing); |
---|
| 253 | return (number)p; |
---|
| 254 | } |
---|
[fba6f18] | 255 | |
---|
[ba2359] | 256 | number naAdd(number a, number b, const coeffs cf) |
---|
[fba6f18] | 257 | { |
---|
[ba2359] | 258 | naTest(a); naTest(b); |
---|
| 259 | if (a == NULL) return naCopy(b, cf); |
---|
| 260 | if (b == NULL) return naCopy(a, cf); |
---|
| 261 | poly aPlusB = p_Add_q(p_Copy((poly)a, naRing), |
---|
| 262 | p_Copy((poly)b, naRing), naRing); |
---|
[0fb5991] | 263 | definiteReduce(aPlusB, naMinpoly, cf); |
---|
| 264 | return (number)aPlusB; |
---|
[fba6f18] | 265 | } |
---|
| 266 | |
---|
| 267 | number naSub(number a, number b, const coeffs cf) |
---|
| 268 | { |
---|
[ba2359] | 269 | naTest(a); naTest(b); |
---|
| 270 | if (b == NULL) return naCopy(a, cf); |
---|
| 271 | poly minusB = p_Neg(p_Copy((poly)b, naRing), naRing); |
---|
[0fb5991] | 272 | if (a == NULL) return (number)minusB; |
---|
[ba2359] | 273 | poly aMinusB = p_Add_q(p_Copy((poly)a, naRing), minusB, naRing); |
---|
[0fb5991] | 274 | definiteReduce(aMinusB, naMinpoly, cf); |
---|
[ba2359] | 275 | return (number)aMinusB; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | number naMult(number a, number b, const coeffs cf) |
---|
| 279 | { |
---|
| 280 | naTest(a); naTest(b); |
---|
| 281 | if (a == NULL) return NULL; |
---|
| 282 | if (b == NULL) return NULL; |
---|
| 283 | poly aTimesB = p_Mult_q(p_Copy((poly)a, naRing), |
---|
| 284 | p_Copy((poly)b, naRing), naRing); |
---|
[0fb5991] | 285 | definiteReduce(aTimesB, naMinpoly, cf); |
---|
| 286 | return (number)aTimesB; |
---|
[ba2359] | 287 | } |
---|
| 288 | |
---|
| 289 | number naDiv(number a, number b, const coeffs cf) |
---|
| 290 | { |
---|
| 291 | naTest(a); naTest(b); |
---|
| 292 | if (b == NULL) WerrorS(nDivBy0); |
---|
| 293 | if (a == NULL) return NULL; |
---|
| 294 | poly bInverse = (poly)naInvers(b, cf); |
---|
| 295 | poly aDivB = p_Mult_q(p_Copy((poly)a, naRing), bInverse, naRing); |
---|
[0fb5991] | 296 | definiteReduce(aDivB, naMinpoly, cf); |
---|
| 297 | return (number)aDivB; |
---|
[ba2359] | 298 | } |
---|
| 299 | |
---|
| 300 | /* 0^0 = 0; |
---|
| 301 | for |exp| <= 7 compute power by a simple multiplication loop; |
---|
| 302 | for |exp| >= 8 compute power along binary presentation of |exp|, e.g. |
---|
| 303 | p^13 = p^1 * p^4 * p^8, where we utilise that |
---|
[2c7f28] | 304 | p^(2^(k+1)) = p^(2^k) * p^(2^k); |
---|
[ba2359] | 305 | intermediate reduction modulo the minimal polynomial is controlled by |
---|
| 306 | the in-place method heuristicReduce(poly, poly, coeffs); see there. |
---|
| 307 | */ |
---|
| 308 | void naPower(number a, int exp, number *b, const coeffs cf) |
---|
| 309 | { |
---|
| 310 | naTest(a); |
---|
| 311 | |
---|
| 312 | /* special cases first */ |
---|
| 313 | if (a == NULL) |
---|
| 314 | { |
---|
[0fb5991] | 315 | if (exp >= 0) *b = NULL; |
---|
[ba2359] | 316 | else WerrorS(nDivBy0); |
---|
| 317 | } |
---|
[0fb5991] | 318 | else if (exp == 0) *b = naInit(1, cf); |
---|
| 319 | else if (exp == 1) *b = naCopy(a, cf); |
---|
| 320 | else if (exp == -1) *b = naInvers(a, cf); |
---|
[ba2359] | 321 | |
---|
| 322 | int expAbs = exp; if (expAbs < 0) expAbs = -expAbs; |
---|
| 323 | |
---|
[2c7f28] | 324 | /* now compute a^expAbs */ |
---|
[ba2359] | 325 | poly pow; poly aAsPoly = (poly)a; |
---|
| 326 | if (expAbs <= 7) |
---|
| 327 | { |
---|
| 328 | pow = p_Copy(aAsPoly, naRing); |
---|
| 329 | for (int i = 2; i <= expAbs; i++) |
---|
| 330 | { |
---|
| 331 | pow = p_Mult_q(pow, p_Copy(aAsPoly, naRing), naRing); |
---|
| 332 | heuristicReduce(pow, naMinpoly, cf); |
---|
| 333 | } |
---|
| 334 | definiteReduce(pow, naMinpoly, cf); |
---|
| 335 | } |
---|
| 336 | else |
---|
| 337 | { |
---|
| 338 | pow = p_ISet(1, naRing); |
---|
| 339 | poly factor = p_Copy(aAsPoly, naRing); |
---|
| 340 | while (expAbs != 0) |
---|
| 341 | { |
---|
| 342 | if (expAbs & 1) |
---|
| 343 | { |
---|
| 344 | pow = p_Mult_q(pow, p_Copy(factor, naRing), naRing); |
---|
| 345 | heuristicReduce(pow, naMinpoly, cf); |
---|
| 346 | } |
---|
| 347 | expAbs = expAbs / 2; |
---|
| 348 | if (expAbs != 0) |
---|
| 349 | { |
---|
| 350 | factor = p_Mult_q(factor, factor, naRing); |
---|
| 351 | heuristicReduce(factor, naMinpoly, cf); |
---|
| 352 | } |
---|
| 353 | } |
---|
[0fb5991] | 354 | p_Delete(&factor, naRing); |
---|
[ba2359] | 355 | definiteReduce(pow, naMinpoly, cf); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | /* invert if original exponent was negative */ |
---|
| 359 | number n = (number)pow; |
---|
| 360 | if (exp < 0) |
---|
| 361 | { |
---|
| 362 | number m = naInvers(n, cf); |
---|
| 363 | naDelete(&n, cf); |
---|
| 364 | n = m; |
---|
| 365 | } |
---|
| 366 | *b = n; |
---|
| 367 | } |
---|
| 368 | |
---|
[2c7f28] | 369 | /* may reduce p modulo the reducer by calling definiteReduce; |
---|
[ba2359] | 370 | the decision is made based on the following heuristic |
---|
| 371 | (which should also only be changed here in this method): |
---|
[4a2260e] | 372 | if (deg(p) > 10*deg(reducer) then perform reduction; |
---|
| 373 | modifies p */ |
---|
| 374 | void heuristicReduce(poly &p, poly reducer, const coeffs cf) |
---|
[ba2359] | 375 | { |
---|
| 376 | #ifdef LDEBUG |
---|
[c28ecf] | 377 | p_Test((poly)p, naRing); |
---|
| 378 | p_Test((poly)reducer, naRing); |
---|
[ba2359] | 379 | #endif |
---|
[2c7f28] | 380 | if (p_Totaldegree(p, naRing) > 10 * p_Totaldegree(reducer, naRing)) |
---|
[ba2359] | 381 | definiteReduce(p, reducer, cf); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void naWrite(number &a, const coeffs cf) |
---|
| 385 | { |
---|
| 386 | naTest(a); |
---|
| 387 | if (a == NULL) |
---|
| 388 | StringAppendS("0"); |
---|
| 389 | else |
---|
| 390 | { |
---|
| 391 | poly aAsPoly = (poly)a; |
---|
| 392 | /* basically, just write aAsPoly using p_Write, |
---|
| 393 | but use brackets around the output, if a is not |
---|
[6ccdd3a] | 394 | a constant living in naCoeffs = cf->extRing->cf */ |
---|
[fd01a8] | 395 | BOOLEAN useBrackets = !(p_IsConstant(aAsPoly, naRing)); |
---|
[ba2359] | 396 | if (useBrackets) StringAppendS("("); |
---|
[fd01a8] | 397 | p_String0(aAsPoly, naRing, naRing); |
---|
[ba2359] | 398 | if (useBrackets) StringAppendS(")"); |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | const char * naRead(const char *s, number *a, const coeffs cf) |
---|
| 403 | { |
---|
| 404 | poly aAsPoly; |
---|
| 405 | const char * result = p_Read(s, aAsPoly, naRing); |
---|
| 406 | *a = (number)aAsPoly; |
---|
| 407 | return result; |
---|
[fba6f18] | 408 | } |
---|
| 409 | |
---|
[ba2359] | 410 | /* implemented by the rule lcm(a, b) = a * b / gcd(a, b) */ |
---|
| 411 | number naLcm(number a, number b, const coeffs cf) |
---|
| 412 | { |
---|
| 413 | naTest(a); naTest(b); |
---|
| 414 | if (a == NULL) return NULL; |
---|
| 415 | if (b == NULL) return NULL; |
---|
| 416 | number theProduct = (number)p_Mult_q(p_Copy((poly)a, naRing), |
---|
| 417 | p_Copy((poly)b, naRing), naRing); |
---|
| 418 | /* note that theProduct needs not be reduced w.r.t. naMinpoly; |
---|
| 419 | but the final division will take care of the necessary reduction */ |
---|
| 420 | number theGcd = naGcd(a, b, cf); |
---|
[0fb5991] | 421 | return naDiv(theProduct, theGcd, cf); |
---|
[ba2359] | 422 | } |
---|
| 423 | |
---|
[6ccdd3a] | 424 | /* expects *param to be castable to AlgExtInfo */ |
---|
[ba2359] | 425 | static BOOLEAN naCoeffIsEqual(const coeffs cf, n_coeffType n, void * param) |
---|
| 426 | { |
---|
| 427 | if (naID != n) return FALSE; |
---|
[6ccdd3a] | 428 | AlgExtInfo *e = (AlgExtInfo *)param; |
---|
[ba2359] | 429 | /* for extension coefficient fields we expect the underlying |
---|
[2c7f28] | 430 | polynomial rings to be IDENTICAL, i.e. the SAME OBJECT; |
---|
[ba2359] | 431 | this expectation is based on the assumption that we have properly |
---|
| 432 | registered cf and perform reference counting rather than creating |
---|
| 433 | multiple copies of the same coefficient field/domain/ring */ |
---|
| 434 | return (naRing == e->r); |
---|
| 435 | /* (Note that then also the minimal ideals will necessarily be |
---|
| 436 | the same, as they are attached to the ring.) */ |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | int naSize(number a, const coeffs cf) |
---|
| 440 | { |
---|
| 441 | if (a == NULL) return -1; |
---|
| 442 | /* this has been taken from the old implementation of field extensions, |
---|
| 443 | where we computed the sum of the degree and the number of terms in |
---|
| 444 | (poly)a; so we leave it at that, for the time being; |
---|
| 445 | maybe, the number of terms alone is a better measure? */ |
---|
| 446 | poly aAsPoly = (poly)a; |
---|
| 447 | int theDegree = 0; int noOfTerms = 0; |
---|
| 448 | while (aAsPoly != NULL) |
---|
| 449 | { |
---|
| 450 | noOfTerms++; |
---|
[2c7f28] | 451 | int d = p_GetExp(aAsPoly, 1, naRing); |
---|
[ba2359] | 452 | if (d > theDegree) theDegree = d; |
---|
| 453 | pIter(aAsPoly); |
---|
| 454 | } |
---|
| 455 | return theDegree + noOfTerms; |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | /* performs polynomial division and overrides p by the remainder |
---|
[4a2260e] | 459 | of division of p by the reducer; |
---|
| 460 | modifies p */ |
---|
| 461 | void definiteReduce(poly &p, poly reducer, const coeffs cf) |
---|
[ba2359] | 462 | { |
---|
| 463 | #ifdef LDEBUG |
---|
[c28ecf] | 464 | p_Test((poly)p, naRing); |
---|
| 465 | p_Test((poly)reducer, naRing); |
---|
[ba2359] | 466 | #endif |
---|
[f0b01f] | 467 | p_PolyDiv(p, reducer, FALSE, naRing); |
---|
[ba2359] | 468 | } |
---|
| 469 | |
---|
[cfb500] | 470 | /* IMPORTANT NOTE: Since an algebraic field extension is again a field, |
---|
| 471 | the gcd of two elements is not very interesting. (It |
---|
| 472 | is actually any unit in the field, i.e., any non- |
---|
| 473 | zero element.) Note that the below method does not operate |
---|
| 474 | in this strong sense but rather computes the gcd of |
---|
| 475 | two given elements in the underlying polynomial ring. */ |
---|
[ba2359] | 476 | number naGcd(number a, number b, const coeffs cf) |
---|
| 477 | { |
---|
| 478 | naTest(a); naTest(b); |
---|
| 479 | if ((a == NULL) && (b == NULL)) WerrorS(nDivBy0); |
---|
| 480 | return (number)p_Gcd((poly)a, (poly)b, naRing); |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | number naInvers(number a, const coeffs cf) |
---|
| 484 | { |
---|
| 485 | naTest(a); |
---|
| 486 | if (a == NULL) WerrorS(nDivBy0); |
---|
[c28ecf] | 487 | poly aFactor = NULL; poly mFactor = NULL; |
---|
[0fb5991] | 488 | poly theGcd = p_ExtGcd((poly)a, aFactor, naMinpoly, mFactor, naRing); |
---|
[4a2260e] | 489 | naTest((number)theGcd); naTest((number)aFactor); naTest((number)mFactor); |
---|
[c28ecf] | 490 | /* the gcd must be 1 since naMinpoly is irreducible and a != NULL: */ |
---|
[0fb5991] | 491 | assume(naIsOne((number)theGcd, cf)); |
---|
| 492 | p_Delete(&theGcd, naRing); |
---|
[f0b01f] | 493 | p_Delete(&mFactor, naRing); |
---|
| 494 | return (number)(aFactor); |
---|
[ba2359] | 495 | } |
---|
| 496 | |
---|
| 497 | /* assumes that src = Q, dst = Q(a) */ |
---|
| 498 | number naMap00(number a, const coeffs src, const coeffs dst) |
---|
| 499 | { |
---|
[2c7f28] | 500 | if (n_IsZero(a, src)) return NULL; |
---|
[6ccdd3a] | 501 | assume(src == dst->extRing->cf); |
---|
| 502 | poly result = p_One(dst->extRing); |
---|
| 503 | p_SetCoeff(result, naCopy(a, src), dst->extRing); |
---|
[ba2359] | 504 | return (number)result; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | /* assumes that src = Z/p, dst = Q(a) */ |
---|
| 508 | number naMapP0(number a, const coeffs src, const coeffs dst) |
---|
| 509 | { |
---|
[2c7f28] | 510 | if (n_IsZero(a, src)) return NULL; |
---|
[ba2359] | 511 | /* mapping via intermediate int: */ |
---|
| 512 | int n = n_Int(a, src); |
---|
[6ccdd3a] | 513 | number q = n_Init(n, dst->extRing->cf); |
---|
| 514 | poly result = p_One(dst->extRing); |
---|
| 515 | p_SetCoeff(result, q, dst->extRing); |
---|
[ba2359] | 516 | return (number)result; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | /* assumes that either src = Q(a), dst = Q(a), or |
---|
[2c7f28] | 520 | src = Z/p(a), dst = Z/p(a) */ |
---|
[ba2359] | 521 | number naCopyMap(number a, const coeffs src, const coeffs dst) |
---|
| 522 | { |
---|
| 523 | return naCopy(a, dst); |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | /* assumes that src = Q, dst = Z/p(a) */ |
---|
| 527 | number naMap0P(number a, const coeffs src, const coeffs dst) |
---|
| 528 | { |
---|
[2c7f28] | 529 | if (n_IsZero(a, src)) return NULL; |
---|
[6ccdd3a] | 530 | int p = rChar(dst->extRing); |
---|
[ba2359] | 531 | int n = nlModP(a, p, src); |
---|
[6ccdd3a] | 532 | number q = n_Init(n, dst->extRing->cf); |
---|
| 533 | poly result = p_One(dst->extRing); |
---|
| 534 | p_SetCoeff(result, q, dst->extRing); |
---|
[ba2359] | 535 | return (number)result; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | /* assumes that src = Z/p, dst = Z/p(a) */ |
---|
| 539 | number naMapPP(number a, const coeffs src, const coeffs dst) |
---|
| 540 | { |
---|
[2c7f28] | 541 | if (n_IsZero(a, src)) return NULL; |
---|
[6ccdd3a] | 542 | assume(src == dst->extRing->cf); |
---|
| 543 | poly result = p_One(dst->extRing); |
---|
| 544 | p_SetCoeff(result, naCopy(a, src), dst->extRing); |
---|
[ba2359] | 545 | return (number)result; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | /* assumes that src = Z/u, dst = Z/p(a), where u != p */ |
---|
| 549 | number naMapUP(number a, const coeffs src, const coeffs dst) |
---|
| 550 | { |
---|
[2c7f28] | 551 | if (n_IsZero(a, src)) return NULL; |
---|
[ba2359] | 552 | /* mapping via intermediate int: */ |
---|
| 553 | int n = n_Int(a, src); |
---|
[6ccdd3a] | 554 | number q = n_Init(n, dst->extRing->cf); |
---|
| 555 | poly result = p_One(dst->extRing); |
---|
| 556 | p_SetCoeff(result, q, dst->extRing); |
---|
[ba2359] | 557 | return (number)result; |
---|
| 558 | } |
---|
| 559 | |
---|
[0654122] | 560 | nMapFunc naSetMap(const coeffs src, const coeffs dst) |
---|
[ba2359] | 561 | { |
---|
[488808e] | 562 | /* dst is expected to be an algebraic field extension */ |
---|
[2c7f28] | 563 | assume(getCoeffType(dst) == naID); |
---|
[141342] | 564 | |
---|
[488808e] | 565 | int h = 0; /* the height of the extension tower given by dst */ |
---|
| 566 | coeffs bDst = nCoeff_bottom(dst, h); /* the bottom field in the tower dst */ |
---|
| 567 | |
---|
| 568 | /* for the time being, we only provide maps if h = 1 and if b is Q or |
---|
| 569 | some field Z/pZ: */ |
---|
| 570 | if (h != 1) return NULL; |
---|
| 571 | if ((!nCoeff_is_Zp(bDst)) && (!nCoeff_is_Q(bDst))) return NULL; |
---|
[ba2359] | 572 | |
---|
[488808e] | 573 | if (nCoeff_is_Q(src) && nCoeff_is_Q(bDst)) |
---|
[ba2359] | 574 | return naMap00; /// Q --> Q(a) |
---|
| 575 | |
---|
[488808e] | 576 | if (nCoeff_is_Zp(src) && nCoeff_is_Q(bDst)) |
---|
[ba2359] | 577 | return naMapP0; /// Z/p --> Q(a) |
---|
| 578 | |
---|
[488808e] | 579 | if (nCoeff_is_Q(src) && nCoeff_is_Zp(bDst)) |
---|
| 580 | return naMap0P; /// Q --> Z/p(a) |
---|
| 581 | |
---|
| 582 | if (nCoeff_is_Zp(src) && nCoeff_is_Zp(bDst)) |
---|
| 583 | { |
---|
| 584 | if (src->ch == dst->ch) return naMapPP; /// Z/p --> Z/p(a) |
---|
| 585 | else return naMapUP; /// Z/u --> Z/p(a) |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | coeffs bSrc = nCoeff_bottom(src, h); /* the bottom field in the tower src */ |
---|
| 589 | if (h != 1) return NULL; |
---|
| 590 | if ((!nCoeff_is_Zp(bSrc)) && (!nCoeff_is_Q(bSrc))) return NULL; |
---|
| 591 | |
---|
| 592 | if (nCoeff_is_Q(bSrc) && nCoeff_is_Q(bDst)) |
---|
[ba2359] | 593 | { |
---|
[2c7f28] | 594 | if (strcmp(rRingVar(0, src->extRing), |
---|
| 595 | rRingVar(0, dst->extRing)) == 0) |
---|
[ba2359] | 596 | return naCopyMap; /// Q(a) --> Q(a) |
---|
| 597 | else |
---|
| 598 | return NULL; /// Q(b) --> Q(a) |
---|
| 599 | } |
---|
| 600 | |
---|
[488808e] | 601 | if (nCoeff_is_Zp(bSrc) && nCoeff_is_Zp(bDst)) |
---|
[ba2359] | 602 | { |
---|
[6ccdd3a] | 603 | if (strcmp(rParameter(src->extRing)[0], |
---|
| 604 | rParameter(dst->extRing)[0]) == 0) |
---|
[ba2359] | 605 | return naCopyMap; /// Z/p(a) --> Z/p(a) |
---|
| 606 | else |
---|
| 607 | return NULL; /// Z/p(b) --> Z/p(a) |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | return NULL; /// default |
---|
| 611 | } |
---|
[fba6f18] | 612 | |
---|
[ba2359] | 613 | BOOLEAN naInitChar(coeffs cf, void * infoStruct) |
---|
[2c7f28] | 614 | { |
---|
[6ccdd3a] | 615 | AlgExtInfo *e = (AlgExtInfo *)infoStruct; |
---|
| 616 | /// first check whether cf->extRing != NULL and delete old ring??? |
---|
| 617 | cf->extRing = e->r; |
---|
| 618 | cf->extRing->minideal = e->i; |
---|
| 619 | |
---|
| 620 | assume(cf->extRing != NULL); // extRing; |
---|
| 621 | assume((cf->extRing->minideal != NULL) && // minideal has one |
---|
| 622 | (IDELEMS(cf->extRing->minideal) != 0) && // non-zero generator |
---|
| 623 | (cf->extRing->minideal->m[0] != NULL) ); // at m[0]; |
---|
| 624 | assume(cf->extRing->cf != NULL); // extRing->cf; |
---|
[fba6f18] | 625 | assume(getCoeffType(cf) == naID); // coeff type; |
---|
| 626 | |
---|
[73a9ffb] | 627 | /* propagate characteristic up so that it becomes |
---|
| 628 | directly accessible in cf: */ |
---|
[6ccdd3a] | 629 | cf->ch = cf->extRing->cf->ch; |
---|
[141342] | 630 | |
---|
[fba6f18] | 631 | #ifdef LDEBUG |
---|
[c28ecf] | 632 | p_Test((poly)naMinpoly, naRing); |
---|
[fba6f18] | 633 | #endif |
---|
| 634 | |
---|
| 635 | cf->cfGreaterZero = naGreaterZero; |
---|
| 636 | cf->cfGreater = naGreater; |
---|
| 637 | cf->cfEqual = naEqual; |
---|
| 638 | cf->cfIsZero = naIsZero; |
---|
| 639 | cf->cfIsOne = naIsOne; |
---|
| 640 | cf->cfIsMOne = naIsMOne; |
---|
| 641 | cf->cfInit = naInit; |
---|
| 642 | cf->cfInt = naInt; |
---|
| 643 | cf->cfNeg = naNeg; |
---|
[ba2359] | 644 | cf->cfPar = naPar; |
---|
| 645 | cf->cfAdd = naAdd; |
---|
[fba6f18] | 646 | cf->cfSub = naSub; |
---|
[ba2359] | 647 | cf->cfMult = naMult; |
---|
| 648 | cf->cfDiv = naDiv; |
---|
| 649 | cf->cfExactDiv = naDiv; |
---|
| 650 | cf->cfPower = naPower; |
---|
| 651 | cf->cfCopy = naCopy; |
---|
| 652 | cf->cfWrite = naWrite; |
---|
| 653 | cf->cfRead = naRead; |
---|
[fba6f18] | 654 | cf->cfDelete = naDelete; |
---|
| 655 | cf->cfSetMap = naSetMap; |
---|
| 656 | cf->cfGetDenom = naGetDenom; |
---|
[0fb5991] | 657 | cf->cfGetNumerator = naGetNumerator; |
---|
[ba2359] | 658 | cf->cfRePart = naCopy; |
---|
[fba6f18] | 659 | cf->cfImPart = naImPart; |
---|
| 660 | cf->cfCoeffWrite = naCoeffWrite; |
---|
| 661 | cf->cfDBTest = naDBTest; |
---|
[ba2359] | 662 | cf->cfGcd = naGcd; |
---|
| 663 | cf->cfLcm = naLcm; |
---|
| 664 | cf->cfSize = naSize; |
---|
| 665 | cf->nCoeffIsEqual = naCoeffIsEqual; |
---|
| 666 | cf->cfInvers = naInvers; |
---|
| 667 | cf->cfIntDiv = naDiv; |
---|
[fba6f18] | 668 | |
---|
| 669 | return FALSE; |
---|
| 670 | } |
---|