source: git/libpolys/coeffs/numbers.cc @ 3159bc

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