source: git/libpolys/coeffs/numbers.cc @ 2544e7

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