source: git/Singular/polys-impl.cc @ 18255d

spielwiese
Last change on this file since 18255d was 18255d, checked in by Olaf Bachmann <obachman@…>, 26 years ago
1998-03-18 Olaf Bachmann <obachman@mathematik.uni-kl.de> * Cleaned up COMP_FAST and related #defines almost everywhere git-svn-id: file:///usr/local/Singular/svn/trunk@1255 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 14.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: polys-impl.cc,v 1.11 1998-03-19 16:05:49 obachman Exp $ */
5
6/***************************************************************
7 *
8 * File:       polys-impl.cc
9 * Purpose:    low-level procuders for polys.
10 *
11 * If you touch anything here, you better know what you are doing.
12 * What is here should not be used directly from other routines -- the
13 * encapsulations in polys.[h,cc] should be used, instead.
14 *
15 ***************************************************************/
16#ifndef POLYS_IMPL_CC
17#define POLYS_IMPL_CC
18
19#include <stdio.h>
20#include <string.h>
21#include "mod2.h"
22#include "tok.h"
23#include "structs.h"
24#include "mmemory.h"
25#include "febase.h"
26#include "numbers.h"
27#include "polys.h"
28#include "ring.h"
29#include "ipid.h"
30
31#ifdef COMP_FAST
32/***************************************************************
33 *
34 * Low - level routines for which deal with var indicies
35 *
36 ***************************************************************/
37// gets var indicies w.r.t. the ring r
38void pGetVarIndicies(ring r, int &VarOffset, 
39                     int &VarLowIndex, int &VarHighIndex)
40{
41  // at the moment, non-default var indicies are only used for simple orderings
42  if ((r->order[0] == ringorder_unspec)  ||
43      (r->order[2] == 0 &&
44       r->order[0] != ringorder_M && r->order[1] != ringorder_M))
45  {
46    short s_order;
47    if (r->order[0] == ringorder_c || r->order[0] == ringorder_C)
48      s_order = r->order[1];
49    else
50      s_order = r->order[0];
51
52    switch(s_order)
53    {
54        case ringorder_dp:
55        case ringorder_wp:
56        case ringorder_ds:
57        case ringorder_ws:
58        case ringorder_unspec:
59          pGetVarIndicies_RevLex(r->N, VarOffset, VarLowIndex, VarHighIndex);
60          break;
61
62#ifdef PDEBUG
63        case ringorder_lp:
64        case ringorder_Dp:
65        case ringorder_Wp:
66        case ringorder_Ds:
67        case ringorder_Ws:
68        case ringorder_ls:
69#else
70        default:
71#endif
72          pGetVarIndicies_Lex(r->N, VarOffset, VarLowIndex, VarHighIndex);
73#ifdef PDEBUG
74          break;
75        default:
76          Werror("wrong internal ordering:%d at %s, l:%d\n",
77                 s_order,__FILE__,__LINE__);
78#endif
79    }
80  }
81  else
82    // default var indicies are used
83    pGetVarIndicies(r->N, VarOffset, VarLowIndex, VarHighIndex);
84}
85
86
87inline void RingCopy2ExpV(poly dest, poly src, ring src_r)
88{
89  for (int i=pVariables; i; i--)
90    pSetExp(dest, i, pRingGetExp(src_r, src, i));
91  pSetComp(dest, pRingGetComp(src_r, src));
92}
93
94// Returns a converted copy (in the sense that the returned poly is
95// poly of currRing) of poly p which is from ring r -- assumes that
96// currRing and r have the same number of variables, i.e. that polys
97// from r can be "fetched" into currRing
98#ifdef PDEBUG
99poly pDBFetchCopy(ring r, poly p,char *f,int l)
100#else
101poly _pFetchCopy(ring r, poly p)
102#endif
103{
104  poly res;
105  poly a;
106  if (p==NULL) return NULL;
107#ifdef PDEBUG
108  res = a = pDBNew(f,l);
109#else
110  res = a = pNew();
111#endif
112  if (r->VarOffset == pVarOffset)
113  {
114    pCopy2(a,p);
115    a->coef=nCopy(p->coef);
116    pSetm(a);
117    if (pNext(p)!=NULL)
118    {
119      pIter(p);
120      do
121      {
122#ifdef PDEBUG
123        a = pNext(a) = pDBNew(f,l);
124#else
125        a = pNext(a) = pNew();
126#endif
127        pCopy2(a,p);
128        a->coef=nCopy(p->coef);
129        pSetm(a);
130        pIter(p);
131      }
132      while (p!=NULL);
133    }
134    pNext(a) = NULL;
135  }
136  else
137  {
138#ifdef PDEBUG
139    a = res = pDBInit(f,l);
140#else
141    a = res = pInit();
142#endif
143    res->coef = nCopy(p->coef);
144    RingCopy2ExpV(res, p, r);
145    pSetm(res);
146    if (pNext(p) != NULL)
147    {
148      pIter(p);
149      do
150      {
151        // the VarOffset's are different: Hence we
152        // convert betweeen a lex order and a revlex order -- to speed
153        // up the sorting, we assemble new poly in inverse order
154#ifdef PDEBUG
155        res = pDBInit(f,l);
156#else
157        res = pInit();
158#endif
159        pNext(res) = a;
160        a = res;
161        res->coef = nCopy(p->coef);
162        RingCopy2ExpV(res, p, r);
163        pSetm(res);
164        pIter(p);
165      }
166      while (p != NULL);
167    }
168  }
169#ifdef PDEBUG
170  res = pOrdPolyMerge(res);
171  pTest(res);
172  return res;
173#else
174  return pOrdPolyMerge(res);
175#endif
176}
177#endif // COMP_FAST
178
179
180/***************************************************************
181 *
182 * Storage Managament Routines
183 *
184 ***************************************************************/
185
186#ifdef PDEBUG
187poly pDBNew(char *f, int l)
188{
189#ifdef MDEBUG
190  poly p = (poly) mmDBAllocBlock(pMonomSize,f,l);
191#else
192  poly p = (poly) mmAllocBlock(pMonomSize);
193#endif
194  memset(p,0,pMonomSize);
195  return p;
196}
197#endif
198
199/*2
200* create a new monomial and init
201*/
202#ifdef PDEBUG
203poly pDBInit(char * f, int l)
204{
205  poly p=pDBNew(f,l);
206  memset(p,0, pMonomSize);
207  nNew(&(p->coef));
208  return p;
209}
210#endif
211
212/*2
213* delete a poly, resets pointer
214* put the monomials in the freelist
215*/
216#ifdef PDEBUG
217void pDBDelete(poly * p, char * f, int l)
218{
219  poly h = *p;
220
221  while (h!=NULL)
222  {
223#ifdef LDEBUG
224    nDBDelete(&(h->coef),f,l);
225#else
226    nDelete(&(h->coef));
227#endif
228    pIter(h);
229#ifdef MDEBUG
230    mmDBFreeBlock((ADDRESS)*p,pMonomSize,f,l);
231#else
232    mmFreeBlock((ADDRESS)*p,pMonomSize);
233#endif
234    *p=h;
235    if (l>0) l= -l;
236  }
237  *p = NULL;
238}
239#else
240void _pDelete(poly* p)
241{
242  poly h = *p;
243  poly pp;
244
245  while (h!=NULL)
246  {
247    nDelete(&(h->coef));
248    pp=h;
249    pIter(h);
250    mmFreeSpecialized((ADDRESS)pp);
251  }
252  *p = NULL;
253}
254#endif
255
256/*2
257* remove first monom
258*/
259#ifdef PDEBUG
260void pDBDelete1(poly * p, char * f, int l)
261{
262  poly h = *p;
263
264  if (h==NULL) return;
265  nDelete(&(h->coef));
266  *p = pNext(h);
267#ifdef MDEBUG
268  mmDBFreeBlock((ADDRESS)h,pMonomSize,f,l);
269#else
270  mmFreeBlock((ADDRESS)h,pMonomSize);
271#endif
272}
273#else
274void _pDelete1(poly* p)
275{
276  poly h = *p;
277
278  if (h==NULL) return;
279  nDelete(&(h->coef));
280  *p = pNext(h);
281  mmFreeSpecialized((ADDRESS)h);
282}
283#endif
284
285void ppDelete(poly* p, ring rg)
286{
287  ring origRing = currRing;
288  rChangeCurrRing(rg, FALSE);
289  pDelete(p);
290  rChangeCurrRing(origRing, FALSE);
291}
292
293/*2
294* remove first monom
295*/
296#ifdef PDEBUG
297void pDBFree1(poly p, char * f, int l)
298{
299  if (p!=NULL)
300  {
301    p->coef=NULL;//nDelete(&(p->coef));
302#ifdef MDEBUG
303    mmDBFreeBlock((ADDRESS)p,pMonomSize,f,l);
304#else
305    mmFreeBlock((ADDRESS)p,pMonomSize);
306#endif
307  }
308}
309#endif
310
311/*2
312* creates a copy of p
313*/
314#ifdef PDEBUG
315poly pDBCopy(poly p,char *f,int l)
316#else
317poly _pCopy(poly p)
318#endif
319{
320  poly w, a;
321
322  if (p==NULL) return NULL;
323  pDBTest(p,f,l);
324#ifdef PDEBUG
325  w = a = pDBNew(f,l);
326#else
327  w = a = pNew();
328#endif
329  memcpy(w,p,pMonomSize);
330  w->coef=nCopy(p->coef);
331  if (pNext(p)!=NULL)
332  {
333    pIter(p);
334    do
335    {
336#ifdef PDEBUG
337      a = pNext(a) = pDBNew(f,l);
338#else
339      a = pNext(a) = pNew();
340#endif
341      memcpy(a,p,pMonomSize);
342      a->coef=nCopy(p->coef);
343      pIter(p);
344    }
345    while (p!=NULL);
346  }
347  pNext(a) = NULL;
348  return w;
349}
350
351
352/*2
353* creates a copy of the initial monomial of p
354* sets the coeff of the copy to a defined value
355*/
356#ifdef PDEBUG
357poly pDBCopy1(poly p,char *f,int l)
358#else
359poly _pCopy1(poly p)
360#endif
361{
362  poly w;
363#ifdef PDEBUG
364  w = pDBNew(f,l);
365#else
366  w = pNew();
367#endif
368  pCopy2(w,p);
369  nNew(&(w->coef));
370  pNext(w) = NULL;
371  return w;
372}
373
374/*2
375* returns (a copy of) the head term of a
376*/
377#ifdef PDEBUG
378poly pDBHead(poly p,char *f, int l)
379#else
380poly _pHead(poly p)
381#endif
382{
383  poly w=NULL;
384
385  if (p!=NULL)
386  {
387#ifdef PDEBUG
388    w = pDBNew(f,l);
389#else
390    w = pNew();
391#endif
392    pCopy2(w,p);
393    pSetCoeff0(w,nCopy(pGetCoeff(p)));
394    pNext(w) = NULL;
395  }
396  return w;
397}
398
399poly pHeadProc(poly p)
400{
401  return pHead(p);
402}
403
404/*2
405* returns (a copy of) the head term of a without the coef
406*/
407#ifdef PDEBUG
408poly pDBHead0(poly p,char *f, int l)
409#else
410poly _pHead0(poly p)
411#endif
412{
413  poly w=NULL;
414
415  if (p!=NULL)
416  {
417#ifdef PDEBUG
418    w = pDBNew(f,l);
419#else
420    w = pNew();
421#endif
422    pCopy2(w,p);
423    pSetCoeff0(w,NULL);
424    pNext(w) = NULL;
425  }
426  return w;
427}
428
429
430/***************************************************************
431 *
432 * Routines for turned on debugging
433 *
434 ***************************************************************/
435
436#ifdef PDEBUG
437
438#if PDEBUG != 0
439Exponent_t pPDSetExp(poly p, int v, Exponent_t e, char* f, int l)
440{
441  if (v == 0)
442  {
443    Print("zero index to exponent in %s:%d\n", f, l);
444  }
445  if (v > pVariables)
446  {
447    Print("index %d to exponent too large in %s:%d\n", v, f, l);
448  }
449  return (p)->exp[_pExpIndex(v)]=(e);
450}
451
452Exponent_t pPDGetExp(poly p, int v, char* f, int l)
453{
454  if (v == 0)
455  {
456    Print("zero index to exponent in %s:%d\n", f, l);
457  }
458  if (v > pVariables)
459  {
460    Print("index %d to exponent too large in %s:%d\n", v, f, l);
461  }
462  return (p)->exp[_pExpIndex(v)];
463}
464
465Exponent_t pPDRingSetExp(ring r, poly p, int v, Exponent_t e, char* f, int l)
466{
467  if (v == 0)
468  {
469    Print("zero index to exponent in %s:%d\n", f, l);
470  }
471  if (v > r->N)
472  {
473    Print("index %d to exponent too large in %s:%d\n", v, f, l);
474  }
475  return (p)->exp[_pRingExpIndex(r, v)]=(e);
476}
477
478Exponent_t pPDRingGetExp(ring r, poly p, int v, char* f, int l)
479{
480  if (v == 0)
481  {
482    Print("zero index to exponent in %s:%d\n", f, l);
483  }
484  if (v > r->N)
485  {
486    Print("index %d to exponent too large in %s:%d\n", v, f, l);
487  }
488  return (p)->exp[_pRingExpIndex(r,v)];
489}
490
491Exponent_t pPDIncrExp(poly p, int v, char* f, int l)
492{
493  if (v == 0)
494  {
495    Print("zero index to exponent in %s:%d\n", f, l);
496  }
497  if (v > pVariables)
498  {
499    Print("index %d to exponent too large in %s:%d\n", v, f, l);
500  }
501  return ((p)->exp[_pExpIndex(v)])++;
502}
503
504Exponent_t pPDDecrExp(poly p, int v, char* f, int l)
505{
506  if (v == 0)
507  {
508    Print("zero index to exponent in %s:%d\n", f, l);
509  }
510  if (v > pVariables)
511  {
512    Print("index %d to exponent too large in %s:%d\n", v, f, l);
513  }
514  return ((p)->exp[_pExpIndex(v)])--;
515}
516
517Exponent_t pPDAddExp(poly p, int v, Exponent_t e, char* f, int l)
518{
519  if (v == 0)
520  {
521    Print("zero index to exponent in %s:%d\n", f, l);
522  }
523  if (v > pVariables)
524  {
525    Print("index %d to exponent too large in %s:%d\n", v, f, l);
526  }
527  return ((p)->exp[_pExpIndex(v)]) += (e);
528}
529
530Exponent_t pPDSubExp(poly p, int v, Exponent_t e, char* f, int l)
531{
532  if (v == 0)
533  {
534    Print("zero index to exponent in %s:%d\n", f, l);
535  }
536  if (v > pVariables)
537  {
538    Print("index %d to exponent too large in %s:%d\n", v, f, l);
539  }
540  return ((p)->exp[_pExpIndex(v)]) -= (e);
541}
542
543Exponent_t pPDMultExp(poly p, int v, Exponent_t e, char* f, int l)
544{
545  if (v == 0)
546  {
547    Print("zero index to exponent in %s:%d\n", f, l);
548  }
549  if (v > pVariables)
550  {
551    Print("index %d to exponent too large in %s:%d\n", v, f, l);
552  }
553  return ((p)->exp[_pExpIndex(v)]) *= (e);
554}
555
556#ifdef COMP_FAST
557
558// checks whether fast monom add did not overflow
559void pDBMonAddFast(poly p1, poly p2, char* f, int l)
560{
561  poly ptemp = pNew();
562  pCopy2(ptemp, p1);
563
564  __pMonAddFast(p1, p2);
565
566  for (int i=1; i<=pVariables; i++)
567  {
568    pAddExp(ptemp, i, pGetExp(p2, i));
569  }
570#ifdef TEST_MAC_ORDER
571  if (bNoAdd) bSetm(ptemp);else
572#endif
573  pGetOrder(ptemp) += pGetOrder(p2);
574
575  if (! pEqual(ptemp, p1))
576  {
577    Print("Error in pMonAddFast in %s:%d\n", f, l);
578  }
579
580  pFree1(ptemp);
581}
582
583#ifdef TEST_MAC_ORDER
584// checks whether fast monom add did not overflow
585void pbDBMonAddFast0(poly p1, poly p2, char* f, int l)
586{
587  poly ptemp = pNew();
588  pCopy2(ptemp, p1);
589
590  _pbMonAddFast0(p1, p2);
591
592  for (int i=1; i<=pVariables; i++)
593  {
594    pAddExp(ptemp, i, pGetExp(p2, i));
595  }
596  if (bNoAdd) bSetm(ptemp);else
597  pGetOrder(ptemp) += pGetOrder(p2);
598
599  if (! pEqual(ptemp, p1))
600  {
601    Print("Error in pbMonAddFast in %s:%d\n", f, l);
602  }
603
604  pFree1(ptemp);
605}
606
607#endif
608void pDBCopyAddFast(poly p1, poly p2, poly p3, char* f, int l)
609{
610  if (p2 == p1 || p3 == p1)
611  {
612    Print("Error in pCopyAddFast: Destination equals source in %s:%d\n", f, l);
613  }
614  poly ptemp = pNew();
615  __pCopyAddFast(ptemp, p2, p3);
616
617  pCopy2(p1, p2);
618  pDBMonAddFast(p1, p3, f, l);
619
620  if (! pEqual(ptemp, p1))
621    Print("Error in pCopyMonAddFast in %s:%d\n", f, l);
622  pFree1(ptemp);
623}
624
625
626static BOOLEAN OldpDivisibleBy(poly a, poly b)
627{
628  if ((a!=NULL)&&((pGetComp(a)==0) || (pGetComp(a) == pGetComp(b))))
629  {
630    for (int i = 1; i<=pVariables; i++)
631      if (pGetExp(a, i) > pGetExp(b,i)) return FALSE;
632    return TRUE;
633  }
634  return FALSE;
635}
636
637
638BOOLEAN pDBDivisibleBy(poly a, poly b, char* f, int l)
639{
640  BOOLEAN istrue = OldpDivisibleBy(a,b);
641  BOOLEAN f_istrue = _pDivisibleBy_orig(a, b);
642
643  if (istrue != f_istrue)
644  {
645    Print("Error in pDivisibleBy in %s:%d\n", f, l);
646    _pDivisibleBy_orig(a, b);
647  }
648  return f_istrue;
649}
650
651BOOLEAN pDBDivisibleBy1(poly a, poly b, char* f, int l)
652{
653  BOOLEAN istrue = OldpDivisibleBy(a,b);
654  BOOLEAN f_istrue = _pDivisibleBy1_orig(a, b);
655
656  if (istrue != f_istrue)
657  {
658    Print("Error in pDivisibleBy1 in %s:%d\n", f, l);
659    _pDivisibleBy1_orig(a, b);
660  }
661  return f_istrue;
662}
663
664BOOLEAN pDBDivisibleBy2(poly a, poly b, char* f, int l)
665{
666  BOOLEAN istrue = OldpDivisibleBy(a,b);
667  BOOLEAN f_istrue = __pDivisibleBy(a, b);
668
669  if (istrue != f_istrue)
670  {
671    Print("Error in pDivisibleBy2 in %s:%d\n", f, l);
672    __pDivisibleBy(a, b);
673  }
674  return f_istrue;
675}
676
677#endif // COMP_FAST
678
679#endif // PDEBUG != 0
680
681
682BOOLEAN pDBTest(poly p, char *f, int l)
683{
684  poly old=NULL;
685  BOOLEAN ismod=FALSE;
686  while (p!=NULL)
687  {
688#ifdef MDEBUG
689    if (!mmDBTestBlock(p,pMonomSize,f,l))
690      return FALSE;
691#endif
692#ifdef LDEBUG
693    if (!nDBTest(p->coef,f,l))
694       return FALSE;
695#endif
696    if ((p->coef==NULL)&&(nGetChar()<2))
697    {
698      Print("NULL coef in poly in %s:%d\n",f,l);
699      return FALSE;
700    }
701    if (nIsZero(p->coef))
702    {
703      Print("zero coef in poly in %s:%d\n",f,l);
704      return FALSE;
705    }
706    int i=pVariables;
707#ifndef DRING
708    for(;i;i--)
709    {
710      if (pGetExp(p,i)<0)
711      {
712        Print("neg. Exponent in %s:%d\n",f,l);
713        return FALSE;
714      }
715    }
716    if (pGetComp(p)<0)
717    {
718      Print("neg Component in %s:%d\n",f,l);
719      return FALSE;
720    }
721#endif
722    if (ismod==0)
723    {
724      if (pGetComp(p)>0) ismod=1;
725      else               ismod=2;
726    }
727    else if (ismod==1)
728    {
729      if (pGetComp(p)==0)
730      {
731        Print("mix vec./poly in %s:%d\n",f,l);
732        return FALSE;
733      }
734    }
735    else if (ismod==2)
736    {
737      if (pGetComp(p)!=0)
738      {
739        Print("mix poly/vec. in %s:%d\n",f,l);
740        return FALSE;
741      }
742    }
743    i=p->Order;
744    pSetm(p);
745    if(i!=p->Order)
746    {
747      Print("wrong ord-field in %s:%d\n",f,l);
748      return FALSE;
749    }
750    old=p;
751    pIter(p);
752    if (pComp(old,p)!=1)
753    {
754      PrintS("wrong order (");
755      wrp(old);
756      Print(") in %s:%d\n",f,l);
757      return FALSE;
758    }
759  }
760  return TRUE;
761}
762#endif // PDEBUG
763
764
765#endif // POLYS_IMPL_CC
Note: See TracBrowser for help on using the repository browser.