Changeset fe2d4c in git for factory/algext.cc


Ignore:
Timestamp:
Jun 20, 2011, 2:10:50 PM (13 years ago)
Author:
Martin Lee <martinlee84@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
4e35a894dd1ecea25a42d6b12a182338771e5732
Parents:
35eb6cf319007843f3388d3f071f3bee96188fbd
Message:
added better compression of polynomials to tryBrownGCD
added better termination test to QGCD


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

Legend:

Unmodified
Added
Removed
  • factory/algext.cc

    r35eb6c rfe2d4c  
    1414#endif
    1515
     16#include "templates/ftmpl_functions.h"
    1617#include "cf_defs.h"
    1718#include "canonicalform.h"
     
    2324#include "cf_map.h"
    2425#include "cf_generator.h"
     26
     27/// compressing two polynomials F and G, M is used for compressing,
     28/// N to reverse the compression
     29static
     30int myCompress (const CanonicalForm& F, const CanonicalForm& G, CFMap & M,
     31                CFMap & N, bool topLevel)
     32{
     33  int n= tmax (F.level(), G.level());
     34  int * degsf= new int [n + 1];
     35  int * degsg= new int [n + 1];
     36
     37  for (int i = 0; i <= n; i++)
     38    degsf[i]= degsg[i]= 0;
     39
     40  degsf= degrees (F, degsf);
     41  degsg= degrees (G, degsg);
     42
     43  int both_non_zero= 0;
     44  int f_zero= 0;
     45  int g_zero= 0;
     46  int both_zero= 0;
     47
     48  if (topLevel)
     49  {
     50    for (int i= 1; i <= n; i++)
     51    {
     52      if (degsf[i] != 0 && degsg[i] != 0)
     53      {
     54        both_non_zero++;
     55        continue;
     56      }
     57      if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
     58      {
     59        f_zero++;
     60        continue;
     61      }
     62      if (degsg[i] == 0 && degsf[i] && i <= F.level())
     63      {
     64        g_zero++;
     65        continue;
     66      }
     67    }
     68
     69    if (both_non_zero == 0)
     70    {
     71      delete [] degsf;
     72      delete [] degsg;
     73      return 0;
     74    }
     75
     76    // map Variables which do not occur in both polynomials to higher levels
     77    int k= 1;
     78    int l= 1;
     79    for (int i= 1; i <= n; i++)
     80    {
     81      if (degsf[i] != 0 && degsg[i] == 0 && i <= F.level())
     82      {
     83        if (k + both_non_zero != i)
     84        {
     85          M.newpair (Variable (i), Variable (k + both_non_zero));
     86          N.newpair (Variable (k + both_non_zero), Variable (i));
     87        }
     88        k++;
     89      }
     90      if (degsf[i] == 0 && degsg[i] != 0 && i <= G.level())
     91      {
     92        if (l + g_zero + both_non_zero != i)
     93        {
     94          M.newpair (Variable (i), Variable (l + g_zero + both_non_zero));
     95          N.newpair (Variable (l + g_zero + both_non_zero), Variable (i));
     96        }
     97        l++;
     98      }
     99    }
     100
     101    // sort Variables x_{i} in increasing order of
     102    // min(deg_{x_{i}}(f),deg_{x_{i}}(g))
     103    int m= tmax (F.level(), G.level());
     104    int min_max_deg;
     105    k= both_non_zero;
     106    l= 0;
     107    int i= 1;
     108    while (k > 0)
     109    {
     110      if (degsf [i] != 0 && degsg [i] != 0)
     111        min_max_deg= tmax (degsf[i], degsg[i]);
     112      else
     113        min_max_deg= 0;
     114      while (min_max_deg == 0)
     115      {
     116        i++;
     117        min_max_deg= tmax (degsf[i], degsg[i]);
     118        if (degsf [i] != 0 && degsg [i] != 0)
     119          min_max_deg= tmax (degsf[i], degsg[i]);
     120        else
     121          min_max_deg= 0;
     122      }
     123      for (int j= i + 1; j <=  m; j++)
     124      {
     125        if (tmax (degsf[j],degsg[j]) <= min_max_deg && degsf[j] != 0 && degsg [j] != 0)
     126        {
     127          min_max_deg= tmax (degsf[j], degsg[j]);
     128          l= j;
     129        }
     130      }
     131      if (l != 0)
     132      {
     133        if (l != k)
     134        {
     135          M.newpair (Variable (l), Variable(k));
     136          N.newpair (Variable (k), Variable(l));
     137          degsf[l]= 0;
     138          degsg[l]= 0;
     139          l= 0;
     140        }
     141        else
     142        {
     143          degsf[l]= 0;
     144          degsg[l]= 0;
     145          l= 0;
     146        }
     147      }
     148      else if (l == 0)
     149      {
     150        if (i != k)
     151        {
     152          M.newpair (Variable (i), Variable (k));
     153          N.newpair (Variable (k), Variable (i));
     154          degsf[i]= 0;
     155          degsg[i]= 0;
     156        }
     157        else
     158        {
     159          degsf[i]= 0;
     160          degsg[i]= 0;
     161        }
     162        i++;
     163      }
     164      k--;
     165    }
     166  }
     167  else
     168  {
     169    //arrange Variables such that no gaps occur
     170    for (int i= 1; i <= n; i++)
     171    {
     172      if (degsf[i] == 0 && degsg[i] == 0)
     173      {
     174        both_zero++;
     175        continue;
     176      }
     177      else
     178      {
     179        if (both_zero != 0)
     180        {
     181          M.newpair (Variable (i), Variable (i - both_zero));
     182          N.newpair (Variable (i - both_zero), Variable (i));
     183        }
     184      }
     185    }
     186  }
     187
     188  delete [] degsf;
     189  delete [] degsg;
     190
     191  return 1;
     192}
    25193
    26194CanonicalForm reduce(const CanonicalForm & f, const CanonicalForm & M)
     
    199367
    200368
    201 void tryBrownGCD( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, bool & fail )
     369void tryBrownGCD( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & M, CanonicalForm & result, bool & fail, bool topLevel )
    202370{ // assume F,G are multivariate polys over Z/p(a) for big prime p, M "univariate" polynomial in an algebraic variable
    203371  // M is assumed to be monic
     
    261429  }
    262430  CFMap MM,NN;
    263   CFArray ps(1,2);
    264   ps[1] = F;
    265   ps[2] = G;
    266   compress(ps,MM,NN); // maps MM, NN are created
     431  int lev= myCompress (F, G, MM, NN, topLevel);
     432  if (lev == 0)
     433  {
     434    result= 1;
     435    return;
     436  }
    267437  CanonicalForm f=MM(F);
    268438  CanonicalForm g=MM(G);
     
    347517    if(gamma_image.isZero()) // skip lc-bad points var(1)-alpha
    348518      continue;
    349     tryBrownGCD( f(alpha, Variable(1)), g(alpha, Variable(1)), M, g_image, fail ); // recursive call with one var less
     519    tryBrownGCD( f(alpha, Variable(1)), g(alpha, Variable(1)), M, g_image, fail, false ); // recursive call with one var less
    350520    if(fail)
    351521      return;
     
    488658  q = 1;
    489659  D = 0;
     660  CanonicalForm test= 0;
     661  bool equal= false;
    490662  for( i=cf_getNumBigPrimes()-1; i>-1; i-- )
    491663  {
     
    530702      setReduce(a,true); // reduce expressions modulo mipo
    531703      On( SW_RATIONAL ); // needed by fdivides
    532       if( fdivides( tmp, f ) && fdivides( tmp, g )) // trial division
     704      if (test != tmp)
     705        test= tmp;
     706      else
     707        equal= true; // modular image did not add any new information
     708      if(equal && fdivides( tmp, f ) && fdivides( tmp, g )) // trial division
    533709      {
    534710        Off( SW_RATIONAL );
     
    762938  while(true)
    763939  {
    764     tryInvert( Lc(result), M, inv, fail );
    765     if(fail)
    766       return;
    767     // here: Lc(result) is invertible modulo M
    768     q = Lc(P)*inv*power( x, P.degree(x)-result.degree(x) );
    769     rem = reduce( P - q*result, M );
     940    tryDivrem (P, result, q, rem, inv, M, fail);
     941    if(fail)
     942      return;
    770943    if( rem.isZero() )
    771944    {
Note: See TracChangeset for help on using the changeset viewer.