source: git/factory/fac_multivar.cc @ 4a7a45

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