source: git/factory/fac_ezgcd.cc @ c92316

spielwiese
Last change on this file since c92316 was c92316, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: code cleanup git-svn-id: file:///usr/local/Singular/svn/trunk@7689 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.3 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: fac_ezgcd.cc,v 1.16 2005-01-25 13:20:33 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            xxx= gcd( (DD[1] = Fb / Db), Db );
126            if (xxx.inCoeffDomain()) {
127                B = F;
128                DD[2] = Db;
129                lcDD[1] = lcF;
130                lcDD[2] = lcF;
131                B *= lcF;
132            }
133            //else  if ( gcd( (DD[1] = Gb / Db), Db ) == 1 ) {
134            else { 
135            xxx=gcd( (DD[1] = Gb / Db), Db );
136            if (xxx.inCoeffDomain()) {
137                B = G;
138                DD[2] = Db;
139                lcDD[1] = lcG;
140                lcDD[2] = lcG;
141                B *= lcG;
142            }
143            else {
144#ifdef DEBUGOUTPUT
145                CanonicalForm dummyres = d * ezgcd_specialcase( F, G, b, bound );
146                DEBDECLEVEL( cerr, "ezgcd" );
147                return dummyres;
148#else
149                return d * ezgcd_specialcase( F, G, b, bound );
150#endif
151            }
152            }
153            /// ---> A7
154            DD[2] = DD[2] * ( b( lcDD[2] ) / lc( DD[2] ) );
155            DD[1] = DD[1] * ( b( lcDD[1] ) / lc( DD[1] ) );
156            bound = enlargeBound( B, DD[2], DD[1], bound );
157            DEBOUTLN( cerr, "(hensel) B    = " << B );
158            DEBOUTLN( cerr, "(hensel) lcB  = " << LC( B, Variable(1) ) );
159            DEBOUTLN( cerr, "(hensel) b(B) = " << b(B) );
160            DEBOUTLN( cerr, "(hensel) DD   = " << DD );
161            DEBOUTLN( cerr, "(hensel) lcDD = " << lcDD );
162            gcdfound = Hensel( B, DD, lcDD, b, bound, x );
163            DEBOUTLN( cerr, "(hensel finished) DD   = " << DD );
164           
165            if (gcdfound)
166            {
167              CanonicalForm cand=DD[2] / content(DD[2],Variable(1));
168              if (B==F)
169              {
170                DEBOUTLN( cerr, "(test) G: "<<G<<" % gcd:"<<cand<<" -> " << G%cand );
171                gcdfound= divides(cand,G);
172              }
173              else
174              { 
175                DEBOUTLN( cerr, "(test) F: "<<F<<" % gcd:"<<cand<<" -> " << F%cand);
176                gcdfound= divides(cand,F);
177              }
178            }
179            /// ---> A8 (gcdfound)
180        }
181    }
182    /// ---> A9
183    DEBDECLEVEL( cerr, "ezgcd" );
184    return d * DD[2] / content(DD[2],Variable(1));
185}
186
187static CanonicalForm
188ezgcd_specialcase ( const CanonicalForm & F, const CanonicalForm & G, REvaluation & b, const modpk & bound )
189{
190    DEBOUTLN( cerr, "ezgcd: special case" );
191    CanonicalForm Ft, Gt, L, LL, Fb, Gb, Db, Lb, D, Ds, Dt, d;
192    CFArray DD( 1, 2 ), lcDD( 1, 2 );
193    Variable x = Variable( 1 );
194    bool gcdfound;
195
196    Dt = 1;
197    /// ---> S1
198    DEBOUTLN( cerr, "ezgcdspec: (S1)" );
199    Ft = ezgcd( F, F.deriv( x ) );
200    if ( Ft.isOne() ) {
201        // In this case F is squarefree and we came here by bad chance
202        // (means: bad evaluation point).  Try again with another
203        // evaluation point.  Bug fix (?) by JS.  The bad example was
204        // gcd.debug -ocr /+USE_EZGCD/@12/CB \
205        //     '(16*B^8-208*B^6*C+927*B^4*C^2-1512*B^2*C^3+432*C^4)' \
206        //     '(4*B^7*C^2-50*B^5*C^3+208*B^3*C^4-288*B*C^5)'
207        b.nextpoint();
208        return ezgcd( F, G, b, true );
209    }
210
211    DEBOUTLN( cerr, "ezgcdspec: (S1) done, Ft = " << Ft );
212    L = F / Ft;
213    /// ---> S2
214    DEBOUTLN( cerr, "ezgcdspec: (S2)" );
215
216    L = ezgcd( L, G, b, true );
217    DEBOUTLN( cerr, "ezgcdspec: (S2) done, Ds = " << Ds );
218    Gt = G / L;
219    Ds = L; Dt = L;
220    Lb = b( L ); Gb = b( Gt ); Fb = b( Ft );
221    d = gcd( LC( L, x ), gcd( LC( Ft, x ), LC( Gt, x ) ) );
222    while ( true ) {
223        /// ---> S3
224        DEBOUTLN( cerr, "ezgcdspec: (S3)" );
225        DEBOUTLN( cerr, "ezgcdspec: Fb = " << Fb );
226        DEBOUTLN( cerr, "ezgcdspec: Gb = " << Gb );
227        DD[1] = gcd( Lb, gcd( Fb, Gb ) );
228        /// ---> S4
229        DEBOUTLN( cerr, "ezgcdspec: (S4)" );
230        if ( degree( DD[1] ) == 0 )
231            return Ds;
232        /// ---> S5
233        DEBOUTLN( cerr, "ezgcdspec: (S5)" );
234        DEBOUTLN( cerr, "ezgcdspec: Lb = " << Lb );
235        DEBOUTLN( cerr, "ezgcdspec: Db = " << DD[1] );
236        Db = DD[1];
237        if ( ! (DD[2] = Lb/DD[1]).isOne() ) {
238            DEBOUTLN( cerr, "ezgcdspec: (S55)" );
239            lcDD[2] = LC( L, x );
240            lcDD[1] = d;
241            DD[1] = ( DD[1] * b( d ) ) / lc( DD[1] );
242            DD[2] = ( DD[2] * b( lcDD[2] ) ) / lc( DD[2] );
243            LL = L * d;
244            modpk newbound = enlargeBound( LL, DD[2], DD[1], bound );
245            DEBOUTLN( cerr, "ezgcdspec: begin with lift." );
246            DEBOUTLN( cerr, "ezgcdspec: B     = " << LL );
247            DEBOUTLN( cerr, "ezgcdspec: DD    = " << DD );
248            DEBOUTLN( cerr, "ezgcdspec: lcDD  = " << lcDD );
249            DEBOUTLN( cerr, "ezgcdspec: b     = " << b );
250            DEBOUTLN( cerr, "ezgcdspec: bound = " << bound.getpk() );
251            DEBOUTLN( cerr, "ezgcdspec: lc(B) = " << LC( LL, x ) );
252            DEBOUTLN( cerr, "ezgcdspec: test  = " << b( LL ) - DD[1] * DD[2] );
253            gcdfound = Hensel( LL, DD, lcDD, b, newbound, x );
254            ASSERT( gcdfound, "fatal error in algorithm" );
255            Dt = pp( DD[1] );
256        }
257        DEBOUTLN( cerr, "ezgcdspec: (S7)" );
258        Ds = Ds * Dt;
259        Fb = Fb / Db;
260        Gb = Gb / Db;
261    }
262    // this point is never reached, but to avoid compiler warnings let's return a value
263    return 0;
264}
265
266static void
267findeval( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db, REvaluation & b, int delta, int degF, int degG )
268{
269    int i;
270    bool ok;
271    if ( delta != 0 )
272        b.nextpoint();
273    DEBOUTLN( cerr, "ezgcd: (findeval) F = " << F  <<", G="<< G);
274    DEBOUTLN( cerr, "ezgcd: (findeval) degF = " << degF << ", degG="<<degG );
275    do {
276        DEBOUTLN( cerr, "ezgcd: (findeval) b = " << b );
277        Fb = b( F );
278        ok = degree( Fb ) == degF;
279        if ( ok ) {
280            Gb = b( G );
281            ok = degree( Gb ) == degG;
282        }
283       
284        if ( ok )
285        {   
286            Db = gcd( Fb, Gb );
287            if ( delta > 0 )
288              ok = degree( Db ) < delta;
289        }
290        if ( ! ok )
291            b.nextpoint();
292    } while ( ! ok );
293}
294
295static modpk
296enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk )
297{
298    DEBOUTLN( cerr, "ezgcd: (enlarge bound) F      = " << F );
299    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb     = " << Lb );
300    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db     = " << Db );
301    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb % p = " << mod( Lb, pk.getp() ) );
302    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db % p = " << mod( Db, pk.getp() ) );
303
304    CanonicalForm limit = power( CanonicalForm(2), degree( Db ) ) *
305        tmax( maxNorm( Lb ), tmax( maxNorm( Db ), maxNorm( F ) ) );
306    int p = pk.getp();
307    int k = pk.getk();
308    CanonicalForm bound = pk.getpk();
309    while ( bound < limit ) {
310        k++;
311        bound *= p;
312    }
313    k *= 2;
314    DEBOUTLN( cerr, "ezgcd: (enlarge bound) newbound = " << power( CanonicalForm( p ), k ) );
315    return modpk( p, k );
316}
317
318static modpk
319findBound ( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & lcF, const CanonicalForm & lcG, int degF, int degG )
320{
321    CanonicalForm limit = power( CanonicalForm(2), tmin( degF, degG ) ) *
322        gcd( icontent( lcF ), icontent( lcG ) ) * tmin( maxNorm( F ), maxNorm( G ) );
323    int p, i = 0;
324    do {
325        p = cf_getBigPrime( i );
326        i++;
327    } while ( mod( lcF, p ).isZero() && mod( lcG, p ).isZero() );
328    CanonicalForm bound = p;
329    i = 1;
330    while ( bound < limit ) {
331        i++;
332        bound *= p;
333    }
334    return modpk( p, i );
335}
Note: See TracBrowser for help on using the repository browser.