source: git/libpolys/coeffs/numbers.cc @ 73a9ffb

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