source: git/factory/fac_multivar.cc @ 6bc4cd

spielwiese
Last change on this file since 6bc4cd was f78374, checked in by Martin Lee <martinlee84@…>, 12 years ago
chg: more changes of squarefree decomposition according to master
  • Property mode set to 100644
File size: 11.0 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#include "timing.h"
9
10#include "cf_defs.h"
11#include "cf_algorithm.h"
12#include "fac_multivar.h"
13#include "fac_univar.h"
14#include "cf_reval.h"
15#include "cf_map.h"
16#include "fac_util.h"
17#include "cf_binom.h"
18#include "cf_iter.h"
19#include "cf_primes.h"
20#include "fac_distrib.h"
21
22void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
23void out_cff(CFFList &L);
24
25TIMING_DEFINE_PRINT(fac_content)
26TIMING_DEFINE_PRINT(fac_findeval)
27TIMING_DEFINE_PRINT(fac_distrib)
28TIMING_DEFINE_PRINT(fac_hensel)
29
30static CFArray
31conv_to_factor_array( const CFFList & L )
32{
33    int n;
34    CFFListIterator I = L;
35    bool negate = false;
36
37    if ( ! I.hasItem() )
38        n = 0;
39    else  if ( I.getItem().factor().inBaseDomain() ) {
40        negate = I.getItem().factor().sign() < 0;
41        I++;
42        n = L.length();
43    }
44    else
45        n = L.length() + 1;
46    CFFListIterator J = I;
47    while ( J.hasItem() ) {
48        n += J.getItem().exp() - 1;
49        J++;
50    }
51    CFArray result( 1, n-1 );
52    int i, j, k;
53    i = 1;
54    while ( I.hasItem() ) {
55        k = I.getItem().exp();
56        for ( j = 1; j <= k; j++ ) {
57            result[i] = I.getItem().factor();
58            i++;
59        }
60        I++;
61    }
62    if ( negate )
63        result[1] = -result[1];
64    return result;
65}
66
67static modpk
68coeffBound ( const CanonicalForm & f, int p )
69{
70    int * degs = degrees( f );
71    int M = 0, i, k = f.level();
72    for ( i = 1; i <= k; i++ )
73        M += degs[i];
74    CanonicalForm b = 2 * maxNorm( f ) * power( CanonicalForm( 3 ), M );
75    CanonicalForm B = p;
76    k = 1;
77    while ( B < b ) {
78        B *= p;
79        k++;
80    }
81    return modpk( p, k );
82}
83
84// static bool
85// nonDivisors ( CanonicalForm omega, CanonicalForm delta, const CFArray & F, CFArray & d )
86// {
87//     DEBOUTLN( cerr, "nondivisors omega = " << omega );
88//     DEBOUTLN( cerr, "nondivisors delta = " << delta );
89//     DEBOUTLN( cerr, "nondivisors F = " << F );
90//     CanonicalForm q, r;
91//     int k = F.size();
92//     d = CFArray( 0, k );
93//     d[0] = delta * omega;
94//     for ( int i = 1; i <= k; i++ ) {
95//         q = abs(F[i]);
96//         for ( int j = i-1; j >= 0; j-- ) {
97//             r = d[j];
98//             do {
99//                 r = gcd( r, q );
100//                 q = q / r;
101//             } while ( r != 1 );
102//             if ( q == 1 )
103//                 return false;
104//         }
105//         d[i] = q;
106//     }
107//     return true;
108// }
109
110static void
111findEvaluation ( const CanonicalForm & U, const CanonicalForm & V, const CanonicalForm & omega, const CFFList & F, Evaluation & A, CanonicalForm & U0, CanonicalForm & delta, CFArray & D, int r )
112{
113    DEBINCLEVEL( cerr, "findEvaluation" );
114    CanonicalForm Vn;
115    CFFListIterator I;
116    int j;
117    bool found = false;
118    CFArray FF = CFArray( 1, F.length() );
119    if ( r > 0 )
120        A.nextpoint();
121    while ( ! found )
122    {
123        Vn = A( V );
124        if ( Vn != 0 )
125        {
126            U0 = A( U );
127            DEBOUTLN( cerr, "U0 = " << U0 );
128            if ( isSqrFree( U0 ) )
129            {
130                delta = content( U0 );
131                DEBOUTLN( cerr, "content( U0 ) = " << delta );
132                for ( I = F, j = 1; I.hasItem(); I++, j++ )
133                    FF[j] = A( I.getItem().factor() );
134                found = nonDivisors( omega, delta, FF, D );
135            }
136            else
137            {
138                DEBOUTLN( cerr, "not sqrfree : " << sqrFree( U0 ) );
139            }
140        }
141        if ( ! found )
142            A.nextpoint();
143    }
144    DEBDECLEVEL( cerr, "findEvaluation" );
145}
146
147#ifdef HAVE_NTL
148int prime_number=0;
149void find_good_prime(const CanonicalForm &f, int &start)
150{
151  if (! f.inBaseDomain() )
152  {
153    CFIterator i = f;
154    for(;;)
155    {
156      if  ( i.hasTerms() )
157      {
158        find_good_prime(i.coeff(),start);
159        if (0==cf_getSmallPrime(start)) return;
160        if((i.exp()!=0) && ((i.exp() % cf_getSmallPrime(start))==0))
161        {
162          start++;
163          i=f;
164        }
165        else  i++;
166      }
167      else break;
168    }
169  }
170  else
171  {
172    if (f.inZ())
173    {
174      if (0==cf_getSmallPrime(start)) return;
175      while((!f.isZero()) && (mod(f,cf_getSmallPrime(start))==0))
176      {
177        start++;
178        if (0==cf_getSmallPrime(start)) return;
179      }
180    }
181/* should not happen!
182    else if (f.inQ())
183    {
184      while((f.den()!=0) && (mod(f.den(),cf_getSmallPrime(start))==0))
185      {
186        start++;
187      }
188      while((f.num()!=0) && (mod(f.num(),cf_getSmallPrime(start))==0))
189      {
190        start++;
191      }
192    }
193    else
194          cout <<"??"<< f <<"\n";
195*/
196  }
197}
198#endif
199
200static CFArray ZFactorizeMulti ( const CanonicalForm & arg )
201{
202    DEBINCLEVEL( cerr, "ZFactorizeMulti" );
203    CFMap M;
204    CanonicalForm UU, U = compress( arg, M );
205    CanonicalForm delta, omega, V = LC( U, 1 );
206    int t = U.level();
207    CFFList F = factorize( V );
208    CFFListIterator I, J;
209    CFArray G, lcG, D;
210    int i, j, r, maxdeg;
211    REvaluation A( 2, t, IntRandom( 50 ) );
212    CanonicalForm U0;
213    CanonicalForm ft, ut, gt, d;
214    modpk b;
215    bool negate = false;
216
217    DEBOUTLN( cerr, "-----------------------------------------------------" );
218    DEBOUTLN( cerr, "trying to factorize U = " << U );
219    DEBOUTLN( cerr, "U is a polynomial of level = " << arg.level() );
220    DEBOUTLN( cerr, "U will be factorized with respect to variable " << Variable(1) );
221    DEBOUTLN( cerr, "the leading coefficient of U with respect to that variable is " << V );
222    DEBOUTLN( cerr, "which is factorized as " << F );
223
224    maxdeg = 0;
225    for ( i = 2; i <= t; i++ )
226    {
227        j = U.degree( Variable( i ) );
228        if ( j > maxdeg ) maxdeg = j;
229    }
230
231    if ( F.getFirst().factor().inCoeffDomain() )
232    {
233        omega = F.getFirst().factor();
234        F.removeFirst();
235        if ( omega < 0 )
236        {
237            negate = true;
238            omega = -omega;
239            U = -U;
240        }
241    }
242    else
243        omega = 1;
244
245    bool goodeval = false;
246    r = 0;
247
248//    for ( i = 0; i < 10*t; i++ )
249//        A.nextpoint();
250
251    while ( ! goodeval )
252    {
253        TIMING_START(fac_findeval);
254        findEvaluation( U, V, omega, F, A, U0, delta, D, r );
255        TIMING_END(fac_findeval);
256        DEBOUTLN( cerr, "the evaluation point to reduce to an univariate problem is " << A );
257        DEBOUTLN( cerr, "corresponding delta = " << delta );
258        DEBOUTLN( cerr, "              omega = " << omega );
259        DEBOUTLN( cerr, "              D     = " << D );
260        DEBOUTLN( cerr, "now factorize the univariate polynomial " << U0 );
261        G = conv_to_factor_array( factorize( U0, false ) );
262        DEBOUTLN( cerr, "which factorizes into " << G );
263        #ifdef HAVE_NTL
264        {
265          int i=prime_number;
266          find_good_prime(arg,i);
267          find_good_prime(U0,i);
268          find_good_prime(U,i);
269          int p=cf_getSmallPrime(i);
270          //printf("found:p=%d (%d)\n",p,i);
271          if (p==0)
272          {
273            return conv_to_factor_array(CFFactor(arg,1));
274            //printf("out of primes - switch ot non-NTL\n");
275          }
276          else if (((i==0)||(i!=prime_number)))
277          {
278            b = coeffBound( U, p );
279            prime_number=i;
280          }
281          // p!=0:
282          modpk bb=coeffBound(U0,p);
283          if (bb.getk() > b.getk() ) b=bb;
284          bb=coeffBound(arg,p);
285          if (bb.getk() > b.getk() ) b=bb;
286        }
287        #else
288        b = coeffBound( U, getZFacModulus().getp() );
289        if ( getZFacModulus().getpk() > b.getpk() )
290            b = getZFacModulus();
291        #endif
292        //printf("p=%d, k=%d\n",b.getp(),b.getk());
293        DEBOUTLN( cerr, "the coefficient bound of the factors of U is " << b.getpk() );
294
295        r = G.size();
296        lcG = CFArray( 1, r );
297        UU = U;
298        DEBOUTLN( cerr, "now trying to distribute the leading coefficients ..." );
299        TIMING_START(fac_distrib);
300        goodeval = distributeLeadingCoeffs( UU, G, lcG, F, D, delta, omega, A, r );
301        TIMING_END(fac_distrib);
302#ifdef DEBUGOUTPUT
303        if ( goodeval )
304        {
305            DEBOUTLN( cerr, "the univariate factors after distribution are " << G );
306            DEBOUTLN( cerr, "the distributed leading coeffs are " << lcG );
307            DEBOUTLN( cerr, "U may have changed and is now " << UU );
308            DEBOUTLN( cerr, "which has leading coefficient " << LC( UU, Variable(1) ) );
309
310            if ( LC( UU, Variable(1) ) != prod( lcG ) || A(UU) != prod( G ) )
311            {
312                DEBOUTLN( cerr, "!!! distribution was not correct !!!" );
313                DEBOUTLN( cerr, "product of leading coeffs is " << prod( lcG ) );
314                DEBOUTLN( cerr, "product of univariate factors is " << prod( G ) );
315                DEBOUTLN( cerr, "the new U is evaluated as " << A(UU) );
316            }
317            else
318                DEBOUTLN( cerr, "leading coeffs correct" );
319        }
320        else
321        {
322            DEBOUTLN( cerr, "we have found a bad evaluation point" );
323        }
324#endif
325        if ( goodeval )
326        {
327            TIMING_START(fac_hensel);
328            goodeval = Hensel( UU, G, lcG, A, b, Variable(1) );
329            TIMING_END(fac_hensel);
330        }
331    }
332    for ( i = 1; i <= r; i++ )
333    {
334        G[i] /= icontent( G[i] );
335        G[i] = M(G[i]);
336    }
337    // negate noch beachten !
338    if ( negate )
339        G[1] = -G[1];
340    DEBDECLEVEL( cerr, "ZFactorMulti" );
341    return G;
342}
343
344CFFList ZFactorizeMultivariate ( const CanonicalForm & f, bool issqrfree )
345{
346    CFFList G, F, R;
347    CFArray GG;
348    CFFListIterator i, j;
349    CFMap M;
350    CanonicalForm g, cont;
351    Variable v1, vm;
352    int k, m, n;
353
354    v1 = Variable(1);
355    if ( issqrfree )
356        F = CFFactor( f, 1 );
357    else
358        F = sqrFree( f );
359
360    for ( i = F; i.hasItem(); i++ )
361    {
362        if ( i.getItem().factor().inCoeffDomain() )
363            R.append( CFFactor( i.getItem().factor(), i.getItem().exp() ) );
364        else
365        {
366            TIMING_START(fac_content);
367            g = compress( i.getItem().factor(), M );
368            // now after compress g contains Variable(1)
369            vm = g.mvar();
370            g = swapvar( g, v1, vm );
371            cont = content( g );
372            g = swapvar( g / cont, v1, vm );
373            cont = swapvar( cont, v1, vm );
374            n = i.getItem().exp();
375            TIMING_END(fac_content);
376            DEBOUTLN( cerr, "now after content ..." );
377            if ( g.isUnivariate() )
378            {
379                G = factorize( g, true );
380                for ( j = G; j.hasItem(); j++ )
381                    if ( ! j.getItem().factor().isOne() )
382                        R.append( CFFactor( M( j.getItem().factor() ), n ) );
383            }
384            else
385            {
386                GG = ZFactorizeMulti( g );
387                m = GG.max();
388                for ( k = GG.min(); k <= m; k++ )
389                    if ( ! GG[k].isOne() )
390                        R.append( CFFactor( M( GG[k] ), n ) );
391            }
392            G = factorize( cont, true );
393            for ( j = G; j.hasItem(); j++ )
394                if ( ! j.getItem().factor().isOne() )
395                    R.append( CFFactor( M( j.getItem().factor() ), n ) );
396        }
397    }
398    return R;
399}
Note: See TracBrowser for help on using the repository browser.