Changeset 806c18 in git for factory/cf_gcd_smallp.cc


Ignore:
Timestamp:
Nov 15, 2010, 4:34:57 PM (13 years ago)
Author:
Hans Schoenemann <hannes@…>
Branches:
(u'spielwiese', '17f1d200f27c5bd38f5dfc6e8a0879242279d1d8')
Children:
7c3bca08c96331a56864c1d35b8c2e8ff2e0be89
Parents:
c840d97af622b4e4da8761738b540e21144f716b
Message:
format

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

Legend:

Unmodified
Added
Removed
  • factory/cf_gcd_smallp.cc

    rc840d9 r806c18  
    66 * @date 22.10.2009
    77 *
    8  * This file implements the GCD of two polynomials over \f$ F_{p} \f$ , 
    9  * \f$ F_{p}(\alpha ) \f$ or GF based on Alg. 7.2. as described in "Algorithms 
     8 * This file implements the GCD of two polynomials over \f$ F_{p} \f$ ,
     9 * \f$ F_{p}(\alpha ) \f$ or GF based on Alg. 7.2. as described in "Algorithms
    1010 * for Computer Algebra" by Geddes, Czapor, Labahnn
    1111 *
     
    1313 *   (c) by The SINGULAR Team, see LICENSE file
    1414 *
    15  * @internal 
     15 * @internal
    1616 * @version \$Id$
    1717 *
     
    4646TIMING_DEFINE_PRINT(newton_interpolation);
    4747
    48 /// compressing two polynomials F and G, M is used for compressing, 
     48/// compressing two polynomials F and G, M is used for compressing,
    4949/// N to reverse the compression
    5050static inline
    5151int myCompress (const CanonicalForm& F, const CanonicalForm& G, CFMap & M,
    52                 CFMap & N, bool& topLevel) 
    53 { 
     52                CFMap & N, bool& topLevel)
     53{
    5454  int n= tmax (F.level(), G.level());
    5555  int * degsf= new int [n + 1];
     
    5858  for (int i = 0; i <= n; i++)
    5959    degsf[i]= degsg[i]= 0;
    60    
     60
    6161  degsf= degrees (F, degsf);
    6262  degsg= degrees (G, degsg);
    63  
     63
    6464  int both_non_zero= 0;
    6565  int f_zero= 0;
     
    6767  int both_zero= 0;
    6868
    69   if (topLevel) 
    70   {
    71     for (int i= 1; i <= n; i++) 
    72     {
    73       if (degsf[i] != 0 && degsg[i] != 0) 
     69  if (topLevel)
     70  {
     71    for (int i= 1; i <= n; i++)
     72    {
     73      if (degsf[i] != 0 && degsg[i] != 0)
    7474      {
    7575        both_non_zero++;
    7676        continue;
    7777      }
    78       if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level()) 
     78      if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
    7979      {
    8080        f_zero++;
    8181        continue;
    8282      }
    83       if (degsg[i] == 0 && degsf[i] && i <= F.level()) 
     83      if (degsg[i] == 0 && degsf[i] && i <= F.level())
    8484      {
    8585        g_zero++;
     
    8888    }
    8989
    90     if (both_non_zero == 0) 
     90    if (both_non_zero == 0)
    9191    {
    9292      delete [] degsf;
     
    9898    int k= 1;
    9999    int l= 1;
    100     for (int i= 1; i <= n; i++) 
    101     { 
    102       if (degsf[i] != 0 && degsg[i] == 0 && i <= F.level()) 
    103       {
    104         if (k + both_non_zero != i) 
     100    for (int i= 1; i <= n; i++)
     101    {
     102      if (degsf[i] != 0 && degsg[i] == 0 && i <= F.level())
     103      {
     104        if (k + both_non_zero != i)
    105105        {
    106106          M.newpair (Variable (i), Variable (k + both_non_zero));
     
    109109        k++;
    110110      }
    111       if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level()) 
    112       {
    113         if (l + g_zero + both_non_zero != i) 
     111      if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
     112      {
     113        if (l + g_zero + both_non_zero != i)
    114114        {
    115115          M.newpair (Variable (i), Variable (l + g_zero + both_non_zero));
     
    119119      }
    120120    }
    121  
     121
    122122    // sort Variables x_{i} in increasing order of
    123     // min(deg_{x_{i}}(f),deg_{x_{i}}(g)) 
     123    // min(deg_{x_{i}}(f),deg_{x_{i}}(g))
    124124    int m= tmin (F.level(), G.level());
    125125    int max_min_deg;
     
    127127    l= 0;
    128128    int i= 1;
    129     while (k > 0) 
     129    while (k > 0)
    130130    {
    131131      max_min_deg= tmin (degsf[i], degsg[i]);
    132       while (max_min_deg == 0) 
     132      while (max_min_deg == 0)
    133133      {
    134134        i++;
    135135        max_min_deg= tmin (degsf[i], degsg[i]);
    136136      }
    137       for (int j= i + 1; j <=  m; j++) 
    138       {
    139         if (tmin (degsf[j],degsg[j]) >= max_min_deg) 
     137      for (int j= i + 1; j <=  m; j++)
     138      {
     139        if (tmin (degsf[j],degsg[j]) >= max_min_deg)
    140140        {
    141141          max_min_deg= tmin (degsf[j], degsg[j]);
    142           l= j; 
     142          l= j;
    143143        }
    144144      }
    145       if (l != 0) 
    146       {
    147         if (l != k) 
     145      if (l != 0)
     146      {
     147        if (l != k)
    148148        {
    149149          M.newpair (Variable (l), Variable(k));
     
    153153          l= 0;
    154154        }
    155         else 
     155        else
    156156        {
    157157          degsf[l]= 0;
     
    159159          l= 0;
    160160        }
    161       } 
    162       else if (l == 0) 
    163       {
    164         if (i != k) 
     161      }
     162      else if (l == 0)
     163      {
     164        if (i != k)
    165165        {
    166166          M.newpair (Variable (i), Variable (k));
     
    169169          degsg[i]= 0;
    170170        }
    171         else 
     171        else
    172172        {
    173173          degsf[i]= 0;
     
    175175        }
    176176        i++;
    177       } 
     177      }
    178178      k--;
    179179    }
    180180  }
    181   else 
     181  else
    182182  {
    183183    //arrange Variables such that no gaps occur
    184     for (int i= 1; i <= n; i++) 
    185     {
    186       if (degsf[i] == 0 && degsg[i] == 0) 
     184    for (int i= 1; i <= n; i++)
     185    {
     186      if (degsf[i] == 0 && degsg[i] == 0)
    187187      {
    188188        both_zero++;
    189189        continue;
    190190      }
    191       else 
    192       {
    193         if (both_zero != 0) 
     191      else
     192      {
     193        if (both_zero != 0)
    194194        {
    195195          M.newpair (Variable (i), Variable (i - both_zero));
     
    207207
    208208int
    209 substituteCheck (const CanonicalForm& F, const CanonicalForm& G) 
     209substituteCheck (const CanonicalForm& F, const CanonicalForm& G)
    210210{
    211211  if (F.inCoeffDomain() || G.inCoeffDomain())
     
    214214  if (degree (F, x) <= 1 || degree (G, x) <= 1)
    215215    return 0;
    216   CanonicalForm f= swapvar (F, F.mvar(), x); //TODO swapping seems to be pretty expensive 
    217   CanonicalForm g= swapvar (G, G.mvar(), x); 
     216  CanonicalForm f= swapvar (F, F.mvar(), x); //TODO swapping seems to be pretty expensive
     217  CanonicalForm g= swapvar (G, G.mvar(), x);
    218218  int sizef= 0;
    219   int sizeg= 0; 
     219  int sizeg= 0;
    220220  for (CFIterator i= f; i.hasTerms(); i++, sizef++)
    221221  {
     
    240240    expg [j]= i.exp();
    241241  }
    242  
     242
    243243  int indf= sizef - 1;
    244244  int indg= sizeg - 1;
     
    247247  if (expg[indg] == 0)
    248248    indg--;
    249    
     249
    250250  if (expg[indg] != expf [indf] || (expg[indg] == 1 && expf[indf] == 1))
    251251  {
     
    265265    }
    266266  }
    267  
     267
    268268  for (int i= indg - 1; i >= 0; i--)
    269269  {
     
    282282
    283283// substiution
    284 void 
    285 subst (const CanonicalForm& F, const CanonicalForm& G, CanonicalForm& A, 
     284void
     285subst (const CanonicalForm& F, const CanonicalForm& G, CanonicalForm& A,
    286286       CanonicalForm& B, const int d
    287287      )
     
    293293    return;
    294294  }
    295  
     295
    296296  CanonicalForm C= 0;
    297   CanonicalForm D= 0; 
     297  CanonicalForm D= 0;
    298298  Variable x= Variable (1);
    299299  CanonicalForm f= swapvar (F, x, F.mvar());
     
    307307}
    308308
    309 CanonicalForm 
     309CanonicalForm
    310310reverseSubst (const CanonicalForm& F, const int d)
    311311{
    312312  if (d == 1)
    313313    return F;
    314  
     314
    315315  Variable x= Variable (1);
    316316  if (degree (F, x) <= 0)
     
    320320  for (CFIterator i= f; i.hasTerms(); i++)
    321321    result += i.coeff()*power (f.mvar(), d*i.exp());
    322   return swapvar (result, x, F.mvar());   
    323 }
    324 
    325 static inline CanonicalForm 
     322  return swapvar (result, x, F.mvar());
     323}
     324
     325static inline CanonicalForm
    326326uni_content (const CanonicalForm & F);
    327327
     
    335335  if (F.level() != x.level() && F.isUnivariate())
    336336    return F.genOne();
    337    
     337
    338338  if (x.level() != 1)
    339339  {
     
    344344  else
    345345    return uni_content (F);
    346 } 
    347 
    348 /// compute the content of F, where F is considered as an element of 
    349 /// \f$ R[x_{1}][x_{2},\ldots ,x_{n}] \f$ 
    350 static inline CanonicalForm 
    351 uni_content (const CanonicalForm & F) 
    352 { 
     346}
     347
     348/// compute the content of F, where F is considered as an element of
     349/// \f$ R[x_{1}][x_{2},\ldots ,x_{n}] \f$
     350static inline CanonicalForm
     351uni_content (const CanonicalForm & F)
     352{
    353353  if (F.inBaseDomain())
    354354    return F.genOne();
     
    360360
    361361  int l= F.level();
    362   if (l == 2) 
     362  if (l == 2)
    363363    return content(F);
    364   else 
     364  else
    365365  {
    366366    CanonicalForm pol, c = 0;
    367367    CFIterator i = F;
    368     for (; i.hasTerms(); i++) 
    369     {
    370       pol= i.coeff(); 
     368    for (; i.hasTerms(); i++)
     369    {
     370      pol= i.coeff();
    371371      pol= uni_content (pol);
    372372      c= gcd (c, pol);
     
    378378}
    379379
    380 CanonicalForm 
    381 extractContents (const CanonicalForm& F, const CanonicalForm& G, 
    382                  CanonicalForm& contentF, CanonicalForm& contentG, 
     380CanonicalForm
     381extractContents (const CanonicalForm& F, const CanonicalForm& G,
     382                 CanonicalForm& contentF, CanonicalForm& contentG,
    383383                 CanonicalForm& ppF, CanonicalForm& ppG, const int d)
    384384{
     
    407407/// is dp.
    408408static inline
    409 CanonicalForm uni_lcoeff (const CanonicalForm& F) 
     409CanonicalForm uni_lcoeff (const CanonicalForm& F)
    410410{
    411411  if (F.level() <= 1)
    412     return F; 
     412    return F;
    413413  else
    414414  {
     
    418418    {
    419419      if (i.exp() + totaldegree (i.coeff(), x, i.coeff().mvar()) == deg)
    420         return uni_lcoeff (i.coeff()); 
     420        return uni_lcoeff (i.coeff());
    421421    }
    422422  }
     
    441441}
    442442
    443 /// compute a random element a of \f$ F_{p}(\alpha ) \f$ , 
     443/// compute a random element a of \f$ F_{p}(\alpha ) \f$ ,
    444444/// s.t. F(a) \f$ \neq 0 \f$ , F is a univariate polynomial, returns
    445 /// fail if there are no field elements left which have not been used before 
    446 static inline CanonicalForm 
     445/// fail if there are no field elements left which have not been used before
     446static inline CanonicalForm
    447447randomElement (const CanonicalForm & F, const Variable & alpha, CFList & list,
    448                bool & fail) 
     448               bool & fail)
    449449{
    450450  fail= false;
     
    457457  int d= degree (mipo);
    458458  int bound= ipower (p, d);
    459   do 
     459  do
    460460  {
    461461    if (list.length() == bound)
     
    464464      break;
    465465    }
    466     if (list.length() < p) 
     466    if (list.length() < p)
    467467    {
    468468      random= genFF.generate();
     
    470470        random= genFF.generate();
    471471    }
    472     else 
     472    else
    473473    {
    474474      random= genAlgExt.generate();
     
    476476        random= genAlgExt.generate();
    477477    }
    478     if (F (random, x) == 0) 
     478    if (F (random, x) == 0)
    479479    {
    480480      list.append (random);
     
    485485}
    486486
    487 /// chooses a suitable extension of \f$ F_{p}(\alpha ) \f$ 
     487/// chooses a suitable extension of \f$ F_{p}(\alpha ) \f$
    488488/// we do not extend \f$ F_{p}(\alpha ) \f$ itself but extend \f$ F_{p} \f$ ,
    489 /// s.t. \f$ F_{p}(\alpha ) \subset F_{p}(\beta ) \f$ 
     489/// s.t. \f$ F_{p}(\alpha ) \subset F_{p}(\beta ) \f$
    490490static inline
    491 void choose_extension (const int& d, const int& num_vars, Variable& beta) 
     491void choose_extension (const int& d, const int& num_vars, Variable& beta)
    492492{
    493493  int p= getCharacteristic();
     
    496496  ZZ_pX NTLirredpoly;
    497497  //TODO: replace d by max_{i} (deg_x{i}(f))
    498   int i= (int) (log ((double) ipower (d + 1, num_vars))/log ((double) p)); 
     498  int i= (int) (log ((double) ipower (d + 1, num_vars))/log ((double) p));
    499499  int m= degree (getMipo (beta));
    500500  if (i <= 1)
    501501    i= 2;
    502   BuildIrred (NTLirredpoly, i*m); 
    503   CanonicalForm mipo= convertNTLZZpX2CF (NTLirredpoly, Variable(1)); 
    504   beta= rootOf (mipo); 
    505 }
    506 
    507 /// GCD of F and G over \f$ F_{p}(\alpha ) \f$ , 
     502  BuildIrred (NTLirredpoly, i*m);
     503  CanonicalForm mipo= convertNTLZZpX2CF (NTLirredpoly, Variable(1));
     504  beta= rootOf (mipo);
     505}
     506
     507/// GCD of F and G over \f$ F_{p}(\alpha ) \f$ ,
    508508/// l and topLevel are only used internally, output is monic
    509509/// based on Alg. 7.2. as described in "Algorithms for
    510510/// Computer Algebra" by Geddes, Czapor, Labahn
    511 CanonicalForm 
    512 GCD_Fp_extension (const CanonicalForm& F, const CanonicalForm& G, 
    513                   Variable & alpha, CFList& l, bool& topLevel) 
    514 { 
     511CanonicalForm
     512GCD_Fp_extension (const CanonicalForm& F, const CanonicalForm& G,
     513                  Variable & alpha, CFList& l, bool& topLevel)
     514{
    515515  CanonicalForm A= F;
    516516  CanonicalForm B= G;
     
    522522  if (G.isUnivariate() && fdivides(G, F)) return G/Lc(G);
    523523  if (F == G) return F/Lc(F);
    524  
     524
    525525  CFMap M,N;
    526526  int best_level= myCompress (A, B, M, N, topLevel);
     
    533533  Variable x= Variable(1);
    534534
    535   //univariate case 
    536   if (A.isUnivariate() && B.isUnivariate()) 
    537     return N (gcd(A,B)); 
    538  
     535  //univariate case
     536  if (A.isUnivariate() && B.isUnivariate())
     537    return N (gcd(A,B));
     538
    539539  int substitute= substituteCheck (A, B);
    540  
     540
    541541  if (substitute > 1)
    542542    subst (A, B, A, B, substitute);
     
    550550    if (best_level <= 2)
    551551      gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, best_level);
    552     else 
    553       gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, 2); 
     552    else
     553      gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, 2);
    554554  }
    555555  else
    556556  {
    557557    cA = uni_content (A);
    558     cB = uni_content (B); 
     558    cB = uni_content (B);
    559559    gcdcAcB= gcd (cA, cB);
    560560    ppA= A/cA;
     
    563563
    564564  CanonicalForm lcA, lcB;  // leading coefficients of A and B
    565   CanonicalForm gcdlcAlcB; 
     565  CanonicalForm gcdlcAlcB;
    566566
    567567  lcA= uni_lcoeff (ppA);
    568568  lcB= uni_lcoeff (ppB);
    569  
    570   if (fdivides (lcA, lcB)) 
    571   { 
     569
     570  if (fdivides (lcA, lcB))
     571  {
    572572    if (fdivides (A, B))
    573       return F/Lc(F);   
     573      return F/Lc(F);
    574574  }
    575575  if (fdivides (lcB, lcA))
    576   { 
    577     if (fdivides (B, A)) 
     576  {
     577    if (fdivides (B, A))
    578578      return G/Lc(G);
    579579  }
     
    587587    if (substitute > 1)
    588588      return N (reverseSubst (gcdcAcB, substitute));
    589     else 
     589    else
    590590      return N(gcdcAcB);
    591591  }
    592592  int d0= totaldegree (ppB, Variable(2), Variable (ppB.level()));
    593   if (d0 < d) 
    594     d= d0; 
     593  if (d0 < d)
     594    d= d0;
    595595  if (d == 0)
    596596  {
    597597    if (substitute > 1)
    598598      return N (reverseSubst (gcdcAcB, substitute));
    599     else 
     599    else
    600600      return N(gcdcAcB);
    601601  }
     
    614614  CanonicalForm prim_elem, im_prim_elem;
    615615  CFList source, dest;
    616   do 
     616  do
    617617  {
    618618    random_element= randomElement (m, V_buf, l, fail);
    619     if (fail) 
     619    if (fail)
    620620    {
    621621      source= CFList();
     
    638638      DEBOUTLN (cerr, "getMipo (V_buf2)= " << getMipo (V_buf2));
    639639      inextension= true;
    640       for (CFListIterator i= l; i.hasItem(); i++) 
    641         i.getItem()= mapUp (i.getItem(), alpha, V_buf, prim_elem, 
     640      for (CFListIterator i= l; i.hasItem(); i++)
     641        i.getItem()= mapUp (i.getItem(), alpha, V_buf, prim_elem,
    642642                             im_prim_elem, source, dest);
    643643      m= mapUp (m, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    644644      G_m= mapUp (G_m, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    645       newtonPoly= mapUp (newtonPoly, alpha, V_buf, prim_elem, im_prim_elem, 
     645      newtonPoly= mapUp (newtonPoly, alpha, V_buf, prim_elem, im_prim_elem,
    646646                          source, dest);
    647647      ppA= mapUp (ppA, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    648648      ppB= mapUp (ppB, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    649       gcdlcAlcB= mapUp (gcdlcAlcB, alpha, V_buf, prim_elem, im_prim_elem, 
     649      gcdlcAlcB= mapUp (gcdlcAlcB, alpha, V_buf, prim_elem, im_prim_elem,
    650650                         source, dest);
    651651
     
    655655      CFList list;
    656656      TIMING_START (gcd_recursion);
    657       G_random_element= 
     657      G_random_element=
    658658      GCD_Fp_extension (ppA (random_element, x), ppB (random_element, x), V_buf,
    659659                        list, topLevel);
    660       TIMING_END_AND_PRINT (gcd_recursion, 
     660      TIMING_END_AND_PRINT (gcd_recursion,
    661661                            "time for recursive call: ");
    662662      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
    663663    }
    664     else 
     664    else
    665665    {
    666666      CFList list;
    667667      TIMING_START (gcd_recursion);
    668       G_random_element= 
     668      G_random_element=
    669669      GCD_Fp_extension (ppA(random_element, x), ppB(random_element, x), V_buf,
    670670                        list, topLevel);
    671       TIMING_END_AND_PRINT (gcd_recursion, 
     671      TIMING_END_AND_PRINT (gcd_recursion,
    672672                            "time for recursive call: ");
    673673      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
    674674    }
    675675
    676     d0= totaldegree (G_random_element, Variable(2), 
     676    d0= totaldegree (G_random_element, Variable(2),
    677677                     Variable (G_random_element.level()));
    678678    if (d0 == 0)
     
    680680      if (substitute > 1)
    681681        return N (reverseSubst (gcdcAcB, substitute));
    682       else 
     682      else
    683683        return N(gcdcAcB);
    684684    }
    685     if (d0 >  d) 
     685    if (d0 >  d)
    686686    {
    687687      if (!find (l, random_element))
     
    690690    }
    691691
    692     G_random_element= 
     692    G_random_element=
    693693    (gcdlcAlcB(random_element, x)/uni_lcoeff (G_random_element))
    694694    * G_random_element;
    695695
    696     d0= totaldegree (G_random_element, Variable(2), 
     696    d0= totaldegree (G_random_element, Variable(2),
    697697                     Variable(G_random_element.level()));
    698     if (d0 <  d) 
     698    if (d0 <  d)
    699699    {
    700700      m= gcdlcAlcB;
     
    706706    TIMING_START (newton_interpolation);
    707707    H= newtonInterp (random_element, G_random_element, newtonPoly, G_m, x);
    708     TIMING_END_AND_PRINT (newton_interpolation, 
     708    TIMING_END_AND_PRINT (newton_interpolation,
    709709                          "time for newton interpolation: ");
    710710
    711     //termination test 
    712     if (uni_lcoeff (H) == gcdlcAlcB) 
     711    //termination test
     712    if (uni_lcoeff (H) == gcdlcAlcB)
    713713    {
    714714      cH= uni_content (H);
    715715      ppH= H/cH;
    716       if (inextension) 
    717       {
    718         CFList u, v; 
     716      if (inextension)
     717      {
     718        CFList u, v;
    719719        //maybe it's better to test if ppH is an element of F(\alpha) before
    720720        //mapping down
    721721        DEBOUTLN (cerr, "ppH before mapDown= " << ppH);
    722         ppH= mapDown (ppH, prim_elem, im_prim_elem, alpha, u, v); 
     722        ppH= mapDown (ppH, prim_elem, im_prim_elem, alpha, u, v);
    723723        ppH /= Lc(ppH);
    724724        DEBOUTLN (cerr, "ppH after mapDown= " << ppH);
    725         if (fdivides (ppH, A) && fdivides (ppH, B))         
     725        if (fdivides (ppH, A) && fdivides (ppH, B))
    726726        {
    727727          if (substitute > 1)
     
    733733        }
    734734      }
    735       else if (fdivides (ppH, A) && fdivides (ppH, B)) 
     735      else if (fdivides (ppH, A) && fdivides (ppH, B))
    736736      {
    737737        if (substitute > 1)
     
    752752}
    753753
    754 /// compute a random element a of GF, s.t. F(a) \f$ \neq 0 \f$ , F is a 
     754/// compute a random element a of GF, s.t. F(a) \f$ \neq 0 \f$ , F is a
    755755/// univariate polynomial, returns fail if there are no field elements left
    756756/// which have not been used before
     
    766766  int d= getGFDegree();
    767767  int bound= ipower (p, d);
    768   do 
     768  do
    769769  {
    770770    if (list.length() == bound)
     
    775775    if (list.length() < 1)
    776776      random= 0;
    777     else 
     777    else
    778778    {
    779779      random= genGF.generate();
     
    781781        random= genGF.generate();
    782782    }
    783     if (F (random, x) == 0) 
     783    if (F (random, x) == 0)
    784784    {
    785785      list.append (random);
     
    796796/// the size of the base field is bounded by 2^16, output is monic
    797797CanonicalForm GCD_GF (const CanonicalForm& F, const CanonicalForm& G,
    798         CFList& l, bool& topLevel) 
    799 { 
     798        CFList& l, bool& topLevel)
     799{
    800800  CanonicalForm A= F;
    801801  CanonicalForm B= G;
     
    807807  if (G.isUnivariate() && fdivides(G, F)) return G/Lc(G);
    808808  if (F == G) return F/Lc(F);
    809  
     809
    810810  CFMap M,N;
    811811  int best_level= myCompress (A, B, M, N, topLevel);
     
    818818  Variable x= Variable(1);
    819819
    820   //univariate case 
    821   if (A.isUnivariate() && B.isUnivariate()) 
    822     return N (gcd (A, B)); 
     820  //univariate case
     821  if (A.isUnivariate() && B.isUnivariate())
     822    return N (gcd (A, B));
    823823
    824824  int substitute= substituteCheck (A, B);
    825  
     825
    826826  if (substitute > 1)
    827827    subst (A, B, A, B, substitute);
     
    835835    if (best_level <= 2)
    836836      gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, best_level);
    837     else 
    838       gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, 2); 
     837    else
     838      gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, 2);
    839839  }
    840840  else
    841841  {
    842842    cA = uni_content (A);
    843     cB = uni_content (B); 
     843    cB = uni_content (B);
    844844    gcdcAcB= gcd (cA, cB);
    845845    ppA= A/cA;
     
    848848
    849849  CanonicalForm lcA, lcB;  // leading coefficients of A and B
    850   CanonicalForm gcdlcAlcB; 
     850  CanonicalForm gcdlcAlcB;
    851851
    852852  lcA= uni_lcoeff (ppA);
    853853  lcB= uni_lcoeff (ppB);
    854854
    855   if (fdivides (lcA, lcB)) 
    856   { 
     855  if (fdivides (lcA, lcB))
     856  {
    857857    if (fdivides (A, B))
    858       return F; 
    859   }   
    860   if (fdivides (lcB, lcA)) 
    861   { 
    862     if (fdivides (B, A)) 
     858      return F;
     859  }
     860  if (fdivides (lcB, lcA))
     861  {
     862    if (fdivides (B, A))
    863863      return G;
    864864  }
     
    871871    if (substitute > 1)
    872872      return N (reverseSubst (gcdcAcB, substitute));
    873     else 
     873    else
    874874      return N(gcdcAcB);
    875875  }
    876876  int d0= totaldegree (ppB, Variable(2), Variable (ppB.level()));
    877   if (d0 < d) 
    878     d= d0; 
     877  if (d0 < d)
     878    d= d0;
    879879  if (d == 0)
    880880  {
    881881    if (substitute > 1)
    882882      return N (reverseSubst (gcdcAcB, substitute));
    883     else 
     883    else
    884884      return N(gcdcAcB);
    885885  }
     
    900900  int expon;
    901901  char gf_name_buf= gf_name;
    902   do 
     902  do
    903903  {
    904904    random_element= GFRandomElement (m, l, fail);
    905     if (fail) 
    906     { 
     905    if (fail)
     906    {
    907907      int num_vars= tmin (getNumVars(A), getNumVars(B));
    908908      num_vars--;
     
    911911      if (expon < 2)
    912912        expon= 2;
    913       kk= getGFDegree(); 
    914       if (ipower (p, kk*expon) < (1 << 16)) 
     913      kk= getGFDegree();
     914      if (ipower (p, kk*expon) < (1 << 16))
    915915        setCharacteristic (p, kk*(int)expon, 'b');
    916       else 
     916      else
    917917      {
    918918        expon= (int) floor((log ((double)((1<<16) - 1)))/(log((double)p)*kk));
     
    922922      inextension= true;
    923923      fail= false;
    924       for (CFListIterator i= l; i.hasItem(); i++) 
     924      for (CFListIterator i= l; i.hasItem(); i++)
    925925        i.getItem()= GFMapUp (i.getItem(), kk);
    926926      m= GFMapUp (m, kk);
     
    934934      CFList list;
    935935      TIMING_START (gcd_recursion);
    936       G_random_element= GCD_GF (ppA(random_element, x), ppB(random_element, x), 
     936      G_random_element= GCD_GF (ppA(random_element, x), ppB(random_element, x),
    937937                                list, topLevel);
    938       TIMING_END_AND_PRINT (gcd_recursion, 
     938      TIMING_END_AND_PRINT (gcd_recursion,
    939939                            "time for recursive call: ");
    940940      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
    941941    }
    942     else 
     942    else
    943943    {
    944944      CFList list;
    945945      TIMING_START (gcd_recursion);
    946       G_random_element= GCD_GF (ppA(random_element, x), ppB(random_element, x), 
     946      G_random_element= GCD_GF (ppA(random_element, x), ppB(random_element, x),
    947947                                list, topLevel);
    948       TIMING_END_AND_PRINT (gcd_recursion, 
     948      TIMING_END_AND_PRINT (gcd_recursion,
    949949                            "time for recursive call: ");
    950950      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
    951951    }
    952952
    953     d0= totaldegree (G_random_element, Variable(2), 
     953    d0= totaldegree (G_random_element, Variable(2),
    954954                     Variable (G_random_element.level()));
    955     if (d0 == 0) 
    956     {
    957       if (inextension) 
     955    if (d0 == 0)
     956    {
     957      if (inextension)
    958958      {
    959959        setCharacteristic (p, k, gf_name_buf);
     
    962962            return N (reverseSubst (gcdcAcB, substitute));
    963963          else
    964             return N(gcdcAcB); 
    965         } 
     964            return N(gcdcAcB);
     965        }
    966966      }
    967967      else
     
    970970          return N (reverseSubst (gcdcAcB, substitute));
    971971        else
    972           return N(gcdcAcB); 
    973       }
    974     } 
    975     if (d0 >  d) 
     972          return N(gcdcAcB);
     973      }
     974    }
     975    if (d0 >  d)
    976976    {
    977977      if (!find (l, random_element))
     
    980980    }
    981981
    982     G_random_element= 
     982    G_random_element=
    983983    (gcdlcAlcB(random_element, x)/uni_lcoeff(G_random_element)) *
    984984     G_random_element;
    985     d0= totaldegree (G_random_element, Variable(2), 
     985    d0= totaldegree (G_random_element, Variable(2),
    986986                     Variable (G_random_element.level()));
    987987
    988     if (d0 < d) 
     988    if (d0 < d)
    989989    {
    990990      m= gcdlcAlcB;
     
    998998    TIMING_END_AND_PRINT (newton_interpolation, "time for newton interpolation: ");
    999999
    1000     //termination test 
    1001     if (uni_lcoeff (H) == gcdlcAlcB) 
     1000    //termination test
     1001    if (uni_lcoeff (H) == gcdlcAlcB)
    10021002    {
    10031003      cH= uni_content (H);
    10041004      ppH= H/cH;
    1005       if (inextension) 
    1006       {
    1007         if (fdivides(ppH, GFMapUp(A, k)) && fdivides(ppH, GFMapUp(B,k))) 
     1005      if (inextension)
     1006      {
     1007        if (fdivides(ppH, GFMapUp(A, k)) && fdivides(ppH, GFMapUp(B,k)))
    10081008        {
    10091009          DEBOUTLN (cerr, "ppH before mapDown= " << ppH);
     
    10191019        }
    10201020      }
    1021       else 
    1022       {
    1023         if (fdivides (ppH, A) && fdivides (ppH, B)) 
     1021      else
     1022      {
     1023        if (fdivides (ppH, A) && fdivides (ppH, B))
    10241024        {
    10251025          if (substitute > 1)
     
    10421042
    10431043/// F is assumed to be an univariate polynomial in x,
    1044 /// computes a random monic irreducible univariate polynomial of random 
     1044/// computes a random monic irreducible univariate polynomial of random
    10451045/// degree < i in x which does not divide F
    1046 CanonicalForm 
    1047 randomIrredpoly (int i, const Variable & x) 
     1046CanonicalForm
     1047randomIrredpoly (int i, const Variable & x)
    10481048{
    10491049  int p= getCharacteristic();
    10501050  ZZ NTLp= to_ZZ (p);
    10511051  ZZ_p::init (NTLp);
    1052   ZZ_pX NTLirredpoly; 
     1052  ZZ_pX NTLirredpoly;
    10531053  CanonicalForm CFirredpoly;
    10541054  BuildIrred (NTLirredpoly, i + 1);
    10551055  CFirredpoly= convertNTLZZpX2CF (NTLirredpoly, x);
    10561056  return CFirredpoly;
    1057 } 
     1057}
    10581058
    10591059static inline
     
    10671067  int p= getCharacteristic();
    10681068  int bound= p;
    1069   do 
     1069  do
    10701070  {
    10711071    if (list.length() == bound)
     
    10761076    if (list.length() < 1)
    10771077      random= 0;
    1078     else 
     1078    else
    10791079    {
    10801080      random= genFF.generate();
     
    10821082        random= genFF.generate();
    10831083    }
    1084     if (F (random, x) == 0) 
     1084    if (F (random, x) == 0)
    10851085    {
    10861086      list.append (random);
     
    10911091}
    10921092
    1093 CanonicalForm GCD_small_p (const CanonicalForm& F, const CanonicalForm&  G, 
    1094                            bool& topLevel, CFList& l) 
     1093CanonicalForm GCD_small_p (const CanonicalForm& F, const CanonicalForm&  G,
     1094                           bool& topLevel, CFList& l)
    10951095{
    10961096  CanonicalForm A= F;
     
    11141114  Variable x= Variable (1);
    11151115
    1116   //univariate case 
    1117   if (A.isUnivariate() && B.isUnivariate()) 
     1116  //univariate case
     1117  if (A.isUnivariate() && B.isUnivariate())
    11181118    return N (gcd (A, B));
    11191119
    11201120  int substitute= substituteCheck (A, B);
    1121  
     1121
    11221122  if (substitute > 1)
    11231123    subst (A, B, A, B, substitute);
     
    11311131    if (best_level <= 2)
    11321132      gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, best_level);
    1133     else 
    1134       gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, 2); 
     1133    else
     1134      gcdcAcB= extractContents (A, B, cA, cB, ppA, ppB, 2);
    11351135  }
    11361136  else
    11371137  {
    11381138    cA = uni_content (A);
    1139     cB = uni_content (B); 
     1139    cB = uni_content (B);
    11401140    gcdcAcB= gcd (cA, cB);
    11411141    ppA= A/cA;
     
    11441144
    11451145  CanonicalForm lcA, lcB;  // leading coefficients of A and B
    1146   CanonicalForm gcdlcAlcB; 
     1146  CanonicalForm gcdlcAlcB;
    11471147  lcA= uni_lcoeff (ppA);
    11481148  lcB= uni_lcoeff (ppB);
    11491149
    1150   if (fdivides (lcA, lcB)) 
    1151   { 
     1150  if (fdivides (lcA, lcB))
     1151  {
    11521152    if (fdivides (A, B))
    11531153      return F/Lc(F);
    1154   }   
     1154  }
    11551155  if (fdivides (lcB, lcA))
    1156   { 
    1157     if (fdivides (B, A)) 
     1156  {
     1157    if (fdivides (B, A))
    11581158      return G/Lc(G);
    11591159  }
    11601160
    1161   gcdlcAlcB= gcd (lcA, lcB); 
    1162  
     1161  gcdlcAlcB= gcd (lcA, lcB);
     1162
    11631163  int d= totaldegree (ppA, Variable (2), Variable (ppA.level()));
    11641164  int d0;
     
    11681168    if (substitute > 1)
    11691169      return N (reverseSubst (gcdcAcB, substitute));
    1170     else 
     1170    else
    11711171      return N(gcdcAcB);
    11721172  }
    11731173  d0= totaldegree (ppB, Variable (2), Variable (ppB.level()));
    11741174
    1175   if (d0 < d) 
     1175  if (d0 < d)
    11761176    d= d0;
    11771177
    1178   if (d == 0) 
     1178  if (d == 0)
    11791179  {
    11801180    if (substitute > 1)
    11811181      return N (reverseSubst (gcdcAcB, substitute));
    1182     else 
     1182    else
    11831183      return N(gcdcAcB);
    11841184  }
     
    11951195  topLevel= false;
    11961196  CFList source, dest;
    1197   do 
     1197  do
    11981198  {
    11991199    if (inextension)
     
    12061206      CFList list;
    12071207      TIMING_START (gcd_recursion);
    1208       G_random_element= 
    1209       GCD_small_p (ppA (random_element,x), ppB (random_element,x), topLevel, 
     1208      G_random_element=
     1209      GCD_small_p (ppA (random_element,x), ppB (random_element,x), topLevel,
    12101210      list);
    1211       TIMING_END_AND_PRINT (gcd_recursion, 
     1211      TIMING_END_AND_PRINT (gcd_recursion,
    12121212                            "time for recursive call: ");
    12131213      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
     
    12151215    else if (!fail && inextension)
    12161216    {
    1217       CFList list; 
     1217      CFList list;
    12181218      TIMING_START (gcd_recursion);
    1219       G_random_element= 
    1220       GCD_Fp_extension (ppA (random_element, x), ppB (random_element, x), alpha, 
     1219      G_random_element=
     1220      GCD_Fp_extension (ppA (random_element, x), ppB (random_element, x), alpha,
    12211221                        list, topLevel);
    1222       TIMING_END_AND_PRINT (gcd_recursion, 
     1222      TIMING_END_AND_PRINT (gcd_recursion,
    12231223                            "time for recursive call: ");
    1224       DEBOUTLN (cerr, "G_random_element= " << G_random_element);   
     1224      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
    12251225    }
    12261226    else if (fail && !inextension)
     
    12321232      int deg= 2;
    12331233      do {
    1234         mipo= randomIrredpoly (deg, x); 
     1234        mipo= randomIrredpoly (deg, x);
    12351235        alpha= rootOf (mipo);
    12361236        inextension= true;
    12371237        fail= false;
    1238         random_element= randomElement (m, alpha, l, fail); 
     1238        random_element= randomElement (m, alpha, l, fail);
    12391239        deg++;
    1240       } while (fail); 
     1240      } while (fail);
    12411241      list= CFList();
    12421242      TIMING_START (gcd_recursion);
    1243       G_random_element= 
    1244       GCD_Fp_extension (ppA (random_element, x), ppB (random_element, x), alpha, 
     1243      G_random_element=
     1244      GCD_Fp_extension (ppA (random_element, x), ppB (random_element, x), alpha,
    12451245                        list, topLevel);
    1246       TIMING_END_AND_PRINT (gcd_recursion, 
     1246      TIMING_END_AND_PRINT (gcd_recursion,
    12471247                            "time for recursive call: ");
    12481248      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
     
    12601260      CanonicalForm prim_elem, im_prim_elem;
    12611261      prim_elem= primitiveElement (alpha, V_buf2, prim_fail);
    1262        
     1262
    12631263      ASSERT (!prim_fail, "failure in integer factorizer");
    12641264      if (prim_fail)
     
    12711271
    12721272      inextensionextension= true;
    1273       for (CFListIterator i= l; i.hasItem(); i++) 
    1274         i.getItem()= mapUp (i.getItem(), alpha, V_buf, prim_elem, 
     1273      for (CFListIterator i= l; i.hasItem(); i++)
     1274        i.getItem()= mapUp (i.getItem(), alpha, V_buf, prim_elem,
    12751275                             im_prim_elem, source, dest);
    12761276      m= mapUp (m, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    12771277      G_m= mapUp (G_m, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    1278       newtonPoly= mapUp (newtonPoly, alpha, V_buf, prim_elem, im_prim_elem, 
     1278      newtonPoly= mapUp (newtonPoly, alpha, V_buf, prim_elem, im_prim_elem,
    12791279                          source, dest);
    12801280      ppA= mapUp (ppA, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    12811281      ppB= mapUp (ppB, alpha, V_buf, prim_elem, im_prim_elem, source, dest);
    1282       gcdlcAlcB= mapUp (gcdlcAlcB, alpha, V_buf, prim_elem, im_prim_elem, 
     1282      gcdlcAlcB= mapUp (gcdlcAlcB, alpha, V_buf, prim_elem, im_prim_elem,
    12831283                         source, dest);
    12841284      fail= false;
     
    12871287      CFList list;
    12881288      TIMING_START (gcd_recursion);
    1289       G_random_element= 
     1289      G_random_element=
    12901290      GCD_Fp_extension (ppA (random_element, x), ppB (random_element, x), V_buf,
    12911291                        list, topLevel);
    1292       TIMING_END_AND_PRINT (gcd_recursion, 
     1292      TIMING_END_AND_PRINT (gcd_recursion,
    12931293                            "time for recursive call: ");
    12941294      DEBOUTLN (cerr, "G_random_element= " << G_random_element);
    12951295      alpha= V_buf;
    1296     } 
    1297 
    1298     d0= totaldegree (G_random_element, Variable(2), 
     1296    }
     1297
     1298    d0= totaldegree (G_random_element, Variable(2),
    12991299                     Variable (G_random_element.level()));
    13001300
     
    13041304        return N (reverseSubst (gcdcAcB, substitute));
    13051305      else
    1306         return N(gcdcAcB); 
    1307     }
    1308     if (d0 >  d) 
    1309     { 
     1306        return N(gcdcAcB);
     1307    }
     1308    if (d0 >  d)
     1309    {
    13101310      if (!find (l, random_element))
    13111311        l.append (random_element);
     
    13141314
    13151315    G_random_element= (gcdlcAlcB(random_element,x)/uni_lcoeff(G_random_element))
    1316                        *G_random_element; 
    1317 
    1318    
    1319     d0= totaldegree (G_random_element, Variable(2), 
     1316                       *G_random_element;
     1317
     1318
     1319    d0= totaldegree (G_random_element, Variable(2),
    13201320                     Variable(G_random_element.level()));
    13211321
    1322     if (d0 <  d) 
     1322    if (d0 <  d)
    13231323    {
    13241324      m= gcdlcAlcB;
     
    13271327      d= d0;
    13281328    }
    1329      
     1329
    13301330    TIMING_START (newton_interpolation);
    13311331    H= newtonInterp (random_element, G_random_element, newtonPoly, G_m, x);
    1332     TIMING_END_AND_PRINT (newton_interpolation, 
     1332    TIMING_END_AND_PRINT (newton_interpolation,
    13331333                          "time for newton_interpolation: ");
    13341334
    13351335    //termination test
    1336     if (uni_lcoeff (H) == gcdlcAlcB) 
     1336    if (uni_lcoeff (H) == gcdlcAlcB)
    13371337    {
    13381338      cH= uni_content (H);
     
    13401340      ppH /= Lc (ppH);
    13411341      DEBOUTLN (cerr, "ppH= " << ppH);
    1342       if (fdivides (ppH, A) && fdivides (ppH, B)) 
     1342      if (fdivides (ppH, A) && fdivides (ppH, B))
    13431343      {
    13441344        if (substitute > 1)
Note: See TracChangeset for help on using the changeset viewer.