source: git/Singular/polys-impl.cc @ c4bbf1f

spielwiese
Last change on this file since c4bbf1f was 37e22d, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes: fixed usage of memory routines (polys-impl.cc) git-svn-id: file:///usr/local/Singular/svn/trunk@1356 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 14.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: polys-impl.cc,v 1.15 1998-04-07 18:37:41 Singular 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 "mmprivat.h"
25#include "mmemory.h"
26#include "febase.h"
27#include "numbers.h"
28#include "polys.h"
29#include "ring.h"
30#include "ipid.h"
31
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, int &VarCompIndex,
39                     int &VarLowIndex, int &VarHighIndex)
40{
41  // at the moment, non-default var indicies are only used for simple orderings
42  if (rHasSimpleOrder(r))
43  {
44    short s_order;
45    if (r->order[0] == ringorder_c || r->order[0] == ringorder_C)
46      s_order = r->order[1];
47    else
48      s_order = r->order[0];
49
50    switch(s_order)
51    {
52        case ringorder_dp:
53        case ringorder_wp:
54        case ringorder_ds:
55        case ringorder_ws:
56        case ringorder_unspec:
57          pGetVarIndicies_RevLex(r->N, VarOffset, VarCompIndex, 
58                                 VarLowIndex, VarHighIndex);
59          break;
60
61#ifdef PDEBUG
62        case ringorder_lp:
63        case ringorder_Dp:
64        case ringorder_Wp:
65        case ringorder_Ds:
66        case ringorder_Ws:
67        case ringorder_ls:
68#else
69        default:
70#endif
71          pGetVarIndicies_Lex(r->N, VarOffset, VarCompIndex, 
72                              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, VarCompIndex, 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
178
179/***************************************************************
180 *
181 * Storage Managament Routines
182 *
183 ***************************************************************/
184
185#ifdef PDEBUG
186poly pDBNew(char *f, int l)
187{
188#ifdef MDEBUG
189  poly p = (poly) mmDBAllocSpecialized(f,l);
190#else
191  poly p = (poly) mmAllocSpecialized();
192#endif
193  memset(p,0,pMonomSize);
194  return p;
195}
196#endif
197
198/*2
199* create a new monomial and init
200*/
201#ifdef PDEBUG
202poly pDBInit(char * f, int l)
203{
204  poly p=pDBNew(f,l);
205  memset(p,0, pMonomSize);
206  nNew(&(p->coef));
207  return p;
208}
209#endif
210
211/*2
212* delete a poly, resets pointer
213* put the monomials in the freelist
214*/
215#ifdef PDEBUG
216void pDBDelete(poly * p, char * f, int l)
217{
218  poly h = *p;
219
220  while (h!=NULL)
221  {
222#ifdef LDEBUG
223    nDBDelete(&(h->coef),f,l);
224#else
225    nDelete(&(h->coef));
226#endif
227    pIter(h);
228#ifdef MDEBUG
229    mmDBFreeSpecialized((ADDRESS)*p,f,l);
230#else
231    mmFreeSpecialized((ADDRESS)*p);
232#endif
233    *p=h;
234    if (l>0) l= -l;
235  }
236  *p = NULL;
237}
238#else
239void _pDelete(poly* p)
240{
241  poly h = *p;
242  poly pp;
243
244  while (h!=NULL)
245  {
246    nDelete(&(h->coef));
247    pp=h;
248    pIter(h);
249    mmFreeSpecialized((ADDRESS)pp);
250  }
251  *p = NULL;
252}
253#endif
254
255/*2
256* remove first monom
257*/
258#ifdef PDEBUG
259void pDBDelete1(poly * p, char * f, int l)
260{
261  poly h = *p;
262
263  if (h==NULL) return;
264  nDelete(&(h->coef));
265  *p = pNext(h);
266#ifdef MDEBUG
267  mmDBFreeSpecialized((ADDRESS)h,f,l);
268#else
269  mmFreeSpecialized((ADDRESS)h);
270#endif
271}
272#else
273void _pDelete1(poly* p)
274{
275  poly h = *p;
276
277  if (h==NULL) return;
278  nDelete(&(h->coef));
279  *p = pNext(h);
280  mmFreeSpecialized((ADDRESS)h);
281}
282#endif
283
284void ppDelete(poly* p, ring rg)
285{
286  ring origRing = currRing;
287  rChangeCurrRing(rg, FALSE);
288  pDelete(p);
289  rChangeCurrRing(origRing, FALSE);
290}
291
292/*2
293* remove first monom
294*/
295#ifdef PDEBUG
296void pDBFree1(poly p, char * f, int l)
297{
298  if (p!=NULL)
299  {
300    p->coef=NULL;//nDelete(&(p->coef));
301#ifdef MDEBUG
302    mmDBFreeSpecialized((ADDRESS)p,f,l);
303#else
304    mmFreeSpecialized((ADDRESS)p);
305#endif
306  }
307}
308#endif
309
310/*2
311* creates a copy of p
312*/
313#ifdef PDEBUG
314poly pDBCopy(poly p,char *f,int l)
315#else
316poly _pCopy(poly p)
317#endif
318{
319  poly w, a;
320
321  if (p==NULL) return NULL;
322  pDBTest(p,f,l);
323#ifdef PDEBUG
324  w = a = pDBNew(f,l);
325#else
326  w = a = pNew();
327#endif
328  memcpy(w,p,pMonomSize);
329  w->coef=nCopy(p->coef);
330  if (pNext(p)!=NULL)
331  {
332    pIter(p);
333    do
334    {
335#ifdef PDEBUG
336      a = pNext(a) = pDBNew(f,l);
337#else
338      a = pNext(a) = pNew();
339#endif
340      memcpy(a,p,pMonomSize);
341      a->coef=nCopy(p->coef);
342      pIter(p);
343    }
344    while (p!=NULL);
345  }
346  pNext(a) = NULL;
347  return w;
348}
349
350
351/*2
352* creates a copy of the initial monomial of p
353* sets the coeff of the copy to a defined value
354*/
355#ifdef PDEBUG
356poly pDBCopy1(poly p,char *f,int l)
357#else
358poly _pCopy1(poly p)
359#endif
360{
361  poly w;
362#ifdef PDEBUG
363  w = pDBNew(f,l);
364#else
365  w = pNew();
366#endif
367  pCopy2(w,p);
368  nNew(&(w->coef));
369  pNext(w) = NULL;
370  return w;
371}
372
373/*2
374* returns (a copy of) the head term of a
375*/
376#ifdef PDEBUG
377poly pDBHead(poly p,char *f, int l)
378#else
379poly _pHead(poly p)
380#endif
381{
382  poly w=NULL;
383
384  if (p!=NULL)
385  {
386#ifdef PDEBUG
387    w = pDBNew(f,l);
388#else
389    w = pNew();
390#endif
391    pCopy2(w,p);
392    pSetCoeff0(w,nCopy(pGetCoeff(p)));
393    pNext(w) = NULL;
394  }
395  return w;
396}
397
398poly pHeadProc(poly p)
399{
400  return pHead(p);
401}
402
403/*2
404* returns (a copy of) the head term of a without the coef
405*/
406#ifdef PDEBUG
407poly pDBHead0(poly p,char *f, int l)
408#else
409poly _pHead0(poly p)
410#endif
411{
412  poly w=NULL;
413
414  if (p!=NULL)
415  {
416#ifdef PDEBUG
417    w = pDBNew(f,l);
418#else
419    w = pNew();
420#endif
421    pCopy2(w,p);
422    pSetCoeff0(w,NULL);
423    pNext(w) = NULL;
424  }
425  return w;
426}
427
428
429/***************************************************************
430 *
431 * Routines for turned on debugging
432 *
433 ***************************************************************/
434
435#ifdef PDEBUG
436
437#if PDEBUG != 0
438Exponent_t pPDSetExp(poly p, int v, Exponent_t e, char* f, int l)
439{
440  if (v == 0)
441  {
442    Print("zero index to exponent in %s:%d\n", f, l);
443  }
444  if (v > pVariables)
445  {
446    Print("index %d to exponent too large in %s:%d\n", v, f, l);
447  }
448  return (p)->exp[_pExpIndex(v)]=(e);
449}
450
451Exponent_t pPDGetExp(poly p, int v, char* f, int l)
452{
453  if (v == 0)
454  {
455    Print("zero index to exponent in %s:%d\n", f, l);
456  }
457  if (v > pVariables)
458  {
459    Print("index %d to exponent too large in %s:%d\n", v, f, l);
460  }
461  return (p)->exp[_pExpIndex(v)];
462}
463
464Exponent_t pPDRingSetExp(ring r, poly p, int v, Exponent_t e, char* f, int l)
465{
466  if (v == 0)
467  {
468    Print("zero index to exponent in %s:%d\n", f, l);
469  }
470  if (v > r->N)
471  {
472    Print("index %d to exponent too large in %s:%d\n", v, f, l);
473  }
474  return (p)->exp[_pRingExpIndex(r, v)]=(e);
475}
476
477Exponent_t pPDRingGetExp(ring r, poly p, int v, char* f, int l)
478{
479  if (v == 0)
480  {
481    Print("zero index to exponent in %s:%d\n", f, l);
482  }
483  if (v > r->N)
484  {
485    Print("index %d to exponent too large in %s:%d\n", v, f, l);
486  }
487  return (p)->exp[_pRingExpIndex(r,v)];
488}
489
490Exponent_t pPDIncrExp(poly p, int v, char* f, int l)
491{
492  if (v == 0)
493  {
494    Print("zero index to exponent in %s:%d\n", f, l);
495  }
496  if (v > pVariables)
497  {
498    Print("index %d to exponent too large in %s:%d\n", v, f, l);
499  }
500  return ((p)->exp[_pExpIndex(v)])++;
501}
502
503Exponent_t pPDDecrExp(poly p, int v, char* f, int l)
504{
505  if (v == 0)
506  {
507    Print("zero index to exponent in %s:%d\n", f, l);
508  }
509  if (v > pVariables)
510  {
511    Print("index %d to exponent too large in %s:%d\n", v, f, l);
512  }
513  return ((p)->exp[_pExpIndex(v)])--;
514}
515
516Exponent_t pPDAddExp(poly p, int v, Exponent_t e, char* f, int l)
517{
518  if (v == 0)
519  {
520    Print("zero index to exponent in %s:%d\n", f, l);
521  }
522  if (v > pVariables)
523  {
524    Print("index %d to exponent too large in %s:%d\n", v, f, l);
525  }
526  return ((p)->exp[_pExpIndex(v)]) += (e);
527}
528
529Exponent_t pPDSubExp(poly p, int v, Exponent_t e, char* f, int l)
530{
531  if (v == 0)
532  {
533    Print("zero index to exponent in %s:%d\n", f, l);
534  }
535  if (v > pVariables)
536  {
537    Print("index %d to exponent too large in %s:%d\n", v, f, l);
538  }
539  return ((p)->exp[_pExpIndex(v)]) -= (e);
540}
541
542Exponent_t pPDMultExp(poly p, int v, Exponent_t e, char* f, int l)
543{
544  if (v == 0)
545  {
546    Print("zero index to exponent in %s:%d\n", f, l);
547  }
548  if (v > pVariables)
549  {
550    Print("index %d to exponent too large in %s:%d\n", v, f, l);
551  }
552  return ((p)->exp[_pExpIndex(v)]) *= (e);
553}
554
555// checks whether fast monom add did not overflow
556void pDBMonAddFast(poly p1, poly p2, char* f, int l)
557{
558  poly ptemp = pNew();
559  pCopy2(ptemp, p1);
560
561  __pMonAddFast(p1, p2);
562
563  for (int i=1; i<=pVariables; i++)
564  {
565    pAddExp(ptemp, i, pGetExp(p2, i));
566  }
567  pGetOrder(ptemp) += pGetOrder(p2);
568
569  if (! pEqual(ptemp, p1))
570  {
571    Print("Error in pMonAddFast in %s:%d\n", f, l);
572  }
573
574  pFree1(ptemp);
575}
576
577void pDBCopyAddFast(poly p1, poly p2, poly p3, char* f, int l)
578{
579  if (p2 == p1 || p3 == p1)
580  {
581    Print("Error in pCopyAddFast: Destination equals source in %s:%d\n", f, l);
582  }
583  poly ptemp = pNew();
584  __pCopyAddFast(ptemp, p2, p3);
585
586  pCopy2(p1, p2);
587  pDBMonAddFast(p1, p3, f, l);
588
589  if (! pEqual(ptemp, p1))
590    Print("Error in pCopyMonAddFast in %s:%d\n", f, l);
591  pFree1(ptemp);
592}
593
594void pDBCopyAddFastHomog(poly p1, poly p2, poly p3, Order_t Order,
595                         char* f, int l)
596{
597  pDBCopyAddFast(p1, p2, p3, f, l);
598  if (p1->Order != Order)
599    Print("Error in pCopyAddFastHomog: Order is different from sum\n");
600}
601
602   
603
604static BOOLEAN OldpDivisibleBy(poly a, poly b)
605{
606  if ((a!=NULL)&&((pGetComp(a)==0) || (pGetComp(a) == pGetComp(b))))
607  {
608    for (int i = 1; i<=pVariables; i++)
609      if (pGetExp(a, i) > pGetExp(b,i)) return FALSE;
610    return TRUE;
611  }
612  return FALSE;
613}
614
615
616BOOLEAN pDBDivisibleBy(poly a, poly b, char* f, int l)
617{
618  BOOLEAN istrue = OldpDivisibleBy(a,b);
619  BOOLEAN f_istrue = _pDivisibleBy_orig(a, b);
620
621  if (istrue != f_istrue)
622  {
623    Print("Error in pDivisibleBy in %s:%d\n", f, l);
624    _pDivisibleBy_orig(a, b);
625  }
626  return f_istrue;
627}
628
629BOOLEAN pDBDivisibleBy1(poly a, poly b, char* f, int l)
630{
631  BOOLEAN istrue = OldpDivisibleBy(a,b);
632  BOOLEAN f_istrue = _pDivisibleBy1_orig(a, b);
633
634  if (istrue != f_istrue)
635  {
636    Print("Error in pDivisibleBy1 in %s:%d\n", f, l);
637    _pDivisibleBy1_orig(a, b);
638  }
639  return f_istrue;
640}
641
642BOOLEAN pDBDivisibleBy2(poly a, poly b, char* f, int l)
643{
644  BOOLEAN istrue = OldpDivisibleBy(a,b);
645  BOOLEAN f_istrue = __pDivisibleBy(a, b);
646
647  if (istrue != f_istrue)
648  {
649    Print("Error in pDivisibleBy2 in %s:%d\n", f, l);
650    __pDivisibleBy(a, b);
651  }
652  return f_istrue;
653}
654
655#endif // PDEBUG != 0
656
657
658BOOLEAN pDBTest(poly p, char *f, int l)
659{
660  poly old=NULL;
661  BOOLEAN ismod=FALSE;
662  while (p!=NULL)
663  {
664#ifdef MDEBUG
665    if (!mmDBTestBlock(p,mm_specSize,f,l))
666      return FALSE;
667#endif
668#ifdef LDEBUG
669    if (!nDBTest(p->coef,f,l))
670      return FALSE;
671#endif
672    if ((p->coef==NULL)&&(nGetChar()<2))
673    {
674      Print("NULL coef in poly in %s:%d\n",f,l);
675      return FALSE;
676    }
677    if (nIsZero(p->coef))
678    {
679      Print("zero coef in poly in %s:%d\n",f,l);
680      return FALSE;
681    }
682    int i=pVariables;
683#ifndef DRING
684    for(;i;i--)
685    {
686      if (pGetExp(p,i)<0)
687      {
688        Print("neg. Exponent in %s:%d\n",f,l);
689        return FALSE;
690      }
691    }
692    if (pGetComp(p)<0)
693    {
694      Print("neg Component in %s:%d\n",f,l);
695      return FALSE;
696    }
697#endif
698    if (ismod==0)
699    {
700      if (pGetComp(p)>0) ismod=1;
701      else               ismod=2;
702    }
703    else if (ismod==1)
704    {
705      if (pGetComp(p)==0)
706      {
707        Print("mix vec./poly in %s:%d\n",f,l);
708        return FALSE;
709      }
710    }
711    else if (ismod==2)
712    {
713      if (pGetComp(p)!=0)
714      {
715        Print("mix poly/vec. in %s:%d\n",f,l);
716        return FALSE;
717      }
718    }
719    i=p->Order;
720    pSetm(p);
721    if(i!=p->Order)
722    {
723      Print("wrong ord-field in %s:%d\n",f,l);
724      return FALSE;
725    }
726    old=p;
727    pIter(p);
728    if (pComp(old,p)!=1)
729    {
730      PrintS("wrong order (");
731      wrp(old);
732      Print(") in %s:%d\n",f,l);
733      return FALSE;
734    }
735  }
736  return TRUE;
737}
738#endif // PDEBUG
739
740
741#endif // POLYS_IMPL_CC
Note: See TracBrowser for help on using the repository browser.