source: git/factory/fac_ezgcd.cc @ c1b9927

spielwiese
Last change on this file since c1b9927 was 6db552, checked in by Hans Schoenemann <hannes@…>, 13 years ago
removed include-wrapppers git-svn-id: file:///usr/local/Singular/svn/trunk@13772 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[493c477]1/* emacs edit mode for this file is -*- C++ -*- */
[341696]2/* $Id$ */
[c1c3693]3
[b973c0]4#include <config.h>
5
[c1c3693]6#include "assert.h"
[0b61044]7#include "debug.h"
8
[c1c3693]9#include "cf_defs.h"
10#include "canonicalform.h"
11#include "fac_util.h"
[6746a4]12#include "cf_algorithm.h"
[c1c3693]13#include "cf_reval.h"
14#include "cf_random.h"
15#include "cf_primes.h"
16#include "fac_distrib.h"
[6db552]17#include "templates/ftmpl_functions.h"
[c1c3693]18
19static void findeval( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db, REvaluation & b, int delta, int degF, int degG );
20
21static CanonicalForm ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG, REvaluation & b, bool internal );
22
23static CanonicalForm ezgcd_specialcase ( const CanonicalForm & F, const CanonicalForm & G, REvaluation & b, const modpk & bound );
24
25static modpk findBound ( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & lcF, const CanonicalForm & lcG, int degF, int degG );
26
27static modpk enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk );
28
29CanonicalForm
30ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG )
31{
32    REvaluation b;
33    return ezgcd( FF, GG, b, false );
34}
35
36static CanonicalForm
37ezgcd ( const CanonicalForm & FF, const CanonicalForm & GG, REvaluation & b, bool internal )
38{
39    CanonicalForm F, G, f, g, d, Fb, Gb, Db, Fbt, Gbt, Dbt, B0, B, D0, lcF, lcG, lcD;
40    CFArray DD( 1, 2 ), lcDD( 1, 2 );
[e22ea7]41    int degF, degG, delta, t;
[c1c3693]42    REvaluation bt;
43    bool gcdfound = false;
44    Variable x = Variable(1);
45    modpk bound;
46
47    DEBINCLEVEL( cerr, "ezgcd" );
[d81ed62]48    DEBOUTLN( cerr, "FF = " << FF );
49    DEBOUTLN( cerr, "GG = " << GG );
[c1c3693]50    f = content( FF, x ); g = content( GG, x ); d = gcd( f, g );
[d81ed62]51    DEBOUTLN( cerr, "f = " << f );
52    DEBOUTLN( cerr, "g = " << g );
[c1c3693]53    F = FF / f; G = GG / g;
54    if ( F.isUnivariate() && G.isUnivariate() )
[e22ea7]55    {
56        DEBDECLEVEL( cerr, "ezgcd" );
[661ca5e]57        if(F.mvar()==G.mvar())
58          d*=gcd(F,G);
59        return d;
[e22ea7]60    }
[c1c3693]61    else  if ( gcd_test_one( F, G, false ) )
[e22ea7]62    {
63        DEBDECLEVEL( cerr, "ezgcd" );
[4e6908]64        return d;
[e22ea7]65    }
[c1c3693]66    lcF = LC( F, x ); lcG = LC( G, x );
67    lcD = gcd( lcF, lcG );
68    delta = 0;
69    degF = degree( F, x ); degG = degree( G, x );
70    t = tmax( F.level(), G.level() );
71    bound = findBound( F, G, lcF, lcG, degF, degG );
72    if ( ! internal )
[afd067]73        b = REvaluation( 2, t, IntRandom( 25 ) );
[c1c3693]74    while ( ! gcdfound ) {
[4e6908]75        /// ---> A2
76        DEBOUTLN( cerr, "search for evaluation, delta = " << delta );
77        DEBOUTLN( cerr, "F = " << F );
78        DEBOUTLN( cerr, "G = " << G );
79        findeval( F, G, Fb, Gb, Db, b, delta, degF, degG );
80        DEBOUTLN( cerr, "found evaluation b = " << b );
81        DEBOUTLN( cerr, "F(b) = " << Fb );
82        DEBOUTLN( cerr, "G(b) = " << Gb );
83        DEBOUTLN( cerr, "D(b) = " << Db );
84        delta = degree( Db );
85        /// ---> A3
86        if ( delta == 0 )
[afd067]87        {
[e22ea7]88          DEBDECLEVEL( cerr, "ezgcd" );
89          return d;
[afd067]90        }
[4e6908]91        /// ---> A4
[e22ea7]92        //deltaold = delta;
93        while ( 1 ) {
[4e6908]94            bt = b;
95            findeval( F, G, Fbt, Gbt, Dbt, bt, delta + 1, degF, degG );
[afd067]96            int dd=degree( Dbt );
[e22ea7]97            if ( dd /*degree( Dbt )*/ == 0 )
[afd067]98            {
[e22ea7]99                DEBDECLEVEL( cerr, "ezgcd" );
[4e6908]100                return d;
[afd067]101            }
[e22ea7]102            if ( dd /*degree( Dbt )*/ == delta )
103                break;
104            else  if ( dd /*degree( Dbt )*/ < delta ) {
105                delta = dd /*degree( Dbt )*/;
[4e6908]106                b = bt;
107                Db = Dbt; Fb = Fbt; Gb = Gbt;
108            }
109        }
110        DEBOUTLN( cerr, "now after A4, delta = " << delta );
111        /// ---> A5
[ebc602]112        if ( degF <= degG && delta == degF && fdivides( F, G ) )
[e22ea7]113        {
114            DEBDECLEVEL( cerr, "ezgcd" );
[4e6908]115            return d*F;
[afd067]116        }
[ebc602]117        if ( degG < degF && delta == degG && fdivides( G, F ) )
[e22ea7]118        {
119            DEBDECLEVEL( cerr, "ezgcd" );
[4e6908]120            return d*G;
[afd067]121        }
[4e6908]122        if ( delta != degF && delta != degG ) {
[bde566]123            bool B_is_F;
[4e6908]124            /// ---> A6
125            CanonicalForm xxx;
126            //if ( gcd( (DD[1] = Fb / Db), Db ) == 1 ) {
[33d8725]127            DD[1] = Fb / Db;
[afd067]128            xxx= gcd( DD[1], Db );
[33d8725]129            DEBOUTLN( cerr, "gcd((Fb/Db),Db) = " << xxx );
130            DEBOUTLN( cerr, "Fb/Db = " << DD[1] );
131            DEBOUTLN( cerr, "Db = " << Db );
[afd067]132            if (xxx.inCoeffDomain()) {
[4e6908]133                B = F;
134                DD[2] = Db;
135                lcDD[1] = lcF;
136                lcDD[2] = lcF;
137                B *= lcF;
[806c18]138                B_is_F=true;
[4e6908]139            }
140            //else  if ( gcd( (DD[1] = Gb / Db), Db ) == 1 ) {
[afd067]141            else
142            {
[33d8725]143              DD[1] = Gb / Db;
[afd067]144              xxx=gcd( DD[1], Db );
[33d8725]145              DEBOUTLN( cerr, "gcd((Gb/Db),Db) = " << xxx );
146              DEBOUTLN( cerr, "Gb/Db = " << DD[1] );
147              DEBOUTLN( cerr, "Db = " << Db );
[afd067]148              if (xxx.inCoeffDomain())
[33d8725]149              {
[4e6908]150                B = G;
151                DD[2] = Db;
152                lcDD[1] = lcG;
153                lcDD[2] = lcG;
154                B *= lcG;
[806c18]155                B_is_F=false;
[33d8725]156              }
157              else
158              {
[c1c3693]159#ifdef DEBUGOUTPUT
[4e6908]160                CanonicalForm dummyres = d * ezgcd_specialcase( F, G, b, bound );
161                DEBDECLEVEL( cerr, "ezgcd" );
162                return dummyres;
[c1c3693]163#else
[4e6908]164                return d * ezgcd_specialcase( F, G, b, bound );
[c1c3693]165#endif
[33d8725]166              }
[afd067]167            }
[4e6908]168            /// ---> A7
169            DD[2] = DD[2] * ( b( lcDD[2] ) / lc( DD[2] ) );
170            DD[1] = DD[1] * ( b( lcDD[1] ) / lc( DD[1] ) );
171            bound = enlargeBound( B, DD[2], DD[1], bound );
172            DEBOUTLN( cerr, "(hensel) B    = " << B );
173            DEBOUTLN( cerr, "(hensel) lcB  = " << LC( B, Variable(1) ) );
174            DEBOUTLN( cerr, "(hensel) b(B) = " << b(B) );
175            DEBOUTLN( cerr, "(hensel) DD   = " << DD );
176            DEBOUTLN( cerr, "(hensel) lcDD = " << lcDD );
177            gcdfound = Hensel( B, DD, lcDD, b, bound, x );
178            DEBOUTLN( cerr, "(hensel finished) DD   = " << DD );
[afd067]179
180            if (gcdfound)
181            {
[bde566]182              CanonicalForm xxx=content(DD[2],Variable(1));
183              CanonicalForm cand=DD[2] / xxx; //content(DD[2],Variable(1));
184#if 0
[ebc602]185              gcdfound= fdivides(cand,G) &&  fdivides(cand,F);
[4a71670]186#else
[bde566]187              if (B_is_F /*B==F*lcF*/)
[afd067]188              {
[e22ea7]189                DEBOUTLN( cerr, "(test) G: "<<G<<" % gcd:"<<cand<<" -> " << G%cand );
[ebc602]190                gcdfound= fdivides(cand,G);
[e22ea7]191              }
[afd067]192              else
193              {
[e22ea7]194                DEBOUTLN( cerr, "(test) F: "<<F<<" % gcd:"<<cand<<" -> " << F%cand);
[ebc602]195                gcdfound= fdivides(cand,F);
[afd067]196              }
[4a71670]197#endif
[afd067]198            }
[4e6908]199            /// ---> A8 (gcdfound)
200        }
[33d8725]201        delta++;
[c1c3693]202    }
203    /// ---> A9
204    DEBDECLEVEL( cerr, "ezgcd" );
205    return d * DD[2] / content(DD[2],Variable(1));
206}
207
208static CanonicalForm
209ezgcd_specialcase ( const CanonicalForm & F, const CanonicalForm & G, REvaluation & b, const modpk & bound )
210{
[30097c]211    CanonicalForm d;
212#if 1
213    Off(SW_USE_EZGCD);
214    //bool ntl0=isOn(SW_USE_NTL_GCD_0);
215    //Off(SW_USE_NTL_GCD_0);
216    //bool ntlp=isOn(SW_USE_NTL_GCD_P);
217    //Off(SW_USE_NTL_GCD_P);
218    d=gcd( F, G );
219    //if (ntl0) On(SW_USE_NTL_GCD_0);
220    //if (ntlp) On(SW_USE_NTL_GCD_P);
221    On(SW_USE_EZGCD);
222    return d;
223#else
[d81ed62]224    DEBOUTLN( cerr, "ezgcd: special case" );
[30097c]225    CanonicalForm Ft, Gt, L, LL, Fb, Gb, Db, Lb, D, Ds, Dt;
[c1c3693]226    CFArray DD( 1, 2 ), lcDD( 1, 2 );
[4e6908]227    Variable x = Variable( 1 );
[c1c3693]228    bool gcdfound;
229
230    Dt = 1;
231    /// ---> S1
[d81ed62]232    DEBOUTLN( cerr, "ezgcdspec: (S1)" );
[c1c3693]233    Ft = ezgcd( F, F.deriv( x ) );
[01e8874]234    if ( Ft.isOne() )
235    {
[4e6908]236        // In this case F is squarefree and we came here by bad chance
237        // (means: bad evaluation point).  Try again with another
238        // evaluation point.  Bug fix (?) by JS.  The bad example was
[6594d4]239        // gcd.debug -ocr /+USE_EZGCD/@12/CB \
[4e6908]240        //     '(16*B^8-208*B^6*C+927*B^4*C^2-1512*B^2*C^3+432*C^4)' \
[6594d4]241        //     '(4*B^7*C^2-50*B^5*C^3+208*B^3*C^4-288*B*C^5)'
[661ca5e]242        b.nextpoint();
[afd067]243        return ezgcd( F, G, b, true );
[6594d4]244    }
[4e6908]245
[d81ed62]246    DEBOUTLN( cerr, "ezgcdspec: (S1) done, Ft = " << Ft );
[c1c3693]247    L = F / Ft;
248    /// ---> S2
[d81ed62]249    DEBOUTLN( cerr, "ezgcdspec: (S2)" );
[6594d4]250
[c1c3693]251    L = ezgcd( L, G, b, true );
[d81ed62]252    DEBOUTLN( cerr, "ezgcdspec: (S2) done, Ds = " << Ds );
[c1c3693]253    Gt = G / L;
254    Ds = L; Dt = L;
255    Lb = b( L ); Gb = b( Gt ); Fb = b( Ft );
256    d = gcd( LC( L, x ), gcd( LC( Ft, x ), LC( Gt, x ) ) );
257    while ( true ) {
[4e6908]258        /// ---> S3
259        DEBOUTLN( cerr, "ezgcdspec: (S3)" );
260        DEBOUTLN( cerr, "ezgcdspec: Fb = " << Fb );
261        DEBOUTLN( cerr, "ezgcdspec: Gb = " << Gb );
262        DD[1] = gcd( Lb, gcd( Fb, Gb ) );
263        /// ---> S4
264        DEBOUTLN( cerr, "ezgcdspec: (S4)" );
265        if ( degree( DD[1] ) == 0 )
266            return Ds;
267        /// ---> S5
268        DEBOUTLN( cerr, "ezgcdspec: (S5)" );
269        DEBOUTLN( cerr, "ezgcdspec: Lb = " << Lb );
270        DEBOUTLN( cerr, "ezgcdspec: Db = " << DD[1] );
271        Db = DD[1];
272        if ( ! (DD[2] = Lb/DD[1]).isOne() ) {
273            DEBOUTLN( cerr, "ezgcdspec: (S55)" );
274            lcDD[2] = LC( L, x );
275            lcDD[1] = d;
276            DD[1] = ( DD[1] * b( d ) ) / lc( DD[1] );
277            DD[2] = ( DD[2] * b( lcDD[2] ) ) / lc( DD[2] );
278            LL = L * d;
279            modpk newbound = enlargeBound( LL, DD[2], DD[1], bound );
280            DEBOUTLN( cerr, "ezgcdspec: begin with lift." );
281            DEBOUTLN( cerr, "ezgcdspec: B     = " << LL );
282            DEBOUTLN( cerr, "ezgcdspec: DD    = " << DD );
283            DEBOUTLN( cerr, "ezgcdspec: lcDD  = " << lcDD );
284            DEBOUTLN( cerr, "ezgcdspec: b     = " << b );
285            DEBOUTLN( cerr, "ezgcdspec: bound = " << bound.getpk() );
286            DEBOUTLN( cerr, "ezgcdspec: lc(B) = " << LC( LL, x ) );
287            DEBOUTLN( cerr, "ezgcdspec: test  = " << b( LL ) - DD[1] * DD[2] );
288            gcdfound = Hensel( LL, DD, lcDD, b, newbound, x );
289            ASSERT( gcdfound, "fatal error in algorithm" );
290            Dt = pp( DD[1] );
291        }
292        DEBOUTLN( cerr, "ezgcdspec: (S7)" );
293        Ds = Ds * Dt;
294        Fb = Fb / Db;
295        Gb = Gb / Db;
[c1c3693]296    }
297    // this point is never reached, but to avoid compiler warnings let's return a value
298    return 0;
[41a8db]299#endif
[c1c3693]300}
301
302static void
303findeval( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db, REvaluation & b, int delta, int degF, int degG )
304{
305    bool ok;
306    if ( delta != 0 )
[661ca5e]307        b.nextpoint();
[e22ea7]308    DEBOUTLN( cerr, "ezgcd: (findeval) F = " << F  <<", G="<< G);
309    DEBOUTLN( cerr, "ezgcd: (findeval) degF = " << degF << ", degG="<<degG );
[c1c3693]310    do {
[4e6908]311        DEBOUTLN( cerr, "ezgcd: (findeval) b = " << b );
312        Fb = b( F );
313        ok = degree( Fb ) == degF;
314        if ( ok ) {
315            Gb = b( G );
316            ok = degree( Gb ) == degG;
317        }
[afd067]318
[c92316]319        if ( ok )
[afd067]320        {
[c92316]321            Db = gcd( Fb, Gb );
322            if ( delta > 0 )
323              ok = degree( Db ) < delta;
[4e6908]324        }
325        if ( ! ok )
[afd067]326        {
[661ca5e]327            b.nextpoint();
[afd067]328        }
[c1c3693]329    } while ( ! ok );
330}
331
332static modpk
333enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk )
334{
[d81ed62]335    DEBOUTLN( cerr, "ezgcd: (enlarge bound) F      = " << F );
336    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb     = " << Lb );
337    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db     = " << Db );
338    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb % p = " << mod( Lb, pk.getp() ) );
339    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db % p = " << mod( Db, pk.getp() ) );
[c1c3693]340
341    CanonicalForm limit = power( CanonicalForm(2), degree( Db ) ) *
[4e6908]342        tmax( maxNorm( Lb ), tmax( maxNorm( Db ), maxNorm( F ) ) );
[c1c3693]343    int p = pk.getp();
344    int k = pk.getk();
345    CanonicalForm bound = pk.getpk();
346    while ( bound < limit ) {
[4e6908]347        k++;
348        bound *= p;
[c1c3693]349    }
350    k *= 2;
[d81ed62]351    DEBOUTLN( cerr, "ezgcd: (enlarge bound) newbound = " << power( CanonicalForm( p ), k ) );
[c1c3693]352    return modpk( p, k );
353}
354
355static modpk
356findBound ( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & lcF, const CanonicalForm & lcG, int degF, int degG )
357{
358    CanonicalForm limit = power( CanonicalForm(2), tmin( degF, degG ) ) *
[4e6908]359        gcd( icontent( lcF ), icontent( lcG ) ) * tmin( maxNorm( F ), maxNorm( G ) );
[c1c3693]360    int p, i = 0;
361    do {
[4e6908]362        p = cf_getBigPrime( i );
363        i++;
[c1c3693]364    } while ( mod( lcF, p ).isZero() && mod( lcG, p ).isZero() );
365    CanonicalForm bound = p;
366    i = 1;
367    while ( bound < limit ) {
[4e6908]368        i++;
369        bound *= p;
[c1c3693]370    }
371    return modpk( p, i );
372}
Note: See TracBrowser for help on using the repository browser.