source: git/coeffs/rmodulo2m.cc @ 1112b76

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