source: git/libpolys/coeffs/numbers.cc @ fb0a699

spielwiese
Last change on this file since fb0a699 was fb0a699, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: fixed a bug, which occured during the last rebase (2.03.2011)
  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT: interface to coefficient aritmetics
8*/
9
10#include <coeffs/config.h>
11#include <misc/auxiliary.h>
12
13
14
15#include <coeffs/coeffs.h>
16
17#include <coeffs/numbers.h>
18
19#include <reporter/reporter.h>
20#include <omalloc/omalloc.h>
21#include <coeffs/numbers.h>
22#include <coeffs/longrat.h>
23#include <coeffs/modulop.h>
24#include <coeffs/gnumpfl.h>
25#include <coeffs/gnumpc.h>
26#include <coeffs/ffields.h>
27#include <coeffs/shortfl.h>
28#ifdef HAVE_RINGS
29#include <coeffs/rmodulo2m.h>
30#include <coeffs/rmodulon.h>
31#include <coeffs/rintegers.h>
32#endif
33#include <string.h>
34#include <stdlib.h>
35
36
37
38//static int characteristic = 0;
39extern int IsPrime(int p);
40
41/*0 implementation*/
42number nNULL; /* the 0 as constant */
43
44n_Procs_s *cf_root=NULL;
45
46void   nNew(number* d) { *d=NULL; }
47void   ndDelete(number* d, const coeffs r) { *d=NULL; }
48void   ndInpMult(number &a, number b, const coeffs r)
49{
50  number n=n_Mult(a,b,r);
51  n_Delete(&a,r);
52  a=n;
53}
54void ndInpAdd(number &a, number b, const coeffs r)
55{
56  number n=n_Add(a,b,r);
57  n_Delete(&a,r);
58  a=n;
59}
60
61#ifdef LDEBUG
62void   nDBDummy1(number* d,char *f, int l) { *d=NULL; }
63BOOLEAN ndDBTest(number a, const char *f, const int l, const coeffs r)
64{
65  return TRUE;
66}
67#endif
68
69void   ndNormalize(number& d, const coeffs r) { }
70
71char * ndName(number n, const coeffs r) { return NULL; }
72
73number ndPar(int i, const coeffs r) { return n_Init(0,r); }
74
75number ndReturn0(number n, const coeffs r) { return n_Init(0,r); }
76
77int    ndParDeg(number n, const coeffs r) { return 0; }
78
79number ndGcd(number a, number b, const coeffs r) { return n_Init(1,r); }
80
81number ndIntMod(number a, number b, const coeffs r) { return n_Init(0,r); }
82
83number ndGetDenom(number &n, const coeffs r) { return n_Init(1,r); }
84number ndGetNumerator(number &a,const coeffs r) { return n_Copy(a,r); }
85
86int ndSize(number a, const coeffs r) { return (int)n_IsZero(a,r)==FALSE; }
87
88number ndCopy(number a, const coeffs) { return a; }
89number ndCopyMap(number a, const coeffs aRing, const coeffs r)
90{
91  assume( getCoeffType(r) == getCoeffType(aRing) );
92  assume( nCoeff_has_simple_Alloc(r) && nCoeff_has_simple_Alloc(aRing) );
93 
94  return a;
95}
96
97number nd_Copy(number a, const coeffs r) { return n_Copy(a, r); }
98
99#ifdef HAVE_RINGS
100BOOLEAN ndDivBy(number a, number b, const coeffs r) { return TRUE; } // assume a,b !=0
101int ndDivComp(number a, number b, const coeffs r) { return 2; }
102BOOLEAN ndIsUnit(number a, const coeffs r) { return !n_IsZero(a,r); }
103number  ndExtGcd (number a, number b, number *s, number *t, const coeffs r) { return n_Init(1,r); }
104#endif
105
106static n_coeffType nLastCoeffs=n_Z2m;
107static cfInitCharProc *nInitCharTable=NULL;
108/*2
109* init operations for coeffs r
110*/
111coeffs nInitChar(n_coeffType t, void * parameter)
112{
113  n_Procs_s *n=cf_root;
114
115  while((n!=NULL) && (n->nCoeffIsEqual!=NULL) && (!n->nCoeffIsEqual(n,t,parameter)))
116      n=n->next;
117
118  if (n==NULL)
119  {
120    n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s));
121    n->next=cf_root;
122    n->ref=1;
123    n->type=t;
124
125    // default entries (different from NULL) for some routines:
126    n->cfPar  = ndPar;
127    n->cfParDeg=ndParDeg;
128    n->cfSize = ndSize;
129    n->cfGetDenom= ndGetDenom;
130    n->cfGetNumerator= ndGetNumerator;
131    n->cfName =  ndName;
132    n->cfImPart=ndReturn0;
133    n->cfDelete= ndDelete;
134    n->cfInpMult=ndInpMult;
135    n->cfCopy=nd_Copy;
136    n->cfIntMod=ndIntMod; /* dummy !! */
137    n->cfNormalize=ndNormalize;
138    n->cfGcd  = ndGcd;
139    n->cfLcm  = ndGcd; /* tricky, isn't it ?*/
140
141#ifdef HAVE_RINGS
142    n->cfDivComp = ndDivComp;
143    n->cfDivBy = ndDivBy;
144    n->cfIsUnit = ndIsUnit;
145    n->cfExtGcd = ndExtGcd;
146    //n->cfGetUnit = (nMapFunc)NULL;
147#endif
148  #if 0 /*vertagt*/
149  if (nField_is_Extension(r))
150  {
151    //ntInitChar(c,TRUE,r);
152    n->cfDelete       = ntDelete;
153    n->nNormalize     = ntNormalize;
154    n->cfInit         = ntInit;
155    n->nPar           = ntPar;
156    n->nParDeg        = ntParDeg;
157    n->n_Int          = ntInt;
158    n->nAdd           = ntAdd;
159    n->nSub           = ntSub;
160    n->nMult          = ntMult;
161    n->nDiv           = ntDiv;
162    n->nExactDiv      = ntDiv;
163    n->nIntDiv        = ntIntDiv;
164    n->nNeg           = ntNeg;
165    n->nInvers        = ntInvers;
166    //n->nCopy          = ntCopy;
167    n->cfCopy         = nt_Copy;
168    n->nGreater       = ntGreater;
169    n->nEqual         = ntEqual;
170    n->nIsZero        = ntIsZero;
171    n->nIsOne         = ntIsOne;
172    n->nIsMOne        = ntIsMOne;
173    n->nGreaterZero   = ntGreaterZero;
174    n->cfWrite        = ntWrite;
175    n->nRead          = ntRead;
176    n->nPower         = ntPower;
177    n->nGcd           = ntGcd;
178    n->nLcm           = ntLcm;
179    n->cfSetMap       = ntSetMap;
180    n->nName          = ntName;
181    n->nSize          = ntSize;
182    n->cfGetDenom     = napGetDenom;
183    n->cfGetNumerator = napGetNumerator;
184#ifdef LDEBUG
185    n->nDBTest        = naDBTest;
186#endif
187  }
188  #endif
189   
190    BOOLEAN nOK=TRUE;
191    // init
192    if ((nInitCharTable!=NULL) && (t<=nLastCoeffs))
193      nOK = (nInitCharTable[t])(n,parameter);
194    else
195       Werror("coeff init missing for %d",(int)t);
196    if (nOK)
197    {
198      omFreeSize(n,sizeof(*n));
199      return NULL;
200    }
201    cf_root=n;
202    // post init settings:
203    if (n->cfRePart==NULL) n->cfRePart=n->cfCopy;
204    if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv;
205   
206#ifdef HAVE_RINGS
207   if (n->cfGetUnit==NULL) n->cfGetUnit=n->cfCopy;
208#endif
209   
210#ifndef NDEBUG
211   assume(n->nCoeffIsEqual!=NULL);
212   if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t);
213   if(n->cfSetChar!=NULL) Warn("cfSetChar is NOT NULL for coeff %d",t);
214   assume(n->cfMult!=NULL);
215   assume(n->cfSub!=NULL);
216   assume(n->cfAdd!=NULL);
217   assume(n->cfDiv!=NULL);
218   assume(n->cfIntDiv!=NULL);
219   assume(n->cfIntMod!=NULL);
220   assume(n->cfExactDiv!=NULL);
221   assume(n->cfInit!=NULL);
222   assume(n->cfPar!=NULL);
223   assume(n->cfParDeg!=NULL);
224   assume(n->cfSize!=NULL);
225   assume(n->cfInt!=NULL);
226   //assume(n->n->cfDivComp!=NULL);
227   //assume(n->cfIsUnit!=NULL);
228   //assume(n->cfGetUnit!=NULL);
229   //assume(n->cfExtGcd!=NULL);
230   assume(n->cfNeg!=NULL);
231   assume(n->cfCopy!=NULL);
232   assume(n->cfRePart!=NULL);
233   assume(n->cfImPart!=NULL);
234   assume(n->cfWrite!=NULL);
235   assume(n->cfRead!=NULL);
236   assume(n->cfNormalize!=NULL);
237   assume(n->cfGreater!=NULL);
238   //assume(n->cfDivBy!=NULL);
239   assume(n->cfEqual!=NULL);
240   assume(n->cfIsZero!=NULL);
241   assume(n->cfIsOne!=NULL);
242   assume(n->cfIsMOne!=NULL);
243   assume(n->cfGreaterZero!=NULL);
244   assume(n->cfPower!=NULL);
245   assume(n->cfGetDenom!=NULL);
246   assume(n->cfGetNumerator!=NULL);
247   assume(n->cfGcd!=NULL);
248   assume(n->cfLcm!=NULL);
249   assume(n->cfDelete!=NULL);
250   assume(n->cfSetMap!=NULL);
251   assume(n->cfName!=NULL);
252   assume(n->cfInpMult!=NULL);
253   assume(n->cfInit_bigint!=NULL);
254#ifdef LDEBUG
255   assume(n->cfDBTest!=NULL);
256#endif
257   assume(n->type==t);
258#endif
259  }
260  else
261  {
262    n->ref++;
263  }
264  return n;
265}
266
267void nKillChar(coeffs r)
268{
269  if (r!=NULL)
270  {
271    r->ref--;
272    if (r->ref<=0)
273    {
274      n_Procs_s tmp;
275      n_Procs_s* n=&tmp;
276      tmp.next=cf_root;
277      while((n->next!=NULL) && (n->next!=r)) n=n->next;
278      if (n->next==r)
279      {
280        n->next=n->next->next;
281        if (cf_root==r) cf_root=n->next;
282        r->cfDelete(&(r->nNULL),r);
283        if (r->cfKillChar!=NULL) r->cfKillChar(r);
284        omFreeSize((void *)r, sizeof(n_Procs_s));
285        r=NULL;
286      }
287      else
288      {
289        WarnS("cf_root list destroyed");
290      }
291      r->cf=NULL;
292    }
293  }
294}
295
296n_coeffType nRegister(n_coeffType n, cfInitCharProc p)
297{
298  if (n==n_unknown)
299  {
300    nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1);
301    if (nInitCharTable==NULL)
302    {
303      nInitCharTable=(cfInitCharProc*)omAlloc0(
304                                          nLastCoeffs*sizeof(cfInitCharProc));
305    }
306    else
307    {
308      nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable,
309                                          (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc),
310                                          ((int)nLastCoeffs)*sizeof(cfInitCharProc));
311    }
312
313    nInitCharTable[nLastCoeffs]=p;
314    return nLastCoeffs;
315  }
316  else
317  {
318    if (nInitCharTable==NULL)
319    {
320      nInitCharTable=(cfInitCharProc*)omAlloc0(
321                                         ((int) nLastCoeffs)*sizeof(cfInitCharProc));
322    }
323    nInitCharTable[n]=p;
324    return n;
325  }
326}
327
Note: See TracBrowser for help on using the repository browser.