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

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