source: git/Singular/kInline.cc @ fdca1c0

fieker-DuValspielwiese
Last change on this file since fdca1c0 was fc83e4, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* bug fixes for tailRing git-svn-id: file:///usr/local/Singular/svn/trunk@4705 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 14.7 KB
RevLine 
[4f8765]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
[fc83e4]9 *  Version: $Id: kInline.cc,v 1.14 2000-11-08 15:34:56 obachman Exp $
[4f8765]10 *******************************************************************/
11#ifndef KINLINE_CC
12#define KINLINE_CC
13
14#if !defined(NO_KINLINE) || defined(KUTIL_CC)
15
[bd4fa1]16#include "p_polys.h"
17#include "p_Procs.h"
18#include "kbuckets.h"
[ff4e34f]19#include "omalloc.h"
[4f8765]20
[ff4e34f]21#define HAVE_TAIL_BIN
[233e4c]22// Hmm ... this I don't understand:
23// with HAVE_LM_BIN, cyclic_7 is appr. 10% slower (on Intel)
[ff4e34f]24// #define HAVE_LM_BIN
25
26KINLINE skStrategy::skStrategy()
27{
28  memset(this, 0, sizeof(skStrategy));
29  tailRing = currRing;
30  P.tailRing = currRing;
[ab9672]31  tl = -1;
32  sl = -1;
[ff4e34f]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, 
[233e4c]47                            (tailRing != NULL ? tailRing->PolyBin:
[ff4e34f]48                             currRing->PolyBin));
[233e4c]49  if (currRing != tailRing)
50    rKillModifiedRing(tailRing);
[ff4e34f]51}
[ab9672]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}
[ff4e34f]61 
[c5f4b9]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;
[ab9672]74    T[i].i_r = -1;
[c5f4b9]75  }
76  return T;
77}
78
[3a1db5]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
[c5f4b9]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));
[ab9672]97  i_r = -1;
[c5f4b9]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
[4e6cf2]176KINLINE void sTObject::Clear()
177{
178  p = NULL;
179  t_p = NULL;
180}
181
182KINLINE poly sTObject::GetLmCurrRing()
[c5f4b9]183{
184  if (p == NULL && t_p != NULL)
185    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
186
187  return p;
188}
[47bc1c]189KINLINE poly sTObject::GetLmTailRing()
190{
191  if (t_p == NULL)
192  {
193    if (p != NULL && tailRing != currRing)
194    {
195      t_p = k_LmInit_currRing_2_tailRing(p, tailRing);
196      return t_p;
197    }
198    return p;
199  }
200  return t_p;
201}
[c5f4b9]202KINLINE poly sTObject::GetLm(ring r)
203{
[47bc1c]204  assume(r == tailRing || r == currRing);
[c5f4b9]205  if (r == currRing) 
[4e6cf2]206    return GetLmCurrRing();
[c5f4b9]207 
208  if (t_p == NULL && p != NULL)
209    t_p = k_LmInit_currRing_2_tailRing(p, tailRing);
210
211  return t_p;
212}
213
[4e6cf2]214KINLINE void sTObject::GetLm(poly &p_r, ring &r_r) const
215{
216  if (t_p != NULL)
217  {
[fc83e4]218    p_r = t_p;
[4e6cf2]219    r_r = tailRing;
220  }
221  else
222  {
223    p_r = p;
224    r_r = currRing;
225  }
226}
227
[ab9672]228KINLINE int sTObject::GetpLength()
229{
230  assume(pLength <= 0 || pLength == ::pLength(p != NULL ? p : t_p));
231  if (pLength <= 0) pLength = ::pLength(p != NULL ? p : t_p);
232  return pLength;
233}
234
[47bc1c]235KINLINE void sTObject::SetLmCurrRing()
236{
237  if (p == NULL && t_p != NULL)
238    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
239}
[4e6cf2]240
[47bc1c]241   
[c5f4b9]242// Iterations
243KINLINE void sTObject::LmDeleteAndIter()
244{
245  assume(p != NULL || t_p != NULL);
246  if (t_p != NULL)
247  {
248    t_p = p_LmDeleteAndNext(t_p, tailRing);
249    if (p != NULL)
250    {
251      p_LmFree(p, currRing);
252      p = NULL;
253    }
254  }
255  else
256  {
257    p = p_LmDeleteAndNext(p, currRing);
258  }
259}
[4f8765]260
[4e6cf2]261
[c5f4b9]262// arithmetic
263KINLINE void sTObject::Mult_nn(number n)
264{
265  if (t_p != NULL)
266  {    t_p = tailRing->p_Procs->p_Mult_nn(t_p, n, tailRing);
267    if (p != NULL) pSetCoeff0(p, pGetCoeff(t_p));
268  }
269  else
270  {
271    p = p_Mult_nn(p, n, currRing, tailRing);
272  }
273}
274
[0f98876]275KINLINE void 
276sTObject::ShallowCopyDelete(ring new_tailRing, omBin new_tailBin,
[fc83e4]277                            pShallowCopyDeleteProc p_shallow_copy_delete, 
278                            BOOLEAN set_max)
[0f98876]279{
280  if (new_tailBin == NULL) new_tailBin = new_tailRing->PolyBin;
281  if (t_p != NULL)
282  {
283    t_p = p_shallow_copy_delete(t_p, tailRing, new_tailRing, new_tailBin);
284    if (p != NULL) 
285      pNext(p) = pNext(t_p);
286    if (new_tailRing == currRing)
287    {
288      if (p == NULL) p = t_p;
289      else p_LmFree(t_p, tailRing);
290      t_p = NULL;
291    }
292  }
293  else if (p != NULL)
294  {
295    if (pNext(p) != NULL)
296    {
297      pNext(p) = p_shallow_copy_delete(pNext(p), 
298                                       tailRing, new_tailRing, new_tailBin);
299    }
300    if (new_tailRing != currRing)
301    {
302      t_p = k_LmInit_currRing_2_tailRing(p, new_tailRing);
303      pNext(t_p) = pNext(p);
304    }
305  }
306  if (max != NULL)
307  {
308    if (new_tailRing == currRing)
309    {
310      p_LmFree(max, tailRing);
311      max = NULL;
312    }
313    else
[fc83e4]314      max = p_shallow_copy_delete(max,tailRing,new_tailRing,new_tailBin);
315  }
316  else if (set_max && new_tailRing != currRing && pNext(t_p) != NULL)
317  {
318    max = p_GetMaxExpP(pNext(t_p), new_tailRing);
[0f98876]319  }
320  tailRing = new_tailRing;
321}
322 
323KINLINE int sTObject::pFDeg()
324{
325  if (p != NULL) return ::pFDeg(p, currRing);
[beb237]326  return tailRing->pFDeg(t_p, tailRing);
[0f98876]327}
[beb237]328
329extern void pCleardenom(poly p);
330extern void pNorm(poly p);
331// manipulations
332KINLINE void  sTObject::pCleardenom()
333{
334  assume(p != NULL);
335  ::pCleardenom(p);
336  if (t_p != NULL)
337    pSetCoeff0(t_p, pGetCoeff(p));
338}
339
340KINLINE void  sTObject::pNorm()
341{
342  assume(p != NULL);
343  ::pNorm(p);
344  if (t_p != NULL)
345    pSetCoeff0(t_p, pGetCoeff(p));
346}
347 
348
[0f98876]349   
[c5f4b9]350/***************************************************************
351 *
352 * Operation on LObjects
353 *
354 ***************************************************************/
355// Initialization
356KINLINE void sLObject::Init(ring r)
357{
358  memset(this, 0, sizeof(sLObject));
[ab9672]359  i_r1 = -1;
360  i_r2 = -1;
361  i_r = -1;
[c5f4b9]362  Set(r);
363}
364KINLINE sLObject::sLObject(ring r)
365{
366  Init(r);
367}
368KINLINE sLObject::sLObject(poly p_in, ring r)
369{
370  Init(r);
371  Set(p_in, r);
372}
373
374KINLINE sLObject::sLObject(poly p_in, ring c_r, ring t_r)
375{
376  Init(t_r);
377  Set(p_in, c_r, t_r);
378}
379
[fc83e4]380KINLINE void sLObject::SetLmTail(poly lm, poly p_tail, int p_Length, int use_bucket, ring tailRing)
[4f8765]381{
[f8ac82a]382  Set(lm, tailRing);
[bd4fa1]383  if (use_bucket)
384  {
385    bucket = kBucketCreate(tailRing);
[fc83e4]386    kBucketInit(bucket, p_tail, p_Length);
[c5f4b9]387    pNext(lm) = NULL;
[fc83e4]388    pLength = 0;
[bd4fa1]389  }
390  else
391  {
[c5f4b9]392    pNext(lm) = p_tail;
[fc83e4]393    pLength = p_Length;
[bd4fa1]394  }
[4f8765]395}
396
[bd4fa1]397KINLINE void sLObject::Tail_Mult_nn(number n)
[4f8765]398{
[bd4fa1]399  if (bucket != NULL)
[4f8765]400  {
[bd4fa1]401    kBucket_Mult_n(bucket, n);
[4f8765]402  }
[bd4fa1]403  else
[4f8765]404  {
[c5f4b9]405    poly _p = (t_p != NULL ? t_p : p);
406    assume(_p != NULL);
407    pNext(_p) = tailRing->p_Procs->p_Mult_nn(pNext(_p), n, tailRing);
[4f8765]408  }
409}
410
[bd4fa1]411KINLINE void sLObject::Tail_Minus_mm_Mult_qq(poly m, poly q, int lq, 
412                                             poly spNoether)
[4f8765]413{
414  if (bucket != NULL)
415  {
[bd4fa1]416    kBucket_Minus_m_Mult_p(bucket, m, q, &lq, spNoether);
[4f8765]417  }
418  else
419  {
[c5f4b9]420    poly _p = (t_p != NULL ? t_p : p);
421    assume(_p != NULL);
[bd4fa1]422    int dummy;
[c5f4b9]423    pNext(_p) = tailRing->p_Procs->p_Minus_mm_Mult_qq(pNext(_p), m, q, 
424                                                      dummy,spNoether,tailRing);
[4f8765]425  }
426}
[bd4fa1]427
[c5f4b9]428KINLINE void sLObject::LmDeleteAndIter()
[4f8765]429{
[c5f4b9]430  sTObject::LmDeleteAndIter();
[bd4fa1]431  if (bucket != NULL)
[4f8765]432  {
[c5f4b9]433    poly _p = kBucketExtractLm(bucket);
434    if (_p == NULL)
[bd4fa1]435    {
436      kBucketDestroy(&bucket);
437      return;
438    }
[c5f4b9]439    Set(_p, tailRing);
[4f8765]440  }
441}
[bd4fa1]442
443KINLINE void sLObject::CanonicalizeP()
[4f8765]444{
[c5f4b9]445  kTest_L(this);
446
[4f8765]447  if (bucket != NULL)
[bd4fa1]448    kBucketCanonicalize(bucket);
[c5f4b9]449
450  if (p == NULL)
451    p = k_LmInit_tailRing_2_currRing(t_p, tailRing);
[bd4fa1]452}
453
[c5f4b9]454KINLINE poly sLObject::GetP(omBin lmBin = NULL)
[bd4fa1]455{
456  kTest_L(this);
[c5f4b9]457  if (p == NULL)
458    p = k_LmInit_tailRing_2_currRing(t_p, tailRing, 
459                                     (lmBin!=NULL?lmBin:currRing->PolyBin));
460  else if (lmBin != NULL && lmBin != currRing->PolyBin)
461    p = p_LmShallowCopyDelete(p, currRing, lmBin);
462 
463  if (bucket != NULL)
[4f8765]464  {
[c5f4b9]465    kBucketClear(bucket, &pNext(p), &pLength);
466    kBucketDestroy(&bucket);
467    pLength++;
468    if (t_p != NULL) pNext(t_p) = pNext(p);
[4f8765]469  }
[bd4fa1]470  kTest_L(this);
471  return p;
472}
473
[0f98876]474KINLINE void 
475sLObject::ShallowCopyDelete(ring new_tailRing, 
476                            pShallowCopyDeleteProc p_shallow_copy_delete)
477{
478  if (bucket != NULL)
479    kBucketShallowCopyDelete(bucket, new_tailRing, new_tailRing->PolyBin,
480                             p_shallow_copy_delete);
481  sTObject::ShallowCopyDelete(new_tailRing, 
[fc83e4]482                              new_tailRing->PolyBin,p_shallow_copy_delete, 
483                              FALSE);
[0f98876]484}
485
[3a1db5]486KINLINE void sLObject::SetShortExpVector()
487{
488  if (t_p != NULL)
489  {
490    sev = p_GetShortExpVector(t_p, tailRing);
491  }
492  else 
493  {
494    sev = p_GetShortExpVector(p, currRing);
495  }
496}
497
498KINLINE sLObject& sLObject::operator=(const sTObject& t)
499{
500  memset(this, 0, sizeof(*this));
501  memcpy(this, &t, sizeof(sTObject));
502  return *this;
503}
504
[ab9672]505KINLINE TObject* sLObject::T_1(const skStrategy* strat)
506{
507  if (p1 == NULL) return NULL;
508  if (i_r1 == -1) i_r1 = kFindInT(p1, strat->T, strat->tl);
509  assume(i_r1 >= 0 && i_r1 <= strat->tl);
510  TObject* T = strat->R[i_r1];
511  assume(T->p == p1);
512  return T;
513}
514
515KINLINE TObject* sLObject::T_2(const skStrategy* strat)
516{
517  if (p1 == NULL) return NULL;
518  assume(p2 != NULL);
519  if (i_r2 == -1) i_r2 = kFindInT(p2, strat->T, strat->tl);
520  assume(i_r2 >= 0 && i_r2 <= strat->tl);
521  TObject* T = strat->R[i_r2];
522  assume(T->p == p2);
523  return T;
524}
525
526KINLINE void    sLObject::T_1_2(const skStrategy* strat, 
527                                TObject* &T_1, TObject* &T_2)
528{
529  if (p1 == NULL)
530  {
531    T_1 = NULL; 
532    T_2 = NULL;
533    return;
534  }
535  assume(p1 != NULL && p2 != NULL);
536  if (i_r1 == -1) i_r1 = kFindInT(p1, strat->T, strat->tl);
537  if (i_r2 == -1) i_r2 = kFindInT(p2, strat->T, strat->tl);
538  assume(i_r1 >= 0 && i_r1 <= strat->tl);
539  assume(i_r2 >= 0 && i_r2 <= strat->tl);
540  T_1 = strat->R[i_r1];
541  T_2 = strat->R[i_r2];
542  assume(T_1->p == p1);
543  assume(T_2->p == p2);
544  return;
545}
[3a1db5]546
[bd4fa1]547/***************************************************************
548 *
549 * Conversion of polys
550 *
551 ***************************************************************/
[ff4e34f]552 
[c5f4b9]553KINLINE poly k_LmInit_currRing_2_tailRing(poly p, ring tailRing, omBin tailBin)
[bd4fa1]554{
[c5f4b9]555 
556  poly np = p_LmInit(p, currRing, tailRing, tailBin);
[bd4fa1]557  pNext(np) = pNext(p);
558  pSetCoeff0(np, pGetCoeff(p));
559  return np;
560}
561
[c5f4b9]562KINLINE poly k_LmInit_tailRing_2_currRing(poly p, ring tailRing, omBin lmBin)
[bd4fa1]563{
[c5f4b9]564  poly np = p_LmInit(p, tailRing, currRing, lmBin);
565  pNext(np) = pNext(p);
566  pSetCoeff0(np, pGetCoeff(p));
567  return np;
[bd4fa1]568}
569
570// this should be made more efficient
[c5f4b9]571KINLINE poly k_LmShallowCopyDelete_currRing_2_tailRing(poly p, ring tailRing, omBin tailBin)
[bd4fa1]572{
[c5f4b9]573  poly np = k_LmInit_currRing_2_tailRing(p, tailRing, tailBin);
574  p_LmFree(p, currRing);
[bd4fa1]575  return np;
[4f8765]576}
577
[c5f4b9]578KINLINE poly k_LmShallowCopyDelete_tailRing_2_currRing(poly p, ring tailRing, omBin lmBin)
[bd4fa1]579{
[c5f4b9]580  poly np = k_LmInit_tailRing_2_currRing(p, tailRing, lmBin);
[bd4fa1]581  p_LmFree(p, tailRing);
582  return np;
583}
[4f8765]584
[c5f4b9]585KINLINE poly k_LmInit_currRing_2_tailRing(poly p, ring tailRing)
[ff4e34f]586{
[c5f4b9]587  return k_LmInit_currRing_2_tailRing(p, tailRing, tailRing->PolyBin);
[ff4e34f]588}
589
[c5f4b9]590KINLINE poly k_LmInit_tailRing_2_currRing(poly p, ring tailRing)
[ff4e34f]591{
[c5f4b9]592  return  k_LmInit_tailRing_2_currRing(p, tailRing, currRing->PolyBin);
[ff4e34f]593}
594
[c5f4b9]595KINLINE poly k_LmShallowCopyDelete_currRing_2_tailRing(poly p, ring tailRing)
[ff4e34f]596{
[c5f4b9]597  return k_LmShallowCopyDelete_currRing_2_tailRing(p, tailRing, tailRing->PolyBin);
[ff4e34f]598}
599
[c5f4b9]600KINLINE poly k_LmShallowCopyDelete_tailRing_2_currRing(poly p, ring tailRing)
[ff4e34f]601{
[c5f4b9]602  return  k_LmShallowCopyDelete_tailRing_2_currRing(p, tailRing, currRing->PolyBin);
[ff4e34f]603}
604
[0f98876]605/***************************************************************
606 *
607 * Lcm business
608 *
609 ***************************************************************/
610// get m1 = LCM(LM(p1), LM(p2))/LM(p1)
611//     m2 = LCM(LM(p1), LM(p2))/LM(p2)
612KINLINE BOOLEAN k_GetLeadTerms(const poly p1, const poly p2, const ring p_r, 
613                               poly &m1, poly &m2, const ring m_r)
614{
615  p_LmCheckPolyRing(p1, p_r);
616  p_LmCheckPolyRing(p2, p_r);
617 
618  int i;
619  Exponent_t x;
620  m1 = p_Init(m_r);
621  m2 = p_Init(m_r);
622 
623  for (i = p_r->N; i; i--)
624  {
625    x = p_GetExpDiff(p1, p2, i, p_r);
626    if (x > 0)
627    {
628      if (x > (long) m_r->bitmask) goto false_return;
629      p_SetExp(m2,i,x, m_r);
630      p_SetExp(m1,i,0, m_r);
631    }
632    else
633    {
634      if (-x > (long) m_r->bitmask) goto false_return;
635      p_SetExp(m1,i,-x, m_r);
636      p_SetExp(m2,i,0, m_r);
637    }
638  }
639
640  p_Setm(m1, m_r);
641  p_Setm(m2, m_r);
642  return TRUE;
643 
644  false_return:
645  p_LmFree(m1, m_r);
646  p_LmFree(m2, m_r);
647  m1 = m2 = NULL;
648  return FALSE;
649}
650
[c5f4b9]651/***************************************************************
652 *
653 * Routines for backwards-Compatibility
654 *
655 *
656 ***************************************************************/
657KINLINE poly ksOldSpolyRed(poly p1, poly p2, poly spNoether)
658{
659  LObject L(p2);
660  TObject T(p1);
661
662  ksReducePoly(&L, &T, spNoether);
663 
[4e6cf2]664  return L.GetLmCurrRing();
[c5f4b9]665}
666
667KINLINE poly ksOldSpolyRedNew(poly p1, poly p2, poly spNoether)
668{
669  LObject L(p_Copy(p2, currRing));
670  TObject T(p1);
671
672  ksReducePoly(&L, &T, spNoether);
673 
[4e6cf2]674  return L.GetLmCurrRing();
[c5f4b9]675}
676
677KINLINE poly ksOldCreateSpoly(poly p1, poly p2, poly spNoether, ring r)
678{
679  LObject L(r);
680  L.p1 = p1;
681  L.p2 = p2;
682 
683  ksCreateSpoly(&L, spNoether);
[4e6cf2]684  return L.GetLmCurrRing();
[c5f4b9]685}
686
[0f98876]687void ksOldSpolyTail(poly p1, poly q, poly q2, poly spNoether, ring r)
[c5f4b9]688{
689  LObject L(q,  currRing, r);
690  TObject T(p1, currRing, r);
691
692  ksReducePolyTail(&L, &T, q2, spNoether);
693}
[0f98876]694
695KINLINE poly redtailBba (poly p,int pos,kStrategy strat)
696{
697  LObject L(p, currRing, strat->tailRing);
698  return redtailBba(&L, pos, strat);
699}
700
[4f8765]701#endif // defined(KINLINE) || defined(KUTIL_CC)
702#endif // KINLINE_CC
703
Note: See TracBrowser for help on using the repository browser.