source: git/kernel/kspoly.cc @ c0f7e2

spielwiese
Last change on this file since c0f7e2 was 93ebe1, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: rings git-svn-id: file:///usr/local/Singular/svn/trunk@10794 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: kspoly.cc,v 1.15 2008-06-25 11:27:31 Singular Exp $ */
5/*
6*  ABSTRACT -  Routines for Spoly creation and reductions
7*/
8
9// #define PDEBUG 2
10#include "mod2.h"
11#include "kutil.h"
12#include "numbers.h"
13#include "p_polys.h"
14#include "p_Procs.h"
15#include "gring.h"
16#ifdef HAVE_RINGS
17#include "polys.h"
18#endif
19
20#ifdef KDEBUG
21int red_count = 0;
22int create_count = 0;
23// define this if reductions are reported on TEST_OPT_DEBUG
24// #define TEST_OPT_DEBUG_RED
25#endif
26
27/***************************************************************
28 *
29 * Reduces PR with PW
30 * Assumes PR != NULL, PW != NULL, Lm(PW) divides Lm(PR)
31 *
32 ***************************************************************/
33int ksReducePoly(LObject* PR,
34                 TObject* PW,
35                 poly spNoether,
36                 number *coef,
37                 kStrategy strat)
38{
39#ifdef KDEBUG
40  red_count++;
41#ifdef TEST_OPT_DEBUG_RED
42  if (TEST_OPT_DEBUG)
43  {
44    Print("Red %d:", red_count); PR->wrp(); Print(" with:");
45    PW->wrp();
46  }
47#endif
48#endif
49  int ret = 0;
50  ring tailRing = PR->tailRing;
51  kTest_L(PR);
52  kTest_T(PW);
53
54  poly p1 = PR->GetLmTailRing();   // p2 | p1
55  poly p2 = PW->GetLmTailRing();   // i.e. will reduce p1 with p2; lm = LT(p1) / LM(p2)
56  poly t2 = pNext(p2), lm = p1;    // t2 = p2 - LT(p2); really compute P = LC(p2)*p1 - LT(p1)/LM(p2)*p2
57  assume(p1 != NULL && p2 != NULL);// Attention, we have rings and there LC(p2) and LC(p1) are special
58  p_CheckPolyRing(p1, tailRing);
59  p_CheckPolyRing(p2, tailRing);
60
61  pAssume1(p2 != NULL && p1 != NULL &&
62           p_DivisibleBy(p2,  p1, tailRing));
63
64  pAssume1(p_GetComp(p1, tailRing) == p_GetComp(p2, tailRing) ||
65           (p_GetComp(p2, tailRing) == 0 &&
66            p_MaxComp(pNext(p2),tailRing) == 0));
67
68#ifdef HAVE_PLURAL
69  if (rIsPluralRing(currRing))
70  {
71    // for the time being: we know currRing==strat->tailRing
72    // no exp-bound checking needed
73    // (only needed if exp-bound(tailring)<exp-b(currRing))
74    number c;
75    if (PR->bucket!=NULL)  nc_kBucketPolyRed(PR->bucket, p2,&c);
76    else 
77    {
78      poly _p = (PR->t_p != NULL ? PR->t_p : PR->p);
79      assume(_p != NULL);
80      nc_PolyPolyRed(_p, p2,&c);
81      if (PR->t_p!=NULL) PR->t_p=_p; else PR->p=_p;
82      PR->pLength=pLength(_p);
83    }
84    if (coef!=NULL) *coef=c;
85    else nDelete(&c);
86    return 0;
87  }
88#endif
89
90  if (t2==NULL)           // Divisor is just one term, therefore it will
91  {                       // just cancel the leading term
92    PR->LmDeleteAndIter();
93    if (coef != NULL) *coef = n_Init(1, tailRing);
94    return 0;
95  }
96
97  p_ExpVectorSub(lm, p2, tailRing); // Calculate the Monomial we must multiply to p2
98
99  if (tailRing != currRing)
100  {
101    // check that reduction does not violate exp bound
102    while (PW->max != NULL && !p_LmExpVectorAddIsOk(lm, PW->max, tailRing))
103    {
104      // undo changes of lm
105      p_ExpVectorAdd(lm, p2, tailRing);
106      if (strat == NULL) return 2;
107      if (! kStratChangeTailRing(strat, PR, PW)) return -1;
108      tailRing = strat->tailRing;
109      p1 = PR->GetLmTailRing();
110      p2 = PW->GetLmTailRing();
111      t2 = pNext(p2);
112      lm = p1;
113      p_ExpVectorSub(lm, p2, tailRing);
114      ret = 1;
115    }
116  }
117
118  // take care of coef buisness
119  if (! n_IsOne(pGetCoeff(p2), tailRing))
120  {
121    number bn = pGetCoeff(lm);
122    number an = pGetCoeff(p2);
123    int ct = ksCheckCoeff(&an, &bn);    // Calculate special LC
124    p_SetCoeff(lm, bn, tailRing);
125    if ((ct == 0) || (ct == 2))
126      PR->Tail_Mult_nn(an);
127    if (coef != NULL) *coef = an;
128    else n_Delete(&an, tailRing);
129  }
130  else
131  {
132    if (coef != NULL) *coef = n_Init(1, tailRing);
133  }
134
135
136  // and finally,
137  PR->Tail_Minus_mm_Mult_qq(lm, t2, PW->GetpLength() - 1, spNoether);
138  assume(PW->GetpLength() == pLength(PW->p != NULL ? PW->p : PW->t_p));
139  PR->LmDeleteAndIter();
140#if defined(KDEBUG) && defined(TEST_OPT_DEBUG_RED)
141  if (TEST_OPT_DEBUG)
142  {
143    Print(" to: "); PR->wrp(); Print("\n");
144  }
145#endif
146  return ret;
147}
148
149/***************************************************************
150 *
151 * Creates S-Poly of p1 and p2
152 *
153 *
154 ***************************************************************/
155void ksCreateSpoly(LObject* Pair,   poly spNoether,
156                   int use_buckets, ring tailRing,
157                   poly m1, poly m2, TObject** R)
158{
159#ifdef KDEBUG
160  create_count++;
161#endif
162  kTest_L(Pair);
163  poly p1 = Pair->p1;
164  poly p2 = Pair->p2;
165  poly last;
166  Pair->tailRing = tailRing;
167
168  assume(p1 != NULL);
169  assume(p2 != NULL);
170  assume(tailRing != NULL);
171
172  poly a1 = pNext(p1), a2 = pNext(p2);
173  number lc1 = pGetCoeff(p1), lc2 = pGetCoeff(p2);
174  int co=0, ct = ksCheckCoeff(&lc1, &lc2); // gcd and zero divisors
175
176  int l1=0, l2=0;
177
178  if (p_GetComp(p1, currRing)!=p_GetComp(p2, currRing))
179  {
180    if (p_GetComp(p1, currRing)==0)
181    {
182      co=1;
183      p_SetCompP(p1,p_GetComp(p2, currRing), currRing, tailRing);
184    }
185    else
186    {
187      co=2;
188      p_SetCompP(p2, p_GetComp(p1, currRing), currRing, tailRing);
189    }
190  }
191
192  // get m1 = LCM(LM(p1), LM(p2))/LM(p1)
193  //     m2 = LCM(LM(p1), LM(p2))/LM(p2)
194  if (m1 == NULL)
195    k_GetLeadTerms(p1, p2, currRing, m1, m2, tailRing);
196
197  pSetCoeff0(m1, lc2);
198  pSetCoeff0(m2, lc1);  // and now, m1 * LT(p1) == m2 * LT(p2)
199
200  if (R != NULL)
201  {
202    if (Pair->i_r1 == -1)
203    {
204      l1 = pLength(p1) - 1;
205    }
206    else
207    {
208      l1 = (R[Pair->i_r1])->GetpLength() - 1;
209    }
210    if (Pair->i_r2 == -1)
211    {
212      l2 = pLength(p2) - 1;
213    }
214    else
215    {
216      l2 = (R[Pair->i_r2])->GetpLength() - 1;
217    }
218  }
219
220  // get m2 * a2
221  if (spNoether != NULL)
222  {
223    l2 = -1;
224    a2 = tailRing->p_Procs->pp_Mult_mm_Noether(a2, m2, spNoether, l2, tailRing,last);
225    assume(l2 == pLength(a2));
226  }
227  else
228    a2 = tailRing->p_Procs->pp_Mult_mm(a2, m2, tailRing,last);
229#ifdef HAVE_RINGS
230  if (!(rField_is_Domain(currRing))) l2 = pLength(a2);
231#endif
232
233  Pair->SetLmTail(m2, a2, l2, use_buckets, tailRing, last);
234
235  // get m2*a2 - m1*a1
236  Pair->Tail_Minus_mm_Mult_qq(m1, a1, l1, spNoether);
237
238  // Clean-up time
239  Pair->LmDeleteAndIter();
240  p_LmDelete(m1, tailRing);
241
242  if (co != 0)
243  {
244    if (co==1)
245    {
246      p_SetCompP(p1,0, currRing, tailRing);
247    }
248    else
249    {
250      p_SetCompP(p2,0, currRing, tailRing);
251    }
252  }
253}
254
255int ksReducePolyTail(LObject* PR, TObject* PW, poly Current, poly spNoether)
256{
257  BOOLEAN ret;
258  number coef;
259  poly Lp =     PR->GetLmCurrRing();
260  poly Save =   PW->GetLmCurrRing();
261
262  kTest_L(PR);
263  kTest_T(PW);
264  pAssume(pIsMonomOf(Lp, Current));
265
266  assume(Lp != NULL && Current != NULL && pNext(Current) != NULL);
267  assume(PR->bucket == NULL);
268
269  LObject Red(pNext(Current), PR->tailRing);
270  TObject With(PW, Lp == Save);
271
272  pAssume(!pHaveCommonMonoms(Red.p, With.p));
273  ret = ksReducePoly(&Red, &With, spNoether, &coef);
274
275  if (!ret)
276  {
277    if (! n_IsOne(coef, currRing))
278    {
279      pNext(Current) = NULL;
280      if (Current == PR->p && PR->t_p != NULL)
281        pNext(PR->t_p) = NULL;
282      PR->Mult_nn(coef);
283    }
284
285    n_Delete(&coef, currRing);
286    pNext(Current) = Red.GetLmTailRing();
287    if (Current == PR->p && PR->t_p != NULL)
288      pNext(PR->t_p) = pNext(Current);
289  }
290
291  if (Lp == Save)
292    With.Delete();
293  return ret;
294}
295
296/***************************************************************
297 *
298 * Auxillary Routines
299 *
300 *
301 ***************************************************************/
302
303/*
304* input - output: a, b
305* returns:
306*   a := a/gcd(a,b), b := b/gcd(a,b)
307*   and return value
308*       0  ->  a != 1,  b != 1
309*       1  ->  a == 1,  b != 1
310*       2  ->  a != 1,  b == 1
311*       3  ->  a == 1,  b == 1
312*   this value is used to control the spolys
313*/
314int ksCheckCoeff(number *a, number *b)
315{
316  int c = 0;
317  number an = *a, bn = *b;
318  nTest(an);
319  nTest(bn);
320
321  number cn = nGcd(an, bn, currRing);
322
323  if(nIsOne(cn))
324  {
325    an = nCopy(an);
326    bn = nCopy(bn);
327  }
328  else
329  {
330    an = nIntDiv(an, cn);
331    bn = nIntDiv(bn, cn);
332  }
333  nDelete(&cn);
334  if (nIsOne(an))
335  {
336    c = 1;
337  }
338  if (nIsOne(bn))
339  {
340    c += 2;
341  }
342  *a = an;
343  *b = bn;
344  return c;
345}
346
347/*2
348* creates the leading term of the S-polynomial of p1 and p2
349* do not destroy p1 and p2
350* remarks:
351*   1. the coefficient is 0 (nNew)
352*   1. a) in the case of coefficient ring, the coefficient is calculated
353*   2. pNext is undefined
354*/
355//static void bbb() { int i=0; }
356poly ksCreateShortSpoly(poly p1, poly p2, ring tailRing)
357{
358  poly a1 = pNext(p1), a2 = pNext(p2);
359  Exponent_t c1=p_GetComp(p1, currRing),c2=p_GetComp(p2, currRing);
360  Exponent_t c;
361  poly m1,m2;
362  number t1 = NULL,t2 = NULL;
363  int cm,i;
364  BOOLEAN equal;
365
366#ifdef HAVE_RINGS
367  BOOLEAN is_Ring=rField_is_Ring(currRing);
368  number lc1 = pGetCoeff(p1), lc2 = pGetCoeff(p2);
369  if (is_Ring)
370  {
371    ksCheckCoeff(&lc1, &lc2); // gcd and zero divisors
372    if (a1 != NULL) t2 = nMult(pGetCoeff(a1),lc2);
373    if (a2 != NULL) t1 = nMult(pGetCoeff(a2),lc1);
374    while (a1 != NULL && nIsZero(t2))
375    {
376      pIter(a1);
377      nDelete(&t2);
378      if (a1 != NULL) t2 = nMult(pGetCoeff(a1),lc2);
379    }
380    while (a2 != NULL && nIsZero(t1))
381    {
382      pIter(a2);
383      nDelete(&t1);
384      if (a2 != NULL) t1 = nMult(pGetCoeff(a2),lc1);
385    }
386  }
387#endif
388
389  if (a1==NULL)
390  {
391    if(a2!=NULL)
392    {
393      m2=p_Init(currRing);
394x2:
395      for (i = pVariables; i; i--)
396      {
397        c = p_GetExpDiff(p1, p2,i, currRing);
398        if (c>0)
399        {
400          p_SetExp(m2,i,(c+p_GetExp(a2,i,tailRing)),currRing);
401        }
402        else
403        {
404          p_SetExp(m2,i,p_GetExp(a2,i,tailRing),currRing);
405        }
406      }
407      if ((c1==c2)||(c2!=0))
408      {
409        p_SetComp(m2,p_GetComp(a2,tailRing), currRing);
410      }
411      else
412      {
413        p_SetComp(m2,c1,currRing);
414      }
415      p_Setm(m2, currRing);
416#ifdef HAVE_RINGS
417      if (is_Ring)
418      {
419          nDelete(&lc1);
420          nDelete(&lc2);
421          nDelete(&t2);
422          pSetCoeff0(m2, t1);
423      }
424      else
425#endif
426        nNew(&(pGetCoeff(m2)));
427      return m2;
428    }
429    else
430    {
431#ifdef HAVE_RINGS
432    if (is_Ring)
433    {
434      nDelete(&lc1);
435      nDelete(&lc2);
436      nDelete(&t1);
437      nDelete(&t2);
438    }
439#endif
440      return NULL;
441    }
442  }
443  if (a2==NULL)
444  {
445    m1=p_Init(currRing);
446x1:
447    for (i = pVariables; i; i--)
448    {
449      c = p_GetExpDiff(p2, p1,i,currRing);
450      if (c>0)
451      {
452        p_SetExp(m1,i,(c+p_GetExp(a1,i, tailRing)),currRing);
453      }
454      else
455      {
456        p_SetExp(m1,i,p_GetExp(a1,i, tailRing), currRing);
457      }
458    }
459    if ((c1==c2)||(c1!=0))
460    {
461      p_SetComp(m1,p_GetComp(a1,tailRing),currRing);
462    }
463    else
464    {
465      p_SetComp(m1,c2,currRing);
466    }
467    p_Setm(m1, currRing);
468#ifdef HAVE_RINGS
469    if (is_Ring)
470    {
471      pSetCoeff0(m1, t2);
472      nDelete(&lc1);
473      nDelete(&lc2);
474      nDelete(&t1);
475    }
476    else
477#endif
478      nNew(&(pGetCoeff(m1)));
479    return m1;
480  }
481  m1 = p_Init(currRing);
482  m2 = p_Init(currRing);
483  for(;;)
484  {
485    for (i = pVariables; i; i--)
486    {
487      c = p_GetExpDiff(p1, p2,i,currRing);
488      if (c > 0)
489      {
490        p_SetExp(m2,i,(c+p_GetExp(a2,i,tailRing)), currRing);
491        p_SetExp(m1,i,p_GetExp(a1,i, tailRing), currRing);
492      }
493      else
494      {
495        p_SetExp(m1,i,(p_GetExp(a1,i,tailRing)-c), currRing);
496        p_SetExp(m2,i,p_GetExp(a2,i, tailRing), currRing);
497      }
498    }
499    if(c1==c2)
500    {
501      p_SetComp(m1,p_GetComp(a1, tailRing), currRing);
502      p_SetComp(m2,p_GetComp(a2, tailRing), currRing);
503    }
504    else
505    {
506      if(c1!=0)
507      {
508        p_SetComp(m1,p_GetComp(a1, tailRing), currRing);
509        p_SetComp(m2,c1, currRing);
510      }
511      else
512      {
513        p_SetComp(m2,p_GetComp(a2, tailRing), currRing);
514        p_SetComp(m1,c2, currRing);
515      }
516    }
517    p_Setm(m1,currRing);
518    p_Setm(m2,currRing);
519    cm = p_LmCmp(m1, m2,currRing);
520    if (cm!=0)
521    {
522      if(cm==1)
523      {
524        p_LmFree(m2,currRing);
525#ifdef HAVE_RINGS
526        if (is_Ring)
527        {
528          pSetCoeff0(m1, t2);
529          nDelete(&lc1);
530          nDelete(&lc2);
531          nDelete(&t1);
532        }
533        else
534#endif
535          nNew(&(pGetCoeff(m1)));
536        return m1;
537      }
538      else
539      {
540        p_LmFree(m1,currRing);
541#ifdef HAVE_RINGS
542        if (is_Ring)
543        {
544          pSetCoeff0(m2, t1);
545          nDelete(&lc1);
546          nDelete(&lc2);
547          nDelete(&t2);
548        }
549        else
550#endif
551          nNew(&(pGetCoeff(m2)));
552        return m2;
553      }
554    }
555#ifdef HAVE_RINGS
556    if (is_Ring)
557    {
558      equal = nEqual(t1,t2);
559    }
560    else
561#endif
562    {
563      t1 = nMult(pGetCoeff(a2),pGetCoeff(p1));
564      t2 = nMult(pGetCoeff(a1),pGetCoeff(p2));
565      equal = nEqual(t1,t2);
566      nDelete(&t2);
567      nDelete(&t1);
568    }
569    if (!equal)
570    {
571      p_LmFree(m2,currRing);
572#ifdef HAVE_RINGS
573      if (is_Ring)
574      {
575          pSetCoeff0(m1, nSub(t1, t2));
576          nDelete(&lc1);
577          nDelete(&lc2);
578          nDelete(&t1);
579          nDelete(&t2);
580      }
581      else
582#endif
583        nNew(&(pGetCoeff(m1)));
584      return m1;
585    }
586    pIter(a1);
587    pIter(a2);
588#ifdef HAVE_RINGS
589    if (is_Ring)
590    {
591      if (a2 != NULL)
592      {
593        nDelete(&t1);
594        t1 = nMult(pGetCoeff(a2),lc1);
595      }
596      if (a1 != NULL)
597      {
598        nDelete(&t2);
599        t2 = nMult(pGetCoeff(a1),lc2);
600      }
601      while ((a1 != NULL) && nIsZero(t2))
602      {
603        pIter(a1);
604        if (a1 != NULL)
605        {
606          nDelete(&t2);
607          t2 = nMult(pGetCoeff(a1),lc2);
608        }
609      }
610      while ((a2 != NULL) && nIsZero(t1))
611      {
612        pIter(a2);
613        if (a2 != NULL)
614        {
615          nDelete(&t1);
616          t1 = nMult(pGetCoeff(a2),lc1);
617        }
618      }
619    }
620#endif
621    if (a2==NULL)
622    {
623      p_LmFree(m2,currRing);
624      if (a1==NULL)
625      {
626#ifdef HAVE_RINGS
627        if (is_Ring)
628        {
629          nDelete(&lc1);
630          nDelete(&lc2);
631          nDelete(&t1);
632          nDelete(&t2);
633        }
634#endif
635        p_LmFree(m1,currRing);
636        return NULL;
637      }
638      goto x1;
639    }
640    if (a1==NULL)
641    {
642      p_LmFree(m1,currRing);
643      goto x2;
644    }
645  }
646}
Note: See TracBrowser for help on using the repository browser.