source: git/coeffs/rmodulo2m.cc @ 925a43c

spielwiese
Last change on this file since 925a43c was 925a43c, checked in by Hans Schoenemann <hannes@…>, 14 years ago
fixes for rmodulo2m
  • Property mode set to 100755
File size: 16.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT: numbers modulo 2^m
7*/
8
9#include <string.h>
10#include "config.h"
11#include <auxiliary.h>
12
13#ifdef HAVE_RINGS
14#include <mylimits.h>
15#include "coeffs.h"
16#include "reporter.h"
17#include "omalloc.h"
18#include "numbers.h"
19#include "longrat.h"
20#include "mpr_complex.h"
21#include "rmodulo2m.h"
22#include "si_gmp.h"
23
24int nr2mExp;
25
26extern omBin gmp_nrz_bin; /* init in rintegers*/
27
28/* for initializing function pointers */
29void nr2mInitChar (coeffs r, void*)
30{
31     nr2mInitExp(r->ch, r);
32     r->cfInit       = nr2mInit;
33     r->cfCopy       = ndCopy;
34     r->cfInt        = nr2mInt;
35     r->cfAdd         = nr2mAdd;
36     r->cfSub         = nr2mSub;
37     r->cfMult        = nr2mMult;
38     r->cfDiv         = nr2mDiv;
39     r->cfIntDiv      = nr2mIntDiv;
40     r->cfIntMod      = nr2mMod;
41     r->cfExactDiv    = nr2mDiv;
42     r->cfNeg         = nr2mNeg;
43     r->cfInvers      = nr2mInvers;
44     r->cfDivBy       = nr2mDivBy;
45     r->cfDivComp     = nr2mDivComp;
46     r->cfGreater     = nr2mGreater;
47     r->cfEqual       = nr2mEqual;
48     r->cfIsZero      = nr2mIsZero;
49     r->cfIsOne       = nr2mIsOne;
50     r->cfIsMOne      = nr2mIsMOne;
51     r->cfGreaterZero = nr2mGreaterZero;
52     r->cfWrite      = nr2mWrite;
53     r->cfRead        = nr2mRead;
54     r->cfPower       = nr2mPower;
55     r->cfSetMap     = nr2mSetMap;
56     r->cfNormalize   = ndNormalize;
57     r->cfLcm         = nr2mLcm;
58     r->cfGcd         = nr2mGcd;
59     r->cfIsUnit      = nr2mIsUnit;
60     r->cfGetUnit     = nr2mGetUnit;
61     r->cfExtGcd      = nr2mExtGcd;
62     r->cfName        = ndName;
63#ifdef LDEBUG
64     r->cfDBTest      = nr2mDBTest;
65#endif
66}
67
68/*
69 * Multiply two numbers
70 */
71number nr2mMult (number a, number b, const coeffs r)
72{
73  if (((NATNUMBER)a == 0) || ((NATNUMBER)b == 0))
74    return (number)0;
75  else
76    return nr2mMultM(a,b,r);
77}
78
79/*
80 * Give the smallest non unit k, such that a * x = k = b * y has a solution
81 */
82number nr2mLcm (number a, number b, const coeffs r)
83{
84  NATNUMBER res = 0;
85  if ((NATNUMBER) a == 0) a = (number) 1;
86  if ((NATNUMBER) b == 0) b = (number) 1;
87  while ((NATNUMBER) a % 2 == 0)
88  {
89    a = (number) ((NATNUMBER) a / 2);
90    if ((NATNUMBER) b % 2 == 0) b = (number) ((NATNUMBER) b / 2);
91    res++;
92  }
93  while ((NATNUMBER) b % 2 == 0)
94  {
95    b = (number) ((NATNUMBER) b / 2);
96    res++;
97  }
98  return (number) (1L << res);  // (2**res)
99}
100
101/*
102 * Give the largest non unit k, such that a = x * k, b = y * k has
103 * a solution.
104 */
105number nr2mGcd (number a, number b, const coeffs r)
106{
107  NATNUMBER res = 0;
108  if ((NATNUMBER) a == 0 && (NATNUMBER) b == 0) return (number) 1;
109  while ((NATNUMBER) a % 2 == 0 && (NATNUMBER) b % 2 == 0)
110  {
111    a = (number) ((NATNUMBER) a / 2);
112    b = (number) ((NATNUMBER) b / 2);
113    res++;
114  }
115//  if ((NATNUMBER) b % 2 == 0)
116//  {
117//    return (number) ((1L << res));// * (NATNUMBER) a);  // (2**res)*a    a ist Einheit
118//  }
119//  else
120//  {
121    return (number) ((1L << res));// * (NATNUMBER) b);  // (2**res)*b    b ist Einheit
122//  }
123}
124
125/*
126 * Give the largest non unit k, such that a = x * k, b = y * k has
127 * a solution.
128 */
129number nr2mExtGcd (number a, number b, number *s, number *t, const coeffs r)
130{
131  NATNUMBER res = 0;
132  if ((NATNUMBER) a == 0 && (NATNUMBER) b == 0) return (number) 1;
133  while ((NATNUMBER) a % 2 == 0 && (NATNUMBER) b % 2 == 0)
134  {
135    a = (number) ((NATNUMBER) a / 2);
136    b = (number) ((NATNUMBER) b / 2);
137    res++;
138  }
139  if ((NATNUMBER) b % 2 == 0)
140  {
141    *t = NULL;
142    *s = nr2mInvers(a,r);
143    return (number) ((1L << res));// * (NATNUMBER) a);  // (2**res)*a    a ist Einheit
144  }
145  else
146  {
147    *s = NULL;
148    *t = nr2mInvers(b,r);
149    return (number) ((1L << res));// * (NATNUMBER) b);  // (2**res)*b    b ist Einheit
150  }
151}
152
153void nr2mPower (number a, int i, number * result, const coeffs r)
154{
155  if (i==0)
156  {
157    *(NATNUMBER *)result = 1;
158  }
159  else if (i==1)
160  {
161    *result = a;
162  }
163  else
164  {
165    nr2mPower(a,i-1,result,r);
166    *result = nr2mMultM(a,*result,r);
167  }
168}
169
170/*
171 * create a number from int
172 */
173number nr2mInit (int i, const coeffs r)
174{
175  if (i == 0) return (number)(NATNUMBER)i;
176
177  long ii = i;
178  NATNUMBER j = (NATNUMBER)1;
179  if (ii < 0) { j = r->nr2mModul; ii = -ii; }
180  NATNUMBER k = (NATNUMBER)ii;
181  k = k & r->nr2mModul;
182  /* now we have: from = j * k mod 2^m */
183  return (number)nr2mMult((number)j, (number)k);
184}
185
186/*
187 * convert a number to an int in ]-k/2 .. k/2],
188 * where k = 2^m; i.e., an int in ]-2^(m-1) .. 2^(m-1)];
189 * note that the code computes a long which will then
190 * automatically casted to int
191 */
192int nr2mInt(number &n, const coeffs r)
193{
194  NATNUMBER nn = (unsigned long)(NATNUMBER)n & r->nr2mModul;
195  unsigned long l = r->nr2mModul >> 1; l++; /* now: l = 2^(m-1) */
196  if ((NATNUMBER)nn > l)
197    return (int)((NATNUMBER)nn - r->nr2mModul - 1);
198  else
199    return (int)((NATNUMBER)nn);
200}
201
202number nr2mAdd (number a, number b, const coeffs r)
203{
204  return nr2mAddM(a,b,r);
205}
206
207number nr2mSub (number a, number b, const coeffs r)
208{
209  return nr2mSubM(a,b,r);
210}
211
212BOOLEAN nr2mIsUnit (number a, const coeffs r)
213{
214  return ((NATNUMBER) a % 2 == 1);
215}
216
217number  nr2mGetUnit (number k, const coeffs r)
218{
219  if (k == NULL)
220    return (number) 1;
221  NATNUMBER tmp = (NATNUMBER) k;
222  while (tmp % 2 == 0)
223    tmp = tmp / 2;
224  return (number) tmp;
225}
226
227BOOLEAN nr2mIsZero (number a, const coeffs r)
228{
229  return 0 == (NATNUMBER)a;
230}
231
232BOOLEAN nr2mIsOne (number a, const coeffs r)
233{
234  return 1 == (NATNUMBER)a;
235}
236
237BOOLEAN nr2mIsMOne (number a, const coeffs r)
238{
239  return (r->nr2mModul  == (NATNUMBER)a) 
240        && (r->nr2mModul != 2);
241}
242
243BOOLEAN nr2mEqual (number a, number b, const coeffs r)
244{
245  return a==b;
246}
247
248BOOLEAN nr2mGreater (number a, number b, const coeffs r)
249{
250  return nr2mDivBy(a, b,r);
251}
252
253/* Is a divisible by b? There are two cases:
254   1) a = 0 mod 2^m; then TRUE iff b = 0 or b is a power of 2
255   2) a, b <> 0; then TRUE iff b/gcd(a, b) is a unit mod 2^m
256   TRUE iff b(gcd(a, b) is a unit */
257BOOLEAN nr2mDivBy (number a, number b, const coeffs r)
258{
259  if (a == NULL)
260  {
261    NATNUMBER c = r->nr2mModul + 1;
262    if (c != 0) /* i.e., if no overflow */
263      return (c % (NATNUMBER)b) == 0;
264    else
265    {
266      /* overflow: we need to check whether b
267         is zero or a power of 2: */
268      c = (NATNUMBER)b;
269      while (c != 0)
270      {
271        if ((c % 2) != 0) return FALSE;
272        c = c >> 1;
273      }
274      return TRUE;
275    }
276  }
277  else
278  {
279    number n = nr2mGcd(a, b, r);
280    n = nr2mDiv(b, n, r);
281    return nr2mIsUnit(n, r);
282  }
283}
284
285int nr2mDivComp(number as, number bs, const coeffs r)
286{
287  NATNUMBER a = (NATNUMBER) as;
288  NATNUMBER b = (NATNUMBER) bs;
289  assume(a != 0 && b != 0);
290  while (a % 2 == 0 && b % 2 == 0)
291  {
292    a = a / 2;
293    b = b / 2;
294  }
295  if (a % 2 == 0)
296  {
297    return -1;
298  }
299  else
300  {
301    if (b % 2 == 1)
302    {
303      return 2;
304    }
305    else
306    {
307      return 1;
308    }
309  }
310}
311
312/* TRUE iff 0 < k <= 2^m / 2 */
313BOOLEAN nr2mGreaterZero (number k, const coeffs r)
314{
315  if ((NATNUMBER)k == 0) return FALSE;
316  if ((NATNUMBER)k > ((r->nr2mModul >> 1) + 1)) return FALSE;
317  return TRUE;
318}
319
320/* assumes that 'a' is odd, i.e., a unit in Z/2^m, and computes
321   the extended gcd of 'a' and 2^m, in order to find some 's'
322   and 't' such that a * s + 2^m * t = gcd(a, 2^m) = 1;
323   this code will always find a positive 's' */
324void specialXGCD(unsigned long& s, unsigned long a, const coeffs R)
325{
326  int_number u = (int_number)omAlloc(sizeof(mpz_t));
327  mpz_init_set_ui(u, a);
328  int_number u0 = (int_number)omAlloc(sizeof(mpz_t));
329  mpz_init(u0);
330  int_number u1 = (int_number)omAlloc(sizeof(mpz_t));
331  mpz_init_set_ui(u1, 1);
332  int_number u2 = (int_number)omAlloc(sizeof(mpz_t));
333  mpz_init(u2);
334  int_number v = (int_number)omAlloc(sizeof(mpz_t));
335  mpz_init_set_ui(v, R->nr2mModul);
336  mpz_add_ui(v, v, 1); /* now: v = 2^m */
337  int_number v0 = (int_number)omAlloc(sizeof(mpz_t));
338  mpz_init(v0);
339  int_number v1 = (int_number)omAlloc(sizeof(mpz_t));
340  mpz_init(v1);
341  int_number v2 = (int_number)omAlloc(sizeof(mpz_t));
342  mpz_init_set_ui(v2, 1);
343  int_number q = (int_number)omAlloc(sizeof(mpz_t));
344  mpz_init(q);
345  int_number r = (int_number)omAlloc(sizeof(mpz_t));
346  mpz_init(r);
347
348  while (mpz_cmp_ui(v, 0) != 0) /* i.e., while v != 0 */
349  {
350    mpz_div(q, u, v);
351    mpz_mod(r, u, v);
352    mpz_set(u, v);
353    mpz_set(v, r);
354    mpz_set(u0, u2);
355    mpz_set(v0, v2);
356    mpz_mul(u2, u2, q); mpz_sub(u2, u1, u2); /* u2 = u1 - q * u2 */
357    mpz_mul(v2, v2, q); mpz_sub(v2, v1, v2); /* v2 = v1 - q * v2 */
358    mpz_set(u1, u0);
359    mpz_set(v1, v0);
360  }
361
362  while (mpz_cmp_ui(u1, 0) < 0) /* i.e., while u1 < 0 */
363  {
364    /* we add 2^m = (2^m - 1) + 1 to u1: */
365    mpz_add_ui(u1, u1, R->nr2mModul);
366    mpz_add_ui(u1, u1, 1);
367  }
368  s = mpz_get_ui(u1); /* now: 0 <= s <= 2^m - 1 */
369
370  mpz_clear(u);  omFree((ADDRESS)u);
371  mpz_clear(u0); omFree((ADDRESS)u0);
372  mpz_clear(u1); omFree((ADDRESS)u1);
373  mpz_clear(u2); omFree((ADDRESS)u2);
374  mpz_clear(v);  omFree((ADDRESS)v);
375  mpz_clear(v0); omFree((ADDRESS)v0);
376  mpz_clear(v1); omFree((ADDRESS)v1);
377  mpz_clear(v2); omFree((ADDRESS)v2);
378  mpz_clear(q); omFree((ADDRESS)q);
379  mpz_clear(r); omFree((ADDRESS)r);
380}
381
382NATNUMBER InvMod(NATNUMBER a, const coeffs r)
383{
384  assume((NATNUMBER)a % 2 != 0);
385  unsigned long s;
386  specialXGCD(s, a, r);
387  return s;
388}
389//#endif
390
391inline number nr2mInversM (number c, const coeffs r)
392{
393  assume((NATNUMBER)c % 2 != 0);
394  // Table !!!
395  NATNUMBER inv;
396  inv = InvMod((NATNUMBER)c,r);
397  return (number) inv;
398}
399
400number nr2mDiv (number a, number b, const coeffs r)
401{
402  if ((NATNUMBER)a==0)
403    return (number)0;
404  else if ((NATNUMBER)b%2==0)
405  {
406    if ((NATNUMBER)b != 0)
407    {
408      while ((NATNUMBER) b%2 == 0 && (NATNUMBER) a%2 == 0)
409      {
410        a = (number) ((NATNUMBER) a / 2);
411        b = (number) ((NATNUMBER) b / 2);
412      }
413    }
414    if ((NATNUMBER) b%2 == 0)
415    {
416      WerrorS("Division not possible, even by cancelling zero divisors.");
417      WerrorS("Result is integer division without remainder.");
418      return (number) ((NATNUMBER) a / (NATNUMBER) b);
419    }
420  }
421  return (number) nr2mMult(a, nr2mInversM(b,r),r);
422}
423
424number nr2mMod (number a, number b, const coeffs R)
425{
426  /*
427    We need to return the number r which is uniquely determined by the
428    following two properties:
429      (1) 0 <= r < |b| (with respect to '<' and '<=' performed in Z x Z)
430      (2) There exists some k in the integers Z such that a = k * b + r.
431    Consider g := gcd(2^m, |b|). Note that then |b|/g is a unit in Z/2^m.
432    Now, there are three cases:
433      (a) g = 1
434          Then |b| is a unit in Z/2^m, i.e. |b| (and also b) divides a.
435          Thus r = 0.
436      (b) g <> 1 and g divides a
437          Then a = (a/g) * (|b|/g)^(-1) * b (up to sign), i.e. again r = 0.
438      (c) g <> 1 and g does not divide a
439          Let's denote the division with remainder of a by g as follows:
440          a = s * g + t. Then t = a - s * g = a - s * (|b|/g)^(-1) * |b|
441          fulfills (1) and (2), i.e. r := t is the correct result. Hence
442          in this third case, r is the remainder of division of a by g in Z.
443    This algorithm is the same as for the case Z/n, except that we may
444    compute the gcd of |b| and 2^m "by hand": We just extract the highest
445    power of 2 (<= 2^m) that is contained in b.
446  */
447  assume((NATNUMBER)b != 0);
448  NATNUMBER g = 1;
449  NATNUMBER b_div = (NATNUMBER)b;
450  if (b_div < 0) b_div = -b_div; // b_div now represents |b|
451  NATNUMBER r = 0;
452  while ((g < R->nr2mModul ) && (b_div > 0) && (b_div % 2 == 0))
453  {
454    b_div = b_div >> 1;
455    g = g << 1;
456  } // g is now the gcd of 2^m and |b|
457
458  if (g != 1) r = (NATNUMBER)a % g;
459  return (number)r;
460}
461
462number nr2mIntDiv (number a, number b, const coeffs r)
463{
464  if ((NATNUMBER)a == 0)
465  {
466    if ((NATNUMBER)b == 0)
467      return (number)1;
468    if ((NATNUMBER)b == 1)
469      return (number)0;
470    NATNUMBER c = r->nr2mModul + 1;
471    if (c != 0) /* i.e., if no overflow */
472      return (number)(c / (NATNUMBER)b);
473    else
474    {
475      /* overflow: c = 2^32 resp. 2^64, depending on platform */
476      int_number cc = (int_number)omAlloc(sizeof(mpz_t));
477      mpz_init_set_ui(cc, r->nr2mModul); mpz_add_ui(cc, cc, 1);
478      mpz_div_ui(cc, cc, (unsigned long)(NATNUMBER)b);
479      unsigned long s = mpz_get_ui(cc);
480      mpz_clear(cc); omFree((ADDRESS)cc);
481      return (number)(NATNUMBER)s;
482    }
483  }
484  else
485  {
486    if ((NATNUMBER)b == 0)
487      return (number)0;
488    return (number)((NATNUMBER) a / (NATNUMBER) b);
489  }
490}
491
492number  nr2mInvers (number c, const coeffs r)
493{
494  if ((NATNUMBER)c%2==0)
495  {
496    WerrorS("division by zero divisor");
497    return (number)0;
498  }
499  return nr2mInversM(c,r);
500}
501
502number nr2mNeg (number c, const coeffs r)
503{
504  if ((NATNUMBER)c==0) return c;
505  return nr2mNegM(c,r);
506}
507
508number nr2mMapMachineInt(number from, const coeffs src, const coeffs dst)
509{
510  NATNUMBER i = ((NATNUMBER) from) % dst->nr2mModul ;
511  return (number) i;
512}
513
514number nr2mMapZp(number from, const coeffs src, const coeffs dst)
515{
516  NATNUMBER j = (NATNUMBER)1;
517  long ii = (long) from;
518  if (ii < 0) { j = dst->nr2mModul; ii = -ii; }
519  NATNUMBER i = (NATNUMBER)ii;
520  i = i & dst->nr2mModul;
521  /* now we have: from = j * i mod 2^m */
522  return (number)nr2mMult((number)i, (number)j, dst);
523}
524
525number nr2mMapQ(number from, const coeffs src, const coeffs dst)
526{
527  int_number erg = (int_number)  omAllocBin(gmp_nrz_bin);
528  mpz_init(erg);
529  int_number k = (int_number) omAlloc(sizeof(mpz_t));
530  mpz_init_set_ui(k, dst->nr2mModul);
531
532  nlGMP(from, (number)erg);
533  mpz_and(erg, erg, k);
534  number res = (number)mpz_get_ui(erg);
535
536  mpz_clear(erg); omFree((ADDRESS)erg);
537  mpz_clear(k);   omFree((ADDRESS)k);
538
539  return (number) res;
540}
541
542number nr2mMapGMP(number from, const coeffs src, const coeffs dst)
543{
544  int_number erg = (int_number)  omAllocBin(gmp_nrz_bin);
545  mpz_init(erg);
546  int_number k = (int_number) omAlloc(sizeof(mpz_t));
547  mpz_init_set_ui(k, dst->nr2mModul);
548
549  mpz_and(erg, (int_number)from, k);
550  number res = (number) mpz_get_ui(erg);
551
552  mpz_clear(erg); omFree((ADDRESS)erg);
553  mpz_clear(k);   omFree((ADDRESS)k);
554
555  return (number) res;
556}
557
558nMapFunc nr2mSetMap(const coeffs src, const coeffs dst)
559{
560  if (nField_is_Ring_2toM(src)
561     && (src->ringflagb == dst->ringflagb))
562  {
563    return ndCopyMap;
564  }
565  if (nField_is_Ring_2toM(src)
566     && (src->ringflagb < dst->ringflagb))
567  { /* i.e. map an integer mod 2^s into Z mod 2^t, where t < s */
568    return nr2mMapMachineInt;
569  }
570  if (nField_is_Ring_2toM(src)
571     && (src->ringflagb > dst->ringflagb))
572  { /* i.e. map an integer mod 2^s into Z mod 2^t, where t > s */
573    // to be done
574  }
575  if (nField_is_Ring_Z(src))
576  {
577    return nr2mMapGMP;
578  }
579  if (nField_is_Q(src))
580  {
581    return nr2mMapQ;
582  }
583  if (nField_is_Zp(src)
584     && (src->ch == 2)
585     && (dst->ringflagb == 1))
586  {
587    return nr2mMapZp;
588  }
589  if (nField_is_Ring_PtoM(src) || nField_is_Ring_ModN(src))
590  {
591    // Computing the n of Z/n
592    int_number modul = (int_number)  omAllocBin(gmp_nrz_bin);
593    mpz_init(modul);
594    mpz_set(modul, src->ringflaga);
595    mpz_pow_ui(modul, modul, src->ringflagb);
596    if (mpz_divisible_2exp_p(modul, dst->ringflagb))
597    {
598      mpz_clear(modul);
599      omFree((void *) modul);
600      return nr2mMapGMP;
601    }
602    mpz_clear(modul);
603    omFree((void *) modul);
604  }
605  return NULL;      // default
606}
607
608/*
609 * set the exponent (allocate and init tables) (TODO)
610 */
611
612void nr2mSetExp(int m, const coeffs r)
613{
614  if (m > 1)
615  {
616    nr2mExp = m;
617    /* we want nr2mModul to be the bit pattern '11..1' consisting
618       of m one's: */
619    r->nr2mModul = 1;
620    for (int i = 1; i < m; i++) r->nr2mModul = (r->nr2mModul * 2) + 1;
621  }
622  else
623  {
624    nr2mExp = 2;
625    r->nr2mModul = 3; /* i.e., '11' in binary representation */
626  }
627}
628
629void nr2mInitExp(int m, const coeffs r)
630{
631  nr2mSetExp(m, r);
632  if (m < 2) WarnS("nr2mInitExp failed: we go on with Z/2^2");
633}
634
635#ifdef LDEBUG
636BOOLEAN nr2mDBTest (number a, const char *f, const int l, const coeffs r)
637{
638  if ((NATNUMBER)a < 0) return FALSE;
639  if (((NATNUMBER)a & r->nr2mModul) != (NATNUMBER)a) return FALSE;
640  return TRUE;
641}
642#endif
643
644void nr2mWrite (number &a, const coeffs r)
645{
646  int i = nr2mInt(a, r);
647  StringAppend("%d", i);
648}
649
650static const char* nr2mEati(const char *s, int *i, const coeffs r)
651{
652
653  if (((*s) >= '0') && ((*s) <= '9'))
654  {
655    (*i) = 0;
656    do
657    {
658      (*i) *= 10;
659      (*i) += *s++ - '0';
660      if ((*i) >= (MAX_INT_VAL / 10)) (*i) = (*i) & r->nr2mModul;
661    }
662    while (((*s) >= '0') && ((*s) <= '9'));
663    (*i) = (*i) & r->nr2mModul;
664  }
665  else (*i) = 1;
666  return s;
667}
668
669const char * nr2mRead (const char *s, number *a, const coeffs r)
670{
671  int z;
672  int n=1;
673
674  s = nr2mEati(s, &z,r);
675  if ((*s) == '/')
676  {
677    s++;
678    s = nr2mEati(s, &n,r);
679  }
680  if (n == 1)
681    *a = (number)z;
682  else
683      *a = nr2mDiv((number)z,(number)n,r);
684  return s;
685}
686#endif
687/* #ifdef HAVE_RINGS */
Note: See TracBrowser for help on using the repository browser.