source: git/factory/fac_ezgcd.cc @ 33d8725

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