source: git/factory/fac_ezgcd.cc @ e4fe2b

spielwiese
Last change on this file since e4fe2b was e4fe2b, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: Fixed huge BUG in cf_gmp.h CHG: starting to cleanup factory
  • Property mode set to 100644
File size: 12.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id$ */
3
4#include "config.h"
5
6#include "cf_assert.h"
7#include "debug.h"
8
9#include "cf_defs.h"
10#include "canonicalform.h"
11#include "fac_util.h"
12#include "cf_algorithm.h"
13#include "cf_reval.h"
14#include "cf_random.h"
15#include "cf_primes.h"
16#include "fac_distrib.h"
17#include "templates/ftmpl_functions.h"
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 );
41    int degF, degG, delta, t;
42    REvaluation bt;
43    bool gcdfound = false;
44    Variable x = Variable(1);
45    modpk bound;
46
47    DEBINCLEVEL( cerr, "ezgcd" );
48    DEBOUTLN( cerr, "FF = " << FF );
49    DEBOUTLN( cerr, "GG = " << GG );
50    f = content( FF, x ); g = content( GG, x ); d = gcd( f, g );
51    DEBOUTLN( cerr, "f = " << f );
52    DEBOUTLN( cerr, "g = " << g );
53    F = FF / f; G = GG / g;
54    if ( F.isUnivariate() && G.isUnivariate() )
55    {
56        DEBDECLEVEL( cerr, "ezgcd" );
57        if(F.mvar()==G.mvar())
58          d*=gcd(F,G);
59        return d;
60    }
61    else  if ( gcd_test_one( F, G, false ) )
62    {
63        DEBDECLEVEL( cerr, "ezgcd" );
64        return d;
65    }
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 )
73        b = REvaluation( 2, t, IntRandom( 25 ) );
74    while ( ! gcdfound ) {
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 )
87        {
88          DEBDECLEVEL( cerr, "ezgcd" );
89          return d;
90        }
91        /// ---> A4
92        //deltaold = delta;
93        while ( 1 ) {
94            bt = b;
95            findeval( F, G, Fbt, Gbt, Dbt, bt, delta + 1, degF, degG );
96            int dd=degree( Dbt );
97            if ( dd /*degree( Dbt )*/ == 0 )
98            {
99                DEBDECLEVEL( cerr, "ezgcd" );
100                return d;
101            }
102            if ( dd /*degree( Dbt )*/ == delta )
103                break;
104            else  if ( dd /*degree( Dbt )*/ < delta ) {
105                delta = dd /*degree( Dbt )*/;
106                b = bt;
107                Db = Dbt; Fb = Fbt; Gb = Gbt;
108            }
109        }
110        DEBOUTLN( cerr, "now after A4, delta = " << delta );
111        /// ---> A5
112        if ( degF <= degG && delta == degF && fdivides( F, G ) )
113        {
114            DEBDECLEVEL( cerr, "ezgcd" );
115            return d*F;
116        }
117        if ( degG < degF && delta == degG && fdivides( G, F ) )
118        {
119            DEBDECLEVEL( cerr, "ezgcd" );
120            return d*G;
121        }
122        if ( delta != degF && delta != degG ) {
123            bool B_is_F;
124            /// ---> A6
125            CanonicalForm xxx;
126            //if ( gcd( (DD[1] = Fb / Db), Db ) == 1 ) {
127            DD[1] = Fb / Db;
128            xxx= gcd( DD[1], Db );
129            DEBOUTLN( cerr, "gcd((Fb/Db),Db) = " << xxx );
130            DEBOUTLN( cerr, "Fb/Db = " << DD[1] );
131            DEBOUTLN( cerr, "Db = " << Db );
132            if (xxx.inCoeffDomain()) {
133                B = F;
134                DD[2] = Db;
135                lcDD[1] = lcF;
136                lcDD[2] = lcF;
137                B *= lcF;
138                B_is_F=true;
139            }
140            //else  if ( gcd( (DD[1] = Gb / Db), Db ) == 1 ) {
141            else
142            {
143              DD[1] = Gb / Db;
144              xxx=gcd( DD[1], Db );
145              DEBOUTLN( cerr, "gcd((Gb/Db),Db) = " << xxx );
146              DEBOUTLN( cerr, "Gb/Db = " << DD[1] );
147              DEBOUTLN( cerr, "Db = " << Db );
148              if (xxx.inCoeffDomain())
149              {
150                B = G;
151                DD[2] = Db;
152                lcDD[1] = lcG;
153                lcDD[2] = lcG;
154                B *= lcG;
155                B_is_F=false;
156              }
157              else
158              {
159#ifdef DEBUGOUTPUT
160                CanonicalForm dummyres = d * ezgcd_specialcase( F, G, b, bound );
161                DEBDECLEVEL( cerr, "ezgcd" );
162                return dummyres;
163#else
164                return d * ezgcd_specialcase( F, G, b, bound );
165#endif
166              }
167            }
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 );
179
180            if (gcdfound)
181            {
182              CanonicalForm xxx=content(DD[2],Variable(1));
183              CanonicalForm cand=DD[2] / xxx; //content(DD[2],Variable(1));
184#if 0
185              gcdfound= fdivides(cand,G) &&  fdivides(cand,F);
186#else
187              if (B_is_F /*B==F*lcF*/)
188              {
189                DEBOUTLN( cerr, "(test) G: "<<G<<" % gcd:"<<cand<<" -> " << G%cand );
190                gcdfound= fdivides(cand,G);
191              }
192              else
193              {
194                DEBOUTLN( cerr, "(test) F: "<<F<<" % gcd:"<<cand<<" -> " << F%cand);
195                gcdfound= fdivides(cand,F);
196              }
197#endif
198            }
199            /// ---> A8 (gcdfound)
200        }
201        delta++;
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{
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
224    DEBOUTLN( cerr, "ezgcd: special case" );
225    CanonicalForm Ft, Gt, L, LL, Fb, Gb, Db, Lb, D, Ds, Dt;
226    CFArray DD( 1, 2 ), lcDD( 1, 2 );
227    Variable x = Variable( 1 );
228    bool gcdfound;
229
230    Dt = 1;
231    /// ---> S1
232    DEBOUTLN( cerr, "ezgcdspec: (S1)" );
233    Ft = ezgcd( F, F.deriv( x ) );
234    if ( Ft.isOne() )
235    {
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
239        // gcd.debug -ocr /+USE_EZGCD/@12/CB
240        //     '(16*B^8-208*B^6*C+927*B^4*C^2-1512*B^2*C^3+432*C^4)'
241        //     '(4*B^7*C^2-50*B^5*C^3+208*B^3*C^4-288*B*C^5)'
242        b.nextpoint();
243        return ezgcd( F, G, b, true );
244    }
245
246    DEBOUTLN( cerr, "ezgcdspec: (S1) done, Ft = " << Ft );
247    L = F / Ft;
248    /// ---> S2
249    DEBOUTLN( cerr, "ezgcdspec: (S2)" );
250
251    L = ezgcd( L, G, b, true );
252    DEBOUTLN( cerr, "ezgcdspec: (S2) done, Ds = " << Ds );
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 ) {
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;
296    }
297    // this point is never reached, but to avoid compiler warnings let's return a value
298    return 0;
299#endif
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 )
307        b.nextpoint();
308    DEBOUTLN( cerr, "ezgcd: (findeval) F = " << F  <<", G="<< G);
309    DEBOUTLN( cerr, "ezgcd: (findeval) degF = " << degF << ", degG="<<degG );
310    do {
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        }
318
319        if ( ok )
320        {
321            Db = gcd( Fb, Gb );
322            if ( delta > 0 )
323              ok = degree( Db ) < delta;
324        }
325        if ( ! ok )
326        {
327            b.nextpoint();
328        }
329    } while ( ! ok );
330}
331
332static modpk
333enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk )
334{
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() ) );
340
341    CanonicalForm limit = power( CanonicalForm(2), degree( Db ) ) *
342        tmax( maxNorm( Lb ), tmax( maxNorm( Db ), maxNorm( F ) ) );
343    int p = pk.getp();
344    int k = pk.getk();
345    CanonicalForm bound = pk.getpk();
346    while ( bound < limit ) {
347        k++;
348        bound *= p;
349    }
350    k *= 2;
351    DEBOUTLN( cerr, "ezgcd: (enlarge bound) newbound = " << power( CanonicalForm( p ), k ) );
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 ) ) *
359        gcd( icontent( lcF ), icontent( lcG ) ) * tmin( maxNorm( F ), maxNorm( G ) );
360    int p, i = 0;
361    do {
362        p = cf_getBigPrime( i );
363        i++;
364    } while ( mod( lcF, p ).isZero() && mod( lcG, p ).isZero() );
365    CanonicalForm bound = p;
366    i = 1;
367    while ( bound < limit ) {
368        i++;
369        bound *= p;
370    }
371    return modpk( p, i );
372}
Note: See TracBrowser for help on using the repository browser.