source: git/kernel/kInline.cc @ d5564f8

spielwiese
Last change on this file since d5564f8 was d5564f8, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: expoent overflow stuff git-svn-id: file:///usr/local/Singular/svn/trunk@11972 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 22.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/***************************************************************
5 *  File:    kInline.cc
6 *  Purpose: implementation of std related inline routines
7 *  Author:  obachman (Olaf Bachmann)
8 *  Created: 8/00
9 *  Version: $Id: kInline.cc,v 1.17 2009-07-10 15:13:56 Singular Exp $
10 *******************************************************************/
11#ifndef KINLINE_CC
12#define KINLINE_CC
13
14#if !defined(NO_KINLINE) || defined(KUTIL_CC)
15
16#include "p_polys.h"
17#include "polys.h"
18#include "p_Procs.h"
19#include "kbuckets.h"
20#include "omalloc.h"
21
22#define HAVE_TAIL_BIN
23// This doesn't really work, fixme, if necessary
24// #define HAVE_LM_BIN
25
26
27KINLINE TObject* skStrategy::S_2_T(int i)
28{
29  assume(i>= 0 && i<=sl);
30  assume(S_2_R[i] >= 0 && S_2_R[i] <= tl);
31  TObject* TT = R[S_2_R[i]];
32  assume(TT != NULL && TT->p == S[i]);
33  return TT;
34}
35
36KINLINE TObject* skStrategy::s_2_t(int i)
37{
38  if (i >= 0 && i <= sl)
39  {
40    int sri= S_2_R[i];
41    if (sri >= 0 && sri <= tl)
42    {
43      TObject* t = R[sri];
44      if (t != NULL && t->p == S[i])
45        return t;
46    }
47    // last but not least, try kFindInT
48    sri = kFindInT(S[i], T, tl);
49    if (sri >= 0)
50      return &(T[sri]);
51  }
52  return NULL;
53}
54
55KINLINE poly skStrategy::kNoetherTail()
56{
57  if (tailRing == currRing)
58    return kNoether;
59  else
60  {
61    assume((kNoether == NULL && t_kNoether == NULL) ||
62           (kNoether != NULL && t_kNoether != NULL));
63    return t_kNoether;
64  }
65}
66
67/***************************************************************
68 *
69 * Operation on TObjects
70 *
71 ***************************************************************/
72
73KINLINE TSet initT ()
74{
75  TSet T = (TSet)omAlloc0(setmaxT*sizeof(TObject));
76  for (int i=setmaxT-1; i>=0; i--)
77  {
78    T[i].tailRing = currRing;
79    T[i].i_r = -1;
80  }
81  return T;
82}
83
84KINLINE TObject** initR()
85{
86  return (TObject**) omAlloc0(setmaxT*sizeof(TObject*));
87}
88
89KINLINE unsigned long* initsevT()
90{
91  return (unsigned long*) omAlloc0(setmaxT*sizeof(unsigned long));
92}
93
94// initialization
95KINLINE void sTObject::Set(ring r)
96{
97  tailRing = r;
98}
99KINLINE void sTObject::Init(ring r)
100{
101  memset(this, 0, sizeof(sTObject));
102  i_r = -1;
103  Set(r);
104}
105KINLINE sTObject::sTObject(ring r)
106{
107  Init(r);
108}
109KINLINE void sTObject::Set(poly p_in, ring r)
110{
111  if (r != currRing)
112  {
113    assume(r == tailRing);
114    p_Test(p_in, r);
115    t_p = p_in;
116  }
117  else
118  {
119    pp_Test(p_in, currRing, tailRing);
120    p = p_in;
121  }
122}
123
124KINLINE sTObject::sTObject(poly p_in, ring r)
125{
126  Init(r);
127  Set(p_in, r);
128}
129
130KINLINE void sTObject::Set(poly p_in, ring c_r, ring t_r)
131{
132  if (c_r != t_r)
133  {
134    assume(c_r == currRing && t_r == tailRing);
135    pp_Test(p_in, currRing, t_r);
136    p = p_in;
137  }
138  else
139  {
140    Set(p_in, c_r);
141  }
142}
143
144KINLINE sTObject::sTObject(poly p_in, ring c_r, ring t_r)
145{
146  Init(t_r);
147  Set(p_in, c_r, t_r);
148}
149
150KINLINE sTObject::sTObject(sTObject* T, int copy)
151{
152  *this = *T;
153  if (copy)
154  {
155    if (t_p != NULL)
156    {
157      t_p = p_Copy(t_p, tailRing);
158      p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
159    }
160    else
161    {
162      p = p_Copy(p, currRing, tailRing);
163    }
164  }
165}
166
167KINLINE void sTObject::Delete()
168{
169  if (t_p != NULL)
170  {
171    p_Delete(&t_p, tailRing);
172    if (p != NULL)
173      p_LmFree(p, currRing);
174  }
175  else
176  {
177    p_Delete(&p, currRing, tailRing);
178  }
179}
180
181KINLINE void sTObject::Clear()
182{
183  p = NULL;
184  t_p = NULL;
185  ecart = 0;
186  length = 0;
187  pLength = 0;
188  FDeg = 0;
189  is_normalized = FALSE;
190}
191
192KINLINE void sTObject::Copy()
193{
194  if (t_p != NULL)
195  {
196    t_p = p_Copy(t_p, tailRing);
197    if (p != NULL)
198    {
199      p = p_Head(p, currRing);
200      if (pNext(t_p) != NULL) pNext(p) = pNext(t_p);
201    }
202  }
203  else
204  {
205    p = p_Copy(p, currRing, tailRing);
206  }
207}
208
209KINLINE poly sTObject::GetLmCurrRing()
210{
211  if (p == NULL && t_p != NULL)
212    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
213
214  return p;
215}
216KINLINE poly sTObject::GetLmTailRing()
217{
218  if (t_p == NULL)
219  {
220    if (p != NULL && tailRing != currRing)
221    {
222      t_p = k_LmInit_currRing_2_tailRing(p, tailRing);
223      return t_p;
224    }
225    return p;
226  }
227  return t_p;
228}
229KINLINE poly sTObject::GetLm(ring r)
230{
231  assume(r == tailRing || r == currRing);
232  if (r == currRing)
233    return GetLmCurrRing();
234
235  if (t_p == NULL && p != NULL)
236    t_p = k_LmInit_currRing_2_tailRing(p, tailRing);
237
238  return t_p;
239}
240
241KINLINE void sTObject::GetLm(poly &p_r, ring &r_r) const
242{
243  if (t_p != NULL)
244  {
245    p_r = t_p;
246    r_r = tailRing;
247  }
248  else
249  {
250    p_r = p;
251    r_r = currRing;
252  }
253}
254
255KINLINE BOOLEAN sTObject::IsNull() const
256{
257  return (p == NULL && t_p == NULL);
258}
259
260KINLINE int sTObject::GetpLength()
261{
262  if (pLength <= 0) pLength = ::pLength(p != NULL ? p : t_p);
263  return pLength;
264}
265
266KINLINE void sTObject::SetLmCurrRing()
267{
268  if (p == NULL && t_p != NULL)
269    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
270}
271
272KINLINE poly sTObject::Next()
273{
274  assume(p != NULL || t_p != NULL);
275  if (t_p != NULL) return pNext(t_p);
276  return pNext(p);
277}
278
279// Iterations
280KINLINE void sTObject::LmDeleteAndIter()
281{
282  assume(p != NULL || t_p != NULL);
283  if (t_p != NULL)
284  {
285    t_p = p_LmDeleteAndNext(t_p, tailRing);
286    if (p != NULL)
287    {
288      p_LmFree(p, currRing);
289      p = NULL;
290    }
291  }
292  else
293  {
294    p = p_LmDeleteAndNext(p, currRing);
295  }
296  is_normalized = FALSE;
297}
298
299
300// arithmetic
301KINLINE void sTObject::Mult_nn(number n)
302{
303  if (t_p != NULL)
304  {    t_p = tailRing->p_Procs->p_Mult_nn(t_p, n, tailRing);
305    if (p != NULL) pSetCoeff0(p, pGetCoeff(t_p));
306  }
307  else
308  {
309    p = p_Mult_nn(p, n, currRing, tailRing);
310  }
311}
312
313KINLINE void sLObject::Normalize()
314{
315  if (t_p != NULL)
316  {
317    pNormalize(t_p);
318    if (p != NULL) pSetCoeff0(p, pGetCoeff(t_p));
319  }
320  else
321  {
322    pNormalize(p);
323  }
324}
325
326KINLINE void sLObject::HeadNormalize()
327{
328  if (t_p != NULL)
329  {
330    nNormalize(pGetCoeff(t_p));
331    if (p != NULL) pSetCoeff0(p, pGetCoeff(t_p));
332  }
333  else
334  {
335    nNormalize(pGetCoeff(p));
336  }
337}
338
339KINLINE void
340sTObject::ShallowCopyDelete(ring new_tailRing, omBin new_tailBin,
341                            pShallowCopyDeleteProc p_shallow_copy_delete,
342                            BOOLEAN set_max)
343{
344  if (new_tailBin == NULL) new_tailBin = new_tailRing->PolyBin;
345  if (t_p != NULL)
346  {
347    t_p = p_shallow_copy_delete(t_p, tailRing, new_tailRing, new_tailBin);
348    if (p != NULL)
349      pNext(p) = pNext(t_p);
350    if (new_tailRing == currRing)
351    {
352      if (p == NULL) p = t_p;
353      else p_LmFree(t_p, tailRing);
354      t_p = NULL;
355    }
356  }
357  else if (p != NULL)
358  {
359    if (pNext(p) != NULL)
360    {
361      pNext(p) = p_shallow_copy_delete(pNext(p),
362                                       tailRing, new_tailRing, new_tailBin);
363    }
364    if (new_tailRing != currRing)
365    {
366      t_p = k_LmInit_currRing_2_tailRing(p, new_tailRing);
367      pNext(t_p) = pNext(p);
368    }
369  }
370  if (max != NULL)
371  {
372    if (new_tailRing == currRing)
373    {
374      p_LmFree(max, tailRing);
375      max = NULL;
376    }
377    else
378      max = p_shallow_copy_delete(max,tailRing,new_tailRing,new_tailBin);
379  }
380  else if (set_max && new_tailRing != currRing && pNext(t_p) != NULL)
381  {
382    max = p_GetMaxExpP(pNext(t_p), new_tailRing);
383  }
384  tailRing = new_tailRing;
385}
386
387KINLINE long sTObject::pFDeg() const
388{
389  if (p != NULL) return ::pFDeg(p, currRing);
390  return tailRing->pFDeg(t_p, tailRing);
391}
392KINLINE long sTObject::pTotalDeg() const
393{
394  if (p != NULL) return pTotaldegree(p, currRing);
395  return pTotaldegree(t_p,tailRing);
396}
397KINLINE long sTObject::SetpFDeg()
398{
399  FDeg = this->pFDeg();
400  return FDeg;
401}
402KINLINE long sTObject::GetpFDeg() const
403{
404  assume(FDeg == this->pFDeg());
405  return FDeg;
406}
407KINLINE long sTObject::pLDeg()
408{
409  return tailRing->pLDeg(GetLmTailRing(), &length, tailRing);
410}
411KINLINE long sTObject::SetDegStuffReturnLDeg()
412{
413  FDeg = this->pFDeg();
414  long d = this->pLDeg();
415  ecart = d - FDeg;
416  return d;
417}
418
419//extern void pCleardenom(poly p);
420extern void pNorm(poly p);
421// manipulations
422KINLINE void  sTObject::pCleardenom()
423{
424  assume(p != NULL);
425  #ifdef HAVE_RATGRING
426  p= ::pCleardenom(p);
427  #else
428  ::pCleardenom(p);
429  #endif
430  if (t_p != NULL)
431    pSetCoeff0(t_p, pGetCoeff(p));
432}
433
434KINLINE void  sTObject::pNorm()
435{
436  assume(p != NULL);
437  if (! is_normalized)
438  {
439    ::pNorm(p);
440    if (t_p != NULL)
441      pSetCoeff0(t_p, pGetCoeff(p));
442    is_normalized = TRUE;
443  }
444}
445
446
447
448/***************************************************************
449 *
450 * Operation on LObjects
451 *
452 ***************************************************************/
453// Initialization
454KINLINE void sLObject::Clear()
455{
456  sTObject::Clear();
457  sev = 0;
458}
459// Initialization
460KINLINE void sLObject::Delete()
461{
462  sTObject::Delete();
463  if (bucket != NULL)
464    kBucketDeleteAndDestroy(&bucket);
465}
466
467KINLINE void sLObject::Init(ring r)
468{
469  memset(this, 0, sizeof(sLObject));
470  i_r1 = -1;
471  i_r2 = -1;
472  i_r = -1;
473  Set(r);
474}
475KINLINE sLObject::sLObject(ring r)
476{
477  Init(r);
478}
479KINLINE sLObject::sLObject(poly p_in, ring r)
480{
481  Init(r);
482  Set(p_in, r);
483}
484
485KINLINE sLObject::sLObject(poly p_in, ring c_r, ring t_r)
486{
487  Init(t_r);
488  Set(p_in, c_r, t_r);
489}
490
491KINLINE void sLObject::PrepareRed(BOOLEAN use_bucket)
492{
493  if (bucket == NULL)
494  {
495    int l = GetpLength();
496    if (use_bucket && l > 1)
497    {
498      poly tp = GetLmTailRing();
499      assume(l == ::pLength(tp));
500      bucket = kBucketCreate(tailRing);
501      kBucketInit(bucket, pNext(tp), l-1);
502      pNext(tp) = NULL;
503      if (p != NULL) pNext(p) = NULL;
504      pLength = 0;
505      last = NULL;
506    }
507  }
508}
509
510KINLINE void sLObject::SetLmTail(poly lm, poly p_tail, int p_Length, int use_bucket, ring _tailRing, poly _last)
511{
512
513  Set(lm, _tailRing);
514  if (use_bucket)
515  {
516    bucket = kBucketCreate(_tailRing);
517    kBucketInit(bucket, p_tail, p_Length);
518    pNext(lm) = NULL;
519    pLength = 0;
520    last = NULL;
521  }
522  else
523  {
524    pNext(lm) = p_tail;
525    pLength = p_Length + 1;
526    last = _last;
527  }
528
529}
530
531KINLINE void sLObject::Tail_Mult_nn(number n)
532{
533  if (bucket != NULL)
534  {
535    kBucket_Mult_n(bucket, n);
536  }
537  else
538  {
539    poly _p = (t_p != NULL ? t_p : p);
540    assume(_p != NULL);
541    pNext(_p) = tailRing->p_Procs->p_Mult_nn(pNext(_p), n, tailRing);
542  }
543}
544
545KINLINE void sLObject::Tail_Minus_mm_Mult_qq(poly m, poly q, int lq,
546                                             poly spNoether)
547{
548  if (bucket != NULL)
549  {
550    kBucket_Minus_m_Mult_p(bucket, m, q, &lq, spNoether);
551  }
552  else
553  {
554    poly _p = (t_p != NULL ? t_p : p);
555    assume(_p != NULL);
556    int shorter;
557    pNext(_p) = tailRing->p_Procs->p_Minus_mm_Mult_qq(pNext(_p), m, q,
558                                                      shorter,spNoether,
559                                                      tailRing, last);
560    pLength += lq - shorter;
561  }
562}
563
564KINLINE void sLObject::LmDeleteAndIter()
565{
566  sTObject::LmDeleteAndIter();
567  if (bucket != NULL)
568  {
569    poly _p = kBucketExtractLm(bucket);
570    if (_p == NULL)
571    {
572      kBucketDestroy(&bucket);
573      p = t_p = NULL;
574      return;
575    }
576    Set(_p, tailRing);
577  }
578  else
579  {
580    pLength--;
581  }
582}
583
584KINLINE poly sLObject::LmExtractAndIter()
585{
586  poly ret = GetLmTailRing();
587  poly pn;
588
589  assume(p != NULL || t_p != NULL);
590
591  if (bucket != NULL)
592  {
593    pn = kBucketExtractLm(bucket);
594    if (pn == NULL)
595      kBucketDestroy(&bucket);
596  }
597  else
598  {
599    pn = pNext(ret);
600  }
601  pLength--;
602  pNext(ret) = NULL;
603  if (p != NULL && t_p != NULL)
604    p_LmFree(p, currRing);
605
606  Set(pn, tailRing);
607  return ret;
608}
609
610KINLINE poly sLObject::CanonicalizeP()
611{
612  //kTest_L(this);
613  int i = -1;
614
615  if (bucket != NULL)
616    i = kBucketCanonicalize(bucket);
617
618  if (p == NULL)
619    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
620
621  if (i >= 0) pNext(p) = bucket->buckets[i];
622  return p;
623}
624
625KINLINE poly sLObject::GetTP()
626{
627  //kTest_L(this);
628  poly tp = GetLmTailRing();
629  assume(tp != NULL);
630
631  if (bucket != NULL)
632  {
633    kBucketClear(bucket, &pNext(tp), &pLength);
634    kBucketDestroy(&bucket);
635    pLength++;
636  }
637  return tp;
638}
639
640
641KINLINE poly sLObject::GetP(omBin lmBin)
642{
643  //kTest_L(this);
644  if (p == NULL)
645  {
646    p = k_LmInit_tailRing_2_currRing(t_p, tailRing,
647                                     (lmBin!=NULL?lmBin:currRing->PolyBin));
648    FDeg = pFDeg();
649  }
650  else if (lmBin != NULL && lmBin != currRing->PolyBin)
651  {
652    p = p_LmShallowCopyDelete(p, currRing, lmBin);
653    FDeg = pFDeg();
654  }
655
656  if (bucket != NULL)
657  {
658    kBucketClear(bucket, &pNext(p), &pLength);
659    kBucketDestroy(&bucket);
660    pLength++;
661    if (t_p != NULL) pNext(t_p) = pNext(p);
662  }
663  //kTest_L(this);
664  return p;
665}
666
667KINLINE void
668sLObject::ShallowCopyDelete(ring new_tailRing,
669                            pShallowCopyDeleteProc p_shallow_copy_delete)
670{
671  if (bucket != NULL)
672    kBucketShallowCopyDelete(bucket, new_tailRing, new_tailRing->PolyBin,
673                             p_shallow_copy_delete);
674  sTObject::ShallowCopyDelete(new_tailRing,
675                              new_tailRing->PolyBin,p_shallow_copy_delete,
676                              FALSE);
677  last = NULL;
678}
679
680KINLINE void sLObject::SetShortExpVector()
681{
682  if (t_p != NULL)
683  {
684    sev = p_GetShortExpVector(t_p, tailRing);
685  }
686  else
687  {
688    sev = p_GetShortExpVector(p, currRing);
689  }
690}
691
692KINLINE void sLObject::Copy()
693{
694  if (bucket != NULL)
695  {
696    int i = kBucketCanonicalize(bucket);
697    kBucket_pt new_bucket = kBucketCreate(tailRing);
698    kBucketInit(new_bucket,
699                p_Copy(bucket->buckets[i], tailRing),
700                bucket->buckets_length[i]);
701    bucket = new_bucket;
702    if (t_p != NULL) pNext(t_p) = NULL;
703    if (p != NULL) pNext(p) = NULL;
704  }
705  TObject::Copy();
706  last = NULL;
707}
708
709KINLINE poly sLObject::CopyGetP()
710{
711  last = NULL;
712  if (bucket != NULL)
713  {
714    int i = kBucketCanonicalize(bucket);
715    poly bp = p_Copy(bucket->buckets[i], tailRing);
716    pLength = bucket->buckets_length[i] + 1;
717    if (bp != NULL)
718    {
719      assume(t_p != NULL || p != NULL);
720      if (t_p != NULL) pNext(t_p) = bp;
721      else pNext(p) = bp;
722    }
723    bucket = NULL;
724  }
725  return sLObject::GetP();
726}
727
728
729KINLINE long sLObject::pLDeg()
730{
731  poly tp = GetLmTailRing();
732  assume(tp != NULL);
733  if (bucket != NULL)
734  {
735    int i = kBucketCanonicalize(bucket);
736    pNext(tp) = bucket->buckets[i];
737    long ldeg = tailRing->pLDeg(tp, &length, tailRing);
738    pNext(tp) = NULL;
739    return ldeg;
740  }
741  else
742    return tailRing->pLDeg(tp, &length, tailRing);
743}
744KINLINE long sLObject::pLDeg(BOOLEAN deg_last)
745{
746  if (! deg_last || bucket != NULL) return sLObject::pLDeg();
747
748  if (last == NULL || pLength == 0)
749    last = pLast(GetLmTailRing(), pLength);
750#ifdef HAVE_ASSUME
751  long fdeg;
752  fdeg = tailRing->pLDeg(GetLmTailRing(), &length, tailRing);
753  assume (pLength == length && fdeg == tailRing->pFDeg(last, tailRing));
754  return fdeg;
755#else
756  length = pLength;
757  return tailRing->pFDeg(last, tailRing);
758#endif
759}
760
761KINLINE long sLObject::SetDegStuffReturnLDeg()
762{
763  FDeg = this->pFDeg();
764  long d = this->pLDeg();
765  ecart = d - FDeg;
766  return d;
767}
768KINLINE long sLObject::SetDegStuffReturnLDeg(BOOLEAN use_last)
769{
770  FDeg = this->pFDeg();
771  long d = this->pLDeg(use_last);
772  ecart = d - FDeg;
773  return d;
774}
775KINLINE int sLObject::GetpLength()
776{
777  if (bucket == NULL)
778    return sTObject::GetpLength();
779  int i = kBucketCanonicalize(bucket);
780  return bucket->buckets_length[i] + 1;
781}
782KINLINE int sLObject::SetLength(BOOLEAN length_pLength)
783{
784  if (length_pLength)
785  {
786    length = this->GetpLength();
787  }
788  else
789    this->pLDeg();
790  return length;
791}
792KINLINE long sLObject::MinComp()
793{
794  poly tp = GetLmTailRing();
795  assume(tp != NULL);
796  if (bucket != NULL)
797  {
798    int i = kBucketCanonicalize(bucket);
799    pNext(tp) = bucket->buckets[i];
800    long m = p_MinComp(tp, tailRing);
801    pNext(tp) = NULL;
802    return m;
803  }
804  else
805    return p_MinComp(tp, tailRing);
806}
807KINLINE long sLObject::Comp()
808{
809  poly pp;
810  ring r;
811  GetLm(pp, r);
812  assume(pp != NULL);
813  return p_GetComp(pp, r);
814}
815
816KINLINE sLObject& sLObject::operator=(const sTObject& t)
817{
818  memset(this, 0, sizeof(*this));
819  memcpy(this, &t, sizeof(sTObject));
820  return *this;
821}
822
823KINLINE TObject* sLObject::T_1(const skStrategy* s)
824{
825  if (p1 == NULL) return NULL;
826  if (i_r1 == -1) i_r1 = kFindInT(p1, s->T, s->tl);
827  assume(i_r1 >= 0 && i_r1 <= s->tl);
828  TObject* T = s->R[i_r1];
829  assume(T->p == p1);
830  return T;
831}
832
833KINLINE TObject* sLObject::T_2(const skStrategy* strat)
834{
835  if (p1 == NULL) return NULL;
836  assume(p2 != NULL);
837  if (i_r2 == -1) i_r2 = kFindInT(p2, strat->T, strat->tl);
838  assume(i_r2 >= 0 && i_r2 <= strat->tl);
839  TObject* T = strat->R[i_r2];
840  assume(T->p == p2);
841  return T;
842}
843
844KINLINE void    sLObject::T_1_2(const skStrategy* strat,
845                                TObject* &T_1, TObject* &T_2)
846{
847  if (p1 == NULL)
848  {
849    T_1 = NULL;
850    T_2 = NULL;
851    return;
852  }
853  assume(p1 != NULL && p2 != NULL);
854  if (i_r1 == -1) i_r1 = kFindInT(p1, strat->T, strat->tl);
855  if (i_r2 == -1) i_r2 = kFindInT(p2, strat->T, strat->tl);
856  assume(i_r1 >= 0 && i_r1 <= strat->tl);
857  assume(i_r2 >= 0 && i_r2 <= strat->tl);
858  T_1 = strat->R[i_r1];
859  T_2 = strat->R[i_r2];
860  assume(T_1->p == p1);
861  assume(T_2->p == p2);
862  return;
863}
864
865/***************************************************************
866 *
867 * Conversion of polys
868 *
869 ***************************************************************/
870
871KINLINE poly k_LmInit_currRing_2_tailRing(poly p, ring tailRing, omBin tailBin)
872{
873
874  poly np = p_LmInit(p, currRing, tailRing, tailBin);
875  pNext(np) = pNext(p);
876  pSetCoeff0(np, pGetCoeff(p));
877  return np;
878}
879
880KINLINE poly k_LmInit_tailRing_2_currRing(poly p, ring tailRing, omBin lmBin)
881{
882  poly np = p_LmInit(p, tailRing, currRing, lmBin);
883  pNext(np) = pNext(p);
884  pSetCoeff0(np, pGetCoeff(p));
885  return np;
886}
887
888// this should be made more efficient
889KINLINE poly k_LmShallowCopyDelete_currRing_2_tailRing(poly p, ring tailRing, omBin tailBin)
890{
891  poly np = k_LmInit_currRing_2_tailRing(p, tailRing, tailBin);
892  p_LmFree(p, currRing);
893  return np;
894}
895
896KINLINE poly k_LmShallowCopyDelete_tailRing_2_currRing(poly p, ring tailRing, omBin lmBin)
897{
898  poly np = k_LmInit_tailRing_2_currRing(p, tailRing, lmBin);
899  p_LmFree(p, tailRing);
900  return np;
901}
902
903KINLINE poly k_LmInit_currRing_2_tailRing(poly p, ring tailRing)
904{
905  return k_LmInit_currRing_2_tailRing(p, tailRing, tailRing->PolyBin);
906}
907
908KINLINE poly k_LmInit_tailRing_2_currRing(poly p, ring tailRing)
909{
910  return  k_LmInit_tailRing_2_currRing(p, tailRing, currRing->PolyBin);
911}
912
913KINLINE poly k_LmShallowCopyDelete_currRing_2_tailRing(poly p, ring tailRing)
914{
915  return k_LmShallowCopyDelete_currRing_2_tailRing(p, tailRing, tailRing->PolyBin);
916}
917
918KINLINE poly k_LmShallowCopyDelete_tailRing_2_currRing(poly p, ring tailRing)
919{
920  return  k_LmShallowCopyDelete_tailRing_2_currRing(p, tailRing, currRing->PolyBin);
921}
922
923/***************************************************************
924 *
925 * Lcm business
926 *
927 ***************************************************************/
928// get m1 = LCM(LM(p1), LM(p2))/LM(p1)
929//     m2 = LCM(LM(p1), LM(p2))/LM(p2)
930KINLINE BOOLEAN k_GetLeadTerms(const poly p1, const poly p2, const ring p_r,
931                               poly &m1, poly &m2, const ring m_r)
932{
933  p_LmCheckPolyRing(p1, p_r);
934  p_LmCheckPolyRing(p2, p_r);
935
936  int i;
937  Exponent_t x;
938  m1 = p_Init(m_r);
939  m2 = p_Init(m_r);
940
941  for (i = p_r->N; i; i--)
942  {
943    x = p_GetExpDiff(p1, p2, i, p_r);
944    if (x > 0)
945    {
946      if (x > (long) m_r->bitmask) goto false_return;
947      p_SetExp(m2,i,x, m_r);
948      p_SetExp(m1,i,0, m_r);
949    }
950    else
951    {
952      if (-x > (long) m_r->bitmask) goto false_return;
953      p_SetExp(m1,i,-x, m_r);
954      p_SetExp(m2,i,0, m_r);
955    }
956  }
957
958  p_Setm(m1, m_r);
959  p_Setm(m2, m_r);
960  return TRUE;
961
962  false_return:
963  p_LmFree(m1, m_r);
964  p_LmFree(m2, m_r);
965  m1 = m2 = NULL;
966  return FALSE;
967}
968
969#ifdef HAVE_RINGS
970// get m1 = LCM(LM(p1), LM(p2))/LM(p1)
971//     m2 = LCM(LM(p1), LM(p2))/LM(p2)   in tailRing
972//    lcm = LCM(LM(p1), LM(p2)           in leadRing
973KINLINE void k_GetStrongLeadTerms(const poly p1, const poly p2, const ring leadRing,
974                               poly &m1, poly &m2, poly &lcm, const ring tailRing)
975{
976  p_LmCheckPolyRing(p1, leadRing);
977  p_LmCheckPolyRing(p2, leadRing);
978
979  int i;
980  Exponent_t x;
981  Exponent_t e1;
982  Exponent_t e2;
983  Exponent_t s;
984  m1 = p_Init(tailRing);
985  m2 = p_Init(tailRing);
986  lcm = p_Init(leadRing);
987
988  int small_index=1;
989  if ((pGetComp(p1)!=0)||(pGetComp(p2)!=0)) small_index=0;
990
991  for (i = leadRing->N; i>=small_index; i--)
992  {
993    e1 = p_GetExp(p1,i,leadRing);
994    e2 = p_GetExp(p2,i,leadRing);
995    x = e1 - e2;
996    if (x > 0)
997    {
998      p_SetExp(m2,i,x, tailRing);
999      p_SetExp(m1,i,0, tailRing);
1000      s = e1;
1001    }
1002    else
1003    {
1004      p_SetExp(m1,i,-x, tailRing);
1005      p_SetExp(m2,i,0, tailRing);
1006      s = e2;
1007    }
1008    p_SetExp(lcm,i,s, leadRing);
1009  }
1010
1011  p_Setm(m1, tailRing);
1012  p_Setm(m2, tailRing);
1013  p_Setm(lcm, leadRing);
1014}
1015#endif
1016
1017/***************************************************************
1018 *
1019 * Misc things
1020 *
1021 ***************************************************************/
1022KINLINE int ksReducePolyTail(LObject* PR, TObject* PW, LObject* Red)
1023{
1024  BOOLEAN ret;
1025  number coef;
1026
1027  assume(PR->GetLmCurrRing() != PW->GetLmCurrRing());
1028  Red->HeadNormalize();
1029  ret = ksReducePoly(Red, PW, NULL, &coef);
1030
1031  if (!ret)
1032  {
1033    if (! n_IsOne(coef, currRing))
1034    {
1035      PR->Mult_nn(coef);
1036      // HANNES: mark for Normalize
1037    }
1038    n_Delete(&coef, currRing);
1039  }
1040  return ret;
1041}
1042
1043/***************************************************************
1044 *
1045 * Routines for backwards-Compatibility
1046 *
1047 *
1048 ***************************************************************/
1049KINLINE poly ksOldSpolyRed(poly p1, poly p2, poly spNoether)
1050{
1051  LObject L(p2);
1052  TObject T(p1);
1053
1054  ksReducePoly(&L, &T, spNoether);
1055
1056  return L.GetLmCurrRing();
1057}
1058
1059KINLINE poly ksOldSpolyRedNew(poly p1, poly p2, poly spNoether)
1060{
1061  LObject L(p_Copy(p2, currRing));
1062  TObject T(p1);
1063
1064  ksReducePoly(&L, &T, spNoether);
1065
1066  return L.GetLmCurrRing();
1067}
1068
1069KINLINE poly ksOldCreateSpoly(poly p1, poly p2, poly spNoether, ring r)
1070{
1071  LObject L(r);
1072  L.p1 = p1;
1073  L.p2 = p2;
1074
1075  ksCreateSpoly(&L, spNoether);
1076  return L.GetLmCurrRing();
1077}
1078
1079void ksOldSpolyTail(poly p1, poly q, poly q2, poly spNoether, ring r)
1080{
1081  LObject L(q,  currRing, r);
1082  TObject T(p1, currRing, r);
1083
1084  ksReducePolyTail(&L, &T, q2, spNoether);
1085}
1086
1087KINLINE poly redtailBba (poly p,int pos,kStrategy strat,BOOLEAN normalize)
1088{
1089  LObject L(p, currRing, strat->tailRing);
1090  return redtailBba(&L, pos, strat,FALSE, normalize);
1091}
1092
1093#ifdef HAVE_RINGS
1094KINLINE poly redtailBba_Z (poly p,int pos,kStrategy strat)
1095{
1096  LObject L(p, currRing, strat->tailRing);
1097  return redtailBba_Z(&L, pos, strat);
1098}
1099#endif
1100
1101KINLINE poly redtailBba(TObject *T, int pos,kStrategy strat)
1102{
1103  LObject L;
1104  L = *T;
1105  poly p = redtailBba(&L, pos, strat, FALSE);
1106  *T = L;
1107  //kTest_T(T);
1108  assume( p == T->p);
1109  return p;
1110}
1111
1112KINLINE void clearS (poly p, unsigned long p_sev, int* at, int* k,
1113                    kStrategy strat)
1114{
1115  assume(p_sev == pGetShortExpVector(p));
1116  if (strat->noClearS) return;
1117  if (!pLmShortDivisibleBy(p,p_sev, strat->S[*at], ~ strat->sevS[*at])) return;
1118  deleteInS((*at),strat);
1119  (*at)--;
1120  (*k)--;
1121}
1122
1123#endif // defined(KINLINE) || defined(KUTIL_CC)
1124#endif // KINLINE_CC
Note: See TracBrowser for help on using the repository browser.