source: git/libpolys/coeffs/numbers.cc @ 48a41a

spielwiese
Last change on this file since 48a41a was 48a41a, checked in by Hans Schoenemann <hannes@…>, 12 years ago
chg: introduce n_ParDeg for pardeg(number)
  • Property mode set to 100644
File size: 9.9 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
78number ndFarey(number,number,const coeffs r)
79{
80  Werror("farey not implemented for (c=%d)",getCoeffType(r));
81  return NULL;
82}
83number ndChineseRemainder(number *,number *,int,const coeffs r)
84{
85  Werror("ChineseRemainder not implemented for (c=%d)",getCoeffType(r));
86  return n_Init(0,r); 
87}
88
89int ndParDeg(number n, const coeffs r)
90{
91  return (-n_IsZero(n,r));
92}
93
94
95BOOLEAN n_IsZeroDivisor( number a, const coeffs r)
96{
97  int c = n_GetChar(r);
98  BOOLEAN ret = n_IsZero(a, r);
99  if( (c != 0) && !ret )
100  {
101    number ch = n_Init( c, r ); 
102    number g = n_Gcd( ch, a, r );
103    ret = !n_IsOne (g, r);
104    n_Delete(&ch, r);
105    n_Delete(&g, r);
106  }
107  return ret; 
108}
109
110void   ndNormalize(number&, const coeffs) { }
111
112char * ndName(number, const coeffs) { return NULL; }
113
114number ndReturn0(number, const coeffs r) { return n_Init(0,r); }
115
116number ndGcd(number, number, const coeffs r) { return n_Init(1,r); }
117
118number ndIntMod(number, number, const coeffs r) { return n_Init(0,r); }
119
120number ndGetDenom(number &, const coeffs r) { return n_Init(1,r); }
121number ndGetNumerator(number &a,const coeffs r) { return n_Copy(a,r); }
122
123int ndSize(number a, const coeffs r) { return (int)n_IsZero(a,r)==FALSE; }
124
125number ndCopy(number a, const coeffs) { return a; }
126number ndCopyMap(number a, const coeffs aRing, const coeffs r)
127{
128  assume( getCoeffType(r) == getCoeffType(aRing) );
129  if ( nCoeff_has_simple_Alloc(r) && nCoeff_has_simple_Alloc(aRing) )
130    return a;
131        else
132    return n_Copy(a, r);
133}
134void ndKillChar(coeffs) {}
135void ndSetChar(const coeffs) {}
136
137number nd_Copy(number a, const coeffs r) { return n_Copy(a, r); }
138
139#ifdef HAVE_RINGS
140BOOLEAN ndDivBy(number, number, const coeffs) { return TRUE; } // assume a,b !=0
141int ndDivComp(number, number, const coeffs) { return 2; }
142BOOLEAN ndIsUnit(number a, const coeffs r) { return !n_IsZero(a,r); }
143number  ndExtGcd (number, number, number *, number *, const coeffs r) { return n_Init(1,r); }
144#endif
145
146#ifdef HAVE_FACTORY
147CanonicalForm ndConvSingNFactoryN( number, BOOLEAN /*setChar*/, const coeffs)
148{
149  CanonicalForm term(0);
150  Werror("no conversion to factory");
151  return term;
152}
153
154number ndConvFactoryNSingN( const CanonicalForm, const coeffs)
155{
156  Werror("no conversion from factory");
157  return NULL;
158}
159#endif
160
161number  ndInit_bigint(number, const coeffs, const coeffs)
162{
163  Werror("no conversion from bigint to this field");
164  return NULL;
165}
166
167/**< [in, out] a bigint number >= 0  */
168/**< [out] the GMP equivalent    */
169/// Converts a non-negative bigint number into a GMP number.
170void ndMPZ(mpz_t result, number &n, const coeffs r)
171{
172  mpz_init_set_si( result, n_Int(n, r) );
173}
174
175number ndInitMPZ(mpz_t m, const coeffs r)
176{ 
177  return n_Init( mpz_get_si(m), r);
178}
179
180
181BOOLEAN ndCoeffIsEqual(const coeffs r, n_coeffType n, void *)
182{
183  /* test, if r is an instance of nInitCoeffs(n,parameter) */
184  /* if paramater is not needed */
185  return (n==r->type);
186}
187
188static n_coeffType nLastCoeffs=n_CF;
189cfInitCharProc nInitCharTableDefault[]=
190{ NULL,        /*n_unknown */
191 npInitChar,   /* n_Zp */
192 nlInitChar,   /* n_Q */
193 nrInitChar,   /* n_R */
194 nfInitChar,   /* n_GF */
195 ngfInitChar,  /* n_long_R */
196 #ifdef HAVE_POLYEXTENSIONS
197 naInitChar,  /* n_algExt */
198 ntInitChar,  /* n_transExt */
199 #else
200 NULL,        /* n_algExt */
201 NULL,        /* n_transExt */
202 #endif   
203 ngcInitChar,  /* n_long_C */
204 #ifdef HAVE_RINGS
205 nrzInitChar,  /* n_Z */
206 nrnInitChar,  /* n_Zn */
207 NULL,         /* n_Zpn */
208 nr2mInitChar, /* n_Z2m */
209 #else
210 NULL,         /* n_Z */
211 NULL,         /* n_Zn */
212 NULL,         /* n_Zpn */
213 NULL,         /* n_Z2m */
214 #endif
215 NULL   /* n_CF */
216};
217
218static cfInitCharProc *nInitCharTable=nInitCharTableDefault;
219/*2
220* init operations for coeffs r
221*/
222coeffs nInitChar(n_coeffType t, void * parameter)
223{
224  n_Procs_s *n=cf_root;
225
226  while((n!=NULL) && (n->nCoeffIsEqual!=NULL) && (!n->nCoeffIsEqual(n,t,parameter)))
227      n=n->next;
228
229  if (n==NULL)
230  {
231    n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s));
232    n->next=cf_root;
233    n->ref=1;
234    n->type=t;
235
236    // default entries (different from NULL) for some routines:
237    n->cfSize = ndSize;
238    n->cfGetDenom= ndGetDenom;
239    n->cfGetNumerator= ndGetNumerator;
240    n->cfName =  ndName;
241    n->cfImPart=ndReturn0;
242    n->cfDelete= ndDelete;
243    n->cfInpMult=ndInpMult;
244    n->cfCopy = ndCopy;
245    n->cfIntMod=ndIntMod; /* dummy !! */
246    n->cfNormalize=ndNormalize;
247    n->cfGcd  = ndGcd;
248    n->cfLcm  = ndGcd; /* tricky, isn't it ?*/
249    n->cfInit_bigint = ndInit_bigint;
250    n->cfInitMPZ = ndInitMPZ;
251    n->cfMPZ = ndMPZ;
252
253    //n->cfKillChar = ndKillChar; /* dummy */
254    n->cfSetChar = ndSetChar; /* dummy */
255    // temp. removed to catch all the coeffs which miss to implement this!
256
257    n->cfChineseRemainder = ndChineseRemainder;
258    n->cfFarey = ndFarey;
259    n->cfParDeg = ndParDeg;
260
261#ifdef HAVE_RINGS
262    n->cfDivComp = ndDivComp;
263    n->cfDivBy = ndDivBy;
264    n->cfIsUnit = ndIsUnit;
265    n->cfExtGcd = ndExtGcd;
266    //n->cfGetUnit = (nMapFunc)NULL;
267#endif
268
269#ifdef fACTORY
270    n->convSingNFactoryN=ndConvSingNFactoryN;
271    n->convFactoryNSingN=ndConvFactoryNSingN;
272#endif
273   
274    BOOLEAN nOK=TRUE;
275    // init
276    if ((t<=nLastCoeffs) && (nInitCharTable[t]!=NULL))
277      nOK = (nInitCharTable[t])(n,parameter);
278    else
279       Werror("Sorry: the coeff type [%d] was not registered: it is missing in nInitCharTable", (int)t);
280    if (nOK)
281    {
282      omFreeSize(n,sizeof(*n));
283      return NULL;
284    }
285    cf_root=n;
286    // post init settings:
287    if (n->cfRePart==NULL) n->cfRePart=n->cfCopy;
288    if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv;
289   
290#ifdef HAVE_RINGS
291    if (n->cfGetUnit==NULL) n->cfGetUnit=n->cfCopy;
292#endif
293   
294#ifndef NDEBUG
295    assume(n->nCoeffIsEqual!=NULL);
296    if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t);
297                assume(n->cfSetChar!=NULL);
298    assume(n->cfMult!=NULL);
299    assume(n->cfSub!=NULL);
300    assume(n->cfAdd!=NULL);
301    assume(n->cfDiv!=NULL);
302    assume(n->cfIntDiv!=NULL);
303    assume(n->cfIntMod!=NULL);
304    assume(n->cfExactDiv!=NULL);
305    assume(n->cfInit!=NULL);
306    assume(n->cfInitMPZ!=NULL);
307    assume(n->cfSize!=NULL);
308    assume(n->cfInt!=NULL);
309    assume(n->cfMPZ!=NULL);
310    //assume(n->n->cfDivComp!=NULL);
311    //assume(n->cfIsUnit!=NULL);
312    //assume(n->cfGetUnit!=NULL);
313    //assume(n->cfExtGcd!=NULL);
314    assume(n->cfNeg!=NULL);
315    assume(n->cfCopy!=NULL);
316    assume(n->cfRePart!=NULL);
317    assume(n->cfImPart!=NULL);
318    assume(n->cfWrite!=NULL);
319    assume(n->cfRead!=NULL);
320    assume(n->cfNormalize!=NULL);
321    assume(n->cfGreater!=NULL);
322    //assume(n->cfDivBy!=NULL);
323    assume(n->cfEqual!=NULL);
324    assume(n->cfIsZero!=NULL);
325    assume(n->cfIsOne!=NULL);
326    assume(n->cfIsMOne!=NULL);
327    assume(n->cfGreaterZero!=NULL);
328    assume(n->cfPower!=NULL);
329    assume(n->cfGetDenom!=NULL);
330    assume(n->cfGetNumerator!=NULL);
331    assume(n->cfGcd!=NULL);
332    assume(n->cfLcm!=NULL);
333    assume(n->cfDelete!=NULL);
334    assume(n->cfSetMap!=NULL);
335    assume(n->cfName!=NULL);
336    assume(n->cfInpMult!=NULL);
337//    assume(n->cfInit_bigint!=NULL);
338    assume(n->cfCoeffWrite != NULL);
339#ifdef LDEBUG
340    assume(n->cfDBTest!=NULL);
341#endif
342    assume(n->type==t);
343#endif
344  }
345  else
346  {
347    n->ref++;
348  }
349  return n;
350}
351
352void nKillChar(coeffs r)
353{
354  if (r!=NULL)
355  {
356    r->ref--;
357    if (r->ref<=0)
358    {
359      n_Procs_s tmp;
360      n_Procs_s* n=&tmp;
361      tmp.next=cf_root;
362      while((n->next!=NULL) && (n->next!=r)) n=n->next;
363      if (n->next==r)
364      {
365        n->next=n->next->next;
366        if (cf_root==r) cf_root=n->next;
367        r->cfDelete(&(r->nNULL),r);
368        if (r->cfKillChar!=NULL) r->cfKillChar(r);
369        omFreeSize((void *)r, sizeof(n_Procs_s));
370        r=NULL;
371      }
372      else
373      {
374        WarnS("cf_root list destroyed");
375      }
376    }
377  }
378}
379
380
381n_coeffType nRegister(n_coeffType n, cfInitCharProc p)
382{
383  if (n==n_unknown)
384  {
385    nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1);
386    if (nInitCharTable==nInitCharTableDefault)
387    {
388      nInitCharTable=(cfInitCharProc*)omAlloc0(
389                                          nLastCoeffs*sizeof(cfInitCharProc));
390      memcpy(nInitCharTable,nInitCharTableDefault,
391              (nLastCoeffs-1)*sizeof(cfInitCharProc));
392    }
393    else
394    {
395      nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable,
396                                          (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc),
397                                          ((int)nLastCoeffs)*sizeof(cfInitCharProc));
398    }
399
400    nInitCharTable[nLastCoeffs]=p;
401    return nLastCoeffs;
402  }
403  else
404  {
405    if (nInitCharTable[n]!=NULL) Print("coeff %d already initialized\n",n);
406    nInitCharTable[n]=p;
407    return n;
408  }
409}
410
Note: See TracBrowser for help on using the repository browser.