source: git/libpolys/coeffs/numbers.cc @ 488808e

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