source: git/factory/fac_multivar.cc @ 0c4a34b

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