source: git/coeffs/numbers.cc @ 740417

spielwiese
Last change on this file since 740417 was 740417, checked in by Hans Schoenemann <hannes@…>, 14 years ago
test assumption on coeffs
  • Property mode set to 100644
File size: 8.0 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#include "coeffs.h"
13#include "reporter.h"
14#include "omalloc.h"
15#include "numbers.h"
16#include "longrat.h"
17#include "modulop.h"
18#include "gnumpfl.h"
19#include "gnumpc.h"
20#include "ffields.h"
21#include "shortfl.h"
22#include "longtrans.h"
23#ifdef HAVE_RINGS
24#include <kernel/rmodulo2m.h>
25#include <kernel/rmodulon.h>
26#include <kernel/rintegers.h>
27
28extern omBin gmp_nrz_bin;
29#endif
30
31#ifndef assume
32#  define assume(a) if(!(a)){ Werror( "Assumption: is wrong: %s\n", #a ); };
33#endif
34
35
36//static int characteristic = 0;
37extern int IsPrime(int p);
38
39/*0 implementation*/
40number nNULL; /* the 0 as constant */
41
42n_Procs_s *cf_root=NULL;
43
44void   nNew(number* d) { *d=NULL; }
45void   ndDelete(number* d, const coeffs r) { *d=NULL; }
46void   ndInpMult(number &a, number b, const coeffs r)
47{
48  number n=n_Mult(a,b,r);
49  n_Delete(&a,r);
50  a=n;
51}
52number ndInpAdd(number &a, number b, const coeffs r)
53{
54  number n=n_Add(a,b,r);
55  n_Delete(&a,r);
56  a=n;
57  return a;
58}
59
60#ifdef LDEBUG
61void   nDBDummy1(number* d,char *f, int l) { *d=NULL; }
62BOOLEAN ndDBTest(number a, const char *f, const int l, const coeffs r)
63{
64  return TRUE;
65}
66#endif
67
68void   ndNormalize(number& d, const coeffs r) { }
69
70char * ndName(number n, const coeffs r) { return NULL; }
71
72number ndPar(int i, const coeffs r) { return n_Init(0,r); }
73
74number ndReturn0(number n, const coeffs r) { return n_Init(0,r); }
75
76int    ndParDeg(number n, const coeffs r) { return 0; }
77
78number ndGcd(number a, number b, const coeffs r) { return n_Init(1,r); }
79
80number ndIntMod(number a, number b, const coeffs r) { return n_Init(0,r); }
81
82number ndGetDenom(number &n, const coeffs r) { return n_Init(1,r); }
83number ndGetNumerator(number &a,const coeffs r) { return n_Copy(a,r); }
84
85int ndSize(number a, const coeffs r) { return (int)n_IsZero(a,r)==FALSE; }
86
87number ndCopy(number a, const coeffs) { return a; }
88number ndCopyMap(number a, const coeffs aRing, const coeffs r)
89{
90  assume( getCoeffType(r) == getCoeffType(aRing) );
91  assume( nField_has_simple_Alloc(r) && nField_has_simple_Alloc(aRing) );
92 
93  return a;
94}
95
96number nd_Copy(number a, const coeffs r) { return n_Copy(a, r); }
97
98#ifdef HAVE_RINGS
99BOOLEAN ndDivBy(number a, number b) { return TRUE; } // assume a,b !=0
100int ndDivComp(number a, number b) { return 2; }
101BOOLEAN ndIsUnit(number a) { return !nIsZero(a); }
102number  ndExtGcd (number a, number b, number *s, number *t) { return nInit(1); }
103#endif
104
105static n_coeffType nLastCoeffs=n_Z2n;
106static cfInitCharProc *nInitCharTable=NULL;
107/*2
108* init operations for coeffs r
109*/
110coeffs nInitChar(n_coeffType t, void * parameter)
111{
112  n_Procs_s *n=cf_root;
113  while((n!=NULL) && (!n->nCoeffIsEqual(n,t,parameter)))
114      n=n->next;
115  if (n==NULL)
116  {
117    n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s));
118    n->next=cf_root;
119    n->ref=1;
120    n->type=t;
121
122    // default entries (different from NULL) for some routines:
123    n->cfPar  = ndPar;
124    n->cfParDeg=ndParDeg;
125    n->cfSize = ndSize;
126    n->cfGetDenom= ndGetDenom;
127    n->cfGetNumerator= ndGetNumerator;
128    n->cfName =  ndName;
129    n->cfImPart=ndReturn0;
130    n->cfDelete= ndDelete;
131    n->cfInpMult=ndInpMult;
132    n->cfCopy=nd_Copy;
133    n->cfIntMod=ndIntMod; /* dummy !! */
134    n->cfNormalize=ndNormalize;
135    n->cfGcd  = ndGcd;
136    n->cfLcm  = ndGcd; /* tricky, isn't it ?*/
137#ifdef HAVE_RINGS
138    n->cfDivComp = ndDivComp;
139    n->cfDivBy = ndDivBy;
140    n->cfIsUnit = ndIsUnit;
141    n->cfExtGcd = ndExtGcd;
142    //n->cfGetUnit = (nMapFunc)NULL;
143#endif
144  #if 0 /*vertagt*/
145  if (nField_is_Extension(r))
146  {
147    //ntInitChar(c,TRUE,r);
148    n->cfDelete       = ntDelete;
149    n->nNormalize     = ntNormalize;
150    n->cfInit         = ntInit;
151    n->nPar           = ntPar;
152    n->nParDeg        = ntParDeg;
153    n->n_Int          = ntInt;
154    n->nAdd           = ntAdd;
155    n->nSub           = ntSub;
156    n->nMult          = ntMult;
157    n->nDiv           = ntDiv;
158    n->nExactDiv      = ntDiv;
159    n->nIntDiv        = ntIntDiv;
160    n->nNeg           = ntNeg;
161    n->nInvers        = ntInvers;
162    //n->nCopy          = ntCopy;
163    n->cfCopy         = nt_Copy;
164    n->nGreater       = ntGreater;
165    n->nEqual         = ntEqual;
166    n->nIsZero        = ntIsZero;
167    n->nIsOne         = ntIsOne;
168    n->nIsMOne        = ntIsMOne;
169    n->nGreaterZero   = ntGreaterZero;
170    n->cfWrite        = ntWrite;
171    n->nRead          = ntRead;
172    n->nPower         = ntPower;
173    n->nGcd           = ntGcd;
174    n->nLcm           = ntLcm;
175    n->cfSetMap       = ntSetMap;
176    n->nName          = ntName;
177    n->nSize          = ntSize;
178    n->cfGetDenom     = napGetDenom;
179    n->cfGetNumerator = napGetNumerator;
180#ifdef LDEBUG
181    n->nDBTest        = naDBTest;
182#endif
183  }
184  #endif
185    cf_root=n;
186    // init
187    if ((nInitCharTable!=NULL) && (t<=nLastCoeffs))
188       (nInitCharTable[t])(n,parameter);
189     else
190       Werror("coeff init missing for %d",(int)t);
191    // post init settings:
192    if (n->cfRePart==NULL) n->cfRePart=n->cfCopy;
193    if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv;
194#ifdef HAVE_RINGS
195   if (n->cfGetUnit==(nMapFunc)NULL) n->cfGetUnit=n->cfCopy;
196#endif
197#ifndef NDEBUG
198   assume(n->nCoeffIsEqual!=NULL);
199   if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t);
200   if(n->cfSetChar!=NULL) Warn("cfSetChar is NOT NULL for coeff %d",t);
201   assume(n->cfMult!=NULL);
202   assume(n->cfSub!=NULL);
203   assume(n->cfAdd!=NULL);
204   assume(n->cfDiv!=NULL);
205   assume(n->cfIntDiv!=NULL);
206   assume(n->cfIntMod!=NULL);
207   assume(n->cfExactDiv!=NULL);
208   assume(n->cfInit!=NULL);
209   assume(n->cfPar!=NULL);
210   assume(n->cfParDeg!=NULL);
211   assume(n->cfSize!=NULL);
212   assume(n->cfInt!=NULL);
213   //assume(n->n->cfDivComp!=NULL);
214   //assume(n->cfIsUnit!=NULL);
215   //assume(n->cfGetUnit!=NULL);
216   //assume(n->cfExtGcd!=NULL);
217   assume(n->cfNeg!=NULL);
218   assume(n->cfCopy!=NULL);
219   assume(n->cfRePart!=NULL);
220   assume(n->cfImPart!=NULL);
221   assume(n->cfWrite!=NULL);
222   assume(n->cfRead!=NULL);
223   assume(n->cfNormalize!=NULL);
224   assume(n->cfGreater!=NULL);
225   //assume(n->cfDivBy!=NULL);
226   assume(n->cfEqual!=NULL);
227   assume(n->cfIsZero!=NULL);
228   assume(n->cfIsOne!=NULL);
229   assume(n->cfIsMOne!=NULL);
230   assume(n->cfGreaterZero!=NULL);
231   assume(n->cfPower!=NULL);
232   assume(n->cfGetDenom!=NULL);
233   assume(n->cfGetNumerator!=NULL);
234   assume(n->cfGcd!=NULL);
235   assume(n->cfLcm!=NULL);
236   assume(n->cfDelete!=NULL);
237   assume(n->cfSetMap!=NULL);
238   assume(n->cfName!=NULL);
239   assume(n->cfInpMult!=NULL);
240   assume(n->cfInit_bigint!=NULL);
241   #ifdef LDEBUG
242   assume(n->cfDBTest!=NULL);
243   #endif
244   assume(n->type==t);
245#endif
246  }
247  else
248  {
249    n->ref++;
250  }
251  return n;
252}
253
254void nKillChar(coeffs r)
255{
256  if (r!=NULL)
257  {
258    r->ref--;
259    if (r->ref<=0)
260    {
261      n_Procs_s tmp;
262      n_Procs_s* n=&tmp;
263      tmp.next=cf_root;
264      while((n->next!=NULL) && (n->next!=r)) n=n->next;
265      if (n->next==r)
266      {
267        n->next=n->next->next;
268        if (cf_root==r) cf_root=n->next;
269        r->cfDelete(&(r->nNULL),r);
270        if (r->cfKillChar!=NULL) r->cfKillChar(r);
271        omFreeSize((void *)r, sizeof(n_Procs_s));
272        r=NULL;
273      }
274      else
275      {
276        WarnS("cf_root list destroyed");
277      }
278      r->cf=NULL;
279    }
280  }
281}
282
283n_coeffType nRegister(n_coeffType n, cfInitCharProc p)
284{
285  if (n==n_unknown)
286  {
287    nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1);
288    if (nInitCharTable==NULL)
289    {
290      nInitCharTable=(cfInitCharProc*)omAlloc0(
291                                          nLastCoeffs*sizeof(cfInitCharProc));
292    }
293    else
294    {
295      nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable,
296                                          (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc),
297                                          ((int)nLastCoeffs)*sizeof(cfInitCharProc));
298    }
299
300    nInitCharTable[nLastCoeffs]=p;
301    return nLastCoeffs;
302  }
303  else
304  {
305    if (nInitCharTable==NULL)
306    {
307      nInitCharTable=(cfInitCharProc*)omAlloc0(
308                                         ((int) nLastCoeffs)*sizeof(cfInitCharProc));
309    }
310    nInitCharTable[n]=p;
311    return n;
312  }
313}
314
Note: See TracBrowser for help on using the repository browser.