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

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