source: git/coeffs/rmodulo2m.cc @ 3bc01c7

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