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

spielwiese
Last change on this file since 854405 was f630e4, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
set coeffs::nNULL if needed...
  • Property mode set to 100644
File size: 10.6 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
13#include "config.h"
14#include <misc/auxiliary.h>
15
16#ifdef HAVE_FACTORY
17#include <factory/factory.h>
18#endif
19
20
21#include "coeffs.h"
22#include <coeffs/numbers.h>
23
24#include <reporter/reporter.h>
25#include <omalloc/omalloc.h>
26#include <coeffs/numbers.h>
27#include <coeffs/longrat.h>
28#include <coeffs/modulop.h>
29#include <coeffs/gnumpfl.h>
30#include <coeffs/gnumpc.h>
31#include <coeffs/ffields.h>
32#include <coeffs/shortfl.h>
33
34#ifdef HAVE_RINGS
35#include <coeffs/rmodulo2m.h>
36#include <coeffs/rmodulon.h>
37#include <coeffs/rintegers.h>
38#endif
39
40#ifdef HAVE_POLYEXTENSIONS
41#include <polys/ext_fields/algext.h>
42#include <polys/ext_fields/transext.h>
43#endif
44
45
46
47//static int characteristic = 0;
48extern int IsPrime(int p);
49
50n_Procs_s *cf_root=NULL;
51
52void   nNew(number* d) { *d=NULL; }
53void   ndDelete(number* d, const coeffs) { *d=NULL; }
54void   ndInpMult(number &a, number b, const coeffs r)
55{
56  number n=n_Mult(a,b,r);
57  n_Delete(&a,r);
58  a=n;
59}
60void ndInpAdd(number &a, number b, const coeffs r)
61{
62  number n=n_Add(a,b,r);
63  n_Delete(&a,r);
64  a=n;
65}
66
67#ifdef LDEBUG
68void   nDBDummy1(number* d,char *, int) { *d=NULL; }
69BOOLEAN ndDBTest(number, const char *, const int, const coeffs)
70{
71  return TRUE;
72}
73#endif
74
75number ndFarey(number,number,const coeffs r)
76{
77  Werror("farey not implemented for (c=%d)",getCoeffType(r));
78  return NULL;
79}
80number ndChineseRemainder(number *,number *,int,const coeffs r)
81{
82  Werror("ChineseRemainder not implemented for (c=%d)",getCoeffType(r));
83  return n_Init(0,r); 
84}
85
86static int ndParDeg(number n, const coeffs r)
87{
88  return (-n_IsZero(n,r));
89}
90
91static number ndParameter(const int i, const coeffs r)
92{
93  Werror("ndParameter: n_Parameter is not implemented/relevant for (coeff_type = %d)",getCoeffType(r));
94  return NULL;
95}
96
97BOOLEAN n_IsZeroDivisor( number a, const coeffs r)
98{
99  int c = n_GetChar(r);
100  BOOLEAN ret = n_IsZero(a, r);
101  if( (c != 0) && !ret )
102  {
103    number ch = n_Init( c, r ); 
104    number g = n_Gcd( ch, a, r );
105    ret = !n_IsOne (g, r);
106    n_Delete(&ch, r);
107    n_Delete(&g, r);
108  }
109  return ret; 
110}
111
112void   ndNormalize(number&, const coeffs) { }
113
114char * ndName(number, const coeffs) { return NULL; }
115
116number ndReturn0(number, const coeffs r) { return n_Init(0,r); }
117
118number ndGcd(number, number, const coeffs r) { return n_Init(1,r); }
119
120number ndIntMod(number, number, const coeffs r) { return n_Init(0,r); }
121
122number ndGetDenom(number &, const coeffs r) { return n_Init(1,r); }
123number ndGetNumerator(number &a,const coeffs r) { return n_Copy(a,r); }
124
125int ndSize(number a, const coeffs r) { return (int)n_IsZero(a,r)==FALSE; }
126
127number ndCopy(number a, const coeffs) { return a; }
128number ndCopyMap(number a, const coeffs aRing, const coeffs r)
129{
130  assume( getCoeffType(r) == getCoeffType(aRing) );
131  if ( nCoeff_has_simple_Alloc(r) && nCoeff_has_simple_Alloc(aRing) )
132    return a;
133        else
134    return n_Copy(a, r);
135}
136void ndKillChar(coeffs) {}
137void ndSetChar(const coeffs) {}
138
139number nd_Copy(number a, const coeffs r) { return n_Copy(a, r); }
140
141#ifdef HAVE_RINGS
142BOOLEAN ndDivBy(number, number, const coeffs) { return TRUE; } // assume a,b !=0
143int ndDivComp(number, number, const coeffs) { return 2; }
144BOOLEAN ndIsUnit(number a, const coeffs r) { return !n_IsZero(a,r); }
145number  ndExtGcd (number, number, number *, number *, const coeffs r) { return n_Init(1,r); }
146#endif
147
148#ifdef HAVE_FACTORY
149CanonicalForm ndConvSingNFactoryN( number, BOOLEAN /*setChar*/, const coeffs)
150{
151  CanonicalForm term(0);
152  Werror("no conversion to factory");
153  return term;
154}
155
156number ndConvFactoryNSingN( const CanonicalForm, const coeffs)
157{
158  Werror("no conversion from factory");
159  return NULL;
160}
161#endif
162
163number  ndInit_bigint(number, const coeffs, const coeffs)
164{
165  Werror("no conversion from bigint to this field");
166  return NULL;
167}
168
169/**< [in, out] a bigint number >= 0  */
170/**< [out] the GMP equivalent    */
171/// Converts a non-negative bigint number into a GMP number.
172void ndMPZ(mpz_t result, number &n, const coeffs r)
173{
174  mpz_init_set_si( result, n_Int(n, r) );
175}
176
177number ndInitMPZ(mpz_t m, const coeffs r)
178{ 
179  return n_Init( mpz_get_si(m), r);
180}
181
182
183BOOLEAN ndCoeffIsEqual(const coeffs r, n_coeffType n, void *)
184{
185  /* test, if r is an instance of nInitCoeffs(n,parameter) */
186  /* if paramater is not needed */
187  return (n==r->type);
188}
189
190static n_coeffType nLastCoeffs=n_CF;
191cfInitCharProc nInitCharTableDefault[]=
192{ NULL,        /*n_unknown */
193 npInitChar,   /* n_Zp */
194 nlInitChar,   /* n_Q */
195 nrInitChar,   /* n_R */
196 nfInitChar,   /* n_GF */
197 ngfInitChar,  /* n_long_R */
198 #ifdef HAVE_POLYEXTENSIONS
199 naInitChar,  /* n_algExt */
200 ntInitChar,  /* n_transExt */
201 #else
202 NULL,        /* n_algExt */
203 NULL,        /* n_transExt */
204 #endif   
205 ngcInitChar,  /* n_long_C */
206 #ifdef HAVE_RINGS
207 nrzInitChar,  /* n_Z */
208 nrnInitChar,  /* n_Zn */
209 NULL,         /* n_Zpn */
210 nr2mInitChar, /* n_Z2m */
211 #else
212 NULL,         /* n_Z */
213 NULL,         /* n_Zn */
214 NULL,         /* n_Zpn */
215 NULL,         /* n_Z2m */
216 #endif
217 NULL   /* n_CF */
218};
219
220static cfInitCharProc *nInitCharTable=nInitCharTableDefault;
221/*2
222* init operations for coeffs r
223*/
224coeffs nInitChar(n_coeffType t, void * parameter)
225{
226  n_Procs_s *n=cf_root;
227
228  while((n!=NULL) && (n->nCoeffIsEqual!=NULL) && (!n->nCoeffIsEqual(n,t,parameter)))
229      n=n->next;
230
231  if (n==NULL)
232  {
233    n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s));
234    n->next=cf_root;
235    n->ref=1;
236    n->type=t;
237
238    // default entries (different from NULL) for some routines:
239    n->cfSize = ndSize;
240    n->cfGetDenom= ndGetDenom;
241    n->cfGetNumerator= ndGetNumerator;
242    n->cfName =  ndName;
243    n->cfImPart=ndReturn0;
244    n->cfDelete= ndDelete;
245    n->cfInpMult=ndInpMult;
246    n->cfCopy = ndCopy;
247    n->cfIntMod=ndIntMod; /* dummy !! */
248    n->cfNormalize=ndNormalize;
249    n->cfGcd  = ndGcd;
250    n->cfLcm  = ndGcd; /* tricky, isn't it ?*/
251    n->cfInit_bigint = ndInit_bigint;
252    n->cfInitMPZ = ndInitMPZ;
253    n->cfMPZ = ndMPZ;
254
255    //n->cfKillChar = ndKillChar; /* dummy */
256    n->cfSetChar = ndSetChar; /* dummy */
257    // temp. removed to catch all the coeffs which miss to implement this!
258
259    n->cfChineseRemainder = ndChineseRemainder;
260    n->cfFarey = ndFarey;
261    n->cfParDeg = ndParDeg;
262   
263    n->cfParameter = ndParameter;
264
265#ifdef HAVE_RINGS
266    n->cfDivComp = ndDivComp;
267    n->cfDivBy = ndDivBy;
268    n->cfIsUnit = ndIsUnit;
269    n->cfExtGcd = ndExtGcd;
270    //n->cfGetUnit = (nMapFunc)NULL;
271#endif
272
273#ifdef fACTORY
274    n->convSingNFactoryN=ndConvSingNFactoryN;
275    n->convFactoryNSingN=ndConvFactoryNSingN;
276#endif
277   
278    BOOLEAN nOK=TRUE;
279    // init
280    if ((t<=nLastCoeffs) && (nInitCharTable[t]!=NULL))
281      nOK = (nInitCharTable[t])(n,parameter);
282    else
283       Werror("Sorry: the coeff type [%d] was not registered: it is missing in nInitCharTable", (int)t);
284    if (nOK)
285    {
286      omFreeSize(n,sizeof(*n));
287      return NULL;
288    }
289    cf_root=n;
290    // post init settings:
291    if (n->cfRePart==NULL) n->cfRePart=n->cfCopy;
292    if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv;
293   
294#ifdef HAVE_RINGS
295    if (n->cfGetUnit==NULL) n->cfGetUnit=n->cfCopy;
296#endif
297
298    if(n->cfWriteShort==NULL)
299      n->cfWriteShort = n->cfWriteLong;
300
301    assume(n->nCoeffIsEqual!=NULL);
302    assume(n->cfSetChar!=NULL);
303    assume(n->cfMult!=NULL);
304    assume(n->cfSub!=NULL);
305    assume(n->cfAdd!=NULL);
306    assume(n->cfDiv!=NULL);
307    assume(n->cfIntDiv!=NULL);
308    assume(n->cfIntMod!=NULL);
309    assume(n->cfExactDiv!=NULL);
310    assume(n->cfInit!=NULL);
311    assume(n->cfInitMPZ!=NULL);
312    assume(n->cfSize!=NULL);
313    assume(n->cfInt!=NULL);
314    assume(n->cfMPZ!=NULL);
315    //assume(n->n->cfDivComp!=NULL);
316    //assume(n->cfIsUnit!=NULL);
317    //assume(n->cfGetUnit!=NULL);
318    //assume(n->cfExtGcd!=NULL);
319    assume(n->cfNeg!=NULL);
320    assume(n->cfCopy!=NULL);
321    assume(n->cfRePart!=NULL);
322    assume(n->cfImPart!=NULL);
323
324    assume(n->cfWriteLong!=NULL);
325    assume(n->cfWriteShort!=NULL);
326
327    assume(n->iNumberOfParameters>= 0);
328
329    assume( (n->iNumberOfParameters == 0 && n->pParameterNames == NULL) ||
330            (n->iNumberOfParameters >  0 && n->pParameterNames != NULL) );           
331
332    assume(n->cfParameter!=NULL);
333    assume(n->cfParDeg!=NULL);
334     
335    assume(n->cfRead!=NULL);
336    assume(n->cfNormalize!=NULL);
337    assume(n->cfGreater!=NULL);
338    //assume(n->cfDivBy!=NULL);
339    assume(n->cfEqual!=NULL);
340    assume(n->cfIsZero!=NULL);
341    assume(n->cfIsOne!=NULL);
342    assume(n->cfIsMOne!=NULL);
343    assume(n->cfGreaterZero!=NULL);
344    assume(n->cfPower!=NULL);
345    assume(n->cfGetDenom!=NULL);
346    assume(n->cfGetNumerator!=NULL);
347    assume(n->cfGcd!=NULL);
348    assume(n->cfLcm!=NULL);
349    assume(n->cfDelete!=NULL);
350    assume(n->cfSetMap!=NULL);
351    assume(n->cfName!=NULL);
352    assume(n->cfInpMult!=NULL);
353//    assume(n->cfInit_bigint!=NULL);
354    assume(n->cfCoeffWrite != NULL);
355#ifdef LDEBUG
356    assume(n->cfDBTest!=NULL);
357#endif
358    assume(n->type==t);
359     
360#ifndef NDEBUG
361    if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t);
362    if(n->cfWriteLong==NULL) Warn("cfWrite is NULL for coeff %d",t);
363    if(n->cfWriteShort==NULL) Warn("cfWriteShort is NULL for coeff %d",t);
364#endif
365     
366   if( n->nNULL == NULL )
367     n->nNULL = n_Init(0, n); // may still remain NULL
368  }
369  else
370  {
371    n->ref++;
372  }
373  return n;
374}
375
376void nKillChar(coeffs r)
377{
378  if (r!=NULL)
379  {
380    r->ref--;
381    if (r->ref<=0)
382    {
383      n_Procs_s tmp;
384      n_Procs_s* n=&tmp;
385      tmp.next=cf_root;
386      while((n->next!=NULL) && (n->next!=r)) n=n->next;
387      if (n->next==r)
388      {
389        n->next=n->next->next;
390        if (cf_root==r) cf_root=n->next;
391        r->cfDelete(&(r->nNULL),r);
392        if (r->cfKillChar!=NULL) r->cfKillChar(r);
393        omFreeSize((void *)r, sizeof(n_Procs_s));
394        r=NULL;
395      }
396      else
397      {
398        WarnS("cf_root list destroyed");
399      }
400    }
401  }
402}
403
404
405n_coeffType nRegister(n_coeffType n, cfInitCharProc p)
406{
407  if (n==n_unknown)
408  {
409    nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1);
410    if (nInitCharTable==nInitCharTableDefault)
411    {
412      nInitCharTable=(cfInitCharProc*)omAlloc0(
413                                          nLastCoeffs*sizeof(cfInitCharProc));
414      memcpy(nInitCharTable,nInitCharTableDefault,
415              (nLastCoeffs-1)*sizeof(cfInitCharProc));
416    }
417    else
418    {
419      nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable,
420                                          (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc),
421                                          ((int)nLastCoeffs)*sizeof(cfInitCharProc));
422    }
423
424    nInitCharTable[nLastCoeffs]=p;
425    return nLastCoeffs;
426  }
427  else
428  {
429    if (nInitCharTable[n]!=NULL) Print("coeff %d already initialized\n",n);
430    nInitCharTable[n]=p;
431    return n;
432  }
433}
434
Note: See TracBrowser for help on using the repository browser.