Changeset 31e857 in git


Ignore:
Timestamp:
Feb 1, 2008, 11:31:44 AM (16 years ago)
Author:
Oliver Wienand <wienand@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', '648d28f488f6ff08f5607ff229b9ad9e4a5b93c2')
Children:
60d118332f6eec4e3ce8410bb77c08ca9711d1a3
Parents:
8e1c4e6b390f61262e5bb20f254ac6e6c5c1d8b1
Message:
rmodulon.cc:
gmp_bin


git-svn-id: file:///usr/local/Singular/svn/trunk@10546 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/rmodulon.cc

    r8e1c4e r31e857  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: rmodulon.cc,v 1.13 2008-01-31 16:13:55 wienand Exp $ */
     4/* $Id: rmodulon.cc,v 1.14 2008-02-01 10:31:44 wienand Exp $ */
    55/*
    66* ABSTRACT: numbers modulo n
     
    2323
    2424typedef MP_INT *int_number;
     25omBin gmp_bin = omGetSpecBin(sizeof(MP_INT));
    2526
    2627int_number nrnModul = NULL;
     
    2930unsigned long long nrnBase = 0;
    3031
    31 
    3232/*
    3333 * create a number from int
     
    3535number nrnInit (int i)
    3636{
    37   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     37  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    3838  mpz_init_set_si(erg, i);
    3939  mpz_mod(erg, erg, nrnModul);
     
    4444{
    4545  mpz_clear((int_number) *a);
    46   omFree((ADDRESS) *a);
     46  omFreeBin((ADDRESS) *a, gmp_bin);
    4747}
    4848
     
    6060number nrnMult (number a, number b)
    6161{
    62   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     62  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    6363  mpz_init(erg);
    6464  mpz_mul(erg, (int_number) a, (int_number) b);
     
    6969void nrnPower (number a, int i, number * result)
    7070{
    71   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     71  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    7272  mpz_init(erg);
    7373  mpz_powm_ui(erg, (int_number) a, i, nrnModul);
     
    7777number nrnAdd (number a, number b)
    7878{
    79   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     79  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    8080  mpz_init(erg);
    8181  mpz_add(erg, (int_number) a, (int_number) b);
     
    8686number nrnSub (number a, number b)
    8787{
    88   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     88  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    8989  mpz_init(erg);
    9090  mpz_sub(erg, (int_number) a, (int_number) b);
     
    9595number nrnNeg (number c)
    9696{
    97   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     97  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    9898  mpz_init(erg);
    9999  mpz_sub(erg, nrnModul, (int_number) c);
     
    103103number  nrnInvers (number c)
    104104{
    105   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     105  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    106106  mpz_init(erg);
    107107  mpz_invert(erg, (int_number) c, nrnModul);
     
    128128number nrnGcd (number a,number b,ring r)
    129129{
    130   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
    131   mpz_init(erg);
    132   if (a == NULL) a = (number) nrnModul;
    133   if (b == NULL) b = (number) nrnModul;
    134   mpz_gcd(erg, (int_number) a, (int_number) b);
    135   mpz_gcd(erg, erg, nrnModul);
     130  if ((a == NULL) && (b == NULL)) return nrnInit(0);
     131  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
     132  mpz_init_set(erg, nrnModul);
     133  if (a != NULL) mpz_gcd(erg, erg, (int_number) a);
     134  if (b != NULL) mpz_gcd(erg, erg, (int_number) b);
    136135  return (number) erg;
    137136}
     
    140139number nrnGcd3 (number a,number b, number c,ring r)
    141140{
    142   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     141  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    143142  mpz_init(erg);
    144143  if (a == NULL) a = (number) nrnModul;
     
    158157number  nrnExtGcd (number a, number b, number *s, number *t)
    159158{
    160   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
    161   int_number bs = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
    162   int_number bt = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     159  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
     160  int_number bs = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
     161  int_number bt = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    163162  mpz_init(erg);
    164163  mpz_init(bs);
     
    212211number  nrnGetUnit (number k)
    213212{
    214   number unit = nrnIntDiv(k, nrnGcd(k, 0, currRing));
    215   number gcd = nrnGcd(unit, 0, currRing);
    216   if (!nrnIsOne(gcd))
    217   {
    218     number tmp = nrnMult(unit, unit);
    219     number gcd_new = nrnGcd(tmp, 0, currRing);
    220     while (!nrnEqual(gcd_new, gcd))
     213  int_number unit = (int_number) nrnGcd(k, 0, currRing);
     214  mpz_tdiv_q(unit, (int_number) k, unit);
     215  int_number gcd = (int_number) nrnGcd((number) unit, 0, currRing);
     216  if (!nrnIsOne((number) gcd))
     217  {
     218    int_number ctmp;
     219    // tmp := unit^2
     220    int_number tmp = (int_number) nrnMult((number) unit,(number) unit);
     221    // gcd_new := gcd(tmp, 0)
     222    int_number gcd_new = (int_number) nrnGcd((number) tmp, 0, currRing);
     223    while (!nrnEqual((number) gcd_new,(number) gcd))
    221224    {
    222       nrnDelete(&gcd, NULL);
     225      // gcd := gcd_new
     226      ctmp = gcd;
    223227      gcd = gcd_new;
    224       tmp = nrnMult(tmp, unit);
    225       gcd_new = nrnGcd(tmp, 0, currRing);
     228      gcd_new = ctmp;
     229      // tmp := tmp * unit
     230      mpz_mul(tmp, tmp, unit);
     231      mpz_mod(tmp, tmp, nrnModul);
     232      // gcd_new := gcd(tmp, 0)
     233      mpz_gcd(gcd_new, tmp, nrnModul);
    226234    }
    227     unit = nrnAdd(unit, nrnIntDiv(0, gcd_new));
    228     nrnDelete(&gcd_new, NULL);
    229   }
    230   nrnDelete(&gcd, NULL);
    231   return unit;
     235    // unit := unit + nrnModul / gcd_new
     236    mpz_tdiv_q(tmp, nrnModul, gcd_new);
     237    mpz_add(unit, unit, tmp);
     238    mpz_mod(unit, unit, nrnModul);
     239    nrnDelete((number*) &gcd_new, NULL);
     240    nrnDelete((number*) &tmp, NULL);
     241  }
     242  nrnDelete((number*) &gcd, NULL);
     243  return (number) unit;
    232244}
    233245
    234246BOOLEAN nrnDivBy (number a,number b)
    235247{
    236   number bs = nrnIntDiv(b, nrnGcd(a, b, NULL));
     248  number bs = nrnGcd(a, b, NULL);
     249  mpz_tdiv_q((int_number) bs, (int_number) b, (int_number) bs);
    237250  bool res = nrnIsUnit(bs);
    238251  nrnDelete(&bs, NULL);
     
    243256{
    244257  if (nrnEqual(a, b)) return 0;
    245   if (nrnDivBy(a, b)) return -1;
    246   if (nrnDivBy(b, a)) return 1;
     258  if (mpz_divisible_p((int_number) a, (int_number) b)) return -1;
     259  if (mpz_divisible_p((int_number) b, (int_number) a)) return 1;
    247260  return 2;
    248261}
     
    251264{
    252265  if (a == NULL) a = (number) nrnModul;
    253   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     266  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    254267  mpz_init(erg);
    255268  if (mpz_divisible_p((int_number) a, (int_number) b))
     
    261274  {
    262275    int_number gcd = (int_number) nrnGcd(a, b, NULL);
    263     int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
    264     mpz_init(erg);
    265276    mpz_divexact(erg, (int_number) b, gcd);
    266277    if (!nrnIsUnit((number) erg))
     
    269280      WarnS("Result is zero.");
    270281      mpz_set_ui(erg, 0);
    271       mpz_clear(gcd);
    272       omFree(gcd);
     282      nrnDelete((number*) &gcd, NULL);
    273283      return (number) erg;
    274284    }
    275     int_number tmp = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
    276     mpz_init(tmp);
    277     tmp = (int_number) nrnInvers((number) erg);
     285    // a / gcd(a,b) * [b / gcd (a,b)]^(-1)
     286    int_number tmp = (int_number) nrnInvers((number) erg);
    278287    mpz_divexact(erg, (int_number) a, gcd);
    279288    mpz_mul(erg, erg, tmp);
    280     mpz_clear(gcd);
    281     omFree(gcd);
    282     mpz_clear(tmp);
    283     omFree(tmp);
     289    nrnDelete((number*) &gcd, NULL);
     290    nrnDelete((number*) &tmp, NULL);
    284291    mpz_mod(erg, erg, nrnModul);
    285292    return (number) erg;
     
    289296number nrnIntDiv (number a,number b)
    290297{
    291   int_number erg = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     298  int_number erg = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    292299  mpz_init(erg);
    293300  if (a == NULL) a = (number) nrnModul;
     
    320327  if (nrnModul != NULL)
    321328  {
    322     mpz_clear(nrnModul);
    323     omFree(nrnModul);
    324     mpz_clear(nrnMinusOne);
    325     omFree(nrnMinusOne);
    326   }
    327   nrnModul = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     329    nrnDelete((number*) &nrnModul, NULL);
     330    nrnDelete((number*) &nrnMinusOne, NULL);
     331  }
     332  nrnModul = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    328333  mpz_init(nrnModul);
    329334  mpz_set_ull(nrnModul, nrnBase);
    330335  mpz_pow_ui(nrnModul, nrnModul, nrnExponent);
    331336
    332   nrnMinusOne = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     337  nrnMinusOne = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    333338  mpz_init(nrnMinusOne);
    334339  mpz_sub_ui(nrnMinusOne, nrnModul, 1);
     
    403408char * nrnRead (char *s, number *a)
    404409{
    405   int_number z = (int_number) omAlloc(sizeof(MP_INT)); // evtl. spaeter mit bin
     410  int_number z = (int_number) omAllocBin(gmp_bin); // evtl. spaeter mit bin
    406411  {
    407412    s = nlCPEatLongC(s, z);
Note: See TracChangeset for help on using the changeset viewer.