source: git/Singular/kInline.cc @ 2166892

fieker-DuValspielwiese
Last change on this file since 2166892 was df8937, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* towards tailRings for local std git-svn-id: file:///usr/local/Singular/svn/trunk@4729 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.5 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.16 2000-11-16 09:54:49 obachman 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 "p_Procs.h"
18#include "kbuckets.h"
19#include "omalloc.h"
20
21#define HAVE_TAIL_BIN
22// Hmm ... this I don't understand:
23// with HAVE_LM_BIN, cyclic_7 is appr. 10% slower (on Intel)
24// #define HAVE_LM_BIN
25
26KINLINE skStrategy::skStrategy()
27{
28  memset(this, 0, sizeof(skStrategy));
29  tailRing = currRing;
30  P.tailRing = currRing;
31  tl = -1;
32  sl = -1;
33#ifdef HAVE_LM_BIN
34  lmBin = omGetStickyBinOfBin(currRing->PolyBin);
35#endif
36#ifdef HAVE_TAIL_BIN
37  tailBin = omGetStickyBinOfBin(currRing->PolyBin);
38#endif
39}
40
41KINLINE skStrategy::~skStrategy()
42{
43  if (lmBin != NULL)
44    omMergeStickyBinIntoBin(lmBin, currRing->PolyBin);
45  if (tailBin != NULL)
46    omMergeStickyBinIntoBin(tailBin, 
47                            (tailRing != NULL ? tailRing->PolyBin:
48                             currRing->PolyBin));
49  if (currRing != tailRing)
50    rKillModifiedRing(tailRing);
51}
52
53KINLINE TObject* skStrategy::S_2_T(int i)
54{
55  assume(i>= 0 && i<=sl);
56  assume(S_2_R[i] >= 0 && S_2_R[i] <= tl);
57  TObject* T = R[S_2_R[i]];
58  assume(T != NULL && T->p == S[i]);
59  return T;
60}
61 
62/***************************************************************
63 *
64 * Operation on TObjects
65 *
66 ***************************************************************/
67
68KINLINE TSet initT () 
69{ 
70  TSet T = (TSet)omAlloc0(setmax*sizeof(TObject)); 
71  for (int i=0; i<setmax; i++)
72  {
73    T[i].tailRing = currRing;
74    T[i].i_r = -1;
75  }
76  return T;
77}
78
79KINLINE TObject** initR()
80{
81  return (TObject**) omAlloc0(setmax*sizeof(TObject*));
82}
83
84KINLINE unsigned long* initsevT()
85{
86  return (unsigned long*) omAlloc0(setmax*sizeof(unsigned long));
87}
88
89// initialization
90KINLINE void sTObject::Set(ring r)
91{
92  tailRing = r;
93}
94KINLINE void sTObject::Init(ring r)
95{
96  memset(this, 0, sizeof(sTObject));
97  i_r = -1;
98  Set(r);
99}
100KINLINE sTObject::sTObject(ring r)
101{
102  Init(r);
103}
104KINLINE void sTObject::Set(poly p_in, ring r)
105{
106  if (r != currRing)
107  {
108    assume(r == tailRing);
109    p_Test(p_in, r);
110    t_p = p_in;
111  }
112  else
113  {
114    pp_Test(p_in, currRing, tailRing);
115    p = p_in;
116  }
117}
118
119KINLINE sTObject::sTObject(poly p_in, ring r) 
120{
121  Init(r);
122  Set(p_in, r);
123}
124
125KINLINE void sTObject::Set(poly p_in, ring c_r, ring t_r)
126{
127  if (c_r != t_r)
128  {
129    assume(c_r == currRing && t_r == tailRing);
130    pp_Test(p_in, currRing, t_r);
131    p = p_in;
132  }
133  else 
134  {
135    Set(p_in, c_r);
136  }
137}
138
139KINLINE sTObject::sTObject(poly p_in, ring c_r, ring t_r)
140{
141  Init(t_r);
142  Set(p_in, c_r, t_r);
143}
144
145KINLINE sTObject::sTObject(sTObject* T, int copy)
146{
147  *this = *T;
148  if (copy)
149  {
150    if (t_p != NULL)
151    {
152      t_p = p_Copy(t_p, tailRing);
153      p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
154    }
155    else
156    {
157      p = p_Copy(p, currRing, tailRing);
158    }
159  }
160}
161
162KINLINE void sTObject::Delete()
163{
164  if (t_p != NULL)
165  {
166    p_Delete(&t_p, tailRing);
167    if (p != NULL)
168      p_LmFree(p, currRing);
169  }
170  else
171  {
172    p_Delete(&p, currRing, tailRing);
173  }
174}
175
176KINLINE void sTObject::Clear()
177{
178  p = NULL;
179  t_p = NULL;
180  ecart = 0;
181  length = 0;
182  pLength = 0;
183  FDeg = 0;
184  is_normalized = FALSE;
185}
186
187KINLINE void sTObject::Copy()
188{
189  if (t_p != NULL)
190  {
191    t_p = p_Copy(t_p, tailRing);
192    if (p != NULL)
193    {
194      p = p_Head(p, currRing);
195      if (pNext(t_p) != NULL) pNext(p) = pNext(t_p);
196    }
197  }
198  else
199  {
200    p = p_Copy(p, currRing, tailRing);
201  }
202}
203
204KINLINE poly sTObject::GetLmCurrRing()
205{
206  if (p == NULL && t_p != NULL)
207    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
208
209  return p;
210}
211KINLINE poly sTObject::GetLmTailRing()
212{
213  if (t_p == NULL)
214  {
215    if (p != NULL && tailRing != currRing)
216    {
217      t_p = k_LmInit_currRing_2_tailRing(p, tailRing);
218      return t_p;
219    }
220    return p;
221  }
222  return t_p;
223}
224KINLINE poly sTObject::GetLm(ring r)
225{
226  assume(r == tailRing || r == currRing);
227  if (r == currRing) 
228    return GetLmCurrRing();
229 
230  if (t_p == NULL && p != NULL)
231    t_p = k_LmInit_currRing_2_tailRing(p, tailRing);
232
233  return t_p;
234}
235
236KINLINE void sTObject::GetLm(poly &p_r, ring &r_r) const
237{
238  if (t_p != NULL)
239  {
240    p_r = t_p;
241    r_r = tailRing;
242  }
243  else
244  {
245    p_r = p;
246    r_r = currRing;
247  }
248}
249
250KINLINE BOOLEAN sTObject::IsNull() const
251{
252  return (p == NULL && t_p == NULL);
253}
254
255KINLINE int sTObject::GetpLength()
256{
257  assume(pLength <= 0 || pLength == ::pLength(p != NULL ? p : t_p));
258  if (pLength <= 0) pLength = ::pLength(p != NULL ? p : t_p);
259  return pLength;
260}
261
262KINLINE void sTObject::SetLmCurrRing()
263{
264  if (p == NULL && t_p != NULL)
265    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
266}
267
268   
269// Iterations
270KINLINE void sTObject::LmDeleteAndIter()
271{
272  assume(p != NULL || t_p != NULL);
273  if (t_p != NULL)
274  {
275    t_p = p_LmDeleteAndNext(t_p, tailRing);
276    if (p != NULL)
277    {
278      p_LmFree(p, currRing);
279      p = NULL;
280    }
281  }
282  else
283  {
284    p = p_LmDeleteAndNext(p, currRing);
285  }
286}
287
288
289// arithmetic
290KINLINE void sTObject::Mult_nn(number n)
291{
292  if (t_p != NULL)
293  {    t_p = tailRing->p_Procs->p_Mult_nn(t_p, n, tailRing);
294    if (p != NULL) pSetCoeff0(p, pGetCoeff(t_p));
295  }
296  else
297  {
298    p = p_Mult_nn(p, n, currRing, tailRing);
299  }
300}
301
302KINLINE void 
303sTObject::ShallowCopyDelete(ring new_tailRing, omBin new_tailBin,
304                            pShallowCopyDeleteProc p_shallow_copy_delete, 
305                            BOOLEAN set_max)
306{
307  if (new_tailBin == NULL) new_tailBin = new_tailRing->PolyBin;
308  if (t_p != NULL)
309  {
310    t_p = p_shallow_copy_delete(t_p, tailRing, new_tailRing, new_tailBin);
311    if (p != NULL) 
312      pNext(p) = pNext(t_p);
313    if (new_tailRing == currRing)
314    {
315      if (p == NULL) p = t_p;
316      else p_LmFree(t_p, tailRing);
317      t_p = NULL;
318    }
319  }
320  else if (p != NULL)
321  {
322    if (pNext(p) != NULL)
323    {
324      pNext(p) = p_shallow_copy_delete(pNext(p), 
325                                       tailRing, new_tailRing, new_tailBin);
326    }
327    if (new_tailRing != currRing)
328    {
329      t_p = k_LmInit_currRing_2_tailRing(p, new_tailRing);
330      pNext(t_p) = pNext(p);
331    }
332  }
333  if (max != NULL)
334  {
335    if (new_tailRing == currRing)
336    {
337      p_LmFree(max, tailRing);
338      max = NULL;
339    }
340    else
341      max = p_shallow_copy_delete(max,tailRing,new_tailRing,new_tailBin);
342  }
343  else if (set_max && new_tailRing != currRing && pNext(t_p) != NULL)
344  {
345    max = p_GetMaxExpP(pNext(t_p), new_tailRing);
346  }
347  tailRing = new_tailRing;
348}
349 
350KINLINE long sTObject::pFDeg() const
351{
352  if (p != NULL) return ::pFDeg(p, currRing);
353  return tailRing->pFDeg(t_p, tailRing);
354}
355KINLINE long sTObject::SetpFDeg()
356{
357  FDeg = this->pFDeg();
358  return FDeg;
359}
360KINLINE long sTObject::GetpFDeg() const
361{
362  assume(FDeg == this->pFDeg());
363  return FDeg;
364}
365KINLINE long sTObject::pLDeg()
366{
367  return ::pLDeg(GetLmTailRing(), &length, tailRing);
368}
369KINLINE long sTObject::SetDegStuffReturnLDeg()
370{
371  FDeg = this->pFDeg();
372  long d = this->pLDeg();
373  ecart = d - FDeg;
374  return d;
375}
376
377extern void pCleardenom(poly p);
378extern void pNorm(poly p);
379// manipulations
380KINLINE void  sTObject::pCleardenom()
381{
382  assume(p != NULL);
383  ::pCleardenom(p);
384  if (t_p != NULL)
385    pSetCoeff0(t_p, pGetCoeff(p));
386}
387
388KINLINE void  sTObject::pNorm()
389{
390  assume(p != NULL);
391  if (! is_normalized)
392  {
393    ::pNorm(p);
394    if (t_p != NULL)
395      pSetCoeff0(t_p, pGetCoeff(p));
396    is_normalized = TRUE;
397  }
398}
399 
400
401   
402/***************************************************************
403 *
404 * Operation on LObjects
405 *
406 ***************************************************************/
407// Initialization
408KINLINE void sLObject::Clear()
409{
410  sTObject::Clear();
411  sev = 0;
412}
413
414KINLINE void sLObject::Init(ring r)
415{
416  memset(this, 0, sizeof(sLObject));
417  i_r1 = -1;
418  i_r2 = -1;
419  i_r = -1;
420  Set(r);
421}
422KINLINE sLObject::sLObject(ring r)
423{
424  Init(r);
425}
426KINLINE sLObject::sLObject(poly p_in, ring r)
427{
428  Init(r);
429  Set(p_in, r);
430}
431
432KINLINE sLObject::sLObject(poly p_in, ring c_r, ring t_r)
433{
434  Init(t_r);
435  Set(p_in, c_r, t_r);
436}
437
438KINLINE void sLObject::SetLmTail(poly lm, poly p_tail, int p_Length, int use_bucket, ring tailRing)
439{
440  Set(lm, tailRing);
441  if (use_bucket)
442  {
443    bucket = kBucketCreate(tailRing);
444    kBucketInit(bucket, p_tail, p_Length);
445    pNext(lm) = NULL;
446    pLength = 0;
447  }
448  else
449  {
450    pNext(lm) = p_tail;
451    pLength = p_Length;
452  }
453}
454
455KINLINE void sLObject::Tail_Mult_nn(number n)
456{
457  if (bucket != NULL)
458  {
459    kBucket_Mult_n(bucket, n);
460  }
461  else
462  {
463    poly _p = (t_p != NULL ? t_p : p);
464    assume(_p != NULL);
465    pNext(_p) = tailRing->p_Procs->p_Mult_nn(pNext(_p), n, tailRing);
466  }
467}
468
469KINLINE void sLObject::Tail_Minus_mm_Mult_qq(poly m, poly q, int lq, 
470                                             poly spNoether)
471{
472  if (bucket != NULL)
473  {
474    kBucket_Minus_m_Mult_p(bucket, m, q, &lq, spNoether);
475  }
476  else
477  {
478    poly _p = (t_p != NULL ? t_p : p);
479    assume(_p != NULL);
480    int dummy;
481    pNext(_p) = tailRing->p_Procs->p_Minus_mm_Mult_qq(pNext(_p), m, q, 
482                                                      dummy,spNoether,tailRing);
483  }
484}
485
486KINLINE void sLObject::LmDeleteAndIter()
487{
488  sTObject::LmDeleteAndIter();
489  if (bucket != NULL)
490  {
491    poly _p = kBucketExtractLm(bucket);
492    if (_p == NULL)
493    {
494      kBucketDestroy(&bucket);
495      return;
496    }
497    Set(_p, tailRing);
498  }
499}
500
501KINLINE void sLObject::CanonicalizeP()
502{
503  kTest_L(this);
504
505  if (bucket != NULL)
506    kBucketCanonicalize(bucket);
507
508  if (p == NULL)
509    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
510}
511
512KINLINE poly sLObject::GetP(omBin lmBin = NULL)
513{
514  kTest_L(this);
515  if (p == NULL)
516    p = k_LmInit_tailRing_2_currRing(t_p, tailRing, 
517                                     (lmBin!=NULL?lmBin:currRing->PolyBin));
518  else if (lmBin != NULL && lmBin != currRing->PolyBin)
519    p = p_LmShallowCopyDelete(p, currRing, lmBin);
520 
521  if (bucket != NULL)
522  {
523    kBucketClear(bucket, &pNext(p), &pLength);
524    kBucketDestroy(&bucket);
525    pLength++;
526    if (t_p != NULL) pNext(t_p) = pNext(p);
527  }
528  kTest_L(this);
529  return p;
530}
531
532KINLINE void 
533sLObject::ShallowCopyDelete(ring new_tailRing, 
534                            pShallowCopyDeleteProc p_shallow_copy_delete)
535{
536  if (bucket != NULL)
537    kBucketShallowCopyDelete(bucket, new_tailRing, new_tailRing->PolyBin,
538                             p_shallow_copy_delete);
539  sTObject::ShallowCopyDelete(new_tailRing, 
540                              new_tailRing->PolyBin,p_shallow_copy_delete, 
541                              FALSE);
542}
543
544KINLINE void sLObject::SetShortExpVector()
545{
546  if (t_p != NULL)
547  {
548    sev = p_GetShortExpVector(t_p, tailRing);
549  }
550  else 
551  {
552    sev = p_GetShortExpVector(p, currRing);
553  }
554}
555
556KINLINE long sLObject::pLDeg()
557{
558  poly tp = GetLmTailRing();
559  assume(tp != NULL);
560  if (bucket != NULL)
561  {
562    int i = kBucketCanonicalize(bucket);
563    pNext(tp) = bucket->buckets[i];
564    long ldeg = ::pLDeg(tp, &length, tailRing);
565    pNext(tp) = NULL;
566    return ldeg;
567  }
568  else
569    return ::pLDeg(tp, &length, tailRing);
570}
571KINLINE long sLObject::SetDegStuffReturnLDeg()
572{
573  FDeg = this->pFDeg();
574  long d = this->pLDeg();
575  ecart = d - FDeg;
576  return d;
577}
578KINLINE long sLObject::SetLength()
579{
580  // this can be improved
581  this->pLDeg();
582  return length;
583}
584KINLINE long sLObject::MinComp()
585{
586  poly tp = GetLmTailRing();
587  assume(tp != NULL);
588  if (bucket != NULL)
589  {
590    int i = kBucketCanonicalize(bucket);
591    pNext(tp) = bucket->buckets[i];
592    long m = p_MinComp(tp, tailRing);
593    pNext(tp) = NULL;
594    return m;
595  }
596  else
597    return p_MinComp(tp, tailRing);
598}
599
600KINLINE sLObject& sLObject::operator=(const sTObject& t)
601{
602  memset(this, 0, sizeof(*this));
603  memcpy(this, &t, sizeof(sTObject));
604  return *this;
605}
606
607KINLINE TObject* sLObject::T_1(const skStrategy* strat)
608{
609  if (p1 == NULL) return NULL;
610  if (i_r1 == -1) i_r1 = kFindInT(p1, strat->T, strat->tl);
611  assume(i_r1 >= 0 && i_r1 <= strat->tl);
612  TObject* T = strat->R[i_r1];
613  assume(T->p == p1);
614  return T;
615}
616
617KINLINE TObject* sLObject::T_2(const skStrategy* strat)
618{
619  if (p1 == NULL) return NULL;
620  assume(p2 != NULL);
621  if (i_r2 == -1) i_r2 = kFindInT(p2, strat->T, strat->tl);
622  assume(i_r2 >= 0 && i_r2 <= strat->tl);
623  TObject* T = strat->R[i_r2];
624  assume(T->p == p2);
625  return T;
626}
627
628KINLINE void    sLObject::T_1_2(const skStrategy* strat, 
629                                TObject* &T_1, TObject* &T_2)
630{
631  if (p1 == NULL)
632  {
633    T_1 = NULL; 
634    T_2 = NULL;
635    return;
636  }
637  assume(p1 != NULL && p2 != NULL);
638  if (i_r1 == -1) i_r1 = kFindInT(p1, strat->T, strat->tl);
639  if (i_r2 == -1) i_r2 = kFindInT(p2, strat->T, strat->tl);
640  assume(i_r1 >= 0 && i_r1 <= strat->tl);
641  assume(i_r2 >= 0 && i_r2 <= strat->tl);
642  T_1 = strat->R[i_r1];
643  T_2 = strat->R[i_r2];
644  assume(T_1->p == p1);
645  assume(T_2->p == p2);
646  return;
647}
648
649/***************************************************************
650 *
651 * Conversion of polys
652 *
653 ***************************************************************/
654 
655KINLINE poly k_LmInit_currRing_2_tailRing(poly p, ring tailRing, omBin tailBin)
656{
657 
658  poly np = p_LmInit(p, currRing, tailRing, tailBin);
659  pNext(np) = pNext(p);
660  pSetCoeff0(np, pGetCoeff(p));
661  return np;
662}
663
664KINLINE poly k_LmInit_tailRing_2_currRing(poly p, ring tailRing, omBin lmBin)
665{
666  poly np = p_LmInit(p, tailRing, currRing, lmBin);
667  pNext(np) = pNext(p);
668  pSetCoeff0(np, pGetCoeff(p));
669  return np;
670}
671
672// this should be made more efficient
673KINLINE poly k_LmShallowCopyDelete_currRing_2_tailRing(poly p, ring tailRing, omBin tailBin)
674{
675  poly np = k_LmInit_currRing_2_tailRing(p, tailRing, tailBin);
676  p_LmFree(p, currRing);
677  return np;
678}
679
680KINLINE poly k_LmShallowCopyDelete_tailRing_2_currRing(poly p, ring tailRing, omBin lmBin)
681{
682  poly np = k_LmInit_tailRing_2_currRing(p, tailRing, lmBin);
683  p_LmFree(p, tailRing);
684  return np;
685}
686
687KINLINE poly k_LmInit_currRing_2_tailRing(poly p, ring tailRing)
688{
689  return k_LmInit_currRing_2_tailRing(p, tailRing, tailRing->PolyBin);
690}
691
692KINLINE poly k_LmInit_tailRing_2_currRing(poly p, ring tailRing)
693{
694  return  k_LmInit_tailRing_2_currRing(p, tailRing, currRing->PolyBin);
695}
696
697KINLINE poly k_LmShallowCopyDelete_currRing_2_tailRing(poly p, ring tailRing)
698{
699  return k_LmShallowCopyDelete_currRing_2_tailRing(p, tailRing, tailRing->PolyBin);
700}
701
702KINLINE poly k_LmShallowCopyDelete_tailRing_2_currRing(poly p, ring tailRing)
703{
704  return  k_LmShallowCopyDelete_tailRing_2_currRing(p, tailRing, currRing->PolyBin);
705}
706
707/***************************************************************
708 *
709 * Lcm business
710 *
711 ***************************************************************/
712// get m1 = LCM(LM(p1), LM(p2))/LM(p1)
713//     m2 = LCM(LM(p1), LM(p2))/LM(p2)
714KINLINE BOOLEAN k_GetLeadTerms(const poly p1, const poly p2, const ring p_r, 
715                               poly &m1, poly &m2, const ring m_r)
716{
717  p_LmCheckPolyRing(p1, p_r);
718  p_LmCheckPolyRing(p2, p_r);
719 
720  int i;
721  Exponent_t x;
722  m1 = p_Init(m_r);
723  m2 = p_Init(m_r);
724 
725  for (i = p_r->N; i; i--)
726  {
727    x = p_GetExpDiff(p1, p2, i, p_r);
728    if (x > 0)
729    {
730      if (x > (long) m_r->bitmask) goto false_return;
731      p_SetExp(m2,i,x, m_r);
732      p_SetExp(m1,i,0, m_r);
733    }
734    else
735    {
736      if (-x > (long) m_r->bitmask) goto false_return;
737      p_SetExp(m1,i,-x, m_r);
738      p_SetExp(m2,i,0, m_r);
739    }
740  }
741
742  p_Setm(m1, m_r);
743  p_Setm(m2, m_r);
744  return TRUE;
745 
746  false_return:
747  p_LmFree(m1, m_r);
748  p_LmFree(m2, m_r);
749  m1 = m2 = NULL;
750  return FALSE;
751}
752
753/***************************************************************
754 *
755 * Routines for backwards-Compatibility
756 *
757 *
758 ***************************************************************/
759KINLINE poly ksOldSpolyRed(poly p1, poly p2, poly spNoether)
760{
761  LObject L(p2);
762  TObject T(p1);
763
764  ksReducePoly(&L, &T, spNoether);
765 
766  return L.GetLmCurrRing();
767}
768
769KINLINE poly ksOldSpolyRedNew(poly p1, poly p2, poly spNoether)
770{
771  LObject L(p_Copy(p2, currRing));
772  TObject T(p1);
773
774  ksReducePoly(&L, &T, spNoether);
775 
776  return L.GetLmCurrRing();
777}
778
779KINLINE poly ksOldCreateSpoly(poly p1, poly p2, poly spNoether, ring r)
780{
781  LObject L(r);
782  L.p1 = p1;
783  L.p2 = p2;
784 
785  ksCreateSpoly(&L, spNoether);
786  return L.GetLmCurrRing();
787}
788
789void ksOldSpolyTail(poly p1, poly q, poly q2, poly spNoether, ring r)
790{
791  LObject L(q,  currRing, r);
792  TObject T(p1, currRing, r);
793
794  ksReducePolyTail(&L, &T, q2, spNoether);
795}
796
797KINLINE poly redtailBba (poly p,int pos,kStrategy strat)
798{
799  LObject L(p, currRing, strat->tailRing);
800  return redtailBba(&L, pos, strat);
801}
802
803#endif // defined(KINLINE) || defined(KUTIL_CC)
804#endif // KINLINE_CC
805
Note: See TracBrowser for help on using the repository browser.