source: git/factory/fac_ezgcd.cc @ 179ce15

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