[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[1e579c6] | 4 | /* $Id: polys.cc,v 1.20 2007-06-20 09:39:24 wienand Exp $ */ |
---|
[35aab3] | 5 | |
---|
| 6 | /* |
---|
| 7 | * ABSTRACT - all basic methods to manipulate polynomials |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /* includes */ |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <string.h> |
---|
| 13 | #include <ctype.h> |
---|
| 14 | #include "mod2.h" |
---|
| 15 | #include "structs.h" |
---|
| 16 | #include "omalloc.h" |
---|
| 17 | #include "febase.h" |
---|
| 18 | #include "numbers.h" |
---|
| 19 | #include "polys.h" |
---|
| 20 | #include "ring.h" |
---|
| 21 | |
---|
[68349d] | 22 | #ifdef HAVE_PLURAL |
---|
| 23 | #include "gring.h" |
---|
| 24 | #endif |
---|
| 25 | |
---|
[35aab3] | 26 | /* ----------- global variables, set by pSetGlobals --------------------- */ |
---|
| 27 | /* computes length and maximal degree of a POLYnomial */ |
---|
| 28 | pLDegProc pLDeg; |
---|
| 29 | /* computes the degree of the initial term, used for std */ |
---|
| 30 | pFDegProc pFDeg; |
---|
| 31 | /* the monomial ordering of the head monomials a and b */ |
---|
| 32 | /* returns -1 if a comes before b, 0 if a=b, 1 otherwise */ |
---|
| 33 | |
---|
| 34 | int pVariables; // number of variables |
---|
| 35 | |
---|
| 36 | /* 1 for polynomial ring, -1 otherwise */ |
---|
| 37 | int pOrdSgn; |
---|
| 38 | // it is of type int, not BOOLEAN because it is also in ip |
---|
| 39 | /* TRUE if the monomial ordering is not compatible with pFDeg */ |
---|
| 40 | BOOLEAN pLexOrder; |
---|
| 41 | |
---|
| 42 | /* ----------- global variables, set by procedures from hecke/kstd1 ----- */ |
---|
| 43 | /* the highest monomial below pHEdge */ |
---|
| 44 | poly ppNoether = NULL; |
---|
| 45 | |
---|
| 46 | /* -------------------------------------------------------- */ |
---|
| 47 | /*2 |
---|
| 48 | * change all global variables to fit the description of the new ring |
---|
| 49 | */ |
---|
| 50 | |
---|
| 51 | |
---|
[8c5988] | 52 | void pSetGlobals(const ring r, BOOLEAN complete) |
---|
[35aab3] | 53 | { |
---|
| 54 | int i; |
---|
| 55 | if (ppNoether!=NULL) pDelete(&ppNoether); |
---|
| 56 | pVariables = r->N; |
---|
| 57 | pOrdSgn = r->OrdSgn; |
---|
| 58 | pFDeg=r->pFDeg; |
---|
| 59 | pLDeg=r->pLDeg; |
---|
| 60 | pLexOrder=r->LexOrder; |
---|
| 61 | |
---|
| 62 | if (complete) |
---|
| 63 | { |
---|
| 64 | test &= ~ TEST_RINGDEP_OPTS; |
---|
| 65 | test |= r->options; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // resets the pFDeg and pLDeg: if pLDeg is not given, it is |
---|
| 70 | // set to currRing->pLDegOrig, i.e. to the respective LDegProc which |
---|
| 71 | // only uses pFDeg (and not pDeg, or pTotalDegree, etc) |
---|
| 72 | void pSetDegProcs(pFDegProc new_FDeg, pLDegProc new_lDeg) |
---|
| 73 | { |
---|
| 74 | assume(new_FDeg != NULL); |
---|
| 75 | pFDeg = new_FDeg; |
---|
| 76 | currRing->pFDeg = new_FDeg; |
---|
| 77 | |
---|
| 78 | if (new_lDeg == NULL) |
---|
| 79 | new_lDeg = currRing->pLDegOrig; |
---|
| 80 | |
---|
| 81 | pLDeg = new_lDeg; |
---|
| 82 | currRing->pLDeg = new_lDeg; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | // restores pFDeg and pLDeg: |
---|
| 87 | extern void pRestoreDegProcs(pFDegProc old_FDeg, pLDegProc old_lDeg) |
---|
| 88 | { |
---|
| 89 | assume(old_FDeg != NULL && old_lDeg != NULL); |
---|
| 90 | pFDeg = old_FDeg; |
---|
| 91 | currRing->pFDeg = old_FDeg; |
---|
| 92 | pLDeg = old_lDeg; |
---|
| 93 | currRing->pLDeg = old_lDeg; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | /*2 |
---|
| 97 | * assumes that the head term of b is a multiple of the head term of a |
---|
| 98 | * and return the multiplicant *m |
---|
| 99 | */ |
---|
| 100 | poly pDivide(poly a, poly b) |
---|
| 101 | { |
---|
| 102 | int i; |
---|
| 103 | poly result = pInit(); |
---|
| 104 | |
---|
| 105 | for(i=(int)pVariables; i; i--) |
---|
| 106 | pSetExp(result,i, pGetExp(a,i)- pGetExp(b,i)); |
---|
| 107 | pSetComp(result, pGetComp(a) - pGetComp(b)); |
---|
| 108 | pSetm(result); |
---|
| 109 | return result; |
---|
| 110 | } |
---|
| 111 | |
---|
[206e158] | 112 | #ifdef HAVE_RINGS //TODO Oliver |
---|
[8251cc] | 113 | #define pDiv_nn(p, n) p_Div_nn(p, n, currRing) |
---|
| 114 | |
---|
| 115 | poly p_Div_nn(poly p, const number n, const ring r) |
---|
| 116 | { |
---|
| 117 | pAssume(!n_IsZero(n,r)); |
---|
| 118 | p_Test(p, r); |
---|
| 119 | |
---|
| 120 | poly q = p; |
---|
| 121 | while (p != NULL) |
---|
| 122 | { |
---|
| 123 | number nc = pGetCoeff(p); |
---|
| 124 | pSetCoeff0(p, n_Div(nc, n, r)); |
---|
| 125 | n_Delete(&nc, r); |
---|
| 126 | pIter(p); |
---|
| 127 | } |
---|
| 128 | p_Test(q, r); |
---|
| 129 | return q; |
---|
| 130 | } |
---|
| 131 | #endif |
---|
| 132 | |
---|
[35aab3] | 133 | /*2 |
---|
| 134 | * divides a by the monomial b, ignores monomials which are not divisible |
---|
| 135 | * assumes that b is not NULL |
---|
| 136 | */ |
---|
| 137 | poly pDivideM(poly a, poly b) |
---|
| 138 | { |
---|
| 139 | if (a==NULL) return NULL; |
---|
| 140 | poly result=a; |
---|
| 141 | poly prev=NULL; |
---|
| 142 | int i; |
---|
[009d80] | 143 | #ifdef HAVE_RINGS |
---|
| 144 | number inv=pGetCoeff(b); |
---|
[8251cc] | 145 | #else |
---|
[35aab3] | 146 | number inv=nInvers(pGetCoeff(b)); |
---|
[8251cc] | 147 | #endif |
---|
[35aab3] | 148 | |
---|
| 149 | while (a!=NULL) |
---|
| 150 | { |
---|
| 151 | if (pDivisibleBy(b,a)) |
---|
| 152 | { |
---|
| 153 | for(i=(int)pVariables; i; i--) |
---|
| 154 | pSubExp(a,i, pGetExp(b,i)); |
---|
| 155 | pSubComp(a, pGetComp(b)); |
---|
| 156 | pSetm(a); |
---|
| 157 | prev=a; |
---|
| 158 | pIter(a); |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | if (prev==NULL) |
---|
| 163 | { |
---|
| 164 | pDeleteLm(&result); |
---|
| 165 | a=result; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | pDeleteLm(&pNext(prev)); |
---|
| 170 | a=pNext(prev); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|
[009d80] | 174 | #ifdef HAVE_RINGS |
---|
| 175 | pDiv_nn(result,inv); |
---|
| 176 | #else |
---|
[35aab3] | 177 | pMult_nn(result,inv); |
---|
[009d80] | 178 | #endif |
---|
[35aab3] | 179 | nDelete(&inv); |
---|
| 180 | pDelete(&b); |
---|
| 181 | return result; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /*2 |
---|
| 185 | * returns the LCM of the head terms of a and b in *m |
---|
| 186 | */ |
---|
| 187 | void pLcm(poly a, poly b, poly m) |
---|
| 188 | { |
---|
| 189 | int i; |
---|
| 190 | for (i=pVariables; i; i--) |
---|
| 191 | { |
---|
| 192 | pSetExp(m,i, si_max( pGetExp(a,i), pGetExp(b,i))); |
---|
| 193 | } |
---|
| 194 | pSetComp(m, si_max(pGetComp(a), pGetComp(b))); |
---|
| 195 | /* Don't do a pSetm here, otherwise hres/lres chockes */ |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /*2 |
---|
| 199 | * convert monomial given as string to poly, e.g. 1x3y5z |
---|
| 200 | */ |
---|
| 201 | char * p_Read(char *st, poly &rc, ring r) |
---|
| 202 | { |
---|
| 203 | if (r==NULL) { rc=NULL;return st;} |
---|
| 204 | int i,j; |
---|
| 205 | rc = p_Init(r); |
---|
| 206 | char *s = r->cf->nRead(st,&(rc->coef)); |
---|
| 207 | if (s==st) |
---|
| 208 | /* i.e. it does not start with a coeff: test if it is a ringvar*/ |
---|
| 209 | { |
---|
| 210 | j = r_IsRingVar(s,r); |
---|
| 211 | if (j >= 0) |
---|
| 212 | { |
---|
| 213 | p_IncrExp(rc,1+j,r); |
---|
| 214 | while (*s!='\0') s++; |
---|
| 215 | goto done; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | while (*s!='\0') |
---|
| 219 | { |
---|
| 220 | char ss[2]; |
---|
| 221 | ss[0] = *s++; |
---|
| 222 | ss[1] = '\0'; |
---|
| 223 | j = r_IsRingVar(ss,r); |
---|
| 224 | if (j >= 0) |
---|
| 225 | { |
---|
[ded4ee] | 226 | char *s_save=s; |
---|
[35aab3] | 227 | s = eati(s,&i); |
---|
[ded4ee] | 228 | if (((unsigned long)i) > r->bitmask) |
---|
| 229 | { |
---|
| 230 | return s_save; |
---|
| 231 | } |
---|
[35aab3] | 232 | p_AddExp(rc,1+j, (Exponent_t)i, r); |
---|
| 233 | } |
---|
| 234 | else |
---|
| 235 | { |
---|
| 236 | s--; |
---|
| 237 | return s; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | done: |
---|
| 241 | if (r->cf->nIsZero(pGetCoeff(rc))) p_DeleteLm(&rc,r); |
---|
| 242 | else |
---|
| 243 | { |
---|
| 244 | p_Setm(rc,r); |
---|
| 245 | } |
---|
| 246 | return s; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | poly pmInit(char *st, BOOLEAN &ok) |
---|
| 250 | { |
---|
| 251 | poly p; |
---|
| 252 | char *s=p_Read(st,p,currRing); |
---|
| 253 | if (*s!='\0') |
---|
| 254 | { |
---|
| 255 | if ((s!=st)&&isdigit(st[0])) |
---|
| 256 | { |
---|
| 257 | errorreported=TRUE; |
---|
| 258 | } |
---|
| 259 | ok=FALSE; |
---|
| 260 | pDelete(&p); |
---|
| 261 | return NULL; |
---|
| 262 | } |
---|
| 263 | ok=!errorreported; |
---|
| 264 | return p; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /*2 |
---|
| 268 | *make p homogeneous by multiplying the monomials by powers of x_varnum |
---|
| 269 | */ |
---|
| 270 | poly pHomogen (poly p, int varnum) |
---|
| 271 | { |
---|
| 272 | poly q=NULL, qn; |
---|
| 273 | int o,ii; |
---|
| 274 | sBucket_pt bp; |
---|
| 275 | |
---|
| 276 | if (p!=NULL) |
---|
| 277 | { |
---|
| 278 | if ((varnum < 1) || (varnum > pVariables)) |
---|
| 279 | { |
---|
| 280 | return NULL; |
---|
| 281 | } |
---|
| 282 | o=pWTotaldegree(p); |
---|
| 283 | q=pNext(p); |
---|
| 284 | while (q != NULL) |
---|
| 285 | { |
---|
| 286 | ii=pWTotaldegree(q); |
---|
| 287 | if (ii>o) o=ii; |
---|
| 288 | pIter(q); |
---|
| 289 | } |
---|
| 290 | q = pCopy(p); |
---|
| 291 | bp = sBucketCreate(currRing); |
---|
| 292 | while (q != NULL) |
---|
| 293 | { |
---|
| 294 | ii = o-pWTotaldegree(q); |
---|
| 295 | if (ii!=0) |
---|
| 296 | { |
---|
| 297 | pAddExp(q,varnum, (Exponent_t)ii); |
---|
| 298 | pSetm(q); |
---|
| 299 | } |
---|
| 300 | qn = pNext(q); |
---|
| 301 | pNext(q) = NULL; |
---|
| 302 | sBucket_Add_p(bp, q, 1); |
---|
| 303 | q = qn; |
---|
| 304 | } |
---|
| 305 | sBucketDestroyAdd(bp, &q, &ii); |
---|
| 306 | } |
---|
| 307 | return q; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | /*2 |
---|
| 311 | *replaces the maximal powers of the leading monomial of p2 in p1 by |
---|
| 312 | *the same powers of n, utility for dehomogenization |
---|
| 313 | */ |
---|
| 314 | poly pDehomogen (poly p1,poly p2,number n) |
---|
| 315 | { |
---|
| 316 | polyset P; |
---|
| 317 | int SizeOfSet=5; |
---|
| 318 | int i; |
---|
| 319 | poly p; |
---|
| 320 | number nn; |
---|
| 321 | |
---|
| 322 | P = (polyset)omAlloc0(5*sizeof(poly)); |
---|
| 323 | //for (i=0; i<5; i++) |
---|
| 324 | //{ |
---|
| 325 | // P[i] = NULL; |
---|
| 326 | //} |
---|
| 327 | pCancelPolyByMonom(p1,p2,&P,&SizeOfSet); |
---|
| 328 | p = P[0]; |
---|
| 329 | //P[0] = NULL ;// for safety, may be removed later |
---|
| 330 | for (i=1; i<SizeOfSet; i++) |
---|
| 331 | { |
---|
| 332 | if (P[i] != NULL) |
---|
| 333 | { |
---|
| 334 | nPower(n,i,&nn); |
---|
| 335 | pMult_nn(P[i],nn); |
---|
| 336 | p = pAdd(p,P[i]); |
---|
| 337 | //P[i] =NULL; // for safety, may be removed later |
---|
| 338 | nDelete(&nn); |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | omFreeSize((ADDRESS)P,SizeOfSet*sizeof(poly)); |
---|
| 342 | return p; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /*4 |
---|
| 346 | *Returns the exponent of the maximal power of the leading monomial of |
---|
| 347 | *p2 in that of p1 |
---|
| 348 | */ |
---|
| 349 | static int pGetMaxPower (poly p1,poly p2) |
---|
| 350 | { |
---|
| 351 | int i,k,res = 32000; /*a very large integer*/ |
---|
| 352 | |
---|
| 353 | if (p1 == NULL) return 0; |
---|
| 354 | for (i=1; i<=pVariables; i++) |
---|
| 355 | { |
---|
| 356 | if ( pGetExp(p2,i) != 0) |
---|
| 357 | { |
---|
| 358 | k = pGetExp(p1,i) / pGetExp(p2,i); |
---|
| 359 | if (k < res) res = k; |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | return res; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | /*2 |
---|
| 366 | *Returns as i-th entry of P the coefficient of the (i-1) power of |
---|
| 367 | *the leading monomial of p2 in p1 |
---|
| 368 | */ |
---|
| 369 | void pCancelPolyByMonom (poly p1,poly p2,polyset * P,int * SizeOfSet) |
---|
| 370 | { |
---|
| 371 | int maxPow; |
---|
| 372 | poly p,qp,Coeff; |
---|
| 373 | |
---|
| 374 | if (*P == NULL) |
---|
| 375 | { |
---|
| 376 | *P = (polyset) omAlloc(5*sizeof(poly)); |
---|
| 377 | *SizeOfSet = 5; |
---|
| 378 | } |
---|
| 379 | p = pCopy(p1); |
---|
| 380 | while (p != NULL) |
---|
| 381 | { |
---|
| 382 | qp = p->next; |
---|
| 383 | p->next = NULL; |
---|
| 384 | maxPow = pGetMaxPower(p,p2); |
---|
| 385 | Coeff = pDivByMonom(p,p2); |
---|
| 386 | if (maxPow > *SizeOfSet) |
---|
| 387 | { |
---|
| 388 | pEnlargeSet(P,*SizeOfSet,maxPow+1-*SizeOfSet); |
---|
| 389 | *SizeOfSet = maxPow+1; |
---|
| 390 | } |
---|
| 391 | (*P)[maxPow] = pAdd((*P)[maxPow],Coeff); |
---|
| 392 | pDelete(&p); |
---|
| 393 | p = qp; |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | /*2 |
---|
| 398 | *returns the leading monomial of p1 divided by the maximal power of that |
---|
| 399 | *of p2 |
---|
| 400 | */ |
---|
| 401 | poly pDivByMonom (poly p1,poly p2) |
---|
| 402 | { |
---|
| 403 | int k, i; |
---|
| 404 | |
---|
| 405 | if (p1 == NULL) return NULL; |
---|
| 406 | k = pGetMaxPower(p1,p2); |
---|
| 407 | if (k == 0) |
---|
| 408 | return pHead(p1); |
---|
| 409 | else |
---|
| 410 | { |
---|
| 411 | number n; |
---|
| 412 | poly p = pInit(); |
---|
| 413 | |
---|
| 414 | p->next = NULL; |
---|
| 415 | for (i=1; i<=pVariables; i++) |
---|
| 416 | { |
---|
| 417 | pSetExp(p,i, pGetExp(p1,i)-k* pGetExp(p2,i)); |
---|
| 418 | } |
---|
| 419 | nPower(p2->coef,k,&n); |
---|
| 420 | pSetCoeff0(p,nDiv(p1->coef,n)); |
---|
| 421 | nDelete(&n); |
---|
| 422 | pSetm(p); |
---|
| 423 | return p; |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | /*----------utilities for syzygies--------------*/ |
---|
| 427 | poly pTakeOutComp(poly * p, int k) |
---|
| 428 | { |
---|
| 429 | poly q = *p,qq=NULL,result = NULL; |
---|
| 430 | |
---|
| 431 | if (q==NULL) return NULL; |
---|
| 432 | if (pGetComp(q)==k) |
---|
| 433 | { |
---|
| 434 | result = q; |
---|
[0ef842] | 435 | do |
---|
[35aab3] | 436 | { |
---|
| 437 | pSetComp(q,0); |
---|
| 438 | pSetmComp(q); |
---|
| 439 | qq = q; |
---|
| 440 | pIter(q); |
---|
| 441 | } |
---|
[0ef842] | 442 | while ((q!=NULL) && (pGetComp(q)==k)); |
---|
[35aab3] | 443 | *p = q; |
---|
| 444 | pNext(qq) = NULL; |
---|
| 445 | } |
---|
| 446 | if (q==NULL) return result; |
---|
| 447 | if (pGetComp(q) > k) |
---|
| 448 | { |
---|
| 449 | pDecrComp(q); |
---|
| 450 | pSetmComp(q); |
---|
| 451 | } |
---|
| 452 | poly pNext_q; |
---|
| 453 | while ((pNext_q=pNext(q))!=NULL) |
---|
| 454 | { |
---|
| 455 | if (pGetComp(pNext_q)==k) |
---|
| 456 | { |
---|
| 457 | if (result==NULL) |
---|
| 458 | { |
---|
| 459 | result = pNext_q; |
---|
| 460 | qq = result; |
---|
| 461 | } |
---|
| 462 | else |
---|
| 463 | { |
---|
| 464 | pNext(qq) = pNext_q; |
---|
| 465 | pIter(qq); |
---|
| 466 | } |
---|
| 467 | pNext(q) = pNext(pNext_q); |
---|
| 468 | pNext(qq) =NULL; |
---|
| 469 | pSetComp(qq,0); |
---|
| 470 | pSetmComp(qq); |
---|
| 471 | } |
---|
| 472 | else |
---|
| 473 | { |
---|
| 474 | /*pIter(q);*/ q=pNext_q; |
---|
| 475 | if (pGetComp(q) > k) |
---|
| 476 | { |
---|
| 477 | pDecrComp(q); |
---|
| 478 | pSetmComp(q); |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | } |
---|
| 482 | return result; |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | // Splits *p into two polys: *q which consists of all monoms with |
---|
| 486 | // component == comp and *p of all other monoms *lq == pLength(*q) |
---|
| 487 | void pTakeOutComp(poly *r_p, Exponent_t comp, poly *r_q, int *lq) |
---|
| 488 | { |
---|
| 489 | spolyrec pp, qq; |
---|
| 490 | poly p, q, p_prev; |
---|
| 491 | int l = 0; |
---|
| 492 | |
---|
| 493 | #ifdef HAVE_ASSUME |
---|
| 494 | int lp = pLength(*r_p); |
---|
| 495 | #endif |
---|
| 496 | |
---|
| 497 | pNext(&pp) = *r_p; |
---|
| 498 | p = *r_p; |
---|
| 499 | p_prev = &pp; |
---|
| 500 | q = &qq; |
---|
| 501 | |
---|
| 502 | while(p != NULL) |
---|
| 503 | { |
---|
| 504 | while (pGetComp(p) == comp) |
---|
| 505 | { |
---|
| 506 | pNext(q) = p; |
---|
| 507 | pIter(q); |
---|
| 508 | pSetComp(p, 0); |
---|
| 509 | pSetmComp(p); |
---|
| 510 | pIter(p); |
---|
| 511 | l++; |
---|
| 512 | if (p == NULL) |
---|
| 513 | { |
---|
| 514 | pNext(p_prev) = NULL; |
---|
| 515 | goto Finish; |
---|
| 516 | } |
---|
| 517 | } |
---|
| 518 | pNext(p_prev) = p; |
---|
| 519 | p_prev = p; |
---|
| 520 | pIter(p); |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | Finish: |
---|
| 524 | pNext(q) = NULL; |
---|
| 525 | *r_p = pNext(&pp); |
---|
| 526 | *r_q = pNext(&qq); |
---|
| 527 | *lq = l; |
---|
| 528 | #ifdef HAVE_ASSUME |
---|
| 529 | assume(pLength(*r_p) + pLength(*r_q) == lp); |
---|
| 530 | #endif |
---|
| 531 | pTest(*r_p); |
---|
| 532 | pTest(*r_q); |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | void pDecrOrdTakeOutComp(poly *r_p, Exponent_t comp, Order_t order, |
---|
| 536 | poly *r_q, int *lq) |
---|
| 537 | { |
---|
| 538 | spolyrec pp, qq; |
---|
| 539 | poly p, q, p_prev; |
---|
| 540 | int l = 0; |
---|
| 541 | |
---|
| 542 | pNext(&pp) = *r_p; |
---|
| 543 | p = *r_p; |
---|
| 544 | p_prev = &pp; |
---|
| 545 | q = &qq; |
---|
| 546 | |
---|
| 547 | #ifdef HAVE_ASSUME |
---|
| 548 | if (p != NULL) |
---|
| 549 | { |
---|
| 550 | while (pNext(p) != NULL) |
---|
| 551 | { |
---|
| 552 | assume(pGetOrder(p) >= pGetOrder(pNext(p))); |
---|
| 553 | pIter(p); |
---|
| 554 | } |
---|
| 555 | } |
---|
| 556 | p = *r_p; |
---|
| 557 | #endif |
---|
| 558 | |
---|
| 559 | while (p != NULL && pGetOrder(p) > order) pIter(p); |
---|
| 560 | |
---|
| 561 | while(p != NULL && pGetOrder(p) == order) |
---|
| 562 | { |
---|
| 563 | while (pGetComp(p) == comp) |
---|
| 564 | { |
---|
| 565 | pNext(q) = p; |
---|
| 566 | pIter(q); |
---|
| 567 | pIter(p); |
---|
| 568 | pSetComp(p, 0); |
---|
| 569 | pSetmComp(p); |
---|
| 570 | l++; |
---|
| 571 | if (p == NULL || pGetOrder(p) != order) |
---|
| 572 | { |
---|
| 573 | pNext(p_prev) = p; |
---|
| 574 | goto Finish; |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | pNext(p_prev) = p; |
---|
| 578 | p_prev = p; |
---|
| 579 | pIter(p); |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | Finish: |
---|
| 583 | pNext(q) = NULL; |
---|
| 584 | *r_p = pNext(&pp); |
---|
| 585 | *r_q = pNext(&qq); |
---|
| 586 | *lq = l; |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | #if 1 |
---|
| 590 | poly pTakeOutComp1(poly * p, int k) |
---|
| 591 | { |
---|
| 592 | poly q = *p; |
---|
| 593 | |
---|
| 594 | if (q==NULL) return NULL; |
---|
| 595 | |
---|
| 596 | poly qq=NULL,result = NULL; |
---|
| 597 | |
---|
| 598 | if (pGetComp(q)==k) |
---|
| 599 | { |
---|
| 600 | result = q; /* *p */ |
---|
| 601 | while ((q!=NULL) && (pGetComp(q)==k)) |
---|
| 602 | { |
---|
| 603 | pSetComp(q,0); |
---|
| 604 | pSetmComp(q); |
---|
| 605 | qq = q; |
---|
| 606 | pIter(q); |
---|
| 607 | } |
---|
| 608 | *p = q; |
---|
| 609 | pNext(qq) = NULL; |
---|
| 610 | } |
---|
| 611 | if (q==NULL) return result; |
---|
| 612 | // if (pGetComp(q) > k) pGetComp(q)--; |
---|
| 613 | while (pNext(q)!=NULL) |
---|
| 614 | { |
---|
| 615 | if (pGetComp(pNext(q))==k) |
---|
| 616 | { |
---|
| 617 | if (result==NULL) |
---|
| 618 | { |
---|
| 619 | result = pNext(q); |
---|
| 620 | qq = result; |
---|
| 621 | } |
---|
| 622 | else |
---|
| 623 | { |
---|
| 624 | pNext(qq) = pNext(q); |
---|
| 625 | pIter(qq); |
---|
| 626 | } |
---|
| 627 | pNext(q) = pNext(pNext(q)); |
---|
| 628 | pNext(qq) =NULL; |
---|
| 629 | pSetComp(qq,0); |
---|
| 630 | pSetmComp(qq); |
---|
| 631 | } |
---|
| 632 | else |
---|
| 633 | { |
---|
| 634 | pIter(q); |
---|
| 635 | // if (pGetComp(q) > k) pGetComp(q)--; |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | return result; |
---|
| 639 | } |
---|
| 640 | #endif |
---|
| 641 | |
---|
| 642 | void pDeleteComp(poly * p,int k) |
---|
| 643 | { |
---|
| 644 | poly q; |
---|
| 645 | |
---|
| 646 | while ((*p!=NULL) && (pGetComp(*p)==k)) pDeleteLm(p); |
---|
| 647 | if (*p==NULL) return; |
---|
| 648 | q = *p; |
---|
| 649 | if (pGetComp(q)>k) |
---|
| 650 | { |
---|
| 651 | pDecrComp(q); |
---|
| 652 | pSetmComp(q); |
---|
| 653 | } |
---|
| 654 | while (pNext(q)!=NULL) |
---|
| 655 | { |
---|
| 656 | if (pGetComp(pNext(q))==k) |
---|
| 657 | pDeleteLm(&(pNext(q))); |
---|
| 658 | else |
---|
| 659 | { |
---|
| 660 | pIter(q); |
---|
| 661 | if (pGetComp(q)>k) |
---|
| 662 | { |
---|
| 663 | pDecrComp(q); |
---|
| 664 | pSetmComp(q); |
---|
| 665 | } |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | } |
---|
| 669 | /*----------end of utilities for syzygies--------------*/ |
---|
| 670 | |
---|
| 671 | /*2 |
---|
| 672 | * pair has no common factor ? or is no polynomial |
---|
| 673 | */ |
---|
| 674 | BOOLEAN pHasNotCF(poly p1, poly p2) |
---|
| 675 | { |
---|
| 676 | |
---|
[225d94] | 677 | if (!TEST_OPT_IDLIFT) |
---|
| 678 | { |
---|
| 679 | if (pGetComp(p1) > 0 || pGetComp(p2) > 0) |
---|
| 680 | return FALSE; |
---|
| 681 | } |
---|
[35aab3] | 682 | int i = 1; |
---|
| 683 | loop |
---|
| 684 | { |
---|
| 685 | if ((pGetExp(p1, i) > 0) && (pGetExp(p2, i) > 0)) return FALSE; |
---|
| 686 | if (i == pVariables) return TRUE; |
---|
| 687 | i++; |
---|
| 688 | } |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | /*2 |
---|
| 692 | *divides p1 by its leading coefficient |
---|
| 693 | */ |
---|
| 694 | void pNorm(poly p1) |
---|
| 695 | { |
---|
| 696 | poly h; |
---|
| 697 | number k, c; |
---|
[009d80] | 698 | #ifdef HAVE_RINGS |
---|
| 699 | if (rField_is_Ring(currRing)) |
---|
[585bbcb] | 700 | { |
---|
[1e579c6] | 701 | if ((p1!=NULL) && rField_has_Units(currRing)) |
---|
[585bbcb] | 702 | { |
---|
| 703 | k = nGetUnit(pGetCoeff(p1)); |
---|
| 704 | if (!nIsOne(k)) |
---|
| 705 | { |
---|
| 706 | c = nDiv(pGetCoeff(p1), k); |
---|
[1e579c6] | 707 | nDelete(pGetCoeff(p1)); |
---|
[585bbcb] | 708 | pSetCoeff0(p1, c); |
---|
| 709 | h = pNext(p1); |
---|
| 710 | while (h != NULL) |
---|
| 711 | { |
---|
| 712 | c = nDiv(pGetCoeff(h), k); |
---|
[1e579c6] | 713 | nDelete(pGetCoeff(h)); |
---|
| 714 | pSetCoeff0(h, c); |
---|
[585bbcb] | 715 | pIter(h); |
---|
| 716 | } |
---|
| 717 | } |
---|
[1e579c6] | 718 | nDelete(&k); |
---|
[585bbcb] | 719 | } |
---|
[1e579c6] | 720 | return; |
---|
[585bbcb] | 721 | } |
---|
[994445] | 722 | else |
---|
[f92547] | 723 | #endif |
---|
[35aab3] | 724 | if (p1!=NULL) |
---|
| 725 | { |
---|
[5e8fe91] | 726 | if (pNext(p1)==NULL) |
---|
| 727 | { |
---|
[98938c] | 728 | pSetCoeff(p1,nInit(1)); |
---|
[5e8fe91] | 729 | return; |
---|
| 730 | } |
---|
[35aab3] | 731 | if (!nIsOne(pGetCoeff(p1))) |
---|
| 732 | { |
---|
| 733 | nNormalize(pGetCoeff(p1)); |
---|
[585bbcb] | 734 | k = pGetCoeff(p1); |
---|
[35aab3] | 735 | c = nInit(1); |
---|
| 736 | pSetCoeff0(p1,c); |
---|
| 737 | h = pNext(p1); |
---|
| 738 | while (h!=NULL) |
---|
| 739 | { |
---|
| 740 | c=nDiv(pGetCoeff(h),k); |
---|
| 741 | if (!nIsOne(c)) nNormalize(c); |
---|
| 742 | pSetCoeff(h,c); |
---|
| 743 | pIter(h); |
---|
| 744 | } |
---|
| 745 | nDelete(&k); |
---|
| 746 | } |
---|
| 747 | else |
---|
| 748 | { |
---|
| 749 | if (nNormalize != nDummy2) |
---|
| 750 | { |
---|
| 751 | h = pNext(p1); |
---|
| 752 | while (h!=NULL) |
---|
| 753 | { |
---|
| 754 | nNormalize(pGetCoeff(h)); |
---|
| 755 | pIter(h); |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | } |
---|
| 759 | } |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | /*2 |
---|
| 763 | *normalize all coefficients |
---|
| 764 | */ |
---|
[585bbcb] | 765 | void p_Normalize(poly p, ring r) |
---|
[35aab3] | 766 | { |
---|
| 767 | if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */ |
---|
| 768 | while (p!=NULL) |
---|
| 769 | { |
---|
| 770 | if (currRing==r) {nTest(pGetCoeff(p));} |
---|
| 771 | n_Normalize(pGetCoeff(p),r); |
---|
| 772 | pIter(p); |
---|
| 773 | } |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | // splits p into polys with Exp(n) == 0 and Exp(n) != 0 |
---|
| 777 | // Poly with Exp(n) != 0 is reversed |
---|
| 778 | static void pSplitAndReversePoly(poly p, int n, poly *non_zero, poly *zero) |
---|
| 779 | { |
---|
| 780 | if (p == NULL) |
---|
| 781 | { |
---|
| 782 | *non_zero = NULL; |
---|
| 783 | *zero = NULL; |
---|
| 784 | return; |
---|
| 785 | } |
---|
| 786 | spolyrec sz; |
---|
| 787 | poly z, n_z, next; |
---|
| 788 | z = &sz; |
---|
| 789 | n_z = NULL; |
---|
| 790 | |
---|
| 791 | while(p != NULL) |
---|
| 792 | { |
---|
| 793 | next = pNext(p); |
---|
| 794 | if (pGetExp(p, n) == 0) |
---|
| 795 | { |
---|
| 796 | pNext(z) = p; |
---|
| 797 | pIter(z); |
---|
| 798 | } |
---|
| 799 | else |
---|
| 800 | { |
---|
| 801 | pNext(p) = n_z; |
---|
| 802 | n_z = p; |
---|
| 803 | } |
---|
| 804 | p = next; |
---|
| 805 | } |
---|
| 806 | pNext(z) = NULL; |
---|
| 807 | *zero = pNext(&sz); |
---|
| 808 | *non_zero = n_z; |
---|
| 809 | return; |
---|
| 810 | } |
---|
| 811 | |
---|
| 812 | /*3 |
---|
| 813 | * substitute the n-th variable by 1 in p |
---|
| 814 | * destroy p |
---|
| 815 | */ |
---|
| 816 | static poly pSubst1 (poly p,int n) |
---|
| 817 | { |
---|
[788529d] | 818 | poly qq=NULL, result = NULL; |
---|
| 819 | poly zero=NULL, non_zero=NULL; |
---|
[35aab3] | 820 | |
---|
| 821 | // reverse, so that add is likely to be linear |
---|
| 822 | pSplitAndReversePoly(p, n, &non_zero, &zero); |
---|
| 823 | |
---|
| 824 | while (non_zero != NULL) |
---|
| 825 | { |
---|
| 826 | assume(pGetExp(non_zero, n) != 0); |
---|
| 827 | qq = non_zero; |
---|
| 828 | pIter(non_zero); |
---|
| 829 | qq->next = NULL; |
---|
| 830 | pSetExp(qq,n,0); |
---|
| 831 | pSetm(qq); |
---|
| 832 | result = pAdd(result,qq); |
---|
| 833 | } |
---|
| 834 | p = pAdd(result, zero); |
---|
| 835 | pTest(p); |
---|
| 836 | return p; |
---|
| 837 | } |
---|
| 838 | |
---|
| 839 | /*3 |
---|
| 840 | * substitute the n-th variable by number e in p |
---|
| 841 | * destroy p |
---|
| 842 | */ |
---|
| 843 | static poly pSubst2 (poly p,int n, number e) |
---|
| 844 | { |
---|
| 845 | assume( ! nIsZero(e) ); |
---|
| 846 | poly qq,result = NULL; |
---|
| 847 | number nn, nm; |
---|
| 848 | poly zero, non_zero; |
---|
| 849 | |
---|
| 850 | // reverse, so that add is likely to be linear |
---|
| 851 | pSplitAndReversePoly(p, n, &non_zero, &zero); |
---|
| 852 | |
---|
| 853 | while (non_zero != NULL) |
---|
| 854 | { |
---|
| 855 | assume(pGetExp(non_zero, n) != 0); |
---|
| 856 | qq = non_zero; |
---|
| 857 | pIter(non_zero); |
---|
| 858 | qq->next = NULL; |
---|
| 859 | nPower(e, pGetExp(qq, n), &nn); |
---|
| 860 | nm = nMult(nn, pGetCoeff(qq)); |
---|
| 861 | pSetCoeff(qq, nm); |
---|
| 862 | nDelete(&nn); |
---|
| 863 | pSetExp(qq, n, 0); |
---|
| 864 | pSetm(qq); |
---|
| 865 | result = pAdd(result,qq); |
---|
| 866 | } |
---|
| 867 | p = pAdd(result, zero); |
---|
| 868 | pTest(p); |
---|
| 869 | return p; |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | |
---|
| 873 | /* delete monoms whose n-th exponent is different from zero */ |
---|
| 874 | poly pSubst0(poly p, int n) |
---|
| 875 | { |
---|
| 876 | spolyrec res; |
---|
| 877 | poly h = &res; |
---|
| 878 | pNext(h) = p; |
---|
| 879 | |
---|
| 880 | while (pNext(h)!=NULL) |
---|
| 881 | { |
---|
| 882 | if (pGetExp(pNext(h),n)!=0) |
---|
| 883 | { |
---|
| 884 | pDeleteLm(&pNext(h)); |
---|
| 885 | } |
---|
| 886 | else |
---|
| 887 | { |
---|
| 888 | pIter(h); |
---|
| 889 | } |
---|
| 890 | } |
---|
| 891 | pTest(pNext(&res)); |
---|
| 892 | return pNext(&res); |
---|
| 893 | } |
---|
| 894 | |
---|
| 895 | /*2 |
---|
| 896 | * substitute the n-th variable by e in p |
---|
| 897 | * destroy p |
---|
| 898 | */ |
---|
| 899 | poly pSubst(poly p, int n, poly e) |
---|
| 900 | { |
---|
| 901 | if (e == NULL) return pSubst0(p, n); |
---|
| 902 | |
---|
| 903 | if (pIsConstant(e)) |
---|
| 904 | { |
---|
| 905 | if (nIsOne(pGetCoeff(e))) return pSubst1(p,n); |
---|
| 906 | else return pSubst2(p, n, pGetCoeff(e)); |
---|
| 907 | } |
---|
| 908 | |
---|
[68349d] | 909 | #ifdef HAVE_PLURAL |
---|
| 910 | if (rIsPluralRing(currRing)) |
---|
| 911 | { |
---|
| 912 | return nc_pSubst(p,n,e); |
---|
| 913 | } |
---|
| 914 | #endif |
---|
| 915 | |
---|
[35aab3] | 916 | int exponent,i; |
---|
| 917 | poly h, res, m; |
---|
| 918 | int *me,*ee; |
---|
| 919 | number nu,nu1; |
---|
| 920 | |
---|
| 921 | me=(int *)omAlloc((pVariables+1)*sizeof(int)); |
---|
| 922 | ee=(int *)omAlloc((pVariables+1)*sizeof(int)); |
---|
| 923 | if (e!=NULL) pGetExpV(e,ee); |
---|
| 924 | res=NULL; |
---|
| 925 | h=p; |
---|
| 926 | while (h!=NULL) |
---|
| 927 | { |
---|
| 928 | if ((e!=NULL) || (pGetExp(h,n)==0)) |
---|
| 929 | { |
---|
| 930 | m=pHead(h); |
---|
| 931 | pGetExpV(m,me); |
---|
| 932 | exponent=me[n]; |
---|
| 933 | me[n]=0; |
---|
| 934 | for(i=pVariables;i>0;i--) |
---|
| 935 | me[i]+=exponent*ee[i]; |
---|
| 936 | pSetExpV(m,me); |
---|
| 937 | if (e!=NULL) |
---|
| 938 | { |
---|
| 939 | nPower(pGetCoeff(e),exponent,&nu); |
---|
| 940 | nu1=nMult(pGetCoeff(m),nu); |
---|
| 941 | nDelete(&nu); |
---|
| 942 | pSetCoeff(m,nu1); |
---|
| 943 | } |
---|
| 944 | res=pAdd(res,m); |
---|
| 945 | } |
---|
| 946 | pDeleteLm(&h); |
---|
| 947 | } |
---|
| 948 | omFreeSize((ADDRESS)me,(pVariables+1)*sizeof(int)); |
---|
| 949 | omFreeSize((ADDRESS)ee,(pVariables+1)*sizeof(int)); |
---|
| 950 | return res; |
---|
| 951 | } |
---|
| 952 | |
---|
[a2466f] | 953 | /* Returns TRUE if |
---|
| 954 | * LM(p) | LM(lcm) |
---|
| 955 | * LC(p) | LC(lcm) only if ring |
---|
| 956 | * Exists i, j: |
---|
| 957 | * LE(p, i) != LE(lcm, i) |
---|
| 958 | * LE(p1, i) != LE(lcm, i) ==> LCM(p1, p) != lcm |
---|
| 959 | * LE(p, j) != LE(lcm, j) |
---|
| 960 | * LE(p2, j) != LE(lcm, j) ==> LCM(p2, p) != lcm |
---|
| 961 | */ |
---|
[35aab3] | 962 | BOOLEAN pCompareChain (poly p,poly p1,poly p2,poly lcm) |
---|
| 963 | { |
---|
| 964 | int k, j; |
---|
| 965 | |
---|
| 966 | if (lcm==NULL) return FALSE; |
---|
| 967 | |
---|
| 968 | for (j=pVariables; j; j--) |
---|
| 969 | if ( pGetExp(p,j) > pGetExp(lcm,j)) return FALSE; |
---|
| 970 | if ( pGetComp(p) != pGetComp(lcm)) return FALSE; |
---|
| 971 | for (j=pVariables; j; j--) |
---|
| 972 | { |
---|
| 973 | if (pGetExp(p1,j)!=pGetExp(lcm,j)) |
---|
| 974 | { |
---|
| 975 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
| 976 | { |
---|
| 977 | for (k=pVariables; k>j; k--) |
---|
| 978 | { |
---|
| 979 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
| 980 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
| 981 | return TRUE; |
---|
| 982 | } |
---|
| 983 | for (k=j-1; k; k--) |
---|
| 984 | { |
---|
| 985 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
| 986 | && (pGetExp(p2,k)!=pGetExp(lcm,k))) |
---|
| 987 | return TRUE; |
---|
| 988 | } |
---|
| 989 | return FALSE; |
---|
| 990 | } |
---|
| 991 | } |
---|
| 992 | else if (pGetExp(p2,j)!=pGetExp(lcm,j)) |
---|
| 993 | { |
---|
| 994 | if (pGetExp(p,j)!=pGetExp(lcm,j)) |
---|
| 995 | { |
---|
| 996 | for (k=pVariables; k>j; k--) |
---|
| 997 | { |
---|
| 998 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
| 999 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
| 1000 | return TRUE; |
---|
| 1001 | } |
---|
| 1002 | for (k=j-1; k!=0 ; k--) |
---|
| 1003 | { |
---|
| 1004 | if ((pGetExp(p,k)!=pGetExp(lcm,k)) |
---|
| 1005 | && (pGetExp(p1,k)!=pGetExp(lcm,k))) |
---|
| 1006 | return TRUE; |
---|
| 1007 | } |
---|
| 1008 | return FALSE; |
---|
| 1009 | } |
---|
| 1010 | } |
---|
| 1011 | } |
---|
| 1012 | return FALSE; |
---|
| 1013 | } |
---|