source: git/factory/fac_ezgcd.cc @ 7af0e48

fieker-DuValspielwiese
Last change on this file since 7af0e48 was 4e6908, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: even mor bug fixes to ezgcd git-svn-id: file:///usr/local/Singular/svn/trunk@5126 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: fac_ezgcd.cc,v 1.14 2001-01-24 18:09:41 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, deltaold, 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        return d * gcd( F, G );
57    else  if ( gcd_test_one( F, G, false ) )
58        return d;
59    lcF = LC( F, x ); lcG = LC( G, x );
60    lcD = gcd( lcF, lcG );
61    delta = 0;
62    degF = degree( F, x ); degG = degree( G, x );
63    t = tmax( F.level(), G.level() );
64    bound = findBound( F, G, lcF, lcG, degF, degG );
65    if ( ! internal )
66        b = REvaluation( 2, t, IntRandom( 100 ) );
67    while ( ! gcdfound ) {
68        /// ---> A2
69        DEBOUTLN( cerr, "search for evaluation, delta = " << delta );
70        DEBOUTLN( cerr, "F = " << F );
71        DEBOUTLN( cerr, "G = " << G );
72        findeval( F, G, Fb, Gb, Db, b, delta, degF, degG );
73        DEBOUTLN( cerr, "found evaluation b = " << b );
74        DEBOUTLN( cerr, "F(b) = " << Fb );
75        DEBOUTLN( cerr, "G(b) = " << Gb );
76        DEBOUTLN( cerr, "D(b) = " << Db );
77        delta = degree( Db );
78        /// ---> A3
79        if ( delta == 0 )
80            return d;
81        /// ---> A4
82        deltaold = delta + 1;
83        while ( deltaold != delta ) {
84            bt = b;
85            findeval( F, G, Fbt, Gbt, Dbt, bt, delta + 1, degF, degG );
86            if ( degree( Dbt ) == 0 )
87                return d;
88            if ( degree( Dbt ) == delta )
89                deltaold = delta;
90            else  if ( degree( Dbt ) < delta ) {
91                deltaold = delta;
92                delta = degree( Dbt );
93                b = bt;
94                Db = Dbt; Fb = Fbt; Gb = Gbt;
95            }
96        }
97        DEBOUTLN( cerr, "now after A4, delta = " << delta );
98        /// ---> A5
99        if ( degF <= degG && delta == degF && divides( F, G ) )
100            return d*F;
101        if ( degG < degF && delta == degG && divides( G, F ) )
102            return d*G;
103        if ( delta != degF && delta != degG ) {
104            /// ---> A6
105            CanonicalForm xxx;
106            //if ( gcd( (DD[1] = Fb / Db), Db ) == 1 ) {
107            xxx= gcd( (DD[1] = Fb / Db), Db );
108            if (xxx.inCoeffDomain()) {
109                B = F;
110                DD[2] = Db;
111                lcDD[1] = lcF;
112                lcDD[2] = lcF;
113                B *= lcF;
114            }
115            //else  if ( gcd( (DD[1] = Gb / Db), Db ) == 1 ) {
116            else { 
117            xxx=gcd( (DD[1] = Gb / Db), Db );
118            if (xxx.inCoeffDomain()) {
119                B = G;
120                DD[2] = Db;
121                lcDD[1] = lcG;
122                lcDD[2] = lcG;
123                B *= lcG;
124            }
125            else {
126#ifdef DEBUGOUTPUT
127                CanonicalForm dummyres = d * ezgcd_specialcase( F, G, b, bound );
128                DEBDECLEVEL( cerr, "ezgcd" );
129                return dummyres;
130#else
131                return d * ezgcd_specialcase( F, G, b, bound );
132#endif
133            }
134            }
135            /// ---> A7
136            DD[2] = DD[2] * ( b( lcDD[2] ) / lc( DD[2] ) );
137            DD[1] = DD[1] * ( b( lcDD[1] ) / lc( DD[1] ) );
138            bound = enlargeBound( B, DD[2], DD[1], bound );
139            DEBOUTLN( cerr, "(hensel) B    = " << B );
140            DEBOUTLN( cerr, "(hensel) lcB  = " << LC( B, Variable(1) ) );
141            DEBOUTLN( cerr, "(hensel) b(B) = " << b(B) );
142            DEBOUTLN( cerr, "(hensel) DD   = " << DD );
143            DEBOUTLN( cerr, "(hensel) lcDD = " << lcDD );
144            gcdfound = Hensel( B, DD, lcDD, b, bound, x );
145            DEBOUTLN( cerr, "(hensel finished) DD   = " << DD );
146            /// ---> A8 (gcdfound)
147        }
148    }
149    /// ---> A9
150    DEBDECLEVEL( cerr, "ezgcd" );
151    return d * DD[2] / content(DD[2],Variable(1));
152}
153
154static CanonicalForm
155ezgcd_specialcase ( const CanonicalForm & F, const CanonicalForm & G, REvaluation & b, const modpk & bound )
156{
157    DEBOUTLN( cerr, "ezgcd: special case" );
158    CanonicalForm Ft, Gt, L, LL, Fb, Gb, Db, Lb, D, Ds, Dt, d;
159    CFArray DD( 1, 2 ), lcDD( 1, 2 );
160    Variable x = Variable( 1 );
161    bool gcdfound;
162
163    Dt = 1;
164    /// ---> S1
165    DEBOUTLN( cerr, "ezgcdspec: (S1)" );
166    Ft = ezgcd( F, F.deriv( x ) );
167    if ( Ft.isOne() ) {
168        // In this case F is squarefree and we came here by bad chance
169        // (means: bad evaluation point).  Try again with another
170        // evaluation point.  Bug fix (?) by JS.  The bad example was
171        // gcd.debug -ocr /+USE_EZGCD/@12/CB \
172        //     '(16*B^8-208*B^6*C+927*B^4*C^2-1512*B^2*C^3+432*C^4)' \
173        //     '(4*B^7*C^2-50*B^5*C^3+208*B^3*C^4-288*B*C^5)'
174        b.nextpoint();
175        return ezgcd( F, G, b, true );
176    }
177
178    DEBOUTLN( cerr, "ezgcdspec: (S1) done, Ft = " << Ft );
179    L = F / Ft;
180    /// ---> S2
181    DEBOUTLN( cerr, "ezgcdspec: (S2)" );
182
183    L = ezgcd( L, G, b, true );
184    DEBOUTLN( cerr, "ezgcdspec: (S2) done, Ds = " << Ds );
185    Gt = G / L;
186    Ds = L; Dt = L;
187    Lb = b( L ); Gb = b( Gt ); Fb = b( Ft );
188    d = gcd( LC( L, x ), gcd( LC( Ft, x ), LC( Gt, x ) ) );
189    while ( true ) {
190        /// ---> S3
191        DEBOUTLN( cerr, "ezgcdspec: (S3)" );
192        DEBOUTLN( cerr, "ezgcdspec: Fb = " << Fb );
193        DEBOUTLN( cerr, "ezgcdspec: Gb = " << Gb );
194        DD[1] = gcd( Lb, gcd( Fb, Gb ) );
195        /// ---> S4
196        DEBOUTLN( cerr, "ezgcdspec: (S4)" );
197        if ( degree( DD[1] ) == 0 )
198            return Ds;
199        /// ---> S5
200        DEBOUTLN( cerr, "ezgcdspec: (S5)" );
201        DEBOUTLN( cerr, "ezgcdspec: Lb = " << Lb );
202        DEBOUTLN( cerr, "ezgcdspec: Db = " << DD[1] );
203        Db = DD[1];
204        if ( ! (DD[2] = Lb/DD[1]).isOne() ) {
205            DEBOUTLN( cerr, "ezgcdspec: (S55)" );
206            lcDD[2] = LC( L, x );
207            lcDD[1] = d;
208            DD[1] = ( DD[1] * b( d ) ) / lc( DD[1] );
209            DD[2] = ( DD[2] * b( lcDD[2] ) ) / lc( DD[2] );
210            LL = L * d;
211            modpk newbound = enlargeBound( LL, DD[2], DD[1], bound );
212            DEBOUTLN( cerr, "ezgcdspec: begin with lift." );
213            DEBOUTLN( cerr, "ezgcdspec: B     = " << LL );
214            DEBOUTLN( cerr, "ezgcdspec: DD    = " << DD );
215            DEBOUTLN( cerr, "ezgcdspec: lcDD  = " << lcDD );
216            DEBOUTLN( cerr, "ezgcdspec: b     = " << b );
217            DEBOUTLN( cerr, "ezgcdspec: bound = " << bound.getpk() );
218            DEBOUTLN( cerr, "ezgcdspec: lc(B) = " << LC( LL, x ) );
219            DEBOUTLN( cerr, "ezgcdspec: test  = " << b( LL ) - DD[1] * DD[2] );
220            gcdfound = Hensel( LL, DD, lcDD, b, newbound, x );
221            ASSERT( gcdfound, "fatal error in algorithm" );
222            Dt = pp( DD[1] );
223        }
224        DEBOUTLN( cerr, "ezgcdspec: (S7)" );
225        Ds = Ds * Dt;
226        Fb = Fb / Db;
227        Gb = Gb / Db;
228    }
229    // this point is never reached, but to avoid compiler warnings let's return a value
230    return 0;
231}
232
233static void
234findeval( const CanonicalForm & F, const CanonicalForm & G, CanonicalForm & Fb, CanonicalForm & Gb, CanonicalForm & Db, REvaluation & b, int delta, int degF, int degG )
235{
236    bool ok;
237    if ( delta != 0 )
238        b.nextpoint();
239    do {
240        DEBOUTLN( cerr, "ezgcd: (findeval) b = " << b );
241        Fb = b( F );
242        ok = degree( Fb ) == degF;
243        if ( ok ) {
244            Gb = b( G );
245            ok = degree( Gb ) == degG;
246        }
247        if ( ok ) {
248            Db = gcd( Fb, Gb );
249            if ( delta > 0 )
250                ok = degree( Db ) < delta;
251        }
252        if ( ! ok )
253            b.nextpoint();
254    } while ( ! ok );
255}
256
257static modpk
258enlargeBound ( const CanonicalForm & F, const CanonicalForm & Lb, const CanonicalForm & Db, const modpk & pk )
259{
260    DEBOUTLN( cerr, "ezgcd: (enlarge bound) F      = " << F );
261    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb     = " << Lb );
262    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db     = " << Db );
263    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Lb % p = " << mod( Lb, pk.getp() ) );
264    DEBOUTLN( cerr, "ezgcd: (enlarge bound) Db % p = " << mod( Db, pk.getp() ) );
265
266    CanonicalForm limit = power( CanonicalForm(2), degree( Db ) ) *
267        tmax( maxNorm( Lb ), tmax( maxNorm( Db ), maxNorm( F ) ) );
268    int p = pk.getp();
269    int k = pk.getk();
270    CanonicalForm bound = pk.getpk();
271    while ( bound < limit ) {
272        k++;
273        bound *= p;
274    }
275    k *= 2;
276    DEBOUTLN( cerr, "ezgcd: (enlarge bound) newbound = " << power( CanonicalForm( p ), k ) );
277    return modpk( p, k );
278}
279
280static modpk
281findBound ( const CanonicalForm & F, const CanonicalForm & G, const CanonicalForm & lcF, const CanonicalForm & lcG, int degF, int degG )
282{
283    CanonicalForm limit = power( CanonicalForm(2), tmin( degF, degG ) ) *
284        gcd( icontent( lcF ), icontent( lcG ) ) * tmin( maxNorm( F ), maxNorm( G ) );
285    int p, i = 0;
286    do {
287        p = cf_getBigPrime( i );
288        i++;
289    } while ( mod( lcF, p ).isZero() && mod( lcG, p ).isZero() );
290    CanonicalForm bound = p;
291    i = 1;
292    while ( bound < limit ) {
293        i++;
294        bound *= p;
295    }
296    return modpk( p, i );
297}
Note: See TracBrowser for help on using the repository browser.