Changeset 79020f in git


Ignore:
Timestamp:
Jan 6, 2012, 9:28:16 PM (12 years ago)
Author:
Oleksandr Motsak <motsak@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
40e88db63c509564c2b00b7142b39ef6b846f72e
Parents:
1cbb1f452324cf72b33ef5a1cb6256d97bcbcbd0
git-author:
Oleksandr Motsak <motsak@mathematik.uni-kl.de>2012-01-06 21:28:16+01:00
git-committer:
Oleksandr Motsak <motsak@mathematik.uni-kl.de>2012-01-06 21:59:37+01:00
Message:
fixed arithmetic overflow by rewriting nlModP

CHG: nlModP (longrat) was rewritten in order to simplify and generalize it
CHG/FIX: nlModP _works_ again as in the old Singular (uses n_Div)
Location:
libpolys
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • libpolys/coeffs/longrat.cc

    r1cbb1f4 r79020f  
    12561256}
    12571257
    1258 int nlModP(number n, int p, const coeffs r)
    1259 {
    1260   if (SR_HDL(n) & SR_INT)
    1261   {
    1262     long i=SR_TO_INT(n);
    1263     if (i<0L) return (((long)p)-((-i)%((long)p)));
    1264     return i%((long)p);
    1265   }
    1266   int iz=(int)mpz_fdiv_ui(n->z,(unsigned long)p);
    1267   if (n->s!=3)
    1268   {
    1269     int in=mpz_fdiv_ui(n->n,(unsigned long)p);
    1270     long  s;
    1271 
    1272     long  u, v, u0, v0, u1, v1, u2, v2, q, r;
    1273 
    1274      u1=1; v1=0;
    1275      u2=0; v2=1;
    1276      u = in; v = p;
    1277 
    1278      while (v != 0)
    1279      {
    1280         q = u / v;
    1281         r = u % v;
    1282         u = v;
    1283         v = r;
    1284         u0 = u2;
    1285         v0 = v2;
    1286         u2 =  u1 - q*u2;
    1287         v2 = v1- q*v2;
    1288         u1 = u0;
    1289         v1 = v0;
    1290      }
    1291 
    1292      s = u1;
    1293      if (s < 0) s+=p;
    1294      u=(s*((long)iz)) % ((long)p); // BUG: integer overflow!!!
    1295      return (int)u;
    1296   }
    1297   return iz;
     1258// Map q \in QQ \to Zp
     1259// src = Q, dst = Zp (or an extension of Zp?)
     1260number nlModP(number q, const coeffs Q, const coeffs Zp)
     1261{
     1262  assume( getCoeffType(Q) == ID );
     1263
     1264  const int p = n_GetChar(Zp);
     1265  assume( p > 0 );
     1266
     1267  const long P = p;
     1268  assume( P > 0 );
     1269
     1270  // embedded long within q => only long numerator has to be converted
     1271  // to int (modulo char.)
     1272  if (SR_HDL(q) & SR_INT)
     1273  {
     1274    long i = SR_TO_INT(q);
     1275    if (i<0L)
     1276      return n_Init( static_cast<int>( P - ((-i)%P) ), Zp);
     1277    else
     1278      return n_Init( static_cast<int>( i % P ), Zp );
     1279  }
     1280
     1281  const unsigned long PP = p;
     1282
     1283  // numerator modulo char. should fit into int
     1284  number z = n_Init( static_cast<int>(mpz_fdiv_ui(q->z, PP)), Zp ); 
     1285
     1286  // denominator != 1?
     1287  if (q->s!=3)
     1288  {
     1289    // denominator modulo char. should fit into int
     1290    number n = n_Init( static_cast<int>(mpz_fdiv_ui(q->n, PP)), Zp );
     1291
     1292    number res = n_Div( z, n, Zp );
     1293
     1294    n_Delete(&z, Zp);
     1295    n_Delete(&n, Zp);
     1296
     1297    return res;
     1298  }
     1299
     1300  return z;
    12981301}
    12991302
  • libpolys/coeffs/longrat.h

    r1cbb1f4 r79020f  
    8787const char *   nlRead (const char *s, number *a, const coeffs r);
    8888void     nlWrite(number &a, const coeffs r);
    89 int      nlModP(number n, int p, const coeffs r);
     89
     90/// Map q \in QQ \to Zp
     91number nlModP(number q, const coeffs Q, const coeffs Zp);
     92
    9093int      nlSize(number n, const coeffs r);
    9194number   nlGetDenom(number &n, const coeffs r);
  • libpolys/coeffs/modulop.cc

    r1cbb1f4 r79020f  
    460460  //r->cfName = ndName;
    461461  r->cfInpMult=ndInpMult;
    462   r->cfInit_bigint= npMap0;
     462  r->cfInit_bigint= nlModP; // npMap0;
    463463#ifdef NV_OPS
    464464  if (c>NV_MAX_PRIME)
     
    545545#endif
    546546
    547 number npMap0(number from, const coeffs src, const coeffs dst_r)
    548 {
    549   int nlModP(number n, int p, const coeffs r);
    550 
    551   return npInit(nlModP(from, dst_r->npPrimeM, src),dst_r);
    552 }
    553 
    554547number npMapP(number from, const coeffs src, const coeffs dst_r)
    555548{
     
    686679  if (nCoeff_is_Q(src))
    687680  {
    688     return npMap0;
     681    return nlModP; // npMap0;
    689682  }
    690683  if ( nCoeff_is_Zp(src) )
  • libpolys/coeffs/modulop.h

    r1cbb1f4 r79020f  
    5555nMapFunc npSetMap(const coeffs src, const coeffs dst);
    5656number  npMapP(number from, const coeffs src, const coeffs r);
    57 number  npMap0(number from, const coeffs src, const coeffs r);
    5857/*-------specials for spolys, do NOT use otherwise--------------------------*/
    5958/* for npMultM, npSubM, npNegM, npEqualM : */
  • libpolys/polys/ext_fields/algext.cc

    r1cbb1f4 r79020f  
    650650  if (n_IsZero(a, src)) return NULL;
    651651  int p = rChar(dst->extRing);
    652   int n = nlModP(a, p, src);
    653   number q = n_Init(n, dst->extRing->cf);
    654   poly result = p_One(dst->extRing);
    655   p_SetCoeff(result, q, dst->extRing);
     652
     653  number q = nlModP(a, src, dst->extRing->cf);
     654
     655  poly result = p_NSet(q, dst->extRing);
     656 
    656657  return (number)result;
    657658}
  • libpolys/polys/ext_fields/transext.cc

    r1cbb1f4 r79020f  
    11191119  if (n_IsZero(a, src)) return NULL;
    11201120  int p = rChar(dst->extRing);
    1121   int n = nlModP(a, p, src);
    1122   number q = n_Init(n, dst->extRing->cf);
    1123   poly g;
     1121  number q = nlModP(a, src, dst->extRing->cf);
     1122
    11241123  if (n_IsZero(q, dst->extRing->cf))
    11251124  {
     
    11271126    return NULL;
    11281127  }
    1129   g = p_One(dst->extRing);
    1130   p_SetCoeff(g, q, dst->extRing);
     1128 
     1129  poly g = p_NSet(q, dst->extRing);
     1130
    11311131  fraction f = (fraction)omAlloc0Bin(fractionObjectBin);
    11321132  NUM(f) = g; // DEN(f) = NULL; COM(f) = 0;
Note: See TracChangeset for help on using the changeset viewer.