source: git/libpolys/coeffs/numbers.cc @ 560a3d

spielwiese
Last change on this file since 560a3d was 560a3d, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
fixing bug (use of non-initialized value) in nlClearDenominators and minor improvements
  • Property mode set to 100644
File size: 12.4 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  assume( nCoeff_is_Ring(r) || nCoeff_is_Zp(r) || nCoeff_is_numeric(r) || nCoeff_is_GF(r) || nCoeff_is_Zp_a(r) );
134
135  numberCollectionEnumerator.Reset();
136 
137#ifdef HAVE_RINGS
138  /// TODO: move to a separate implementation
139  if (nCoeff_is_Ring(r))
140  {
141    if (nCoeff_has_Units(r))
142    {
143      c = n_GetUnit(numberCollectionEnumerator.Current(), r);
144     
145      if (!n_IsOne(c, r))
146      {
147        number inv = n_Invers(c, r);
148
149        do
150        {
151          n_InpMult(numberCollectionEnumerator.Current(), inv, r);
152        }
153        while( numberCollectionEnumerator.MoveNext() );
154
155        n_Delete(&inv, r);       
156      }     
157    } else c = n_Init(1, r);
158   
159    return;
160  }
161#endif
162
163  assume(!nCoeff_is_Ring(r));
164
165  c = numberCollectionEnumerator.Current();
166 
167  n_Normalize(c, r);
168
169  if (!n_IsOne(c, r))
170  {   
171    numberCollectionEnumerator.Current() = n_Init(1, r); // ???
172   
173    number inv = n_Invers(c, r);
174   
175    while( numberCollectionEnumerator.MoveNext() )
176    {
177      number &n = numberCollectionEnumerator.Current();
178      n_Normalize(n, r); // ?
179      n_InpMult(n, inv, r);
180    }
181   
182    n_Delete(&inv, r);
183  }
184}
185
186void ndClearDenominators(ICoeffsEnumerator& /*numberCollectionEnumerator*/, number& d, const coeffs r)
187{
188  assume( r != NULL );
189  assume( !(nCoeff_is_Q(r) || nCoeff_is_transExt(r) || nCoeff_is_algExt(r)) );
190  assume( nCoeff_is_Ring(r) || nCoeff_is_Zp(r) || nCoeff_is_numeric(r) || nCoeff_is_GF(r) );
191
192  d = n_Init(1, r);
193}
194
195number ndCopy(number a, const coeffs) { return a; }
196number ndCopyMap(number a, const coeffs aRing, const coeffs r)
197{
198  assume( getCoeffType(r) == getCoeffType(aRing) );
199  if ( nCoeff_has_simple_Alloc(r) && nCoeff_has_simple_Alloc(aRing) )
200    return a;
201        else
202    return n_Copy(a, r);
203}
204void ndKillChar(coeffs) {}
205void ndSetChar(const coeffs) {}
206
207number nd_Copy(number a, const coeffs r) { return n_Copy(a, r); }
208
209#ifdef HAVE_RINGS
210BOOLEAN ndDivBy(number, number, const coeffs) { return TRUE; } // assume a,b !=0
211int ndDivComp(number, number, const coeffs) { return 2; }
212BOOLEAN ndIsUnit(number a, const coeffs r) { return !n_IsZero(a,r); }
213number  ndExtGcd (number, number, number *, number *, const coeffs r) { return n_Init(1,r); }
214#endif
215
216#ifdef HAVE_FACTORY
217CanonicalForm ndConvSingNFactoryN( number, BOOLEAN /*setChar*/, const coeffs)
218{
219  CanonicalForm term(0);
220  Werror("no conversion to factory");
221  return term;
222}
223
224number ndConvFactoryNSingN( const CanonicalForm, const coeffs)
225{
226  Werror("no conversion from factory");
227  return NULL;
228}
229#endif
230
231number  ndInit_bigint(number, const coeffs, const coeffs)
232{
233  Werror("no conversion from bigint to this field");
234  return NULL;
235}
236
237/**< [in, out] a bigint number >= 0  */
238/**< [out] the GMP equivalent    */
239/// Converts a non-negative bigint number into a GMP number.
240void ndMPZ(mpz_t result, number &n, const coeffs r)
241{
242  mpz_init_set_si( result, n_Int(n, r) );
243}
244
245number ndInitMPZ(mpz_t m, const coeffs r)
246{ 
247  return n_Init( mpz_get_si(m), r);
248}
249
250
251BOOLEAN ndCoeffIsEqual(const coeffs r, n_coeffType n, void *)
252{
253  /* test, if r is an instance of nInitCoeffs(n,parameter) */
254  /* if paramater is not needed */
255  return (n==r->type);
256}
257
258static n_coeffType nLastCoeffs=n_CF;
259cfInitCharProc nInitCharTableDefault[]=
260{ NULL,        /*n_unknown */
261 npInitChar,   /* n_Zp */
262 nlInitChar,   /* n_Q */
263 nrInitChar,   /* n_R */
264 nfInitChar,   /* n_GF */
265 ngfInitChar,  /* n_long_R */
266 #ifdef HAVE_POLYEXTENSIONS
267 naInitChar,  /* n_algExt */
268 ntInitChar,  /* n_transExt */
269 #else
270 NULL,        /* n_algExt */
271 NULL,        /* n_transExt */
272 #endif   
273 ngcInitChar,  /* n_long_C */
274 #ifdef HAVE_RINGS
275 nrzInitChar,  /* n_Z */
276 nrnInitChar,  /* n_Zn */
277 NULL,         /* n_Zpn */
278 nr2mInitChar, /* n_Z2m */
279 #else
280 NULL,         /* n_Z */
281 NULL,         /* n_Zn */
282 NULL,         /* n_Zpn */
283 NULL,         /* n_Z2m */
284 #endif
285 NULL   /* n_CF */
286};
287
288static cfInitCharProc *nInitCharTable=nInitCharTableDefault;
289/*2
290* init operations for coeffs r
291*/
292coeffs nInitChar(n_coeffType t, void * parameter)
293{
294  n_Procs_s *n=cf_root;
295
296  while((n!=NULL) && (n->nCoeffIsEqual!=NULL) && (!n->nCoeffIsEqual(n,t,parameter)))
297      n=n->next;
298
299  if (n==NULL)
300  {
301    n=(n_Procs_s*)omAlloc0(sizeof(n_Procs_s));
302    n->next=cf_root;
303    n->ref=1;
304    n->type=t;
305
306    // default entries (different from NULL) for some routines:
307    n->cfSize = ndSize;
308    n->cfGetDenom= ndGetDenom;
309    n->cfGetNumerator= ndGetNumerator;
310    n->cfName =  ndName;
311    n->cfImPart=ndReturn0;
312    n->cfDelete= ndDelete;
313    n->cfInpMult=ndInpMult;
314    n->cfCopy = ndCopy;
315    n->cfIntMod=ndIntMod; /* dummy !! */
316    n->cfNormalize=ndNormalize;
317    n->cfGcd  = ndGcd;
318    n->cfLcm  = ndGcd; /* tricky, isn't it ?*/
319    n->cfInit_bigint = ndInit_bigint;
320    n->cfInitMPZ = ndInitMPZ;
321    n->cfMPZ = ndMPZ;
322
323    //n->cfKillChar = ndKillChar; /* dummy */
324    n->cfSetChar = ndSetChar; /* dummy */
325    // temp. removed to catch all the coeffs which miss to implement this!
326
327    n->cfChineseRemainder = ndChineseRemainder;
328    n->cfFarey = ndFarey;
329    n->cfParDeg = ndParDeg;
330   
331    n->cfParameter = ndParameter;
332
333    n->cfClearContent = ndClearContent;
334    n->cfClearDenominators = ndClearDenominators;
335
336#ifdef HAVE_RINGS
337    n->cfDivComp = ndDivComp;
338    n->cfDivBy = ndDivBy;
339    n->cfIsUnit = ndIsUnit;
340    n->cfExtGcd = ndExtGcd;
341    //n->cfGetUnit = (nMapFunc)NULL;
342#endif
343
344#ifdef fACTORY
345    n->convSingNFactoryN=ndConvSingNFactoryN;
346    n->convFactoryNSingN=ndConvFactoryNSingN;
347#endif
348   
349    BOOLEAN nOK=TRUE;
350    // init
351    if ((t<=nLastCoeffs) && (nInitCharTable[t]!=NULL))
352      nOK = (nInitCharTable[t])(n,parameter);
353    else
354       Werror("Sorry: the coeff type [%d] was not registered: it is missing in nInitCharTable", (int)t);
355    if (nOK)
356    {
357      omFreeSize(n,sizeof(*n));
358      return NULL;
359    }
360    cf_root=n;
361    // post init settings:
362    if (n->cfRePart==NULL) n->cfRePart=n->cfCopy;
363    if (n->cfIntDiv==NULL) n->cfIntDiv=n->cfDiv;
364   
365#ifdef HAVE_RINGS
366    if (n->cfGetUnit==NULL) n->cfGetUnit=n->cfCopy;
367#endif
368
369    if(n->cfWriteShort==NULL)
370      n->cfWriteShort = n->cfWriteLong;
371
372    assume(n->nCoeffIsEqual!=NULL);
373    assume(n->cfSetChar!=NULL);
374    assume(n->cfMult!=NULL);
375    assume(n->cfSub!=NULL);
376    assume(n->cfAdd!=NULL);
377    assume(n->cfDiv!=NULL);
378    assume(n->cfIntDiv!=NULL);
379    assume(n->cfIntMod!=NULL);
380    assume(n->cfExactDiv!=NULL);
381    assume(n->cfInit!=NULL);
382    assume(n->cfInitMPZ!=NULL);
383    assume(n->cfSize!=NULL);
384    assume(n->cfInt!=NULL);
385    assume(n->cfMPZ!=NULL);
386    //assume(n->n->cfDivComp!=NULL);
387    //assume(n->cfIsUnit!=NULL);
388    //assume(n->cfGetUnit!=NULL);
389    //assume(n->cfExtGcd!=NULL);
390    assume(n->cfNeg!=NULL);
391    assume(n->cfCopy!=NULL);
392    assume(n->cfRePart!=NULL);
393    assume(n->cfImPart!=NULL);
394
395    assume(n->cfWriteLong!=NULL);
396    assume(n->cfWriteShort!=NULL);
397
398    assume(n->iNumberOfParameters>= 0);
399
400    assume( (n->iNumberOfParameters == 0 && n->pParameterNames == NULL) ||
401            (n->iNumberOfParameters >  0 && n->pParameterNames != NULL) );           
402
403    assume(n->cfParameter!=NULL);
404    assume(n->cfParDeg!=NULL);
405     
406    assume(n->cfRead!=NULL);
407    assume(n->cfNormalize!=NULL);
408    assume(n->cfGreater!=NULL);
409    //assume(n->cfDivBy!=NULL);
410    assume(n->cfEqual!=NULL);
411    assume(n->cfIsZero!=NULL);
412    assume(n->cfIsOne!=NULL);
413    assume(n->cfIsMOne!=NULL);
414    assume(n->cfGreaterZero!=NULL);
415    assume(n->cfPower!=NULL);
416    assume(n->cfGetDenom!=NULL);
417    assume(n->cfGetNumerator!=NULL);
418    assume(n->cfGcd!=NULL);
419    assume(n->cfLcm!=NULL);
420    assume(n->cfDelete!=NULL);
421    assume(n->cfSetMap!=NULL);
422    assume(n->cfName!=NULL);
423    assume(n->cfInpMult!=NULL);
424//    assume(n->cfInit_bigint!=NULL);
425    assume(n->cfCoeffWrite != NULL);
426
427    assume(n->cfClearContent != NULL);
428    assume(n->cfClearDenominators != NULL);
429   
430#ifdef LDEBUG
431    assume(n->cfDBTest!=NULL);
432#endif
433    assume(n->type==t);
434     
435#ifndef NDEBUG
436    if(n->cfKillChar==NULL) Warn("cfKillChar is NULL for coeff %d",t);
437    if(n->cfWriteLong==NULL) Warn("cfWrite is NULL for coeff %d",t);
438    if(n->cfWriteShort==NULL) Warn("cfWriteShort is NULL for coeff %d",t);
439#endif
440     
441   if( n->nNULL == NULL )
442     n->nNULL = n_Init(0, n); // may still remain NULL
443  }
444  else
445  {
446    n->ref++;
447  }
448  return n;
449}
450
451void nKillChar(coeffs r)
452{
453  if (r!=NULL)
454  {
455    r->ref--;
456    if (r->ref<=0)
457    {
458      n_Procs_s tmp;
459      n_Procs_s* n=&tmp;
460      tmp.next=cf_root;
461      while((n->next!=NULL) && (n->next!=r)) n=n->next;
462      if (n->next==r)
463      {
464        n->next=n->next->next;
465        if (cf_root==r) cf_root=n->next;
466        r->cfDelete(&(r->nNULL),r);
467        if (r->cfKillChar!=NULL) r->cfKillChar(r);
468        omFreeSize((void *)r, sizeof(n_Procs_s));
469        r=NULL;
470      }
471      else
472      {
473        WarnS("cf_root list destroyed");
474      }
475    }
476  }
477}
478
479
480n_coeffType nRegister(n_coeffType n, cfInitCharProc p)
481{
482  if (n==n_unknown)
483  {
484    nLastCoeffs=(n_coeffType)(int(nLastCoeffs)+1);
485    if (nInitCharTable==nInitCharTableDefault)
486    {
487      nInitCharTable=(cfInitCharProc*)omAlloc0(
488                                          nLastCoeffs*sizeof(cfInitCharProc));
489      memcpy(nInitCharTable,nInitCharTableDefault,
490              (nLastCoeffs-1)*sizeof(cfInitCharProc));
491    }
492    else
493    {
494      nInitCharTable=(cfInitCharProc*)omReallocSize(nInitCharTable,
495                                          (((int)nLastCoeffs)-1)*sizeof(cfInitCharProc),
496                                          ((int)nLastCoeffs)*sizeof(cfInitCharProc));
497    }
498
499    nInitCharTable[nLastCoeffs]=p;
500    return nLastCoeffs;
501  }
502  else
503  {
504    if (nInitCharTable[n]!=NULL) Print("coeff %d already initialized\n",n);
505    nInitCharTable[n]=p;
506    return n;
507  }
508}
509
Note: See TracBrowser for help on using the repository browser.