source: git/libpolys/coeffs/numbers.cc @ 9952bd

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