[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT - all basic methods to manipulate ideals |
---|
| 6 | */ |
---|
| 7 | |
---|
[2ad10e9] | 8 | |
---|
[35aab3] | 9 | /* includes */ |
---|
[2ad10e9] | 10 | #include "config.h" |
---|
| 11 | #include <misc/auxiliary.h> |
---|
[af598e] | 12 | |
---|
[f71e8c5] | 13 | #include <omalloc/omalloc.h> |
---|
[af598e] | 14 | |
---|
| 15 | #include <misc/options.h> |
---|
[f5c2d02] | 16 | #include <misc/intvec.h> |
---|
[af598e] | 17 | |
---|
[7829fb] | 18 | // #include <coeffs/longrat.h> |
---|
| 19 | #include "matpol.h" |
---|
| 20 | |
---|
[af598e] | 21 | #include "monomials/p_polys.h" |
---|
| 22 | #include "weight.h" |
---|
[a2d993] | 23 | #include "sbuckets.h" |
---|
[529fa4] | 24 | #include "clapsing.h" |
---|
[35aab3] | 25 | |
---|
[7829fb] | 26 | #include "simpleideals.h" |
---|
| 27 | |
---|
[fba6f18] | 28 | omBin sip_sideal_bin = omGetSpecBin(sizeof(sip_sideal)); |
---|
[9765f3] | 29 | |
---|
[2f5547] | 30 | static poly * idpower; |
---|
| 31 | /*collects the monomials in makemonoms, must be allocated befor*/ |
---|
| 32 | static int idpowerpoint; |
---|
| 33 | /*index of the actual monomial in idpower*/ |
---|
| 34 | static poly * givenideal; |
---|
| 35 | /*the ideal from which a power is computed*/ |
---|
| 36 | |
---|
[35aab3] | 37 | /*2 |
---|
| 38 | * initialise an ideal |
---|
| 39 | */ |
---|
| 40 | ideal idInit(int idsize, int rank) |
---|
| 41 | { |
---|
| 42 | /*- initialise an ideal -*/ |
---|
| 43 | ideal hh = (ideal )omAllocBin(sip_sideal_bin); |
---|
| 44 | hh->nrows = 1; |
---|
| 45 | hh->rank = rank; |
---|
| 46 | IDELEMS(hh) = idsize; |
---|
| 47 | if (idsize>0) |
---|
| 48 | { |
---|
| 49 | hh->m = (poly *)omAlloc0(idsize*sizeof(poly)); |
---|
| 50 | } |
---|
| 51 | else |
---|
| 52 | hh->m=NULL; |
---|
| 53 | return hh; |
---|
| 54 | } |
---|
| 55 | |
---|
[e9c3b2] | 56 | #ifdef PDEBUG |
---|
[e070895] | 57 | // this is only for outputting an ideal within the debugger |
---|
[645a19] | 58 | void idShow(const ideal id, const ring lmRing, const ring tailRing, const int debugPrint) |
---|
[35aab3] | 59 | { |
---|
[645a19] | 60 | assume( debugPrint >= 0 ); |
---|
[bead81] | 61 | |
---|
[52e2f6] | 62 | if( id == NULL ) |
---|
[f44fb9] | 63 | PrintS("(NULL)"); |
---|
[52e2f6] | 64 | else |
---|
[35aab3] | 65 | { |
---|
[6867f5] | 66 | Print("Module of rank %ld,real rank %ld and %d generators.\n", |
---|
[f71e8c5] | 67 | id->rank,id_RankFreeModule(id, lmRing, tailRing),IDELEMS(id)); |
---|
[645a19] | 68 | |
---|
| 69 | int j = (id->ncols*id->nrows) - 1; |
---|
| 70 | while ((j > 0) && (id->m[j]==NULL)) j--; |
---|
| 71 | for (int i = 0; i <= j; i++) |
---|
[35aab3] | 72 | { |
---|
[645a19] | 73 | Print("generator %d: ",i); p_DebugPrint(id->m[i], lmRing, tailRing, debugPrint); |
---|
[35aab3] | 74 | } |
---|
| 75 | } |
---|
| 76 | } |
---|
[e070895] | 77 | #endif |
---|
[35aab3] | 78 | |
---|
[dd5534] | 79 | /* index of generator with leading term in ground ring (if any); |
---|
| 80 | otherwise -1 */ |
---|
[f71e8c5] | 81 | int id_PosConstant(ideal id, const ring r) |
---|
[dd5534] | 82 | { |
---|
| 83 | int k; |
---|
| 84 | for (k = IDELEMS(id)-1; k>=0; k--) |
---|
| 85 | { |
---|
[f71e8c5] | 86 | if (p_LmIsConstantComp(id->m[k], r) == TRUE) |
---|
[dd5534] | 87 | return k; |
---|
| 88 | } |
---|
| 89 | return -1; |
---|
| 90 | } |
---|
| 91 | |
---|
[35aab3] | 92 | /*2 |
---|
| 93 | * initialise the maximal ideal (at 0) |
---|
| 94 | */ |
---|
[f71e8c5] | 95 | ideal id_MaxIdeal (const ring r) |
---|
[35aab3] | 96 | { |
---|
| 97 | int l; |
---|
| 98 | ideal hh=NULL; |
---|
| 99 | |
---|
[f71e8c5] | 100 | hh=idInit(rVar(r),1); |
---|
| 101 | for (l=0; l<rVar(r); l++) |
---|
[35aab3] | 102 | { |
---|
[f71e8c5] | 103 | hh->m[l] = p_One(r); |
---|
| 104 | p_SetExp(hh->m[l],l+1,1,r); |
---|
| 105 | p_Setm(hh->m[l],r); |
---|
[35aab3] | 106 | } |
---|
| 107 | return hh; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /*2 |
---|
| 111 | * deletes an ideal/matrix |
---|
| 112 | */ |
---|
| 113 | void id_Delete (ideal * h, ring r) |
---|
| 114 | { |
---|
| 115 | int j,elems; |
---|
| 116 | if (*h == NULL) |
---|
| 117 | return; |
---|
| 118 | elems=j=(*h)->nrows*(*h)->ncols; |
---|
| 119 | if (j>0) |
---|
| 120 | { |
---|
| 121 | do |
---|
| 122 | { |
---|
| 123 | p_Delete(&((*h)->m[--j]), r); |
---|
| 124 | } |
---|
| 125 | while (j>0); |
---|
| 126 | omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems); |
---|
| 127 | } |
---|
| 128 | omFreeBin((ADDRESS)*h, sip_sideal_bin); |
---|
| 129 | *h=NULL; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | /*2 |
---|
| 134 | * Shallowdeletes an ideal/matrix |
---|
| 135 | */ |
---|
| 136 | void id_ShallowDelete (ideal *h, ring r) |
---|
| 137 | { |
---|
| 138 | int j,elems; |
---|
| 139 | if (*h == NULL) |
---|
| 140 | return; |
---|
| 141 | elems=j=(*h)->nrows*(*h)->ncols; |
---|
| 142 | if (j>0) |
---|
| 143 | { |
---|
| 144 | do |
---|
| 145 | { |
---|
| 146 | p_ShallowDelete(&((*h)->m[--j]), r); |
---|
| 147 | } |
---|
| 148 | while (j>0); |
---|
| 149 | omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems); |
---|
| 150 | } |
---|
| 151 | omFreeBin((ADDRESS)*h, sip_sideal_bin); |
---|
| 152 | *h=NULL; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /*2 |
---|
| 156 | *gives an ideal the minimal possible size |
---|
| 157 | */ |
---|
| 158 | void idSkipZeroes (ideal ide) |
---|
| 159 | { |
---|
| 160 | int k; |
---|
| 161 | int j = -1; |
---|
| 162 | BOOLEAN change=FALSE; |
---|
| 163 | for (k=0; k<IDELEMS(ide); k++) |
---|
| 164 | { |
---|
| 165 | if (ide->m[k] != NULL) |
---|
| 166 | { |
---|
| 167 | j++; |
---|
| 168 | if (change) |
---|
| 169 | { |
---|
| 170 | ide->m[j] = ide->m[k]; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | else |
---|
| 174 | { |
---|
| 175 | change=TRUE; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | if (change) |
---|
| 179 | { |
---|
| 180 | if (j == -1) |
---|
| 181 | j = 0; |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | for (k=j+1; k<IDELEMS(ide); k++) |
---|
| 185 | ide->m[k] = NULL; |
---|
| 186 | } |
---|
| 187 | pEnlargeSet(&(ide->m),IDELEMS(ide),j+1-IDELEMS(ide)); |
---|
| 188 | IDELEMS(ide) = j+1; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
[71955d] | 192 | int idElem(const ideal F) |
---|
| 193 | { |
---|
| 194 | int i=0,j=IDELEMS(F)-1; |
---|
| 195 | |
---|
| 196 | while(j>=0) |
---|
| 197 | { |
---|
| 198 | if ((F->m)[j]!=NULL) i++; |
---|
| 199 | j--; |
---|
| 200 | } |
---|
| 201 | return i; |
---|
| 202 | } |
---|
| 203 | |
---|
[2b3caae] | 204 | /*2 |
---|
| 205 | * copies the first k (>= 1) entries of the given ideal |
---|
| 206 | * and returns these as a new ideal |
---|
| 207 | * (Note that the copied polynomials may be zero.) |
---|
| 208 | */ |
---|
[f71e8c5] | 209 | ideal id_CopyFirstK (const ideal ide, const int k,const ring r) |
---|
[2b3caae] | 210 | { |
---|
| 211 | ideal newI = idInit(k, 0); |
---|
| 212 | for (int i = 0; i < k; i++) |
---|
[f71e8c5] | 213 | newI->m[i] = p_Copy(ide->m[i],r); |
---|
[2b3caae] | 214 | return newI; |
---|
| 215 | } |
---|
| 216 | |
---|
[35aab3] | 217 | /*2 |
---|
| 218 | * ideal id = (id[i]) |
---|
| 219 | * result is leadcoeff(id[i]) = 1 |
---|
| 220 | */ |
---|
[9aa29b] | 221 | void id_Norm(ideal id, const ring r) |
---|
[35aab3] | 222 | { |
---|
[699567] | 223 | for (int i=IDELEMS(id)-1; i>=0; i--) |
---|
[35aab3] | 224 | { |
---|
| 225 | if (id->m[i] != NULL) |
---|
| 226 | { |
---|
[9aa29b] | 227 | p_Norm(id->m[i],r); |
---|
[35aab3] | 228 | } |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /*2 |
---|
[dd5534] | 233 | * ideal id = (id[i]), c any unit |
---|
[35aab3] | 234 | * if id[i] = c*id[j] then id[j] is deleted for j > i |
---|
| 235 | */ |
---|
[f5c2d02] | 236 | void id_DelMultiples(ideal id, const ring r) |
---|
[35aab3] | 237 | { |
---|
[699567] | 238 | int i, j; |
---|
| 239 | int k = IDELEMS(id)-1; |
---|
| 240 | for (i=k; i>=0; i--) |
---|
[35aab3] | 241 | { |
---|
| 242 | if (id->m[i]!=NULL) |
---|
| 243 | { |
---|
[699567] | 244 | for (j=k; j>i; j--) |
---|
[35aab3] | 245 | { |
---|
[dd5534] | 246 | if (id->m[j]!=NULL) |
---|
[35aab3] | 247 | { |
---|
[dd5534] | 248 | #ifdef HAVE_RINGS |
---|
[f5c2d02] | 249 | if (rField_is_Ring(r)) |
---|
[dd5534] | 250 | { |
---|
| 251 | /* if id[j] = c*id[i] then delete id[j]. |
---|
| 252 | In the below cases of a ground field, we |
---|
| 253 | check whether id[i] = c*id[j] and, if so, |
---|
| 254 | delete id[j] for historical reasons (so |
---|
| 255 | that previous output does not change) */ |
---|
[f5c2d02] | 256 | if (p_ComparePolys(id->m[j], id->m[i],r)) p_Delete(&id->m[j],r); |
---|
[dd5534] | 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
[f5c2d02] | 260 | if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r); |
---|
[dd5534] | 261 | } |
---|
| 262 | #else |
---|
[f5c2d02] | 263 | if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r); |
---|
[3d0808] | 264 | #endif |
---|
[35aab3] | 265 | } |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /*2 |
---|
| 272 | * ideal id = (id[i]) |
---|
| 273 | * if id[i] = id[j] then id[j] is deleted for j > i |
---|
| 274 | */ |
---|
[4a08e7] | 275 | void id_DelEquals(ideal id, const ring r) |
---|
[35aab3] | 276 | { |
---|
[7ac29f] | 277 | int i, j; |
---|
| 278 | int k = IDELEMS(id)-1; |
---|
| 279 | for (i=k; i>=0; i--) |
---|
[35aab3] | 280 | { |
---|
[7ac29f] | 281 | if (id->m[i]!=NULL) |
---|
[35aab3] | 282 | { |
---|
[7ac29f] | 283 | for (j=k; j>i; j--) |
---|
[35aab3] | 284 | { |
---|
[7ac29f] | 285 | if ((id->m[j]!=NULL) |
---|
[4a08e7] | 286 | && (p_EqualPolys(id->m[i], id->m[j],r))) |
---|
[7ac29f] | 287 | { |
---|
[4a08e7] | 288 | p_Delete(&id->m[j],r); |
---|
[7ac29f] | 289 | } |
---|
[35aab3] | 290 | } |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | // |
---|
[a8b44d] | 296 | // Delete id[j], if Lm(j) == Lm(i) and both LC(j), LC(i) are units and j > i |
---|
[35aab3] | 297 | // |
---|
[119853] | 298 | void id_DelLmEquals(ideal id, const ring r) |
---|
[35aab3] | 299 | { |
---|
[7ac29f] | 300 | int i, j; |
---|
| 301 | int k = IDELEMS(id)-1; |
---|
| 302 | for (i=k; i>=0; i--) |
---|
[35aab3] | 303 | { |
---|
[73df93] | 304 | if (id->m[i] != NULL) |
---|
[35aab3] | 305 | { |
---|
[7ac29f] | 306 | for (j=k; j>i; j--) |
---|
[35aab3] | 307 | { |
---|
[7ac29f] | 308 | if ((id->m[j] != NULL) |
---|
[119853] | 309 | && p_LmEqual(id->m[i], id->m[j],r) |
---|
[a8b44d] | 310 | #ifdef HAVE_RINGS |
---|
[c9c118] | 311 | && n_IsUnit(pGetCoeff(id->m[i]),r->cf) && n_IsUnit(pGetCoeff(id->m[j]),r->cf) |
---|
[a8b44d] | 312 | #endif |
---|
| 313 | ) |
---|
[35aab3] | 314 | { |
---|
[119853] | 315 | p_Delete(&id->m[j],r); |
---|
[35aab3] | 316 | } |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[a8b44d] | 322 | // |
---|
| 323 | // delete id[j], if LT(j) == coeff*mon*LT(i) and vice versa, i.e., |
---|
| 324 | // delete id[i], if LT(i) == coeff*mon*LT(j) |
---|
| 325 | // |
---|
[3d0808] | 326 | void id_DelDiv(ideal id, const ring r) |
---|
[35aab3] | 327 | { |
---|
[7ac29f] | 328 | int i, j; |
---|
| 329 | int k = IDELEMS(id)-1; |
---|
| 330 | for (i=k; i>=0; i--) |
---|
[35aab3] | 331 | { |
---|
[73df93] | 332 | if (id->m[i] != NULL) |
---|
[35aab3] | 333 | { |
---|
[7ac29f] | 334 | for (j=k; j>i; j--) |
---|
[35aab3] | 335 | { |
---|
[7ac29f] | 336 | if (id->m[j]!=NULL) |
---|
[35aab3] | 337 | { |
---|
[a8b44d] | 338 | #ifdef HAVE_RINGS |
---|
[3d0808] | 339 | if (rField_is_Ring(r)) |
---|
[a8b44d] | 340 | { |
---|
[3d0808] | 341 | if (p_DivisibleByRingCase(id->m[i], id->m[j],r)) |
---|
[a8b44d] | 342 | { |
---|
[3d0808] | 343 | p_Delete(&id->m[j],r); |
---|
| 344 | } |
---|
| 345 | else if (p_DivisibleByRingCase(id->m[j], id->m[i],r)) |
---|
| 346 | { |
---|
| 347 | p_Delete(&id->m[i],r); |
---|
| 348 | break; |
---|
[a8b44d] | 349 | } |
---|
| 350 | } |
---|
| 351 | else |
---|
| 352 | { |
---|
| 353 | #endif |
---|
| 354 | /* the case of a ground field: */ |
---|
[3d0808] | 355 | if (p_DivisibleBy(id->m[i], id->m[j],r)) |
---|
[7ac29f] | 356 | { |
---|
[3d0808] | 357 | p_Delete(&id->m[j],r); |
---|
[7ac29f] | 358 | } |
---|
[3d0808] | 359 | else if (p_DivisibleBy(id->m[j], id->m[i],r)) |
---|
[7ac29f] | 360 | { |
---|
[3d0808] | 361 | p_Delete(&id->m[i],r); |
---|
[7ac29f] | 362 | break; |
---|
| 363 | } |
---|
[a8b44d] | 364 | #ifdef HAVE_RINGS |
---|
| 365 | } |
---|
[3d0808] | 366 | #endif |
---|
[35aab3] | 367 | } |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | /*2 |
---|
| 374 | *test if the ideal has only constant polynomials |
---|
| 375 | */ |
---|
[2e7dee] | 376 | BOOLEAN id_IsConstant(ideal id, const ring r) |
---|
[35aab3] | 377 | { |
---|
| 378 | int k; |
---|
| 379 | for (k = IDELEMS(id)-1; k>=0; k--) |
---|
| 380 | { |
---|
[6f3273] | 381 | if (!p_IsConstantPoly(id->m[k],r)) |
---|
[35aab3] | 382 | return FALSE; |
---|
| 383 | } |
---|
| 384 | return TRUE; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | /*2 |
---|
| 388 | * copy an ideal |
---|
| 389 | */ |
---|
[2e7dee] | 390 | ideal id_Copy(ideal h1, const ring r) |
---|
[d523f3] | 391 | { |
---|
| 392 | int i; |
---|
| 393 | ideal h2; |
---|
| 394 | |
---|
| 395 | //#ifdef TEST |
---|
| 396 | if (h1 == NULL) |
---|
| 397 | { |
---|
| 398 | h2=idInit(1,1); |
---|
| 399 | } |
---|
| 400 | else |
---|
| 401 | //#endif |
---|
| 402 | { |
---|
| 403 | h2=idInit(IDELEMS(h1),h1->rank); |
---|
| 404 | for (i=IDELEMS(h1)-1; i>=0; i--) |
---|
| 405 | h2->m[i] = p_Copy(h1->m[i],r); |
---|
| 406 | } |
---|
| 407 | return h2; |
---|
| 408 | } |
---|
[35aab3] | 409 | |
---|
| 410 | #ifdef PDEBUG |
---|
[91a72f] | 411 | void id_DBTest(ideal h1, int level, const char *f,const int l, const ring r) |
---|
[35aab3] | 412 | { |
---|
| 413 | int i; |
---|
| 414 | |
---|
| 415 | if (h1 != NULL) |
---|
| 416 | { |
---|
| 417 | // assume(IDELEMS(h1) > 0); for ideal/module, does not apply to matrix |
---|
| 418 | omCheckAddrSize(h1,sizeof(*h1)); |
---|
| 419 | omdebugAddrSize(h1->m,h1->ncols*h1->nrows*sizeof(poly)); |
---|
| 420 | /* to be able to test matrices: */ |
---|
| 421 | for (i=(h1->ncols*h1->nrows)-1; i>=0; i--) |
---|
[91a72f] | 422 | _p_Test(h1->m[i], r, level); |
---|
| 423 | int new_rk=id_RankFreeModule(h1,r); |
---|
[35aab3] | 424 | if(new_rk > h1->rank) |
---|
| 425 | { |
---|
| 426 | dReportError("wrong rank %d (should be %d) in %s:%d\n", |
---|
| 427 | h1->rank, new_rk, f,l); |
---|
| 428 | omPrintAddrInfo(stderr, h1, " for ideal"); |
---|
| 429 | h1->rank=new_rk; |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | #endif |
---|
| 434 | |
---|
[599813] | 435 | ///3 for idSort: compare a and b revlex inclusive module comp. |
---|
[2e4757c] | 436 | static int p_Comp_RevLex(poly a, poly b,BOOLEAN nolex, const ring R) |
---|
[35aab3] | 437 | { |
---|
| 438 | if (b==NULL) return 1; |
---|
| 439 | if (a==NULL) return -1; |
---|
| 440 | |
---|
[3d0808] | 441 | if (nolex) |
---|
[2c872b] | 442 | { |
---|
[2e4757c] | 443 | int r=p_LmCmp(a,b,R); |
---|
[2c872b] | 444 | if (r!=0) return r; |
---|
[2e4757c] | 445 | number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf); |
---|
| 446 | r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */ |
---|
| 447 | n_Delete(&h, R->cf); |
---|
[2c872b] | 448 | return r; |
---|
| 449 | } |
---|
[2e4757c] | 450 | int l=rVar(R); |
---|
| 451 | while ((l>0) && (p_GetExp(a,l,R)==p_GetExp(b,l,R))) l--; |
---|
[35aab3] | 452 | if (l==0) |
---|
| 453 | { |
---|
[2e4757c] | 454 | if (p_GetComp(a,R)==p_GetComp(b,R)) |
---|
[2c872b] | 455 | { |
---|
[2e4757c] | 456 | number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf); |
---|
| 457 | int r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */ |
---|
| 458 | n_Delete(&h,R->cf); |
---|
[2c872b] | 459 | return r; |
---|
| 460 | } |
---|
[2e4757c] | 461 | if (p_GetComp(a,R)>p_GetComp(b,R)) return 1; |
---|
[35aab3] | 462 | } |
---|
[2e4757c] | 463 | else if (p_GetExp(a,l,R)>p_GetExp(b,l,R)) |
---|
[35aab3] | 464 | return 1; |
---|
| 465 | return -1; |
---|
| 466 | } |
---|
| 467 | |
---|
[599813] | 468 | // sorts the ideal w.r.t. the actual ringordering |
---|
| 469 | // uses lex-ordering when nolex = FALSE |
---|
| 470 | intvec *id_Sort(const ideal id, const BOOLEAN nolex, const ring r) |
---|
[35aab3] | 471 | { |
---|
| 472 | intvec * result = new intvec(IDELEMS(id)); |
---|
[18dab28] | 473 | int i, j, actpos=0, newpos; |
---|
[35aab3] | 474 | int diff, olddiff, lastcomp, newcomp; |
---|
| 475 | BOOLEAN notFound; |
---|
| 476 | |
---|
| 477 | for (i=0;i<IDELEMS(id);i++) |
---|
| 478 | { |
---|
| 479 | if (id->m[i]!=NULL) |
---|
| 480 | { |
---|
| 481 | notFound = TRUE; |
---|
| 482 | newpos = actpos / 2; |
---|
| 483 | diff = (actpos+1) / 2; |
---|
| 484 | diff = (diff+1) / 2; |
---|
[91a72f] | 485 | lastcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r); |
---|
[35aab3] | 486 | if (lastcomp<0) |
---|
| 487 | { |
---|
| 488 | newpos -= diff; |
---|
| 489 | } |
---|
| 490 | else if (lastcomp>0) |
---|
| 491 | { |
---|
| 492 | newpos += diff; |
---|
| 493 | } |
---|
| 494 | else |
---|
| 495 | { |
---|
| 496 | notFound = FALSE; |
---|
| 497 | } |
---|
| 498 | //while ((newpos>=0) && (newpos<actpos) && (notFound)) |
---|
| 499 | while (notFound && (newpos>=0) && (newpos<actpos)) |
---|
| 500 | { |
---|
[91a72f] | 501 | newcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r); |
---|
[35aab3] | 502 | olddiff = diff; |
---|
| 503 | if (diff>1) |
---|
| 504 | { |
---|
| 505 | diff = (diff+1) / 2; |
---|
| 506 | if ((newcomp==1) |
---|
| 507 | && (actpos-newpos>1) |
---|
| 508 | && (diff>1) |
---|
| 509 | && (newpos+diff>=actpos)) |
---|
| 510 | { |
---|
| 511 | diff = actpos-newpos-1; |
---|
| 512 | } |
---|
| 513 | else if ((newcomp==-1) |
---|
| 514 | && (diff>1) |
---|
| 515 | && (newpos<diff)) |
---|
| 516 | { |
---|
| 517 | diff = newpos; |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | if (newcomp<0) |
---|
| 521 | { |
---|
| 522 | if ((olddiff==1) && (lastcomp>0)) |
---|
| 523 | notFound = FALSE; |
---|
| 524 | else |
---|
| 525 | newpos -= diff; |
---|
| 526 | } |
---|
| 527 | else if (newcomp>0) |
---|
| 528 | { |
---|
| 529 | if ((olddiff==1) && (lastcomp<0)) |
---|
| 530 | { |
---|
| 531 | notFound = FALSE; |
---|
| 532 | newpos++; |
---|
| 533 | } |
---|
| 534 | else |
---|
| 535 | { |
---|
| 536 | newpos += diff; |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | else |
---|
| 540 | { |
---|
| 541 | notFound = FALSE; |
---|
| 542 | } |
---|
| 543 | lastcomp = newcomp; |
---|
| 544 | if (diff==0) notFound=FALSE; /*hs*/ |
---|
| 545 | } |
---|
| 546 | if (newpos<0) newpos = 0; |
---|
| 547 | if (newpos>actpos) newpos = actpos; |
---|
[91a72f] | 548 | while ((newpos<actpos) && (p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r)==0)) |
---|
[35aab3] | 549 | newpos++; |
---|
| 550 | for (j=actpos;j>newpos;j--) |
---|
| 551 | { |
---|
| 552 | (*result)[j] = (*result)[j-1]; |
---|
| 553 | } |
---|
| 554 | (*result)[newpos] = i; |
---|
| 555 | actpos++; |
---|
| 556 | } |
---|
| 557 | } |
---|
| 558 | for (j=0;j<actpos;j++) (*result)[j]++; |
---|
| 559 | return result; |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | /*2 |
---|
| 563 | * concat the lists h1 and h2 without zeros |
---|
| 564 | */ |
---|
[2f5936] | 565 | ideal id_SimpleAdd (ideal h1,ideal h2, const ring R) |
---|
[35aab3] | 566 | { |
---|
| 567 | int i,j,r,l; |
---|
| 568 | ideal result; |
---|
| 569 | |
---|
[2f5936] | 570 | if (h1==NULL) return id_Copy(h2,R); |
---|
| 571 | if (h2==NULL) return id_Copy(h1,R); |
---|
[35aab3] | 572 | j = IDELEMS(h1)-1; |
---|
| 573 | while ((j >= 0) && (h1->m[j] == NULL)) j--; |
---|
| 574 | i = IDELEMS(h2)-1; |
---|
| 575 | while ((i >= 0) && (h2->m[i] == NULL)) i--; |
---|
| 576 | r = si_max(h1->rank,h2->rank); |
---|
| 577 | if (i+j==(-2)) |
---|
| 578 | return idInit(1,r); |
---|
| 579 | else |
---|
| 580 | result=idInit(i+j+2,r); |
---|
| 581 | for (l=j; l>=0; l--) |
---|
| 582 | { |
---|
[2f5936] | 583 | result->m[l] = p_Copy(h1->m[l],R); |
---|
[35aab3] | 584 | } |
---|
| 585 | r = i+j+1; |
---|
| 586 | for (l=i; l>=0; l--, r--) |
---|
| 587 | { |
---|
[2f5936] | 588 | result->m[r] = p_Copy(h2->m[l],R); |
---|
[35aab3] | 589 | } |
---|
| 590 | return result; |
---|
| 591 | } |
---|
| 592 | |
---|
[e070895] | 593 | /*2 |
---|
[ded085] | 594 | * insert h2 into h1 (if h2 is not the zero polynomial) |
---|
| 595 | * return TRUE iff h2 was indeed inserted |
---|
[e070895] | 596 | */ |
---|
[ded085] | 597 | BOOLEAN idInsertPoly (ideal h1, poly h2) |
---|
[e070895] | 598 | { |
---|
[ded085] | 599 | if (h2==NULL) return FALSE; |
---|
[e070895] | 600 | int j = IDELEMS(h1)-1; |
---|
| 601 | while ((j >= 0) && (h1->m[j] == NULL)) j--; |
---|
| 602 | j++; |
---|
| 603 | if (j==IDELEMS(h1)) |
---|
| 604 | { |
---|
| 605 | pEnlargeSet(&(h1->m),IDELEMS(h1),16); |
---|
| 606 | IDELEMS(h1)+=16; |
---|
| 607 | } |
---|
| 608 | h1->m[j]=h2; |
---|
[ded085] | 609 | return TRUE; |
---|
[e070895] | 610 | } |
---|
| 611 | |
---|
[1a68d1d] | 612 | /*2 |
---|
[2b3caae] | 613 | * insert h2 into h1 depending on the two boolean parameters: |
---|
| 614 | * - if zeroOk is true, then h2 will also be inserted when it is zero |
---|
| 615 | * - if duplicateOk is true, then h2 will also be inserted when it is |
---|
| 616 | * already present in h1 |
---|
[ded085] | 617 | * return TRUE iff h2 was indeed inserted |
---|
[1a68d1d] | 618 | */ |
---|
[2f5936] | 619 | BOOLEAN id_InsertPolyWithTests (ideal h1, const int validEntries, |
---|
| 620 | const poly h2, const bool zeroOk, const bool duplicateOk, const ring r) |
---|
[1a68d1d] | 621 | { |
---|
[2b3caae] | 622 | if ((!zeroOk) && (h2 == NULL)) return FALSE; |
---|
| 623 | if (!duplicateOk) |
---|
[1a68d1d] | 624 | { |
---|
[2b3caae] | 625 | bool h2FoundInH1 = false; |
---|
| 626 | int i = 0; |
---|
| 627 | while ((i < validEntries) && (!h2FoundInH1)) |
---|
| 628 | { |
---|
[2f5936] | 629 | h2FoundInH1 = p_EqualPolys(h1->m[i], h2,r); |
---|
[2b3caae] | 630 | i++; |
---|
| 631 | } |
---|
| 632 | if (h2FoundInH1) return FALSE; |
---|
[1a68d1d] | 633 | } |
---|
[2b3caae] | 634 | if (validEntries == IDELEMS(h1)) |
---|
| 635 | { |
---|
| 636 | pEnlargeSet(&(h1->m), IDELEMS(h1), 16); |
---|
| 637 | IDELEMS(h1) += 16; |
---|
| 638 | } |
---|
| 639 | h1->m[validEntries] = h2; |
---|
| 640 | return TRUE; |
---|
[1a68d1d] | 641 | } |
---|
| 642 | |
---|
[35aab3] | 643 | /*2 |
---|
| 644 | * h1 + h2 |
---|
| 645 | */ |
---|
[2f5936] | 646 | ideal id_Add (ideal h1,ideal h2, const ring r) |
---|
[35aab3] | 647 | { |
---|
[2f5936] | 648 | ideal result = id_SimpleAdd(h1,h2,r); |
---|
| 649 | id_Compactify(result,r); |
---|
[35c62a9] | 650 | return result; |
---|
[35aab3] | 651 | } |
---|
| 652 | |
---|
| 653 | /*2 |
---|
| 654 | * h1 * h2 |
---|
| 655 | */ |
---|
[a665eb] | 656 | ideal id_Mult (ideal h1,ideal h2, const ring r) |
---|
[35aab3] | 657 | { |
---|
| 658 | int i,j,k; |
---|
| 659 | ideal hh; |
---|
| 660 | |
---|
| 661 | j = IDELEMS(h1); |
---|
| 662 | while ((j > 0) && (h1->m[j-1] == NULL)) j--; |
---|
| 663 | i = IDELEMS(h2); |
---|
| 664 | while ((i > 0) && (h2->m[i-1] == NULL)) i--; |
---|
| 665 | j = j * i; |
---|
| 666 | if (j == 0) |
---|
| 667 | hh = idInit(1,1); |
---|
| 668 | else |
---|
| 669 | hh=idInit(j,1); |
---|
| 670 | if (h1->rank<h2->rank) |
---|
| 671 | hh->rank = h2->rank; |
---|
| 672 | else |
---|
| 673 | hh->rank = h1->rank; |
---|
| 674 | if (j==0) return hh; |
---|
| 675 | k = 0; |
---|
| 676 | for (i=0; i<IDELEMS(h1); i++) |
---|
| 677 | { |
---|
| 678 | if (h1->m[i] != NULL) |
---|
| 679 | { |
---|
| 680 | for (j=0; j<IDELEMS(h2); j++) |
---|
| 681 | { |
---|
| 682 | if (h2->m[j] != NULL) |
---|
| 683 | { |
---|
[a665eb] | 684 | hh->m[k] = pp_Mult_qq(h1->m[i],h2->m[j],r); |
---|
[35aab3] | 685 | k++; |
---|
| 686 | } |
---|
| 687 | } |
---|
| 688 | } |
---|
| 689 | } |
---|
| 690 | { |
---|
[a665eb] | 691 | id_Compactify(hh,r); |
---|
[10ea45f] | 692 | return hh; |
---|
[35aab3] | 693 | } |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | /*2 |
---|
| 697 | *returns true if h is the zero ideal |
---|
| 698 | */ |
---|
| 699 | BOOLEAN idIs0 (ideal h) |
---|
| 700 | { |
---|
| 701 | int i; |
---|
| 702 | |
---|
| 703 | if (h == NULL) return TRUE; |
---|
[9dd6270] | 704 | i = IDELEMS(h)-1; |
---|
| 705 | while ((i >= 0) && (h->m[i] == NULL)) |
---|
[35aab3] | 706 | { |
---|
| 707 | i--; |
---|
| 708 | } |
---|
[9dd6270] | 709 | if (i < 0) |
---|
[35aab3] | 710 | return TRUE; |
---|
| 711 | else |
---|
| 712 | return FALSE; |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | /*2 |
---|
| 716 | * return the maximal component number found in any polynomial in s |
---|
| 717 | */ |
---|
[2f5547] | 718 | long id_RankFreeModule (ideal s, ring lmRing, ring tailRing) |
---|
[35aab3] | 719 | { |
---|
| 720 | if (s!=NULL) |
---|
| 721 | { |
---|
| 722 | int j=0; |
---|
| 723 | |
---|
| 724 | if (rRing_has_Comp(tailRing) && rRing_has_Comp(lmRing)) |
---|
| 725 | { |
---|
| 726 | int l=IDELEMS(s); |
---|
| 727 | poly *p=s->m; |
---|
| 728 | int k; |
---|
| 729 | for (; l != 0; l--) |
---|
| 730 | { |
---|
| 731 | if (*p!=NULL) |
---|
| 732 | { |
---|
| 733 | pp_Test(*p, lmRing, tailRing); |
---|
| 734 | k = p_MaxComp(*p, lmRing, tailRing); |
---|
| 735 | if (k>j) j = k; |
---|
| 736 | } |
---|
| 737 | p++; |
---|
| 738 | } |
---|
| 739 | } |
---|
| 740 | return j; |
---|
| 741 | } |
---|
| 742 | return -1; |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | BOOLEAN idIsModule(ideal id, ring r) |
---|
| 746 | { |
---|
| 747 | if (id != NULL && rRing_has_Comp(r)) |
---|
| 748 | { |
---|
| 749 | int j, l = IDELEMS(id); |
---|
| 750 | for (j=0; j<l; j++) |
---|
| 751 | { |
---|
| 752 | if (id->m[j] != NULL && p_GetComp(id->m[j], r) > 0) return TRUE; |
---|
| 753 | } |
---|
| 754 | } |
---|
| 755 | return FALSE; |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | |
---|
| 759 | /*2 |
---|
| 760 | *returns true if id is homogenous with respect to the aktual weights |
---|
| 761 | */ |
---|
[a665eb] | 762 | BOOLEAN id_HomIdeal (ideal id, ideal Q, const ring r) |
---|
[35aab3] | 763 | { |
---|
| 764 | int i; |
---|
| 765 | BOOLEAN b; |
---|
| 766 | if ((id == NULL) || (IDELEMS(id) == 0)) return TRUE; |
---|
| 767 | i = 0; |
---|
| 768 | b = TRUE; |
---|
| 769 | while ((i < IDELEMS(id)) && b) |
---|
| 770 | { |
---|
[a665eb] | 771 | b = p_IsHomogeneous(id->m[i],r); |
---|
[35aab3] | 772 | i++; |
---|
| 773 | } |
---|
| 774 | if ((b) && (Q!=NULL) && (IDELEMS(Q)>0)) |
---|
| 775 | { |
---|
| 776 | i=0; |
---|
| 777 | while ((i < IDELEMS(Q)) && b) |
---|
| 778 | { |
---|
[a665eb] | 779 | b = p_IsHomogeneous(Q->m[i],r); |
---|
[35aab3] | 780 | i++; |
---|
| 781 | } |
---|
| 782 | } |
---|
| 783 | return b; |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | /*2 |
---|
| 787 | *initialized a field with r numbers between beg and end for the |
---|
| 788 | *procedure idNextChoise |
---|
| 789 | */ |
---|
| 790 | void idInitChoise (int r,int beg,int end,BOOLEAN *endch,int * choise) |
---|
| 791 | { |
---|
| 792 | /*returns the first choise of r numbers between beg and end*/ |
---|
| 793 | int i; |
---|
| 794 | for (i=0; i<r; i++) |
---|
| 795 | { |
---|
| 796 | choise[i] = 0; |
---|
| 797 | } |
---|
| 798 | if (r <= end-beg+1) |
---|
| 799 | for (i=0; i<r; i++) |
---|
| 800 | { |
---|
| 801 | choise[i] = beg+i; |
---|
| 802 | } |
---|
| 803 | if (r > end-beg+1) |
---|
| 804 | *endch = TRUE; |
---|
| 805 | else |
---|
| 806 | *endch = FALSE; |
---|
| 807 | } |
---|
| 808 | |
---|
| 809 | /*2 |
---|
| 810 | *returns the next choise of r numbers between beg and end |
---|
| 811 | */ |
---|
| 812 | void idGetNextChoise (int r,int end,BOOLEAN *endch,int * choise) |
---|
| 813 | { |
---|
| 814 | int i = r-1,j; |
---|
| 815 | while ((i >= 0) && (choise[i] == end)) |
---|
| 816 | { |
---|
| 817 | i--; |
---|
| 818 | end--; |
---|
| 819 | } |
---|
| 820 | if (i == -1) |
---|
| 821 | *endch = TRUE; |
---|
| 822 | else |
---|
| 823 | { |
---|
| 824 | choise[i]++; |
---|
| 825 | for (j=i+1; j<r; j++) |
---|
| 826 | { |
---|
| 827 | choise[j] = choise[i]+j-i; |
---|
| 828 | } |
---|
| 829 | *endch = FALSE; |
---|
| 830 | } |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | /*2 |
---|
| 834 | *takes the field choise of d numbers between beg and end, cancels the t-th |
---|
| 835 | *entree and searches for the ordinal number of that d-1 dimensional field |
---|
| 836 | * w.r.t. the algorithm of construction |
---|
| 837 | */ |
---|
| 838 | int idGetNumberOfChoise(int t, int d, int begin, int end, int * choise) |
---|
| 839 | { |
---|
| 840 | int * localchoise,i,result=0; |
---|
| 841 | BOOLEAN b=FALSE; |
---|
| 842 | |
---|
| 843 | if (d<=1) return 1; |
---|
| 844 | localchoise=(int*)omAlloc((d-1)*sizeof(int)); |
---|
| 845 | idInitChoise(d-1,begin,end,&b,localchoise); |
---|
| 846 | while (!b) |
---|
| 847 | { |
---|
| 848 | result++; |
---|
| 849 | i = 0; |
---|
| 850 | while ((i<t) && (localchoise[i]==choise[i])) i++; |
---|
| 851 | if (i>=t) |
---|
| 852 | { |
---|
| 853 | i = t+1; |
---|
| 854 | while ((i<d) && (localchoise[i-1]==choise[i])) i++; |
---|
| 855 | if (i>=d) |
---|
[f71e8c5] | 856 | { |
---|
| 857 | omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int)); |
---|
| 858 | return result; |
---|
[35aab3] | 859 | } |
---|
| 860 | } |
---|
[f71e8c5] | 861 | idGetNextChoise(d-1,end,&b,localchoise); |
---|
[35aab3] | 862 | } |
---|
[f71e8c5] | 863 | omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int)); |
---|
| 864 | return 0; |
---|
[35aab3] | 865 | } |
---|
| 866 | |
---|
| 867 | /*2 |
---|
[f71e8c5] | 868 | *computes the binomial coefficient |
---|
[35aab3] | 869 | */ |
---|
[f71e8c5] | 870 | int binom (int n,int r) |
---|
| 871 | { |
---|
| 872 | int i,result; |
---|
[35aab3] | 873 | |
---|
[f71e8c5] | 874 | if (r==0) return 1; |
---|
| 875 | if (n-r<r) return binom(n,n-r); |
---|
| 876 | result = n-r+1; |
---|
| 877 | for (i=2;i<=r;i++) |
---|
[35aab3] | 878 | { |
---|
[f71e8c5] | 879 | result *= n-r+i; |
---|
| 880 | if (result<0) |
---|
[35aab3] | 881 | { |
---|
[f71e8c5] | 882 | WarnS("overflow in binomials"); |
---|
| 883 | return 0; |
---|
[35aab3] | 884 | } |
---|
[f71e8c5] | 885 | result /= i; |
---|
[35aab3] | 886 | } |
---|
[f71e8c5] | 887 | return result; |
---|
[35aab3] | 888 | } |
---|
[f71e8c5] | 889 | |
---|
[35aab3] | 890 | /*2 |
---|
[f71e8c5] | 891 | *the free module of rank i |
---|
[35aab3] | 892 | */ |
---|
[2f5547] | 893 | ideal id_FreeModule (int i, const ring r) |
---|
[35aab3] | 894 | { |
---|
[f71e8c5] | 895 | int j; |
---|
| 896 | ideal h; |
---|
| 897 | |
---|
| 898 | h=idInit(i,i); |
---|
| 899 | for (j=0; j<i; j++) |
---|
[35aab3] | 900 | { |
---|
[2f5936] | 901 | h->m[j] = p_One(r); |
---|
[2f5547] | 902 | p_SetComp(h->m[j],j+1,r); |
---|
| 903 | p_SetmComp(h->m[j],r); |
---|
[35aab3] | 904 | } |
---|
[f71e8c5] | 905 | return h; |
---|
| 906 | } |
---|
[35aab3] | 907 | |
---|
| 908 | /*2 |
---|
| 909 | *computes recursively all monomials of a certain degree |
---|
| 910 | *in every step the actvar-th entry in the exponential |
---|
| 911 | *vector is incremented and the other variables are |
---|
| 912 | *computed by recursive calls of makemonoms |
---|
| 913 | *if the last variable is reached, the difference to the |
---|
| 914 | *degree is computed directly |
---|
| 915 | *vars is the number variables |
---|
| 916 | *actvar is the actual variable to handle |
---|
| 917 | *deg is the degree of the monomials to compute |
---|
| 918 | *monomdeg is the actual degree of the monomial in consideration |
---|
| 919 | */ |
---|
[2f5547] | 920 | static void makemonoms(int vars,int actvar,int deg,int monomdeg, const ring r) |
---|
[35aab3] | 921 | { |
---|
| 922 | poly p; |
---|
| 923 | int i=0; |
---|
| 924 | |
---|
| 925 | if ((idpowerpoint == 0) && (actvar ==1)) |
---|
| 926 | { |
---|
[2f5936] | 927 | idpower[idpowerpoint] = p_One(r); |
---|
[35aab3] | 928 | monomdeg = 0; |
---|
| 929 | } |
---|
| 930 | while (i<=deg) |
---|
| 931 | { |
---|
| 932 | if (deg == monomdeg) |
---|
| 933 | { |
---|
[2f5547] | 934 | p_Setm(idpower[idpowerpoint],r); |
---|
[35aab3] | 935 | idpowerpoint++; |
---|
| 936 | return; |
---|
| 937 | } |
---|
| 938 | if (actvar == vars) |
---|
| 939 | { |
---|
[2f5547] | 940 | p_SetExp(idpower[idpowerpoint],actvar,deg-monomdeg,r); |
---|
| 941 | p_Setm(idpower[idpowerpoint],r); |
---|
| 942 | p_Test(idpower[idpowerpoint],r); |
---|
[35aab3] | 943 | idpowerpoint++; |
---|
| 944 | return; |
---|
| 945 | } |
---|
| 946 | else |
---|
| 947 | { |
---|
[2f5547] | 948 | p = p_Copy(idpower[idpowerpoint],r); |
---|
| 949 | makemonoms(vars,actvar+1,deg,monomdeg,r); |
---|
[35aab3] | 950 | idpower[idpowerpoint] = p; |
---|
| 951 | } |
---|
| 952 | monomdeg++; |
---|
[2f5547] | 953 | p_SetExp(idpower[idpowerpoint],actvar,p_GetExp(idpower[idpowerpoint],actvar,r)+1,r); |
---|
| 954 | p_Setm(idpower[idpowerpoint],r); |
---|
| 955 | p_Test(idpower[idpowerpoint],r); |
---|
[35aab3] | 956 | i++; |
---|
| 957 | } |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | /*2 |
---|
| 961 | *returns the deg-th power of the maximal ideal of 0 |
---|
| 962 | */ |
---|
[a665eb] | 963 | ideal id_MaxIdeal(int deg, const ring r) |
---|
[35aab3] | 964 | { |
---|
| 965 | if (deg < 0) |
---|
| 966 | { |
---|
| 967 | WarnS("maxideal: power must be non-negative"); |
---|
| 968 | } |
---|
| 969 | if (deg < 1) |
---|
| 970 | { |
---|
| 971 | ideal I=idInit(1,1); |
---|
[2f5936] | 972 | I->m[0]=p_One(r); |
---|
[35aab3] | 973 | return I; |
---|
| 974 | } |
---|
| 975 | if (deg == 1) |
---|
| 976 | { |
---|
[2f5547] | 977 | return id_MaxIdeal(r); |
---|
[35aab3] | 978 | } |
---|
| 979 | |
---|
[a665eb] | 980 | int vars = rVar(r); |
---|
[35aab3] | 981 | int i = binom(vars+deg-1,deg); |
---|
| 982 | if (i<=0) return idInit(1,1); |
---|
| 983 | ideal id=idInit(i,1); |
---|
| 984 | idpower = id->m; |
---|
| 985 | idpowerpoint = 0; |
---|
[2f5547] | 986 | makemonoms(vars,1,deg,0,r); |
---|
[35aab3] | 987 | idpower = NULL; |
---|
| 988 | idpowerpoint = 0; |
---|
| 989 | return id; |
---|
| 990 | } |
---|
| 991 | |
---|
| 992 | /*2 |
---|
| 993 | *computes recursively all generators of a certain degree |
---|
| 994 | *of the ideal "givenideal" |
---|
| 995 | *elms is the number elements in the given ideal |
---|
| 996 | *actelm is the actual element to handle |
---|
| 997 | *deg is the degree of the power to compute |
---|
| 998 | *gendeg is the actual degree of the generator in consideration |
---|
| 999 | */ |
---|
[2f5547] | 1000 | static void makepotence(int elms,int actelm,int deg,int gendeg, const ring r) |
---|
[35aab3] | 1001 | { |
---|
| 1002 | poly p; |
---|
| 1003 | int i=0; |
---|
| 1004 | |
---|
| 1005 | if ((idpowerpoint == 0) && (actelm ==1)) |
---|
| 1006 | { |
---|
[2f5936] | 1007 | idpower[idpowerpoint] = p_One(r); |
---|
[35aab3] | 1008 | gendeg = 0; |
---|
| 1009 | } |
---|
| 1010 | while (i<=deg) |
---|
| 1011 | { |
---|
| 1012 | if (deg == gendeg) |
---|
| 1013 | { |
---|
| 1014 | idpowerpoint++; |
---|
| 1015 | return; |
---|
| 1016 | } |
---|
| 1017 | if (actelm == elms) |
---|
| 1018 | { |
---|
[2f5547] | 1019 | p=p_Power(p_Copy(givenideal[actelm-1],r),deg-gendeg,r); |
---|
| 1020 | idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p,r); |
---|
[35aab3] | 1021 | idpowerpoint++; |
---|
| 1022 | return; |
---|
| 1023 | } |
---|
| 1024 | else |
---|
| 1025 | { |
---|
[2f5547] | 1026 | p = p_Copy(idpower[idpowerpoint],r); |
---|
| 1027 | makepotence(elms,actelm+1,deg,gendeg,r); |
---|
[35aab3] | 1028 | idpower[idpowerpoint] = p; |
---|
| 1029 | } |
---|
| 1030 | gendeg++; |
---|
[2f5547] | 1031 | idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p_Copy(givenideal[actelm-1],r),r); |
---|
[35aab3] | 1032 | i++; |
---|
| 1033 | } |
---|
| 1034 | } |
---|
| 1035 | |
---|
| 1036 | /*2 |
---|
| 1037 | *returns the deg-th power of the ideal gid |
---|
| 1038 | */ |
---|
| 1039 | //ideal idPower(ideal gid,int deg) |
---|
| 1040 | //{ |
---|
| 1041 | // int i; |
---|
| 1042 | // ideal id; |
---|
| 1043 | // |
---|
| 1044 | // if (deg < 1) deg = 1; |
---|
| 1045 | // i = binom(IDELEMS(gid)+deg-1,deg); |
---|
| 1046 | // id=idInit(i,1); |
---|
| 1047 | // idpower = id->m; |
---|
| 1048 | // givenideal = gid->m; |
---|
| 1049 | // idpowerpoint = 0; |
---|
| 1050 | // makepotence(IDELEMS(gid),1,deg,0); |
---|
| 1051 | // idpower = NULL; |
---|
| 1052 | // givenideal = NULL; |
---|
| 1053 | // idpowerpoint = 0; |
---|
| 1054 | // return id; |
---|
| 1055 | //} |
---|
[a2d993] | 1056 | static void id_NextPotence(ideal given, ideal result, |
---|
| 1057 | int begin, int end, int deg, int restdeg, poly ap, const ring r) |
---|
[35aab3] | 1058 | { |
---|
| 1059 | poly p; |
---|
| 1060 | int i; |
---|
| 1061 | |
---|
[a2d993] | 1062 | p = p_Power(p_Copy(given->m[begin],r),restdeg,r); |
---|
[35aab3] | 1063 | i = result->nrows; |
---|
[a2d993] | 1064 | result->m[i] = p_Mult_q(p_Copy(ap,r),p,r); |
---|
[35aab3] | 1065 | //PrintS("."); |
---|
| 1066 | (result->nrows)++; |
---|
| 1067 | if (result->nrows >= IDELEMS(result)) |
---|
| 1068 | { |
---|
| 1069 | pEnlargeSet(&(result->m),IDELEMS(result),16); |
---|
| 1070 | IDELEMS(result) += 16; |
---|
| 1071 | } |
---|
| 1072 | if (begin == end) return; |
---|
| 1073 | for (i=restdeg-1;i>0;i--) |
---|
| 1074 | { |
---|
[a2d993] | 1075 | p = p_Power(p_Copy(given->m[begin],r),i,r); |
---|
| 1076 | p = p_Mult_q(p_Copy(ap,r),p,r); |
---|
| 1077 | id_NextPotence(given, result, begin+1, end, deg, restdeg-i, p,r); |
---|
| 1078 | p_Delete(&p,r); |
---|
[35aab3] | 1079 | } |
---|
[a2d993] | 1080 | id_NextPotence(given, result, begin+1, end, deg, restdeg, ap,r); |
---|
[35aab3] | 1081 | } |
---|
| 1082 | |
---|
[2f5936] | 1083 | ideal id_Power(ideal given,int exp, const ring r) |
---|
[35aab3] | 1084 | { |
---|
| 1085 | ideal result,temp; |
---|
| 1086 | poly p1; |
---|
| 1087 | int i; |
---|
| 1088 | |
---|
| 1089 | if (idIs0(given)) return idInit(1,1); |
---|
[2f5936] | 1090 | temp = id_Copy(given,r); |
---|
[35aab3] | 1091 | idSkipZeroes(temp); |
---|
| 1092 | i = binom(IDELEMS(temp)+exp-1,exp); |
---|
| 1093 | result = idInit(i,1); |
---|
| 1094 | result->nrows = 0; |
---|
| 1095 | //Print("ideal contains %d elements\n",i); |
---|
[2f5936] | 1096 | p1=p_One(r); |
---|
[a2d993] | 1097 | id_NextPotence(temp,result,0,IDELEMS(temp)-1,exp,exp,p1,r); |
---|
[2f5936] | 1098 | p_Delete(&p1,r); |
---|
| 1099 | id_Delete(&temp,r); |
---|
[35aab3] | 1100 | result->nrows = 1; |
---|
[2f5936] | 1101 | id_DelEquals(result,r); |
---|
[ff2fd1] | 1102 | idSkipZeroes(result); |
---|
[35aab3] | 1103 | return result; |
---|
| 1104 | } |
---|
| 1105 | |
---|
| 1106 | /*2 |
---|
| 1107 | *skips all zeroes and double elements, searches also for units |
---|
| 1108 | */ |
---|
[2f5936] | 1109 | void id_Compactify(ideal id, const ring r) |
---|
[35aab3] | 1110 | { |
---|
[18dab28] | 1111 | int i; |
---|
[35aab3] | 1112 | BOOLEAN b=FALSE; |
---|
| 1113 | |
---|
| 1114 | i = IDELEMS(id)-1; |
---|
| 1115 | while ((! b) && (i>=0)) |
---|
| 1116 | { |
---|
[2f5936] | 1117 | b=p_IsUnit(id->m[i],r); |
---|
[35aab3] | 1118 | i--; |
---|
| 1119 | } |
---|
| 1120 | if (b) |
---|
| 1121 | { |
---|
[2f5936] | 1122 | for(i=IDELEMS(id)-1;i>=0;i--) p_Delete(&id->m[i],r); |
---|
| 1123 | id->m[0]=p_One(r); |
---|
[35aab3] | 1124 | } |
---|
| 1125 | else |
---|
| 1126 | { |
---|
[2f5936] | 1127 | id_DelMultiples(id,r); |
---|
[35aab3] | 1128 | } |
---|
[962de7] | 1129 | idSkipZeroes(id); |
---|
[35aab3] | 1130 | } |
---|
| 1131 | |
---|
| 1132 | /*2 |
---|
| 1133 | * returns the ideals of initial terms |
---|
| 1134 | */ |
---|
[a2d993] | 1135 | ideal id_Head(ideal h,const ring r) |
---|
[35aab3] | 1136 | { |
---|
| 1137 | ideal m = idInit(IDELEMS(h),h->rank); |
---|
| 1138 | int i; |
---|
| 1139 | |
---|
| 1140 | for (i=IDELEMS(h)-1;i>=0; i--) |
---|
| 1141 | { |
---|
[a2d993] | 1142 | if (h->m[i]!=NULL) m->m[i]=p_Head(h->m[i],r); |
---|
[35aab3] | 1143 | } |
---|
| 1144 | return m; |
---|
| 1145 | } |
---|
| 1146 | |
---|
[a2d993] | 1147 | ideal id_Homogen(ideal h, int varnum,const ring r) |
---|
[35aab3] | 1148 | { |
---|
| 1149 | ideal m = idInit(IDELEMS(h),h->rank); |
---|
| 1150 | int i; |
---|
| 1151 | |
---|
| 1152 | for (i=IDELEMS(h)-1;i>=0; i--) |
---|
| 1153 | { |
---|
[a2d993] | 1154 | m->m[i]=p_Homogen(h->m[i],varnum,r); |
---|
[35aab3] | 1155 | } |
---|
| 1156 | return m; |
---|
| 1157 | } |
---|
| 1158 | |
---|
| 1159 | /*------------------type conversions----------------*/ |
---|
[a2d993] | 1160 | ideal id_Vec2Ideal(poly vec, const ring R) |
---|
[35aab3] | 1161 | { |
---|
| 1162 | ideal result=idInit(1,1); |
---|
| 1163 | omFree((ADDRESS)result->m); |
---|
| 1164 | result->m=NULL; // remove later |
---|
[a2d993] | 1165 | p_Vec2Polys(vec, &(result->m), &(IDELEMS(result)),R); |
---|
[35aab3] | 1166 | return result; |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | |
---|
| 1170 | // converts mat to module, destroys mat |
---|
[a2d993] | 1171 | ideal id_Matrix2Module(matrix mat, const ring R) |
---|
[35aab3] | 1172 | { |
---|
| 1173 | int mc=MATCOLS(mat); |
---|
| 1174 | int mr=MATROWS(mat); |
---|
| 1175 | ideal result = idInit(si_max(mc,1),si_max(mr,1)); |
---|
[18dab28] | 1176 | int i,j,l; |
---|
[35aab3] | 1177 | poly h; |
---|
[a2d993] | 1178 | sBucket_pt bucket = sBucketCreate(R); |
---|
[35aab3] | 1179 | |
---|
| 1180 | for(j=0;j<mc /*MATCOLS(mat)*/;j++) /* j is also index in result->m */ |
---|
| 1181 | { |
---|
| 1182 | for (i=1;i<=mr /*MATROWS(mat)*/;i++) |
---|
| 1183 | { |
---|
| 1184 | h = MATELEM(mat,i,j+1); |
---|
| 1185 | if (h!=NULL) |
---|
| 1186 | { |
---|
[ca3e7b] | 1187 | l=pLength(h); |
---|
[35aab3] | 1188 | MATELEM(mat,i,j+1)=NULL; |
---|
[a2d993] | 1189 | p_SetCompP(h,i, R); |
---|
[35aab3] | 1190 | sBucket_Merge_p(bucket, h, l); |
---|
| 1191 | } |
---|
| 1192 | } |
---|
| 1193 | sBucketClearMerge(bucket, &(result->m[j]), &l); |
---|
| 1194 | } |
---|
[cbeafc2] | 1195 | sBucketDestroy(&bucket); |
---|
[35aab3] | 1196 | |
---|
| 1197 | // obachman: need to clean this up |
---|
[a2d993] | 1198 | id_Delete((ideal*) &mat,R); |
---|
[35aab3] | 1199 | return result; |
---|
| 1200 | } |
---|
| 1201 | |
---|
| 1202 | /*2 |
---|
| 1203 | * converts a module into a matrix, destroyes the input |
---|
| 1204 | */ |
---|
[a2d993] | 1205 | matrix id_Module2Matrix(ideal mod, const ring R) |
---|
[35aab3] | 1206 | { |
---|
| 1207 | matrix result = mpNew(mod->rank,IDELEMS(mod)); |
---|
[37e36e] | 1208 | long i,cp; |
---|
[35aab3] | 1209 | poly p,h; |
---|
| 1210 | |
---|
| 1211 | for(i=0;i<IDELEMS(mod);i++) |
---|
| 1212 | { |
---|
[d0164d9] | 1213 | p=pReverse(mod->m[i]); |
---|
[35aab3] | 1214 | mod->m[i]=NULL; |
---|
| 1215 | while (p!=NULL) |
---|
| 1216 | { |
---|
| 1217 | h=p; |
---|
| 1218 | pIter(p); |
---|
| 1219 | pNext(h)=NULL; |
---|
[37e36e] | 1220 | cp = si_max((long)1,p_GetComp(h, R)); // if used for ideals too |
---|
| 1221 | //cp = p_GetComp(h,R); |
---|
[a2d993] | 1222 | p_SetComp(h,0,R); |
---|
| 1223 | p_SetmComp(h,R); |
---|
[35aab3] | 1224 | #ifdef TEST |
---|
| 1225 | if (cp>mod->rank) |
---|
| 1226 | { |
---|
[2d2e40] | 1227 | Print("## inv. rank %ld -> %ld\n",mod->rank,cp); |
---|
[35aab3] | 1228 | int k,l,o=mod->rank; |
---|
| 1229 | mod->rank=cp; |
---|
| 1230 | matrix d=mpNew(mod->rank,IDELEMS(mod)); |
---|
| 1231 | for (l=1; l<=o; l++) |
---|
| 1232 | { |
---|
| 1233 | for (k=1; k<=IDELEMS(mod); k++) |
---|
| 1234 | { |
---|
| 1235 | MATELEM(d,l,k)=MATELEM(result,l,k); |
---|
| 1236 | MATELEM(result,l,k)=NULL; |
---|
| 1237 | } |
---|
| 1238 | } |
---|
[a2d993] | 1239 | id_Delete((ideal *)&result,R); |
---|
[35aab3] | 1240 | result=d; |
---|
| 1241 | } |
---|
| 1242 | #endif |
---|
[a2d993] | 1243 | MATELEM(result,cp,i+1) = p_Add_q(MATELEM(result,cp,i+1),h,R); |
---|
[35aab3] | 1244 | } |
---|
| 1245 | } |
---|
| 1246 | // obachman 10/99: added the following line, otherwise memory leack! |
---|
[a2d993] | 1247 | id_Delete(&mod,R); |
---|
[35aab3] | 1248 | return result; |
---|
| 1249 | } |
---|
| 1250 | |
---|
[a2d993] | 1251 | matrix id_Module2formatedMatrix(ideal mod,int rows, int cols, const ring R) |
---|
[35aab3] | 1252 | { |
---|
| 1253 | matrix result = mpNew(rows,cols); |
---|
[a2d993] | 1254 | int i,cp,r=id_RankFreeModule(mod,R),c=IDELEMS(mod); |
---|
[35aab3] | 1255 | poly p,h; |
---|
| 1256 | |
---|
| 1257 | if (r>rows) r = rows; |
---|
| 1258 | if (c>cols) c = cols; |
---|
| 1259 | for(i=0;i<c;i++) |
---|
| 1260 | { |
---|
[bafaec0] | 1261 | p=pReverse(mod->m[i]); |
---|
[35aab3] | 1262 | mod->m[i]=NULL; |
---|
| 1263 | while (p!=NULL) |
---|
| 1264 | { |
---|
| 1265 | h=p; |
---|
| 1266 | pIter(p); |
---|
| 1267 | pNext(h)=NULL; |
---|
[a2d993] | 1268 | cp = p_GetComp(h,R); |
---|
[35aab3] | 1269 | if (cp<=r) |
---|
| 1270 | { |
---|
[a2d993] | 1271 | p_SetComp(h,0,R); |
---|
| 1272 | p_SetmComp(h,R); |
---|
| 1273 | MATELEM(result,cp,i+1) = p_Add_q(MATELEM(result,cp,i+1),h,R); |
---|
[35aab3] | 1274 | } |
---|
| 1275 | else |
---|
[a2d993] | 1276 | p_Delete(&h,R); |
---|
[35aab3] | 1277 | } |
---|
| 1278 | } |
---|
[a2d993] | 1279 | id_Delete(&mod,R); |
---|
[35aab3] | 1280 | return result; |
---|
| 1281 | } |
---|
| 1282 | |
---|
| 1283 | /*2 |
---|
| 1284 | * substitute the n-th variable by the monomial e in id |
---|
| 1285 | * destroy id |
---|
| 1286 | */ |
---|
[a2d993] | 1287 | ideal id_Subst(ideal id, int n, poly e, const ring r) |
---|
[35aab3] | 1288 | { |
---|
| 1289 | int k=MATROWS((matrix)id)*MATCOLS((matrix)id); |
---|
| 1290 | ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id)); |
---|
| 1291 | |
---|
| 1292 | res->rank = id->rank; |
---|
| 1293 | for(k--;k>=0;k--) |
---|
| 1294 | { |
---|
[a2d993] | 1295 | res->m[k]=p_Subst(id->m[k],n,e,r); |
---|
[35aab3] | 1296 | id->m[k]=NULL; |
---|
| 1297 | } |
---|
[a2d993] | 1298 | id_Delete(&id,r); |
---|
[35aab3] | 1299 | return res; |
---|
| 1300 | } |
---|
| 1301 | |
---|
[a2d993] | 1302 | BOOLEAN id_HomModule(ideal m, ideal Q, intvec **w, const ring R) |
---|
[35aab3] | 1303 | { |
---|
| 1304 | if (w!=NULL) *w=NULL; |
---|
[a2d993] | 1305 | if ((Q!=NULL) && (!id_HomIdeal(Q,NULL,R))) return FALSE; |
---|
[43ebb1] | 1306 | if (idIs0(m)) |
---|
| 1307 | { |
---|
[a12776] | 1308 | if (w!=NULL) (*w)=new intvec(m->rank); |
---|
[43ebb1] | 1309 | return TRUE; |
---|
| 1310 | } |
---|
[35aab3] | 1311 | |
---|
[4e63600] | 1312 | long cmax=1,order=0,ord,* diff,diffmin=32000; |
---|
| 1313 | int *iscom; |
---|
[18dab28] | 1314 | int i; |
---|
[35aab3] | 1315 | poly p=NULL; |
---|
[1f5db38] | 1316 | pFDegProc d; |
---|
[a2d993] | 1317 | if (R->pLexOrder && (R->order[0]==ringorder_lp)) |
---|
[99bdcf] | 1318 | d=p_Totaldegree; |
---|
[bead81] | 1319 | else |
---|
[9765f3] | 1320 | d=R->pFDeg; |
---|
[35aab3] | 1321 | int length=IDELEMS(m); |
---|
[a2d993] | 1322 | poly* P=m->m; |
---|
| 1323 | poly* F=(poly*)omAlloc(length*sizeof(poly)); |
---|
[35aab3] | 1324 | for (i=length-1;i>=0;i--) |
---|
| 1325 | { |
---|
| 1326 | p=F[i]=P[i]; |
---|
[a2d993] | 1327 | cmax=si_max(cmax,(long)p_MaxComp(p,R)); |
---|
[35aab3] | 1328 | } |
---|
[4e63600] | 1329 | cmax++; |
---|
| 1330 | diff = (long *)omAlloc0(cmax*sizeof(long)); |
---|
[35aab3] | 1331 | if (w!=NULL) *w=new intvec(cmax-1); |
---|
| 1332 | iscom = (int *)omAlloc0(cmax*sizeof(int)); |
---|
| 1333 | i=0; |
---|
| 1334 | while (i<=length) |
---|
| 1335 | { |
---|
| 1336 | if (i<length) |
---|
| 1337 | { |
---|
| 1338 | p=F[i]; |
---|
[a2d993] | 1339 | while ((p!=NULL) && (iscom[p_GetComp(p,R)]==0)) pIter(p); |
---|
[35aab3] | 1340 | } |
---|
| 1341 | if ((p==NULL) && (i<length)) |
---|
| 1342 | { |
---|
| 1343 | i++; |
---|
| 1344 | } |
---|
| 1345 | else |
---|
| 1346 | { |
---|
[4e63600] | 1347 | if (p==NULL) /* && (i==length) */ |
---|
[35aab3] | 1348 | { |
---|
| 1349 | i=0; |
---|
| 1350 | while ((i<length) && (F[i]==NULL)) i++; |
---|
| 1351 | if (i>=length) break; |
---|
| 1352 | p = F[i]; |
---|
| 1353 | } |
---|
[1f5db38] | 1354 | //if (pLexOrder && (currRing->order[0]==ringorder_lp)) |
---|
| 1355 | // order=pTotaldegree(p); |
---|
| 1356 | //else |
---|
[35aab3] | 1357 | // order = p->order; |
---|
[1f5db38] | 1358 | // order = pFDeg(p,currRing); |
---|
[a2d993] | 1359 | order = d(p,R) +diff[p_GetComp(p,R)]; |
---|
[1f5db38] | 1360 | //order += diff[pGetComp(p)]; |
---|
[35aab3] | 1361 | p = F[i]; |
---|
| 1362 | //Print("Actual p=F[%d]: ",i);pWrite(p); |
---|
| 1363 | F[i] = NULL; |
---|
| 1364 | i=0; |
---|
| 1365 | } |
---|
| 1366 | while (p!=NULL) |
---|
| 1367 | { |
---|
[a2d993] | 1368 | if (R->pLexOrder && (R->order[0]==ringorder_lp)) |
---|
| 1369 | ord=p_Totaldegree(p,R); |
---|
[4e63600] | 1370 | else |
---|
[35aab3] | 1371 | // ord = p->order; |
---|
[9765f3] | 1372 | ord = R->pFDeg(p,R); |
---|
[a2d993] | 1373 | if (iscom[p_GetComp(p,R)]==0) |
---|
[35aab3] | 1374 | { |
---|
[a2d993] | 1375 | diff[p_GetComp(p,R)] = order-ord; |
---|
| 1376 | iscom[p_GetComp(p,R)] = 1; |
---|
[35aab3] | 1377 | /* |
---|
| 1378 | *PrintS("new diff: "); |
---|
| 1379 | *for (j=0;j<cmax;j++) Print("%d ",diff[j]); |
---|
| 1380 | *PrintLn(); |
---|
| 1381 | *PrintS("new iscom: "); |
---|
| 1382 | *for (j=0;j<cmax;j++) Print("%d ",iscom[j]); |
---|
| 1383 | *PrintLn(); |
---|
| 1384 | *Print("new set %d, order %d, ord %d, diff %d\n",pGetComp(p),order,ord,diff[pGetComp(p)]); |
---|
| 1385 | */ |
---|
| 1386 | } |
---|
| 1387 | else |
---|
| 1388 | { |
---|
| 1389 | /* |
---|
| 1390 | *PrintS("new diff: "); |
---|
| 1391 | *for (j=0;j<cmax;j++) Print("%d ",diff[j]); |
---|
| 1392 | *PrintLn(); |
---|
| 1393 | *Print("order %d, ord %d, diff %d\n",order,ord,diff[pGetComp(p)]); |
---|
| 1394 | */ |
---|
[a2d993] | 1395 | if (order != (ord+diff[p_GetComp(p,R)])) |
---|
[35aab3] | 1396 | { |
---|
| 1397 | omFreeSize((ADDRESS) iscom,cmax*sizeof(int)); |
---|
[4e63600] | 1398 | omFreeSize((ADDRESS) diff,cmax*sizeof(long)); |
---|
[35aab3] | 1399 | omFreeSize((ADDRESS) F,length*sizeof(poly)); |
---|
| 1400 | delete *w;*w=NULL; |
---|
| 1401 | return FALSE; |
---|
| 1402 | } |
---|
| 1403 | } |
---|
| 1404 | pIter(p); |
---|
| 1405 | } |
---|
| 1406 | } |
---|
| 1407 | omFreeSize((ADDRESS) iscom,cmax*sizeof(int)); |
---|
| 1408 | omFreeSize((ADDRESS) F,length*sizeof(poly)); |
---|
[4e63600] | 1409 | for (i=1;i<cmax;i++) (**w)[i-1]=(int)(diff[i]); |
---|
[35aab3] | 1410 | for (i=1;i<cmax;i++) |
---|
| 1411 | { |
---|
| 1412 | if (diff[i]<diffmin) diffmin=diff[i]; |
---|
| 1413 | } |
---|
| 1414 | if (w!=NULL) |
---|
| 1415 | { |
---|
| 1416 | for (i=1;i<cmax;i++) |
---|
| 1417 | { |
---|
[4e63600] | 1418 | (**w)[i-1]=(int)(diff[i]-diffmin); |
---|
[35aab3] | 1419 | } |
---|
| 1420 | } |
---|
[4e63600] | 1421 | omFreeSize((ADDRESS) diff,cmax*sizeof(long)); |
---|
[35aab3] | 1422 | return TRUE; |
---|
| 1423 | } |
---|
| 1424 | |
---|
[a2d993] | 1425 | ideal id_Jet(ideal i,int d, const ring R) |
---|
[35aab3] | 1426 | { |
---|
| 1427 | ideal r=idInit((i->nrows)*(i->ncols),i->rank); |
---|
| 1428 | r->nrows = i-> nrows; |
---|
| 1429 | r->ncols = i-> ncols; |
---|
| 1430 | //r->rank = i-> rank; |
---|
| 1431 | int k; |
---|
| 1432 | for(k=(i->nrows)*(i->ncols)-1;k>=0; k--) |
---|
| 1433 | { |
---|
[a2d993] | 1434 | r->m[k]=pp_Jet(i->m[k],d,R); |
---|
[35aab3] | 1435 | } |
---|
| 1436 | return r; |
---|
| 1437 | } |
---|
| 1438 | |
---|
[a2d993] | 1439 | ideal id_JetW(ideal i,int d, intvec * iv, const ring R) |
---|
[35aab3] | 1440 | { |
---|
| 1441 | ideal r=idInit(IDELEMS(i),i->rank); |
---|
| 1442 | if (ecartWeights!=NULL) |
---|
| 1443 | { |
---|
| 1444 | WerrorS("cannot compute weighted jets now"); |
---|
| 1445 | } |
---|
| 1446 | else |
---|
| 1447 | { |
---|
[a2d993] | 1448 | short *w=iv2array(iv,R); |
---|
[35aab3] | 1449 | int k; |
---|
| 1450 | for(k=0; k<IDELEMS(i); k++) |
---|
| 1451 | { |
---|
[a2d993] | 1452 | r->m[k]=pp_JetW(i->m[k],d,w,R); |
---|
[35aab3] | 1453 | } |
---|
[a2d993] | 1454 | omFreeSize((ADDRESS)w,(rVar(R)+1)*sizeof(short)); |
---|
[35aab3] | 1455 | } |
---|
| 1456 | return r; |
---|
| 1457 | } |
---|
| 1458 | |
---|
| 1459 | /*3 |
---|
[b8f199] | 1460 | * searches for the next unit in the components of the module arg and |
---|
| 1461 | * returns the first one; |
---|
[35aab3] | 1462 | */ |
---|
[d16ea9] | 1463 | int id_ReadOutPivot(ideal arg,int* comp, const ring r) |
---|
[35aab3] | 1464 | { |
---|
[1d138c] | 1465 | if (idIs0(arg)) return -1; |
---|
[8421b8] | 1466 | int i=0,j, generator=-1; |
---|
| 1467 | int rk_arg=arg->rank; //idRankFreeModule(arg); |
---|
| 1468 | int * componentIsUsed =(int *)omAlloc((rk_arg+1)*sizeof(int)); |
---|
[fc7902] | 1469 | poly p; |
---|
[35aab3] | 1470 | |
---|
[8421b8] | 1471 | while ((generator<0) && (i<IDELEMS(arg))) |
---|
[35aab3] | 1472 | { |
---|
[8421b8] | 1473 | memset(componentIsUsed,0,(rk_arg+1)*sizeof(int)); |
---|
[35aab3] | 1474 | p = arg->m[i]; |
---|
| 1475 | while (p!=NULL) |
---|
| 1476 | { |
---|
[2f5936] | 1477 | j = p_GetComp(p,r); |
---|
[8421b8] | 1478 | if (componentIsUsed[j]==0) |
---|
[35aab3] | 1479 | { |
---|
[b8f199] | 1480 | #ifdef HAVE_RINGS |
---|
[2f5936] | 1481 | if (p_LmIsConstantComp(p,r) && |
---|
| 1482 | (!rField_is_Ring(r) || n_IsUnit(pGetCoeff(p),r->cf))) |
---|
[b8f199] | 1483 | { |
---|
| 1484 | #else |
---|
[2f5936] | 1485 | if (p_LmIsConstantComp(p,r)) |
---|
[35aab3] | 1486 | { |
---|
[b8f199] | 1487 | #endif |
---|
[35aab3] | 1488 | generator = i; |
---|
[8421b8] | 1489 | componentIsUsed[j] = 1; |
---|
[35aab3] | 1490 | } |
---|
| 1491 | else |
---|
| 1492 | { |
---|
[8421b8] | 1493 | componentIsUsed[j] = -1; |
---|
[35aab3] | 1494 | } |
---|
| 1495 | } |
---|
[8421b8] | 1496 | else if (componentIsUsed[j]>0) |
---|
[35aab3] | 1497 | { |
---|
[8421b8] | 1498 | (componentIsUsed[j])++; |
---|
[35aab3] | 1499 | } |
---|
| 1500 | pIter(p); |
---|
| 1501 | } |
---|
| 1502 | i++; |
---|
| 1503 | } |
---|
| 1504 | i = 0; |
---|
| 1505 | *comp = -1; |
---|
| 1506 | for (j=0;j<=rk_arg;j++) |
---|
| 1507 | { |
---|
[8421b8] | 1508 | if (componentIsUsed[j]>0) |
---|
[35aab3] | 1509 | { |
---|
[8421b8] | 1510 | if ((*comp==-1) || (componentIsUsed[j]<i)) |
---|
[35aab3] | 1511 | { |
---|
| 1512 | *comp = j; |
---|
[8421b8] | 1513 | i= componentIsUsed[j]; |
---|
[35aab3] | 1514 | } |
---|
| 1515 | } |
---|
| 1516 | } |
---|
[8421b8] | 1517 | omFree(componentIsUsed); |
---|
[35aab3] | 1518 | return generator; |
---|
| 1519 | } |
---|
| 1520 | |
---|
[955025] | 1521 | #if 0 |
---|
[35aab3] | 1522 | static void idDeleteComp(ideal arg,int red_comp) |
---|
| 1523 | { |
---|
| 1524 | int i,j; |
---|
| 1525 | poly p; |
---|
| 1526 | |
---|
| 1527 | for (i=IDELEMS(arg)-1;i>=0;i--) |
---|
| 1528 | { |
---|
| 1529 | p = arg->m[i]; |
---|
| 1530 | while (p!=NULL) |
---|
| 1531 | { |
---|
| 1532 | j = pGetComp(p); |
---|
| 1533 | if (j>red_comp) |
---|
| 1534 | { |
---|
| 1535 | pSetComp(p,j-1); |
---|
| 1536 | pSetm(p); |
---|
| 1537 | } |
---|
| 1538 | pIter(p); |
---|
| 1539 | } |
---|
| 1540 | } |
---|
| 1541 | (arg->rank)--; |
---|
| 1542 | } |
---|
[955025] | 1543 | #endif |
---|
| 1544 | |
---|
[a2d993] | 1545 | intvec * id_QHomWeight(ideal id, const ring r) |
---|
[35aab3] | 1546 | { |
---|
| 1547 | poly head, tail; |
---|
| 1548 | int k; |
---|
| 1549 | int in=IDELEMS(id)-1, ready=0, all=0, |
---|
[a665eb] | 1550 | coldim=rVar(r), rowmax=2*coldim; |
---|
[35aab3] | 1551 | if (in<0) return NULL; |
---|
| 1552 | intvec *imat=new intvec(rowmax+1,coldim,0); |
---|
| 1553 | |
---|
| 1554 | do |
---|
| 1555 | { |
---|
| 1556 | head = id->m[in--]; |
---|
| 1557 | if (head!=NULL) |
---|
| 1558 | { |
---|
| 1559 | tail = pNext(head); |
---|
| 1560 | while (tail!=NULL) |
---|
| 1561 | { |
---|
| 1562 | all++; |
---|
| 1563 | for (k=1;k<=coldim;k++) |
---|
[a2d993] | 1564 | IMATELEM(*imat,all,k) = p_GetExpDiff(head,tail,k,r); |
---|
[35aab3] | 1565 | if (all==rowmax) |
---|
| 1566 | { |
---|
| 1567 | ivTriangIntern(imat, ready, all); |
---|
| 1568 | if (ready==coldim) |
---|
| 1569 | { |
---|
| 1570 | delete imat; |
---|
| 1571 | return NULL; |
---|
| 1572 | } |
---|
| 1573 | } |
---|
| 1574 | pIter(tail); |
---|
| 1575 | } |
---|
| 1576 | } |
---|
| 1577 | } while (in>=0); |
---|
| 1578 | if (all>ready) |
---|
| 1579 | { |
---|
| 1580 | ivTriangIntern(imat, ready, all); |
---|
| 1581 | if (ready==coldim) |
---|
| 1582 | { |
---|
| 1583 | delete imat; |
---|
| 1584 | return NULL; |
---|
| 1585 | } |
---|
| 1586 | } |
---|
| 1587 | intvec *result = ivSolveKern(imat, ready); |
---|
| 1588 | delete imat; |
---|
| 1589 | return result; |
---|
| 1590 | } |
---|
| 1591 | |
---|
[a2d993] | 1592 | BOOLEAN id_IsZeroDim(ideal I, const ring r) |
---|
[35aab3] | 1593 | { |
---|
[a665eb] | 1594 | BOOLEAN *UsedAxis=(BOOLEAN *)omAlloc0(rVar(r)*sizeof(BOOLEAN)); |
---|
[35aab3] | 1595 | int i,n; |
---|
| 1596 | poly po; |
---|
| 1597 | BOOLEAN res=TRUE; |
---|
| 1598 | for(i=IDELEMS(I)-1;i>=0;i--) |
---|
| 1599 | { |
---|
| 1600 | po=I->m[i]; |
---|
[a2d993] | 1601 | if ((po!=NULL) &&((n=p_IsPurePower(po,r))!=0)) UsedAxis[n-1]=TRUE; |
---|
[35aab3] | 1602 | } |
---|
[a665eb] | 1603 | for(i=rVar(r)-1;i>=0;i--) |
---|
[35aab3] | 1604 | { |
---|
| 1605 | if(UsedAxis[i]==FALSE) {res=FALSE; break;} // not zero-dim. |
---|
| 1606 | } |
---|
[a665eb] | 1607 | omFreeSize(UsedAxis,rVar(r)*sizeof(BOOLEAN)); |
---|
[35aab3] | 1608 | return res; |
---|
| 1609 | } |
---|
| 1610 | |
---|
[2f5936] | 1611 | void id_Normalize(ideal I,const ring r) |
---|
[35aab3] | 1612 | { |
---|
[2f5936] | 1613 | if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */ |
---|
[35aab3] | 1614 | int i; |
---|
| 1615 | for(i=IDELEMS(I)-1;i>=0;i--) |
---|
| 1616 | { |
---|
[2f5936] | 1617 | p_Normalize(I->m[i],r); |
---|
[35aab3] | 1618 | } |
---|
| 1619 | } |
---|
[225d94] | 1620 | |
---|
[ad3aa7] | 1621 | int id_MinDegW(ideal M,intvec *w, const ring r) |
---|
| 1622 | { |
---|
| 1623 | int d=-1; |
---|
| 1624 | for(int i=0;i<IDELEMS(M);i++) |
---|
| 1625 | { |
---|
| 1626 | if (M->m[i]!=NULL) |
---|
| 1627 | { |
---|
| 1628 | int d0=p_MinDeg(M->m[i],w,r); |
---|
| 1629 | if(-1<d0&&((d0<d)||(d==-1))) |
---|
| 1630 | d=d0; |
---|
| 1631 | } |
---|
| 1632 | } |
---|
| 1633 | return d; |
---|
| 1634 | } |
---|
| 1635 | |
---|
[2ad10e9] | 1636 | // #include <kernel/clapsing.h> |
---|
[225d94] | 1637 | |
---|
[90a60f] | 1638 | /*2 |
---|
| 1639 | * transpose a module |
---|
| 1640 | */ |
---|
| 1641 | ideal id_Transp(ideal a, const ring rRing) |
---|
| 1642 | { |
---|
| 1643 | int r = a->rank, c = IDELEMS(a); |
---|
| 1644 | ideal b = idInit(r,c); |
---|
| 1645 | |
---|
| 1646 | for (int i=c; i>0; i--) |
---|
| 1647 | { |
---|
| 1648 | poly p=a->m[i-1]; |
---|
| 1649 | while(p!=NULL) |
---|
| 1650 | { |
---|
| 1651 | poly h=p_Head(p, rRing); |
---|
| 1652 | int co=p_GetComp(h, rRing)-1; |
---|
| 1653 | p_SetComp(h, i, rRing); |
---|
| 1654 | p_Setm(h, rRing); |
---|
| 1655 | b->m[co] = p_Add_q(b->m[co], h, rRing); |
---|
| 1656 | pIter(p); |
---|
| 1657 | } |
---|
| 1658 | } |
---|
| 1659 | return b; |
---|
| 1660 | } |
---|
| 1661 | |
---|
| 1662 | |
---|
| 1663 | |
---|
| 1664 | /*2 |
---|
| 1665 | * The following is needed to compute the image of certain map used in |
---|
| 1666 | * the computation of cohomologies via BGG |
---|
| 1667 | * let M = { w_1, ..., w_k }, k = size(M) == ncols(M), n = nvars(currRing). |
---|
| 1668 | * assuming that nrows(M) <= m*n; the procedure computes: |
---|
| 1669 | * transpose(M) * transpose( var(1) I_m | ... | var(n) I_m ) :== transpose(module{f_1, ... f_k}), |
---|
| 1670 | * where f_i = \sum_{j=1}^{m} (w_i, v_j) gen(j), (w_i, v_j) is a `scalar` multiplication. |
---|
| 1671 | * that is, if w_i = (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m) then |
---|
| 1672 | |
---|
| 1673 | (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m) |
---|
| 1674 | * var_1 ... var_1 | var_2 ... var_2 | ... | var_n ... var(n) |
---|
| 1675 | * gen_1 ... gen_m | gen_1 ... gen_m | ... | gen_1 ... gen_m |
---|
| 1676 | + => |
---|
| 1677 | f_i = |
---|
| 1678 | |
---|
| 1679 | a^1_1 * var(1) * gen(1) + ... + a^1_m * var(1) * gen(m) + |
---|
| 1680 | a^2_1 * var(2) * gen(1) + ... + a^2_m * var(2) * gen(m) + |
---|
| 1681 | ... |
---|
| 1682 | a^n_1 * var(n) * gen(1) + ... + a^n_m * var(n) * gen(m); |
---|
| 1683 | |
---|
| 1684 | NOTE: for every f_i we run only ONCE along w_i saving partial sums into a temporary array of polys of size m |
---|
| 1685 | */ |
---|
[9c1b63] | 1686 | ideal id_TensorModuleMult(const int m, const ideal M, const ring rRing) |
---|
[90a60f] | 1687 | { |
---|
| 1688 | // #ifdef DEBU |
---|
| 1689 | // WarnS("tensorModuleMult!!!!"); |
---|
| 1690 | |
---|
| 1691 | assume(m > 0); |
---|
| 1692 | assume(M != NULL); |
---|
| 1693 | |
---|
| 1694 | const int n = rRing->N; |
---|
| 1695 | |
---|
| 1696 | assume(M->rank <= m * n); |
---|
| 1697 | |
---|
| 1698 | const int k = IDELEMS(M); |
---|
| 1699 | |
---|
| 1700 | ideal idTemp = idInit(k,m); // = {f_1, ..., f_k } |
---|
| 1701 | |
---|
| 1702 | for( int i = 0; i < k; i++ ) // for every w \in M |
---|
| 1703 | { |
---|
| 1704 | poly pTempSum = NULL; |
---|
| 1705 | |
---|
| 1706 | poly w = M->m[i]; |
---|
| 1707 | |
---|
| 1708 | while(w != NULL) // for each term of w... |
---|
| 1709 | { |
---|
| 1710 | poly h = p_Head(w, rRing); |
---|
| 1711 | |
---|
| 1712 | const int gen = p_GetComp(h, rRing); // 1 ... |
---|
| 1713 | |
---|
| 1714 | assume(gen > 0); |
---|
| 1715 | assume(gen <= n*m); |
---|
| 1716 | |
---|
| 1717 | // TODO: write a formula with %, / instead of while! |
---|
| 1718 | /* |
---|
| 1719 | int c = gen; |
---|
| 1720 | int v = 1; |
---|
| 1721 | while(c > m) |
---|
| 1722 | { |
---|
| 1723 | c -= m; |
---|
| 1724 | v++; |
---|
| 1725 | } |
---|
| 1726 | */ |
---|
| 1727 | |
---|
[592906] | 1728 | int cc = gen % m; |
---|
[90a60f] | 1729 | if( cc == 0) cc = m; |
---|
| 1730 | int vv = 1 + (gen - cc) / m; |
---|
| 1731 | |
---|
| 1732 | // assume( cc == c ); |
---|
| 1733 | // assume( vv == v ); |
---|
| 1734 | |
---|
| 1735 | // 1<= c <= m |
---|
| 1736 | assume( cc > 0 ); |
---|
| 1737 | assume( cc <= m ); |
---|
| 1738 | |
---|
| 1739 | assume( vv > 0 ); |
---|
| 1740 | assume( vv <= n ); |
---|
| 1741 | |
---|
| 1742 | assume( (cc + (vv-1)*m) == gen ); |
---|
| 1743 | |
---|
[9c1b63] | 1744 | p_IncrExp(h, vv, rRing); // h *= var(j) && // p_AddExp(h, vv, 1, rRing); |
---|
[592906] | 1745 | p_SetComp(h, cc, rRing); |
---|
[90a60f] | 1746 | |
---|
| 1747 | p_Setm(h, rRing); // addjust degree after the previous steps! |
---|
| 1748 | |
---|
| 1749 | pTempSum = p_Add_q(pTempSum, h, rRing); // it is slow since h will be usually put to the back of pTempSum!!! |
---|
| 1750 | |
---|
| 1751 | pIter(w); |
---|
| 1752 | } |
---|
| 1753 | |
---|
| 1754 | idTemp->m[i] = pTempSum; |
---|
| 1755 | } |
---|
| 1756 | |
---|
| 1757 | // simplify idTemp??? |
---|
| 1758 | |
---|
| 1759 | ideal idResult = id_Transp(idTemp, rRing); |
---|
| 1760 | |
---|
| 1761 | id_Delete(&idTemp, rRing); |
---|
| 1762 | |
---|
| 1763 | return(idResult); |
---|
| 1764 | } |
---|