source: git/factory/fac_ezgcd.cc @ a52291

spielwiese
Last change on this file since a52291 was 5b8726d, checked in by Niels Ranosch <ranosch@…>, 12 years ago
FIX: unused function arguments should better not have names as compiler warns about them (=> commented out argument names)
  • Property mode set to 100644
File size: 12.5 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    /*
225     * I am not sure, if these lines are much of use.
226     * Remove?
227     */
228    DEBOUTLN( cerr, "ezgcd: special case" );
229    CanonicalForm Ft, Gt, L, LL, Fb, Gb, Db, Lb, D, Ds, Dt;
230    CFArray DD( 1, 2 ), lcDD( 1, 2 );
231    Variable x = Variable( 1 );
232    bool gcdfound;
233
234    Dt = 1;
235    /// ---> S1
236    DEBOUTLN( cerr, "ezgcdspec: (S1)" );
237    Ft = ezgcd( F, F.deriv( x ) );
238    if ( Ft.isOne() )
239    {
240        // In this case F is squarefree and we came here by bad chance
241        // (means: bad evaluation point).  Try again with another
242        // evaluation point.  Bug fix (?) by JS.  The bad example was
243        // gcd.debug -ocr /+USE_EZGCD/@12/CB
244        //     '(16*B^8-208*B^6*C+927*B^4*C^2-1512*B^2*C^3+432*C^4)'
245        //     '(4*B^7*C^2-50*B^5*C^3+208*B^3*C^4-288*B*C^5)'
246        b.nextpoint();
247        return ezgcd( F, G, b, true );
248    }
249
250    DEBOUTLN( cerr, "ezgcdspec: (S1) done, Ft = " << Ft );
251    L = F / Ft;
252    /// ---> S2
253    DEBOUTLN( cerr, "ezgcdspec: (S2)" );
254
255    L = ezgcd( L, G, b, true );
256    DEBOUTLN( cerr, "ezgcdspec: (S2) done, Ds = " << Ds );
257    Gt = G / L;
258    Ds = L; Dt = L;
259    Lb = b( L ); Gb = b( Gt ); Fb = b( Ft );
260    d = gcd( LC( L, x ), gcd( LC( Ft, x ), LC( Gt, x ) ) );
261    while ( true ) {
262        /// ---> S3
263        DEBOUTLN( cerr, "ezgcdspec: (S3)" );
264        DEBOUTLN( cerr, "ezgcdspec: Fb = " << Fb );
265        DEBOUTLN( cerr, "ezgcdspec: Gb = " << Gb );
266        DD[1] = gcd( Lb, gcd( Fb, Gb ) );
267        /// ---> S4
268        DEBOUTLN( cerr, "ezgcdspec: (S4)" );
269        if ( degree( DD[1] ) == 0 )
270            return Ds;
271        /// ---> S5
272        DEBOUTLN( cerr, "ezgcdspec: (S5)" );
273        DEBOUTLN( cerr, "ezgcdspec: Lb = " << Lb );
274        DEBOUTLN( cerr, "ezgcdspec: Db = " << DD[1] );
275        Db = DD[1];
276        if ( ! (DD[2] = Lb/DD[1]).isOne() ) {
277            DEBOUTLN( cerr, "ezgcdspec: (S55)" );
278            lcDD[2] = LC( L, x );
279            lcDD[1] = d;
280            DD[1] = ( DD[1] * b( d ) ) / lc( DD[1] );
281            DD[2] = ( DD[2] * b( lcDD[2] ) ) / lc( DD[2] );
282            LL = L * d;
283            modpk newbound = enlargeBound( LL, DD[2], DD[1], bound );
284            DEBOUTLN( cerr, "ezgcdspec: begin with lift." );
285            DEBOUTLN( cerr, "ezgcdspec: B     = " << LL );
286            DEBOUTLN( cerr, "ezgcdspec: DD    = " << DD );
287            DEBOUTLN( cerr, "ezgcdspec: lcDD  = " << lcDD );
288            DEBOUTLN( cerr, "ezgcdspec: b     = " << b );
289            DEBOUTLN( cerr, "ezgcdspec: bound = " << bound.getpk() );
290            DEBOUTLN( cerr, "ezgcdspec: lc(B) = " << LC( LL, x ) );
291            DEBOUTLN( cerr, "ezgcdspec: test  = " << b( LL ) - DD[1] * DD[2] );
292            gcdfound = Hensel( LL, DD, lcDD, b, newbound, x );
293            ASSERT( gcdfound, "fatal error in algorithm" );
294            Dt = pp( DD[1] );
295        }
296        DEBOUTLN( cerr, "ezgcdspec: (S7)" );
297        Ds = Ds * Dt;
298        Fb = Fb / Db;
299        Gb = Gb / Db;
300    }
301    // this point is never reached, but to avoid compiler warnings let's return a value
302    return 0;
303#endif
304}
305
306static void
307findeval( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db, REvaluation & b, int delta, int degF, int degG )
308{
309    bool ok;
310    if ( delta != 0 )
311        b.nextpoint();
312    DEBOUTLN( cerr, "ezgcd: (findeval) F = " << F  <<", G="<< G);
313    DEBOUTLN( cerr, "ezgcd: (findeval) degF = " << degF << ", degG="<<degG );
314    do {
315        DEBOUTLN( cerr, "ezgcd: (findeval) b = " << b );
316        Fb = b( F );
317        ok = degree( Fb ) == degF;
318        if ( ok ) {
319            Gb = b( G );
320            ok = degree( Gb ) == degG;
321        }
322
323        if ( ok )
324        {
325            Db = gcd( Fb, Gb );
326            if ( delta > 0 )
327              ok = degree( Db ) < delta;
328        }
329        if ( ! ok )
330        {
331            b.nextpoint();
332        }
333    } while ( ! ok );
334}
335
336static modpk
337enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk )
338{
339    DEBOUTLN( cerr, "ezgcd: (enlarge bound) F      = " << F );
340    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb     = " << Lb );
341    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db     = " << Db );
342    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb % p = " << mod( Lb, pk.getp() ) );
343    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db % p = " << mod( Db, pk.getp() ) );
344
345    CanonicalForm limit = power( CanonicalForm(2), degree( Db ) ) *
346        tmax( maxNorm( Lb ), tmax( maxNorm( Db ), maxNorm( F ) ) );
347    int p = pk.getp();
348    int k = pk.getk();
349    CanonicalForm bound = pk.getpk();
350    while ( bound < limit ) {
351        k++;
352        bound *= p;
353    }
354    k *= 2;
355    DEBOUTLN( cerr, "ezgcd: (enlarge bound) newbound = " << power( CanonicalForm( p ), k ) );
356    return modpk( p, k );
357}
358
359static modpk
360findBound ( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & lcF, const CanonicalForm & lcG, int degF, int degG )
361{
362    CanonicalForm limit = power( CanonicalForm(2), tmin( degF, degG ) ) *
363        gcd( icontent( lcF ), icontent( lcG ) ) * tmin( maxNorm( F ), maxNorm( G ) );
364    int p, i = 0;
365    do {
366        p = cf_getBigPrime( i );
367        i++;
368    } while ( mod( lcF, p ).isZero() && mod( lcG, p ).isZero() );
369    CanonicalForm bound = p;
370    i = 1;
371    while ( bound < limit ) {
372        i++;
373        bound *= p;
374    }
375    return modpk( p, i );
376}
Note: See TracBrowser for help on using the repository browser.