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

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