source: git/libpolys/coeffs/modulop.cc @ dc4782

spielwiese
Last change on this file since dc4782 was dc4782, checked in by Hans Schoenemann <hannes@…>, 10 years ago
chg: factory/libfac is not optional, removing HAVE_FACTORY/HAVE_LIBFAC
  • Property mode set to 100644
File size: 15.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: numbers modulo p (<=32003)
6*/
7
8#ifdef HAVE_CONFIG_H
9#include "libpolysconfig.h"
10#endif /* HAVE_CONFIG_H */
11#include <misc/auxiliary.h>
12
13#include <factory/factory.h>
14
15#include <string.h>
16#include <omalloc/omalloc.h>
17#include <coeffs/coeffs.h>
18#include <reporter/reporter.h>
19#include <coeffs/numbers.h>
20#include <coeffs/longrat.h>
21#include <coeffs/mpr_complex.h>
22#include <misc/mylimits.h>
23#include <coeffs/modulop.h>
24
25// int npGen=0;
26
27/// Our Type!
28static const n_coeffType ID = n_Zp;
29
30#ifdef NV_OPS
31#pragma GCC diagnostic ignored "-Wlong-long"
32static inline number nvMultM(number a, number b, const coeffs r)
33{
34  assume( getCoeffType(r) == ID );
35
36#if SIZEOF_LONG == 4
37#define ULONG64 (unsigned long long)(unsigned long)
38#else
39#define ULONG64 (unsigned long)
40#endif
41  return (number)
42      (unsigned long)((ULONG64 a)*(ULONG64 b) % (ULONG64 r->ch));
43}
44number  nvMult        (number a, number b, const coeffs r);
45number  nvDiv         (number a, number b, const coeffs r);
46number  nvInvers      (number c, const coeffs r);
47void    nvPower       (number a, int i, number * result, const coeffs r);
48#endif
49
50
51
52
53BOOLEAN npGreaterZero (number k, const coeffs r)
54{
55  assume( n_Test(k, r) );
56
57  int h = (int)((long) k);
58  return ((int)h !=0) && (h <= (r->ch>>1));
59}
60
61//unsigned long npMultMod(unsigned long a, unsigned long b, int npPrimeM)
62//{
63//  unsigned long c = a*b;
64//  c = c % npPrimeM;
65//  assume(c == (unsigned long) npMultM((number) a, (number) b, npPrimeM));
66//  return c;
67//}
68
69number npMult (number a,number b, const coeffs r)
70{
71  assume( n_Test(a, r) );
72  assume( n_Test(b, r) );
73
74  if (((long)a == 0) || ((long)b == 0))
75    return (number)0;
76  number c = npMultM(a,b, r);
77  assume( n_Test(c, r) );
78  return c;
79}
80
81/*2
82* create a number from int
83*/
84number npInit (long i, const coeffs r)
85{
86  long ii=i % (long)r->ch;
87  if (ii <  0L)                         ii += (long)r->ch;
88
89  number c = (number)ii;
90  assume( n_Test(c, r) );
91  return c;
92
93}
94
95
96/*2
97 * convert a number to an int in (-p/2 .. p/2]
98 */
99int npInt(number &n, const coeffs r)
100{
101  assume( n_Test(n, r) );
102
103  if ((long)n > (((long)r->ch) >>1)) return (int)((long)n -((long)r->ch));
104  else                               return (int)((long)n);
105}
106
107number npAdd (number a, number b, const coeffs r)
108{
109  assume( n_Test(a, r) );
110  assume( n_Test(b, r) );
111
112  number c = npAddM(a,b, r);
113
114  assume( n_Test(c, r) );
115
116  return c;
117}
118
119number npSub (number a, number b, const coeffs r)
120{
121  assume( n_Test(a, r) );
122  assume( n_Test(b, r) );
123
124  number c = npSubM(a,b,r);
125
126  assume( n_Test(c, r) );
127
128  return c;
129}
130
131BOOLEAN npIsZero (number  a, const coeffs r)
132{
133  assume( n_Test(a, r) );
134
135  return 0 == (long)a;
136}
137
138BOOLEAN npIsOne (number a, const coeffs r)
139{
140  assume( n_Test(a, r) );
141
142  return 1 == (long)a;
143}
144
145BOOLEAN npIsMOne (number a, const coeffs r)
146{
147  assume( n_Test(a, r) );
148
149  return ((r->npPminus1M == (long)a)&&((long)1!=(long)a));
150}
151
152#ifdef HAVE_DIV_MOD
153
154#ifdef USE_NTL_XGCD
155
156//ifdef HAVE_NTL // in ntl.a
157//extern void XGCD(long& d, long& s, long& t, long a, long b);
158#include <NTL/ZZ.h>
159#ifdef NTL_CLIENT
160NTL_CLIENT
161#endif
162
163#endif
164
165long InvMod(long a, const coeffs R)
166{
167   long d, s, t;
168
169#ifdef USE_NTL_XGCD
170   XGCD(d, s, t, a, R->ch);
171   assume (d == 1);
172#else
173   long  u, v, u0, v0, u1, v1, u2, v2, q, r;
174
175   assume(a>0);
176   u1=1; u2=0;
177   u = a; v = R->ch;
178
179   while (v != 0)
180   {
181      q = u / v;
182      r = u % v;
183      u = v;
184      v = r;
185      u0 = u2;
186      u2 = u1 - q*u2;
187      u1 = u0;
188   }
189
190   assume(u==1);
191   s = u1;
192#endif
193   if (s < 0)
194      return s + R->ch;
195   else
196      return s;
197}
198#endif
199
200inline number npInversM (number c, const coeffs r)
201{
202  assume( n_Test(c, r) );
203#ifndef HAVE_DIV_MOD
204  number d = (number)(long)r->npExpTable[r->npPminus1M - r->npLogTable[(long)c]];
205#else
206  long inv=(long)r->npInvTable[(long)c];
207  if (inv==0)
208  {
209    inv=InvMod((long)c,r);
210    r->npInvTable[(long)c]=inv;
211  }
212  number d = (number)inv;
213#endif
214  assume( n_Test(d, r) );
215  return d;
216
217}
218
219number npDiv (number a,number b, const coeffs r)
220{
221  assume( n_Test(a, r) );
222  assume( n_Test(b, r) );
223
224//#ifdef NV_OPS
225//  if (r->ch>NV_MAX_PRIME)
226//    return nvDiv(a,b);
227//#endif
228  if ((long)a==0)
229    return (number)0;
230  number d;
231
232#ifndef HAVE_DIV_MOD
233  if ((long)b==0)
234  {
235    WerrorS(nDivBy0);
236    return (number)0;
237  }
238
239  int s = r->npLogTable[(long)a] - r->npLogTable[(long)b];
240  if (s < 0)
241    s += r->npPminus1M;
242  d = (number)(long)r->npExpTable[s];
243#else
244  number inv=npInversM(b,r);
245  d = npMultM(a,inv,r);
246#endif
247
248  assume( n_Test(d, r) );
249  return d;
250
251}
252number  npInvers (number c, const coeffs r)
253{
254  assume( n_Test(c, r) );
255
256  if ((long)c==0)
257  {
258    WerrorS("1/0");
259    return (number)0;
260  }
261  number d = npInversM(c,r);
262
263  assume( n_Test(d, r) );
264  return d;
265
266}
267
268number npNeg (number c, const coeffs r)
269{
270  assume( n_Test(c, r) );
271
272  if ((long)c==0) return c;
273
274#if 0
275  number d = npNegM(c,r);
276  assume( n_Test(d, r) );
277  return d;
278#else
279  c = npNegM(c,r);
280  assume( n_Test(c, r) );
281  return c;
282#endif
283}
284
285BOOLEAN npGreater (number a,number b, const coeffs r)
286{
287  assume( n_Test(a, r) );
288  assume( n_Test(b, r) );
289
290  //return (long)a != (long)b;
291  return (long)a > (long)b;
292}
293
294BOOLEAN npEqual (number a,number b, const coeffs r)
295{
296  assume( n_Test(a, r) );
297  assume( n_Test(b, r) );
298
299//  return (long)a == (long)b;
300
301  return npEqualM(a,b,r);
302}
303
304void npWrite (number &a, const coeffs r)
305{
306  assume( n_Test(a, r) );
307
308  if ((long)a>(((long)r->ch) >>1)) StringAppend("-%d",(int)(((long)r->ch)-((long)a)));
309  else                             StringAppend("%d",(int)((long)a));
310}
311
312void npPower (number a, int i, number * result, const coeffs r)
313{
314  assume( n_Test(a, r) );
315
316  if (i==0)
317  {
318    //npInit(1,result);
319    *(long *)result = 1;
320  }
321  else if (i==1)
322  {
323    *result = a;
324  }
325  else
326  {
327    npPower(a,i-1,result,r);
328    *result = npMultM(a,*result,r);
329  }
330}
331
332static const char* npEati(const char *s, int *i, const coeffs r)
333{
334
335  if (((*s) >= '0') && ((*s) <= '9'))
336  {
337    unsigned long ii=0L;
338    do
339    {
340      ii *= 10;
341      ii += *s++ - '0';
342      if (ii >= (MAX_INT_VAL / 10)) ii = ii % r->ch;
343    }
344    while (((*s) >= '0') && ((*s) <= '9'));
345    if (ii >= r->ch) ii = ii % r->ch;
346    *i=(int)ii;
347  }
348  else (*i) = 1;
349  return s;
350}
351
352const char * npRead (const char *s, number *a, const coeffs r)
353{
354  int z;
355  int n=1;
356
357  s = npEati(s, &z, r);
358  if ((*s) == '/')
359  {
360    s++;
361    s = npEati(s, &n, r);
362  }
363  if (n == 1)
364    *a = (number)(long)z;
365  else
366  {
367    if ((z==0)&&(n==0)) WerrorS(nDivBy0);
368    else
369    {
370#ifdef NV_OPS
371      if (r->ch>NV_MAX_PRIME)
372        *a = nvDiv((number)(long)z,(number)(long)n,r);
373      else
374#endif
375        *a = npDiv((number)(long)z,(number)(long)n,r);
376    }
377  }
378  assume( n_Test(*a, r) );
379  return s;
380}
381
382/*2
383* set the charcteristic (allocate and init tables)
384*/
385
386void npKillChar(coeffs r)
387{
388  #ifdef HAVE_DIV_MOD
389  if (r->npInvTable!=NULL)
390  omFreeSize( (void *)r->npInvTable, r->ch*sizeof(unsigned short) );
391  r->npInvTable=NULL;
392  #else
393  if (r->npExpTable!=NULL)
394  {
395    omFreeSize( (void *)r->npExpTable, r->ch*sizeof(unsigned short) );
396    omFreeSize( (void *)r->npLogTable, r->ch*sizeof(unsigned short) );
397    r->npExpTable=NULL; r->npLogTable=NULL;
398  }
399  #endif
400}
401
402static BOOLEAN npCoeffsEqual(const coeffs r, n_coeffType n, void * parameter)
403{
404  /* test, if r is an instance of nInitCoeffs(n,parameter) */
405  return (n==n_Zp) && (r->ch==(int)(long)parameter);
406}
407CanonicalForm npConvSingNFactoryN( number n, BOOLEAN setChar, const coeffs r )
408{
409  if (setChar) setCharacteristic( r->ch );
410  CanonicalForm term(npInt( n,r ));
411  return term;
412}
413
414number npConvFactoryNSingN( const CanonicalForm n, const coeffs r)
415{
416  if (n.isImm())
417  {
418    return npInit(n.intval(),r);
419  }
420  else
421  {
422    assume(0);
423    return NULL;
424  }
425}
426
427BOOLEAN npInitChar(coeffs r, void* p)
428{
429  assume( getCoeffType(r) == ID );
430  const int c = (int) (long) p;
431
432  assume( c > 0 );
433
434  int i, w;
435
436  r->ch = c;
437  r->npPminus1M = c /*r->ch*/ - 1;
438
439  //r->cfInitChar=npInitChar;
440  r->cfKillChar=npKillChar;
441  r->nCoeffIsEqual=npCoeffsEqual;
442
443  r->cfMult  = npMult;
444  r->cfSub   = npSub;
445  r->cfAdd   = npAdd;
446  r->cfDiv   = npDiv;
447  r->cfIntDiv= npDiv;
448  //r->cfIntMod= ndIntMod;
449  r->cfExactDiv= npDiv;
450  r->cfInit = npInit;
451  //r->cfSize  = ndSize;
452  r->cfInt  = npInt;
453  #ifdef HAVE_RINGS
454  //r->cfDivComp = NULL; // only for ring stuff
455  //r->cfIsUnit = NULL; // only for ring stuff
456  //r->cfGetUnit = NULL; // only for ring stuff
457  //r->cfExtGcd = NULL; // only for ring stuff
458  // r->cfDivBy = NULL; // only for ring stuff
459  #endif
460  r->cfNeg   = npNeg;
461  r->cfInvers= npInvers;
462  //r->cfCopy  = ndCopy;
463  //r->cfRePart = ndCopy;
464  //r->cfImPart = ndReturn0;
465  r->cfWriteLong = npWrite;
466  r->cfRead = npRead;
467  //r->cfNormalize=ndNormalize;
468  r->cfGreater = npGreater;
469  r->cfEqual = npEqual;
470  r->cfIsZero = npIsZero;
471  r->cfIsOne = npIsOne;
472  r->cfIsMOne = npIsMOne;
473  r->cfGreaterZero = npGreaterZero;
474  r->cfPower = npPower;
475  r->cfGetDenom = ndGetDenom;
476  r->cfGetNumerator = ndGetNumerator;
477  //r->cfGcd  = ndGcd;
478  //r->cfLcm  = ndGcd;
479  //r->cfDelete= ndDelete;
480  r->cfSetMap = npSetMap;
481  //r->cfName = ndName;
482  r->cfInpMult=ndInpMult;
483  r->cfInit_bigint= nlModP; // npMap0;
484#ifdef NV_OPS
485  if (c>NV_MAX_PRIME)
486  {
487    r->cfMult  = nvMult;
488    r->cfDiv   = nvDiv;
489    r->cfExactDiv= nvDiv;
490    r->cfInvers= nvInvers;
491    r->cfPower= nvPower;
492  }
493#endif
494  r->cfCoeffWrite=npCoeffWrite;
495#ifdef LDEBUG
496  // debug stuff
497  r->cfDBTest=npDBTest;
498#endif
499
500  r->convSingNFactoryN=npConvSingNFactoryN;
501  r->convFactoryNSingN=npConvFactoryNSingN;
502
503  // the variables:
504  r->nNULL = (number)0;
505  r->type = n_Zp;
506  r->ch = c;
507  r->has_simple_Alloc=TRUE;
508  r->has_simple_Inverse=TRUE;
509
510  // the tables
511#ifdef NV_OPS
512  if (r->ch <=NV_MAX_PRIME)
513#endif
514  {
515#if !defined(HAVE_DIV_MOD) || !defined(HAVE_MULT_MOD)
516    r->npExpTable=(unsigned short *)omAlloc( r->ch*sizeof(unsigned short) );
517    r->npLogTable=(unsigned short *)omAlloc( r->ch*sizeof(unsigned short) );
518    r->npExpTable[0] = 1;
519    r->npLogTable[0] = 0;
520    if (r->ch > 2)
521    {
522      w = 1;
523      loop
524      {
525        r->npLogTable[1] = 0;
526        w++;
527        i = 0;
528        loop
529        {
530          i++;
531          r->npExpTable[i] =(int)(((long)w * (long)r->npExpTable[i-1])
532                               % r->ch);
533          r->npLogTable[r->npExpTable[i]] = i;
534          if /*(i == r->ch - 1 ) ||*/ (/*(*/ r->npExpTable[i] == 1 /*)*/)
535            break;
536        }
537        if (i == r->ch - 1)
538          break;
539      }
540    }
541    else
542    {
543      r->npExpTable[1] = 1;
544      r->npLogTable[1] = 0;
545    }
546#endif
547#ifdef HAVE_DIV_MOD
548    r->npInvTable=(unsigned short*)omAlloc0( r->ch*sizeof(unsigned short) );
549#endif
550  }
551  return FALSE;
552}
553
554#ifdef LDEBUG
555BOOLEAN npDBTest (number a, const char *f, const int l, const coeffs r)
556{
557  if (((long)a<0) || ((long)a>r->ch))
558  {
559    Print("wrong mod p number %ld at %s,%d\n",(long)a,f,l);
560    return FALSE;
561  }
562  return TRUE;
563}
564#endif
565
566number npMapP(number from, const coeffs src, const coeffs dst_r)
567{
568  long i = (long)from;
569  if (i>src->ch/2)
570  {
571    i-=src->ch;
572    while (i < 0) i+=dst_r->ch;
573  }
574  i%=dst_r->ch;
575  return (number)i;
576}
577
578static number npMapLongR(number from, const coeffs /*src*/, const coeffs dst_r)
579{
580  gmp_float *ff=(gmp_float*)from;
581  mpf_t *f=ff->_mpfp();
582  number res;
583  mpz_ptr dest,ndest;
584  int size,i;
585  int e,al,bl;
586  long iz;
587  mp_ptr qp,dd,nn;
588
589  size = (*f)[0]._mp_size;
590  if (size == 0)
591    return npInit(0,dst_r);
592  if(size<0)
593    size = -size;
594
595  qp = (*f)[0]._mp_d;
596  while(qp[0]==0)
597  {
598    qp++;
599    size--;
600  }
601
602  if(dst_r->ch>2)
603    e=(*f)[0]._mp_exp-size;
604  else
605    e=0;
606  res = ALLOC_RNUMBER();
607#if defined(LDEBUG)
608  res->debug=123456;
609#endif
610  dest = res->z;
611
612  long in=0;
613  if (e<0)
614  {
615    al = dest->_mp_size = size;
616    if (al<2) al = 2;
617    dd = (mp_ptr)omAlloc(sizeof(mp_limb_t)*al);
618    for (i=0;i<size;i++) dd[i] = qp[i];
619    bl = 1-e;
620    nn = (mp_ptr)omAlloc(sizeof(mp_limb_t)*bl);
621    nn[bl-1] = 1;
622    for (i=bl-2;i>=0;i--) nn[i] = 0;
623    ndest = res->n;
624    ndest->_mp_d = nn;
625    ndest->_mp_alloc = ndest->_mp_size = bl;
626    res->s = 0;
627    in=mpz_fdiv_ui(ndest,dst_r->ch);
628    mpz_clear(ndest);
629  }
630  else
631  {
632    al = dest->_mp_size = size+e;
633    if (al<2) al = 2;
634    dd = (mp_ptr)omAlloc(sizeof(mp_limb_t)*al);
635    for (i=0;i<size;i++) dd[i+e] = qp[i];
636    for (i=0;i<e;i++) dd[i] = 0;
637    res->s = 3;
638  }
639
640  dest->_mp_d = dd;
641  dest->_mp_alloc = al;
642  iz=mpz_fdiv_ui(dest,dst_r->ch);
643  mpz_clear(dest);
644  if(res->s==0)
645    iz=(long)npDiv((number)iz,(number)in,dst_r);
646  FREE_RNUMBER(res); // Q!?
647  return (number)iz;
648}
649
650#ifdef HAVE_RINGS
651/*2
652* convert from a GMP integer
653*/
654number npMapGMP(number from, const coeffs /*src*/, const coeffs dst)
655{
656  int_number erg = (int_number) omAlloc(sizeof(mpz_t)); // evtl. spaeter mit bin
657  mpz_init(erg);
658
659  mpz_mod_ui(erg, (int_number) from, dst->ch);
660  number r = (number) mpz_get_si(erg);
661
662  mpz_clear(erg);
663  omFree((void *) erg);
664  return (number) r;
665}
666
667/*2
668* convert from an machine long
669*/
670number npMapMachineInt(number from, const coeffs /*src*/,const coeffs dst)
671{
672  long i = (long) (((unsigned long) from) % dst->ch);
673  return (number) i;
674}
675#endif
676
677number npMapCanonicalForm (number a, const coeffs /*src*/, const coeffs dst)
678{
679  setCharacteristic (dst ->ch);
680  CanonicalForm f= CanonicalForm ((InternalCF*)(a));
681  return (number) (f.intval());
682}
683
684nMapFunc npSetMap(const coeffs src, const coeffs dst)
685{
686#ifdef HAVE_RINGS
687  if (nCoeff_is_Ring_2toM(src))
688  {
689    return npMapMachineInt;
690  }
691  if (nCoeff_is_Ring_Z(src) || nCoeff_is_Ring_PtoM(src) || nCoeff_is_Ring_ModN(src))
692  {
693    return npMapGMP;
694  }
695#endif
696  if (nCoeff_is_Q(src))
697  {
698    return nlModP; // npMap0;
699  }
700  if ( nCoeff_is_Zp(src) )
701  {
702    if (n_GetChar(src) == n_GetChar(dst))
703    {
704      return ndCopyMap;
705    }
706    else
707    {
708      return npMapP;
709    }
710  }
711  if (nCoeff_is_long_R(src))
712  {
713    return npMapLongR;
714  }
715  if (nCoeff_is_CF (src))
716  {
717    return npMapCanonicalForm;
718  }
719  return NULL;      /* default */
720}
721
722// -----------------------------------------------------------
723//  operation for very large primes (32003< p < 2^31-1)
724// ----------------------------------------------------------
725#ifdef NV_OPS
726
727number nvMult (number a,number b, const coeffs r)
728{
729  //if (((long)a == 0) || ((long)b == 0))
730  //  return (number)0;
731  //else
732    return nvMultM(a,b,r);
733}
734
735void   nvInpMult(number &a, number b, const coeffs r)
736{
737  number n=nvMultM(a,b,r);
738  a=n;
739}
740
741
742inline long nvInvMod(long a, const coeffs R)
743{
744#ifdef HAVE_DIV_MOD
745  return InvMod(a, R);
746#else
747/// TODO: use "long InvMod(long a, const coeffs R)"?!
748
749   long  s;
750
751   long  u, u0, u1, u2, q, r; // v0, v1, v2,
752
753   u1=1; // v1=0;
754   u2=0; // v2=1;
755   u = a;
756
757   long v = R->ch;
758
759   while (v != 0)
760   {
761      q = u / v;
762      r = u % v;
763      u = v;
764      v = r;
765      u0 = u2;
766//      v0 = v2;
767      u2 = u1 - q*u2;
768//      v2 = v1 - q*v2;
769      u1 = u0;
770//      v1 = v0;
771   }
772
773   s = u1;
774   //t = v1;
775   if (s < 0)
776      return s + R->ch;
777   else
778     return s;
779#endif
780}
781
782inline number nvInversM (number c, const coeffs r)
783{
784  long inv=nvInvMod((long)c,r);
785  return (number)inv;
786}
787
788number nvDiv (number a,number b, const coeffs r)
789{
790  if ((long)a==0)
791    return (number)0;
792  else if ((long)b==0)
793  {
794    WerrorS(nDivBy0);
795    return (number)0;
796  }
797  else
798  {
799    number inv=nvInversM(b,r);
800    return nvMultM(a,inv,r);
801  }
802}
803number  nvInvers (number c, const coeffs r)
804{
805  if ((long)c==0)
806  {
807    WerrorS(nDivBy0);
808    return (number)0;
809  }
810  return nvInversM(c,r);
811}
812void nvPower (number a, int i, number * result, const coeffs r)
813{
814  if (i==0)
815  {
816    //npInit(1,result);
817    *(long *)result = 1;
818  }
819  else if (i==1)
820  {
821    *result = a;
822  }
823  else
824  {
825    nvPower(a,i-1,result,r);
826    *result = nvMultM(a,*result,r);
827  }
828}
829#endif
830
831void    npCoeffWrite  (const coeffs r, BOOLEAN /*details*/)
832{
833  Print("//   characteristic : %d\n",r->ch);
834}
835
Note: See TracBrowser for help on using the repository browser.