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

spielwiese
Last change on this file since b19ab84 was b19ab84, checked in by Hans Schoenemann <hannes@…>, 12 years ago
add some missing routines to 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 "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
138
139static n_coeffType nLastCoeffs=n_Z2m;
140static cfInitCharProc *nInitCharTable=NULL;
141/*2
142* init operations for coeffs r
143*/
144coeffs nInitChar(n_coeffType t, void * parameter)
145{
146  n_Procs_s *n=cf_root;
147
148  while((n!=NULL) && (n->nCoeffIsEqual!=NULL) && (!n->nCoeffIsEqual(n,t,parameter)))
149      n=n->next;
150
151  if (n==NULL)
152  {
153    n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s));
154    n->next=cf_root;
155    n->ref=1;
156    n->type=t;
157
158    // default entries (different from NULL) for some routines:
159    n->cfPar  = ndPar;
160    n->cfParDeg=ndParDeg;
161    n->cfSize = ndSize;
162    n->cfGetDenom= ndGetDenom;
163    n->cfGetNumerator= ndGetNumerator;
164    n->cfName =  ndName;
165    n->cfImPart=ndReturn0;
166    n->cfDelete= ndDelete;
167    n->cfInpMult=ndInpMult;
168    n->cfCopy=nd_Copy;
169    n->cfIntMod=ndIntMod; /* dummy !! */
170    n->cfNormalize=ndNormalize;
171    n->cfGcd  = ndGcd;
172    n->cfLcm  = ndGcd; /* tricky, isn't it ?*/
173    //n->cfKillChar = ndKillChar; /* dummy */
174    // temp. removed to catch all the coeffs which miss to implement this!
175
176#ifdef HAVE_RINGS
177    n->cfDivComp = ndDivComp;
178    n->cfDivBy = ndDivBy;
179    n->cfIsUnit = ndIsUnit;
180    n->cfExtGcd = ndExtGcd;
181    //n->cfGetUnit = (nMapFunc)NULL;
182#endif
183
184#ifdef fACTORY
185    n->convSingNFactoryN=ndConvSingNFactoryN;
186    n->convFactoryNSingN=ndConvFactoryNSingN;
187#endif
188   
189    BOOLEAN nOK=TRUE;
190    // init
191    if ((nInitCharTable!=NULL) && (t<=nLastCoeffs))
192      nOK = (nInitCharTable[t])(n,parameter);
193    else
194       Werror("coeff init missing for %d",(int)t);
195    if (nOK)
196    {
197      omFreeSize(n,sizeof(*n));
198      return NULL;
199    }
200    cf_root=n;
201    // post init settings:
202    if (n->cfRePart==NULL) n->cfRePart=n->cfCopy;
203    if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv;
204   
205#ifdef HAVE_RINGS
206    if (n->cfGetUnit==NULL) n->cfGetUnit=n->cfCopy;
207#endif
208   
209#ifndef NDEBUG
210    assume(n->nCoeffIsEqual!=NULL);
211    if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t);
212    if(n->cfSetChar!=NULL) Warn("cfSetChar is NOT NULL for coeff %d",t);
213    assume(n->cfMult!=NULL);
214    assume(n->cfSub!=NULL);
215    assume(n->cfAdd!=NULL);
216    assume(n->cfDiv!=NULL);
217    assume(n->cfIntDiv!=NULL);
218    assume(n->cfIntMod!=NULL);
219    assume(n->cfExactDiv!=NULL);
220    assume(n->cfInit!=NULL);
221    assume(n->cfPar!=NULL);
222    assume(n->cfParDeg!=NULL);
223    assume(n->cfSize!=NULL);
224    assume(n->cfInt!=NULL);
225    //assume(n->n->cfDivComp!=NULL);
226    //assume(n->cfIsUnit!=NULL);
227    //assume(n->cfGetUnit!=NULL);
228    //assume(n->cfExtGcd!=NULL);
229    assume(n->cfNeg!=NULL);
230    assume(n->cfCopy!=NULL);
231    assume(n->cfRePart!=NULL);
232    assume(n->cfImPart!=NULL);
233    assume(n->cfWrite!=NULL);
234    assume(n->cfRead!=NULL);
235    assume(n->cfNormalize!=NULL);
236    assume(n->cfGreater!=NULL);
237    //assume(n->cfDivBy!=NULL);
238    assume(n->cfEqual!=NULL);
239    assume(n->cfIsZero!=NULL);
240    assume(n->cfIsOne!=NULL);
241    assume(n->cfIsMOne!=NULL);
242    assume(n->cfGreaterZero!=NULL);
243    assume(n->cfPower!=NULL);
244    assume(n->cfGetDenom!=NULL);
245    assume(n->cfGetNumerator!=NULL);
246    assume(n->cfGcd!=NULL);
247    assume(n->cfLcm!=NULL);
248    assume(n->cfDelete!=NULL);
249    assume(n->cfSetMap!=NULL);
250    assume(n->cfName!=NULL);
251    assume(n->cfInpMult!=NULL);
252    assume(n->cfInit_bigint!=NULL);
253    assume(n->cfCoeffWrite != NULL);
254#ifdef LDEBUG
255    assume(n->cfDBTest!=NULL);
256#endif
257    assume(n->type==t);
258#endif
259  }
260  else
261  {
262    n->ref++;
263  }
264  return n;
265}
266
267void nKillChar(coeffs r)
268{
269  if (r!=NULL)
270  {
271    r->ref--;
272    if (r->ref<=0)
273    {
274      n_Procs_s tmp;
275      n_Procs_s* n=&tmp;
276      tmp.next=cf_root;
277      while((n->next!=NULL) && (n->next!=r)) n=n->next;
278      if (n->next==r)
279      {
280        n->next=n->next->next;
281        if (cf_root==r) cf_root=n->next;
282        r->cfDelete(&(r->nNULL),r);
283        if (r->cfKillChar!=NULL) r->cfKillChar(r);
284        omFreeSize((void *)r, sizeof(n_Procs_s));
285        r=NULL;
286      }
287      else
288      {
289        WarnS("cf_root list destroyed");
290      }
291      r->cf=NULL;
292    }
293  }
294}
295
296n_coeffType nRegister(n_coeffType n, cfInitCharProc p)
297{
298  if (n==n_unknown)
299  {
300    nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1);
301    if (nInitCharTable==NULL)
302    {
303      nInitCharTable=(cfInitCharProc*)omAlloc0(
304                                          nLastCoeffs*sizeof(cfInitCharProc));
305    }
306    else
307    {
308      nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable,
309                                          (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc),
310                                          ((int)nLastCoeffs)*sizeof(cfInitCharProc));
311    }
312
313    nInitCharTable[nLastCoeffs]=p;
314    return nLastCoeffs;
315  }
316  else
317  {
318    if (nInitCharTable==NULL)
319    {
320      nInitCharTable=(cfInitCharProc*)omAlloc0(
321                                         ((int) nLastCoeffs)*sizeof(cfInitCharProc));
322    }
323    nInitCharTable[n]=p;
324    return n;
325  }
326}
327
Note: See TracBrowser for help on using the repository browser.