source: git/libpolys/coeffs/numbers.cc @ c462b55

fieker-DuValspielwiese
Last change on this file since c462b55 was 16f8f1, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
ADD: minor changes to coeffs, numbers and tests
  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT: interface to coefficient aritmetics
8*/
9
10#include <coeffs/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
69void   ndNormalize(number& d, const coeffs r) { }
70
71char * ndName(number n, const coeffs r) { return NULL; }
72
73number ndPar(int i, const coeffs r) { return n_Init(0,r); }
74
75number ndReturn0(number n, const coeffs r) { return n_Init(0,r); }
76
77int    ndParDeg(number n, const coeffs r) { return 0; }
78
79number ndGcd(number a, number b, const coeffs r) { return n_Init(1,r); }
80
81number ndIntMod(number a, number b, const coeffs r) { return n_Init(0,r); }
82
83number ndGetDenom(number &n, const coeffs r) { return n_Init(1,r); }
84number ndGetNumerator(number &a,const coeffs r) { return n_Copy(a,r); }
85
86int ndSize(number a, const coeffs r) { return (int)n_IsZero(a,r)==FALSE; }
87
88number ndCopy(number a, const coeffs) { return a; }
89number ndCopyMap(number a, const coeffs aRing, const coeffs r)
90{
91  assume( getCoeffType(r) == getCoeffType(aRing) );
92  assume( nCoeff_has_simple_Alloc(r) && nCoeff_has_simple_Alloc(aRing) );
93 
94  return a;
95}
96
97number nd_Copy(number a, const coeffs r) { return n_Copy(a, r); }
98
99#ifdef HAVE_RINGS
100BOOLEAN ndDivBy(number a, number b, const coeffs r) { return TRUE; } // assume a,b !=0
101int ndDivComp(number a, number b, const coeffs r) { return 2; }
102BOOLEAN ndIsUnit(number a, const coeffs r) { return !n_IsZero(a,r); }
103number  ndExtGcd (number a, number b, number *s, number *t, const coeffs r) { return n_Init(1,r); }
104#endif
105
106static n_coeffType nLastCoeffs=n_Z2m;
107static cfInitCharProc *nInitCharTable=NULL;
108/*2
109* init operations for coeffs r
110*/
111coeffs nInitChar(n_coeffType t, void * parameter)
112{
113  n_Procs_s *n=cf_root;
114
115  while((n!=NULL) && (n->nCoeffIsEqual!=NULL) && (!n->nCoeffIsEqual(n,t,parameter)))
116      n=n->next;
117
118  if (n==NULL)
119  {
120    n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s));
121    n->next=cf_root;
122    n->ref=1;
123    n->type=t;
124
125    // default entries (different from NULL) for some routines:
126    n->cfPar  = ndPar;
127    n->cfParDeg=ndParDeg;
128    n->cfSize = ndSize;
129    n->cfGetDenom= ndGetDenom;
130    n->cfGetNumerator= ndGetNumerator;
131    n->cfName =  ndName;
132    n->cfImPart=ndReturn0;
133    n->cfDelete= ndDelete;
134    n->cfInpMult=ndInpMult;
135    n->cfCopy=nd_Copy;
136    n->cfIntMod=ndIntMod; /* dummy !! */
137    n->cfNormalize=ndNormalize;
138    n->cfGcd  = ndGcd;
139    n->cfLcm  = ndGcd; /* tricky, isn't it ?*/
140
141#ifdef HAVE_RINGS
142    n->cfDivComp = ndDivComp;
143    n->cfDivBy = ndDivBy;
144    n->cfIsUnit = ndIsUnit;
145    n->cfExtGcd = ndExtGcd;
146    //n->cfGetUnit = (nMapFunc)NULL;
147#endif
148   
149    BOOLEAN nOK=TRUE;
150    // init
151    if ((nInitCharTable!=NULL) && (t<=nLastCoeffs))
152      nOK = (nInitCharTable[t])(n,parameter);
153    else
154       Werror("coeff init missing for %d",(int)t);
155    if (nOK)
156    {
157      omFreeSize(n,sizeof(*n));
158      return NULL;
159    }
160    cf_root=n;
161    // post init settings:
162    if (n->cfRePart==NULL) n->cfRePart=n->cfCopy;
163    if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv;
164   
165#ifdef HAVE_RINGS
166   if (n->cfGetUnit==NULL) n->cfGetUnit=n->cfCopy;
167#endif
168   
169#ifndef NDEBUG
170   assume(n->nCoeffIsEqual!=NULL);
171   if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t);
172   if(n->cfSetChar!=NULL) Warn("cfSetChar is NOT NULL for coeff %d",t);
173   assume(n->cfMult!=NULL);
174   assume(n->cfSub!=NULL);
175   assume(n->cfAdd!=NULL);
176   assume(n->cfDiv!=NULL);
177   assume(n->cfIntDiv!=NULL);
178   assume(n->cfIntMod!=NULL);
179   assume(n->cfExactDiv!=NULL);
180   assume(n->cfInit!=NULL);
181   assume(n->cfPar!=NULL);
182   assume(n->cfParDeg!=NULL);
183   assume(n->cfSize!=NULL);
184   assume(n->cfInt!=NULL);
185   //assume(n->n->cfDivComp!=NULL);
186   //assume(n->cfIsUnit!=NULL);
187   //assume(n->cfGetUnit!=NULL);
188   //assume(n->cfExtGcd!=NULL);
189   assume(n->cfNeg!=NULL);
190   assume(n->cfCopy!=NULL);
191   assume(n->cfRePart!=NULL);
192   assume(n->cfImPart!=NULL);
193   assume(n->cfWrite!=NULL);
194   assume(n->cfRead!=NULL);
195   assume(n->cfNormalize!=NULL);
196   assume(n->cfGreater!=NULL);
197   //assume(n->cfDivBy!=NULL);
198   assume(n->cfEqual!=NULL);
199   assume(n->cfIsZero!=NULL);
200   assume(n->cfIsOne!=NULL);
201   assume(n->cfIsMOne!=NULL);
202   assume(n->cfGreaterZero!=NULL);
203   assume(n->cfPower!=NULL);
204   assume(n->cfGetDenom!=NULL);
205   assume(n->cfGetNumerator!=NULL);
206   assume(n->cfGcd!=NULL);
207   assume(n->cfLcm!=NULL);
208   assume(n->cfDelete!=NULL);
209   assume(n->cfSetMap!=NULL);
210   assume(n->cfName!=NULL);
211   assume(n->cfInpMult!=NULL);
212   assume(n->cfInit_bigint!=NULL);
213#ifdef LDEBUG
214   assume(n->cfDBTest!=NULL);
215#endif
216   assume(n->type==t);
217#endif
218  }
219  else
220  {
221    n->ref++;
222  }
223  return n;
224}
225
226void nKillChar(coeffs r)
227{
228  if (r!=NULL)
229  {
230    r->ref--;
231    if (r->ref<=0)
232    {
233      n_Procs_s tmp;
234      n_Procs_s* n=&tmp;
235      tmp.next=cf_root;
236      while((n->next!=NULL) && (n->next!=r)) n=n->next;
237      if (n->next==r)
238      {
239        n->next=n->next->next;
240        if (cf_root==r) cf_root=n->next;
241        r->cfDelete(&(r->nNULL),r);
242        if (r->cfKillChar!=NULL) r->cfKillChar(r);
243        omFreeSize((void *)r, sizeof(n_Procs_s));
244        r=NULL;
245      }
246      else
247      {
248        WarnS("cf_root list destroyed");
249      }
250      r->cf=NULL;
251    }
252  }
253}
254
255n_coeffType nRegister(n_coeffType n, cfInitCharProc p)
256{
257  if (n==n_unknown)
258  {
259    nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1);
260    if (nInitCharTable==NULL)
261    {
262      nInitCharTable=(cfInitCharProc*)omAlloc0(
263                                          nLastCoeffs*sizeof(cfInitCharProc));
264    }
265    else
266    {
267      nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable,
268                                          (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc),
269                                          ((int)nLastCoeffs)*sizeof(cfInitCharProc));
270    }
271
272    nInitCharTable[nLastCoeffs]=p;
273    return nLastCoeffs;
274  }
275  else
276  {
277    if (nInitCharTable==NULL)
278    {
279      nInitCharTable=(cfInitCharProc*)omAlloc0(
280                                         ((int) nLastCoeffs)*sizeof(cfInitCharProc));
281    }
282    nInitCharTable[n]=p;
283    return n;
284  }
285}
286
Note: See TracBrowser for help on using the repository browser.