source: git/factory/facFqFactorize.cc @ 09723d

spielwiese
Last change on this file since 09723d was 09723d, checked in by Martin Lee <martinlee84@…>, 13 years ago
better way to recover factors git-svn-id: file:///usr/local/Singular/svn/trunk@14383 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 70.7 KB
Line 
1/*****************************************************************************\
2 * Computer Algebra System SINGULAR
3\*****************************************************************************/
4/** @file facFqFactorize.cc
5 *
6 * This file implements functions for factoring a multivariate polynomial over
7 * a finite field.
8 *
9 * ABSTRACT: "Efficient Multivariate Factorization over Finite Fields" by
10 * L. Bernardin & M. Monagon. Precomputation of leading coefficients is
11 * described in "Sparse Hensel lifting" by E. Kaltofen
12 *
13 * @author Martin Lee
14 *
15 * @internal @version \$Id$
16 *
17 **/
18/*****************************************************************************/
19
20#include <config.h>
21
22#include "assert.h"
23#include "debug.h"
24#include "timing.h"
25
26#include "facFqFactorizeUtil.h"
27#include "facFqFactorize.h"
28#include "cf_random.h"
29#include "facHensel.h"
30#include "cf_gcd_smallp.h"
31#include "cf_map_ext.h"
32#include "algext.h"
33
34#ifdef HAVE_NTL
35#include <NTL/ZZ_pEX.h>
36#include "NTLconvert.h"
37
38TIMING_DEFINE_PRINT(fac_bi_factorizer);
39TIMING_DEFINE_PRINT(fac_hensel_lift);
40TIMING_DEFINE_PRINT(fac_factor_recombination);
41
42static inline
43CanonicalForm
44listGCD (const CFList& L);
45
46static inline
47CanonicalForm
48myContent (const CanonicalForm& F)
49{
50  CanonicalForm G= swapvar (F, F.mvar(), Variable (1));
51  CFList L;
52  for (CFIterator i= G; i.hasTerms(); i++)
53    L.append (i.coeff());
54  if (L.length() == 2)
55    return swapvar (gcd (L.getFirst(), L.getLast()), F.mvar(), Variable (1));
56  if (L.length() == 1)
57    return LC (F, Variable (1));
58  return swapvar (listGCD (L), F.mvar(), Variable (1));
59}
60
61static inline
62CanonicalForm
63listGCD (const CFList& L)
64{
65  if (L.length() == 1)
66    return L.getFirst();
67  if (L.length() == 2)
68    return gcd (L.getFirst(), L.getLast());
69  else
70  {
71    CFList lHi, lLo;
72    CanonicalForm resultHi, resultLo;
73    int length= L.length()/2;
74    int j= 0;
75    for (CFListIterator i= L; j < length; i++, j++)
76      lHi.append (i.getItem());
77    lLo= Difference (L, lHi);
78    resultHi= listGCD (lHi);
79    resultLo= listGCD (lLo);
80    if (resultHi.isOne() || resultLo.isOne())
81      return 1;
82    return gcd (resultHi, resultLo);
83  }
84}
85
86static inline
87CanonicalForm
88myContent (const CanonicalForm& F, const Variable& x)
89{
90  if (degree (F, x) <= 0)
91    return 1;
92  CanonicalForm G= F;
93  bool swap= false;
94  if (x != F.mvar())
95  {
96    swap= true;
97    G= swapvar (F, x, F.mvar());
98  }
99  CFList L;
100  Variable alpha;
101  for (CFIterator i= G; i.hasTerms(); i++)
102    L.append (i.coeff());
103  if (L.length() == 2)
104  {
105    if (swap)
106      return swapvar (gcd (L.getFirst(), L.getLast()), F.mvar(), x);
107    else
108      return gcd (L.getFirst(), L.getLast());
109  }
110  if (L.length() == 1)
111  {
112    return LC (F, x);
113  }
114  if (swap)
115    return swapvar (listGCD (L), F.mvar(), x);
116  else
117    return listGCD (L);
118}
119
120CanonicalForm myCompress (const CanonicalForm& F, CFMap& N)
121{
122  int n= F.level();
123  int * degsf= new int [n + 1];
124  int ** swap;
125  swap= new int* [n + 1];
126  for (int i= 0; i <= n; i++)
127  {
128    degsf[i]= 0;
129    swap [i]= new int [2];
130    swap [i] [0]= 0;
131    swap [i] [1]= 0;
132  }
133  int i= 1;
134  n= 1;
135  degsf= degrees (F, degsf);
136
137  CanonicalForm result= F;
138  while ( i <= F.level() )
139  {
140    while( degsf[i] == 0 ) i++;
141    swap[n][0]= i;
142    swap[n][1]= degsf[i];
143    if (i != n)
144      result= swapvar (result, Variable (n), Variable(i));
145    n++; i++;
146  }
147
148  int buf1, buf2;
149  n--;
150
151  for (i= 1; i < n; i++)
152  {
153    for (int j= 1; j < n - i + 1; j++)
154    {
155      if (swap[j][1] < swap[j + 1][1])
156      {
157        buf1= swap [j + 1] [0];
158        buf2= swap [j + 1] [1];
159        swap[j + 1] [0]= swap[j] [0];
160        swap[j + 1] [1]= swap[j] [1];
161        swap[j][0]= buf1;
162        swap[j][1]= buf2;
163        result= swapvar (result, Variable (j + 1), Variable (j));
164      }
165    }
166  }
167
168  for (i= n; i > 0; i--)
169  {
170    if (i != swap[i] [0])
171      N.newpair (Variable (i), Variable (swap[i] [0]));
172  }
173
174  for (i= 0; i <= n; i++)
175    delete [] swap[i];
176  delete [] swap;
177
178  delete [] degsf;
179
180  return result;
181}
182
183CFList
184extFactorRecombination (const CFList& factors, const CanonicalForm& F,
185                        const CFList& M, const ExtensionInfo& info,
186                        const CFList& evaluation)
187{
188  Variable alpha= info.getAlpha();
189  Variable beta= info.getBeta();
190  CanonicalForm gamma= info.getGamma();
191  CanonicalForm delta= info.getDelta();
192  int k= info.getGFDegree();
193  CFList source, dest;
194  if (factors.length() == 1)
195  {
196    CanonicalForm buf= reverseShift (F, evaluation);
197    return CFList (mapDown (buf, info, source, dest));
198  }
199  if (factors.length() < 1)
200    return CFList();
201
202  int degMipoBeta= 1;
203  if (!k && beta.level() != 1)
204    degMipoBeta= degree (getMipo (beta));
205
206  CFList T, S;
207  T= factors;
208
209  int s= 1;
210  CFList result;
211  CanonicalForm buf;
212
213  buf= F;
214
215  CanonicalForm g, LCBuf= LC (buf, Variable (1));
216  CanonicalForm buf2, quot;
217  int * v= new int [T.length()];
218  for (int i= 0; i < T.length(); i++)
219    v[i]= 0;
220  bool noSubset= false;
221  CFArray TT;
222  TT= copy (factors);
223  bool recombination= false;
224  bool trueFactor= false;
225  while (T.length() >= 2*s)
226  {
227    while (noSubset == false)
228    {
229      if (T.length() == s)
230      {
231        delete [] v;
232        if (recombination)
233        {
234          T.insert (LCBuf);
235          g= prodMod (T, M);
236          T.removeFirst();
237          result.append (g/myContent (g));
238          g= reverseShift (g, evaluation);
239          g /= Lc (g);
240          appendTestMapDown (result, g, info, source, dest);
241          return result;
242        }
243        else
244        {
245          buf= reverseShift (buf, evaluation);
246          return CFList (buf);
247        }
248      }
249
250      S= subset (v, s, TT, noSubset);
251      if (noSubset) break;
252
253      S.insert (LCBuf);
254      g= prodMod (S, M);
255      S.removeFirst();
256      g /= myContent (g);
257      if (fdivides (g, buf, quot))
258      {
259        buf2= reverseShift (g, evaluation);
260        buf2 /= Lc (buf2);
261        if (!k && beta == Variable (1))
262        {
263          if (degree (buf2, alpha) < degMipoBeta)
264          {
265            appendTestMapDown (result, buf2, info, source, dest);
266            buf= quot;
267            LCBuf= LC (buf, Variable (1));
268            recombination= true;
269            trueFactor= true;
270          }
271        }
272        else
273        {
274          if (!isInExtension (buf2, gamma, k, delta, source, dest))
275          {
276            appendTestMapDown (result, buf2, info, source, dest);
277            buf /= g;
278            LCBuf= LC (buf, Variable (1));
279            recombination= true;
280            trueFactor= true;
281          }
282        }
283
284        if (trueFactor)
285        {
286          T= Difference (T, S);
287
288          if (T.length() < 2*s || T.length() == s)
289          {
290            buf= reverseShift (buf, evaluation);
291            buf /= Lc (buf);
292            appendTestMapDown (result, buf, info, source, dest);
293            delete [] v;
294            return result;
295          }
296          trueFactor= false;
297          TT= copy (T);
298          indexUpdate (v, s, T.length(), noSubset);
299          if (noSubset) break;
300        }
301      }
302    }
303    s++;
304    if (T.length() < 2*s || T.length() == s)
305    {
306      buf= reverseShift (buf, evaluation);
307      appendTestMapDown (result, buf, info, source, dest);
308      delete [] v;
309      return result;
310    }
311    for (int i= 0; i < T.length(); i++)
312      v[i]= 0;
313    noSubset= false;
314  }
315  if (T.length() < 2*s)
316  {
317    buf= reverseShift (F, evaluation);
318    appendMapDown (result, buf, info, source, dest);
319  }
320
321  delete [] v;
322  return result;
323}
324
325CFList
326factorRecombination (const CanonicalForm& F, const CFList& factors,
327                     const CFList& M)
328{
329  if (factors.length() == 1)
330    return CFList(F);
331  if (factors.length() < 1)
332    return CFList();
333
334  CFList T, S;
335
336  T= factors;
337
338  int s= 1;
339  CFList result;
340  CanonicalForm LCBuf= LC (F, Variable (1));
341  CanonicalForm g, buf= F;
342  int * v= new int [T.length()];
343  for (int i= 0; i < T.length(); i++)
344    v[i]= 0;
345  bool noSubset= false;
346  CFArray TT;
347  TT= copy (factors);
348  Variable y= F.level() - 1;
349  bool recombination= false;
350  CanonicalForm h, quot;
351  while (T.length() >= 2*s)
352  {
353    while (noSubset == false)
354    {
355      if (T.length() == s)
356      {
357        delete [] v;
358        if (recombination)
359        {
360          T.insert (LC (buf));
361          g= prodMod (T, M);
362          result.append (g/myContent (g));
363          return result;
364        }
365        else
366          return CFList (F);
367      }
368      S= subset (v, s, TT, noSubset);
369      if (noSubset) break;
370      S.insert (LCBuf);
371      g= prodMod (S, M);
372      S.removeFirst();
373      g /= myContent (g);
374      if (fdivides (g, buf, quot))
375      {
376        recombination= true;
377        result.append (g);
378        buf= quot;
379        LCBuf= LC (buf, Variable(1));
380        T= Difference (T, S);
381        if (T.length() < 2*s || T.length() == s)
382        {
383          result.append (buf);
384          delete [] v;
385          return result;
386        }
387        TT= copy (T);
388        indexUpdate (v, s, T.length(), noSubset);
389        if (noSubset) break;
390      }
391    }
392    s++;
393    if (T.length() < 2*s || T.length() == s)
394    {
395      result.append (buf);
396      delete [] v;
397      return result;
398    }
399    for (int i= 0; i < T.length(); i++)
400      v[i]= 0;
401    noSubset= false;
402  }
403  if (T.length() < 2*s)
404    result.append (F);
405
406  delete [] v;
407  return result;
408}
409
410int
411liftBoundAdaption (const CanonicalForm& F, const CFList& factors, bool&
412                   success, const int deg, const CFList& MOD, const int bound)
413{
414  int adaptedLiftBound= 0;
415  CanonicalForm buf= F;
416  Variable y= F.mvar();
417  CanonicalForm LCBuf= LC (buf, Variable (1));
418  CanonicalForm g, quot;
419  CFList M= MOD;
420  M.append (power (y, deg));
421  int d= bound;
422  int e= 0;
423  int nBuf;
424  for (CFListIterator i= factors; i.hasItem(); i++)
425  {
426    g= mulMod (i.getItem(), LCBuf, M);
427    g /= myContent (g);
428    if (fdivides (g, buf, quot))
429    {
430      nBuf= degree (g, y) + degree (LC (g, 1), y);
431      d -= nBuf;
432      e= tmax (e, nBuf);
433      buf= quot;
434      LCBuf= LC (buf, Variable (1));
435    }
436  }
437  adaptedLiftBound= d;
438
439  if (adaptedLiftBound < deg)
440  {
441    if (adaptedLiftBound < degree (F) + 1)
442    {
443      if (d == 1)
444      {
445        if (e + 1 > deg)
446        {
447          adaptedLiftBound= deg;
448          success= false;
449        }
450        else
451        {
452          success= true;
453          if (e + 1 < degree (F) + 1)
454            adaptedLiftBound= deg;
455          else
456            adaptedLiftBound= e + 1;
457        }
458      }
459      else
460      {
461        success= true;
462        adaptedLiftBound= deg;
463      }
464    }
465    else
466    {
467      success= true;
468    }
469  }
470  return adaptedLiftBound;
471}
472
473int
474extLiftBoundAdaption (const CanonicalForm& F, const CFList& factors, bool&
475                      success, const ExtensionInfo& info, const CFList& eval,
476                      const int deg, const CFList& MOD, const int bound)
477{
478  Variable alpha= info.getAlpha();
479  Variable beta= info.getBeta();
480  CanonicalForm gamma= info.getGamma();
481  CanonicalForm delta= info.getDelta();
482  int k= info.getGFDegree();
483  int adaptedLiftBound= 0;
484  CanonicalForm buf= F;
485  Variable y= F.mvar();
486  CanonicalForm LCBuf= LC (buf, Variable (1));
487  CanonicalForm g, gg, quot;
488  CFList M= MOD;
489  M.append (power (y, deg));
490  adaptedLiftBound= 0;
491  int d= bound;
492  int e= 0;
493  int nBuf;
494  int degMipoBeta= 1;
495  if (!k && beta.level() != 1)
496    degMipoBeta= degree (getMipo (beta));
497
498  CFList source, dest;
499  for (CFListIterator i= factors; i.hasItem(); i++)
500  {
501    g= mulMod (i.getItem(), LCBuf, M);
502    g /= myContent (g);
503    if (fdivides (g, buf, quot))
504    {
505      gg= reverseShift (g, eval);
506      gg /= Lc (gg);
507      if (!k && beta == Variable (1))
508      {
509        if (degree (gg, alpha) < degMipoBeta)
510        {
511          buf= quot;
512          nBuf= degree (g, y) + degree (LC (g, Variable (1)), y);
513          d -= nBuf;
514          e= tmax (e, nBuf);
515          LCBuf= LC (buf, Variable (1));
516        }
517      }
518      else
519      {
520        if (!isInExtension (gg, gamma, k, delta, source, dest))
521        {
522          buf= quot;
523          nBuf= degree (g, y) + degree (LC (g, Variable (1)), y);
524          d -= nBuf;
525          e= tmax (e, nBuf);
526          LCBuf= LC (buf, Variable (1));
527        }
528      }
529    }
530  }
531  adaptedLiftBound= d;
532
533  if (adaptedLiftBound < deg)
534  {
535    if (adaptedLiftBound < degree (F) + 1)
536    {
537      if (d == 1)
538      {
539        if (e + 1 > deg)
540        {
541          adaptedLiftBound= deg;
542          success= false;
543        }
544        else
545        {
546          success= true;
547          if (e + 1 < degree (F) + 1)
548            adaptedLiftBound= deg;
549          else
550            adaptedLiftBound= e + 1;
551        }
552      }
553      else
554      {
555        success= true;
556        adaptedLiftBound= deg;
557      }
558    }
559    else
560    {
561      success= true;
562    }
563  }
564
565  return adaptedLiftBound;
566}
567
568CFList
569earlyFactorDetect (CanonicalForm& F, CFList& factors, int& adaptedLiftBound,
570                   bool& success, const int deg, const CFList& MOD,
571                   const int bound)
572{
573  CFList result;
574  CFList T= factors;
575  CanonicalForm buf= F;
576  Variable y= F.mvar();
577  CanonicalForm LCBuf= LC (buf, Variable (1));
578  CanonicalForm g, quot;
579  CFList M= MOD;
580  M.append (power (y, deg));
581  adaptedLiftBound= 0;
582  int d= bound;
583  int e= 0;
584  int nBuf;
585  for (CFListIterator i= factors; i.hasItem(); i++)
586  {
587    g= mulMod (i.getItem(), LCBuf, M);
588    g /= myContent (g);
589    if (fdivides (g, buf, quot))
590    {
591      result.append (g);
592      nBuf= degree (g, y) + degree (LC (g, Variable (1)), y);
593      d -= nBuf;
594      e= tmax (e, nBuf);
595      buf= quot;
596      LCBuf= LC (buf, Variable (1));
597      T= Difference (T, CFList (i.getItem()));
598    }
599  }
600  adaptedLiftBound= d;
601
602  if (adaptedLiftBound < deg)
603  {
604    if (adaptedLiftBound < degree (F) + 1)
605    {
606      if (d == 1)
607        adaptedLiftBound= tmin (e + 1, deg);
608      else
609        adaptedLiftBound= deg;
610    }
611    factors= T;
612    F= buf;
613    success= true;
614  }
615  return result;
616}
617
618CFList
619extEarlyFactorDetect (CanonicalForm& F, CFList& factors, int& adaptedLiftBound,
620                      bool& success, const ExtensionInfo& info, const CFList&
621                      eval, const int deg, const CFList& MOD, const int bound)
622{
623  Variable alpha= info.getAlpha();
624  Variable beta= info.getBeta();
625  CanonicalForm gamma= info.getGamma();
626  CanonicalForm delta= info.getDelta();
627  int k= info.getGFDegree();
628  CFList result;
629  CFList T= factors;
630  CanonicalForm buf= F;
631  Variable y= F.mvar();
632  CanonicalForm LCBuf= LC (buf, Variable (1));
633  CanonicalForm g, gg, quot;
634  CFList M= MOD;
635  M.append (power (y, deg));
636  adaptedLiftBound= 0;
637  int d= bound;
638  int e= 0;
639  int nBuf;
640  CFList source, dest;
641
642  int degMipoBeta= 1;
643  if (!k && beta.level() != 1)
644    degMipoBeta= degree (getMipo (beta));
645
646  for (CFListIterator i= factors; i.hasItem(); i++)
647  {
648    g= mulMod (i.getItem(), LCBuf, M);
649    g /= myContent (g);
650    if (fdivides (g, buf, quot))
651    {
652      gg= reverseShift (g, eval);
653      gg /= Lc (gg);
654      if (!k && beta == Variable (1))
655      {
656        if (degree (gg, alpha) < degMipoBeta)
657        {
658          appendTestMapDown (result, gg, info, source, dest);
659          buf= quot;
660          nBuf= degree (g, y) + degree (LC (g, Variable (1)), y);
661          d -= nBuf;
662          e= tmax (e, nBuf);
663          LCBuf= LC (buf, Variable (1));
664          T= Difference (T, CFList (i.getItem()));
665        }
666      }
667      else
668      {
669        if (!isInExtension (gg, gamma, k, delta, source, dest))
670        {
671          appendTestMapDown (result, gg, info, source, dest);
672          buf= quot;
673          nBuf= degree (g, y) + degree (LC (g, Variable (1)), y);
674          d -= nBuf;
675          e= tmax (e, nBuf);
676          LCBuf= LC (buf, Variable (1));
677          T= Difference (T, CFList (i.getItem()));
678         }
679      }
680    }
681  }
682  adaptedLiftBound= d;
683
684  if (adaptedLiftBound < deg)
685  {
686    if (adaptedLiftBound < degree (F) + 1)
687    {
688      if (d == 1)
689        adaptedLiftBound= tmin (e + 1, deg);
690      else
691        adaptedLiftBound= deg;
692    }
693    success= true;
694    factors= T;
695    F= buf;
696  }
697  return result;
698}
699
700CFList
701evalPoints (const CanonicalForm& F, CFList & eval, const Variable& alpha,
702            CFList& list, const bool& GF, bool& fail)
703{
704  int k= F.level() - 1;
705  Variable x= Variable (1);
706  CFList result;
707  FFRandom genFF;
708  GFRandom genGF;
709  int p= getCharacteristic ();
710  double bound;
711  if (alpha != Variable (1))
712  {
713    bound= pow ((double) p, (double) degree (getMipo(alpha)));
714    bound= pow ((double) bound, (double) k);
715  }
716  else if (GF)
717  {
718    bound= pow ((double) p, (double) getGFDegree());
719    bound= pow ((double) bound, (double) k);
720  }
721  else
722    bound= pow ((double) p, (double) k);
723
724  CanonicalForm random;
725  CanonicalForm deriv_x, gcd_deriv;
726  do
727  {
728    random= 0;
729    // possible overflow if list.length() does not fit into a int
730    if (list.length() >= bound)
731    {
732      fail= true;
733      break;
734    }
735    for (int i= 0; i < k; i++)
736    {
737      if (list.isEmpty())
738        result.append (0);
739      else if (GF)
740      {
741        result.append (genGF.generate());
742        random += result.getLast()*power (x, i);
743      }
744      else if (alpha.level() != 1)
745      {
746        AlgExtRandomF genAlgExt (alpha);
747        result.append (genAlgExt.generate());
748        random += result.getLast()*power (x, i);
749      }
750      else
751      {
752        result.append (genFF.generate());
753        random += result.getLast()*power (x, i);
754      }
755    }
756    if (find (list, random))
757    {
758      result= CFList();
759      continue;
760    }
761    int l= F.level();
762    eval.insert (F);
763    bool bad= false;
764    for (CFListIterator i= result; i.hasItem(); i++, l--)
765    {
766      eval.insert (eval.getFirst()(i.getItem(), l));
767      if (degree (eval.getFirst(), l - 1) != degree (F, l - 1))
768      {
769        if (!find (list, random))
770          list.append (random);
771        result= CFList();
772        eval= CFList();
773        bad= true;
774        break;
775      }
776    }
777
778    if (bad)
779      continue;
780
781    if (degree (eval.getFirst()) != degree (F, 1))
782    {
783      if (!find (list, random))
784        list.append (random);
785      result= CFList();
786      eval= CFList();
787      continue;
788    }
789
790    deriv_x= deriv (eval.getFirst(), x);
791    gcd_deriv= gcd (eval.getFirst(), deriv_x);
792    if (degree (gcd_deriv) > 0)
793    {
794      if (!find (list, random))
795        list.append (random);
796      result= CFList();
797      eval= CFList();
798      continue;
799    }
800    CFListIterator i= eval;
801    i++;
802    CanonicalForm contentx= content (i.getItem(), x);
803    if (degree (contentx) > 0)
804    {
805      if (!find (list, random))
806        list.append (random);
807      result= CFList();
808      eval= CFList();
809      continue;
810    }
811
812    if (list.length() >= bound)
813    {
814      fail= true;
815      break;
816    }
817  } while (find (list, random));
818
819  if (!eval.isEmpty())
820    eval.removeFirst();
821
822  return result;
823}
824
825static inline
826int newMainVariableSearch (CanonicalForm& A, CFList& Aeval, CFList&
827                           evaluation, const Variable& alpha, const int lev,
828                           CanonicalForm& g
829                          )
830{
831  Variable x= Variable (1);
832  CanonicalForm derivI, buf;
833  bool GF= (CFFactory::gettype() == GaloisFieldDomain);
834  int swapLevel= 0;
835  CFList list;
836  bool fail= false;
837  buf= A;
838  Aeval= CFList();
839  evaluation= CFList();
840  for (int i= lev; i <= A.level(); i++)
841  {
842    derivI= deriv (buf, Variable (i));
843    if (!derivI.isZero())
844    {
845      g= gcd (buf, derivI);
846      if (degree (g) > 0)
847        return -1;
848
849      buf= swapvar (buf, x, Variable (i));
850      Aeval= CFList();
851      evaluation= CFList();
852      fail= false;
853      evaluation= evalPoints (buf, Aeval, alpha, list, GF, fail);
854      if (!fail)
855      {
856        A= buf;
857        swapLevel= i;
858        break;
859      }
860      else
861        buf= A;
862    }
863  }
864  return swapLevel;
865}
866
867CanonicalForm lcmContent (const CanonicalForm& A, CFList& contentAi)
868{
869  int i= A.level();
870  contentAi.append (myContent (A, i));
871  contentAi.append (myContent (A, i - 1));
872  CanonicalForm result= lcm (contentAi.getFirst(), contentAi.getLast());
873  for (i= i - 2; i > 0; i--)
874  {
875    contentAi.append (content (A, i));
876    result= lcm (result, contentAi.getLast());
877  }
878  return result;
879}
880
881CFList
882henselLiftAndEarly (CanonicalForm& A, CFList& MOD, int*& liftBounds, bool&
883                    earlySuccess, CFList& earlyFactors, const CFList& Aeval,
884                    const CFList& biFactors, const CFList& evaluation,
885                    const ExtensionInfo& info)
886{
887  bool extension= info.isInExtension();
888  CFList bufFactors= biFactors;
889  bufFactors.insert (LC (Aeval.getFirst(), 1));
890
891  sortList (bufFactors, Variable (1));
892
893  CFList diophant;
894  CFArray Pi;
895  int smallFactorDeg= 11; //tunable parameter
896  CFList result;
897  int adaptedLiftBound= 0;
898  int liftBound= liftBounds[1];
899
900  earlySuccess= false;
901  CFList earlyReconstFactors;
902  CFListIterator j= Aeval;
903  j++;
904  CanonicalForm buf= j.getItem();
905  CFMatrix Mat= CFMatrix (liftBound, bufFactors.length() - 1);
906  MOD= CFList (power (Variable (2), liftBounds[0]));
907  if (smallFactorDeg >= liftBound)
908  {
909    result= henselLift23 (Aeval, bufFactors, liftBounds, diophant, Pi, Mat);
910  }
911  else if (smallFactorDeg >= degree (buf) + 1)
912  {
913    liftBounds[1]= degree (buf) + 1;
914    result= henselLift23 (Aeval, bufFactors, liftBounds, diophant, Pi, Mat);
915    if (Aeval.length() == 2)
916    {
917      if (!extension)
918        earlyFactors= earlyFactorDetect
919                       (buf, result, adaptedLiftBound, earlySuccess,
920                        degree (buf) + 1, MOD, liftBound);
921      else
922        earlyFactors= extEarlyFactorDetect
923                       (buf, result, adaptedLiftBound, earlySuccess,
924                        info, evaluation, degree
925                        (buf) + 1, MOD, liftBound);
926    }
927    else
928    {
929      if (!extension)
930        adaptedLiftBound= liftBoundAdaption (buf, result, earlySuccess,
931                                             degree (buf) + 1, MOD, liftBound);
932      else
933        adaptedLiftBound= extLiftBoundAdaption (buf, result, earlySuccess, info,
934                                                evaluation, degree (buf) + 1,
935                                                MOD, liftBound);
936    }
937    if (!earlySuccess)
938    {
939      result.insert (LC (buf, 1));
940      liftBounds[1]= adaptedLiftBound;
941      liftBound= adaptedLiftBound;
942      henselLiftResume (buf, result, degree (buf) + 1, liftBound,
943                        Pi, diophant, Mat, MOD);
944    }
945    else
946      liftBounds[1]= adaptedLiftBound;
947  }
948  else if (smallFactorDeg < degree (buf) + 1)
949  {
950    liftBounds[1]= smallFactorDeg;
951    result= henselLift23 (Aeval, bufFactors, liftBounds, diophant, Pi, Mat);
952    if (Aeval.length() == 2)
953    {
954      if (!extension)
955        earlyFactors= earlyFactorDetect (buf, result, adaptedLiftBound,
956                                         earlySuccess, smallFactorDeg, MOD,
957                                         liftBound);
958      else
959        earlyFactors= extEarlyFactorDetect (buf, result, adaptedLiftBound,
960                                            earlySuccess, info, evaluation,
961                                            smallFactorDeg, MOD, liftBound);
962    }
963    else
964    {
965      if (!extension)
966        adaptedLiftBound= liftBoundAdaption (buf, result, earlySuccess,
967                                             smallFactorDeg, MOD, liftBound);
968      else
969        adaptedLiftBound= extLiftBoundAdaption (buf, result, earlySuccess, info,
970                                                evaluation, smallFactorDeg, MOD,
971                                                liftBound);
972    }
973
974    if (!earlySuccess)
975    {
976      result.insert (LC (buf, 1));
977      henselLiftResume (buf, result, smallFactorDeg, degree (buf) + 1,
978                        Pi, diophant, Mat, MOD);
979      if (Aeval.length() == 2)
980      {
981         if (!extension)
982           earlyFactors= earlyFactorDetect (buf, result, adaptedLiftBound,
983                                            earlySuccess, degree (buf) + 1,
984                                            MOD, liftBound);
985         else
986           earlyFactors= extEarlyFactorDetect (buf, result, adaptedLiftBound,
987                                               earlySuccess, info, evaluation,
988                                               degree (buf) + 1, MOD,
989                                               liftBound);
990      }
991      else
992      {
993        if (!extension)
994          adaptedLiftBound= liftBoundAdaption (buf, result, earlySuccess,
995                                               degree (buf) + 1, MOD,liftBound);
996        else
997          adaptedLiftBound= extLiftBoundAdaption (buf, result, earlySuccess,
998                                                  info, evaluation,
999                                                  degree (buf) + 1, MOD,
1000                                                  liftBound);
1001      }
1002      if (!earlySuccess)
1003      {
1004        result.insert (LC (buf, 1));
1005        liftBounds[1]= adaptedLiftBound;
1006        liftBound= adaptedLiftBound;
1007        henselLiftResume (buf, result, degree (buf) + 1, liftBound,
1008                          Pi, diophant, Mat, MOD);
1009      }
1010      else
1011        liftBounds[1]= adaptedLiftBound;
1012    }
1013    else
1014      liftBounds[1]= adaptedLiftBound;
1015  }
1016
1017  MOD.append (power (Variable (3), liftBounds[1]));
1018
1019  if (Aeval.length() > 2)
1020  {
1021    CFListIterator j= Aeval;
1022    j++;
1023    CFList bufEval;
1024    bufEval.append (j.getItem());
1025    j++;
1026    int liftBoundsLength= Aeval.getLast().level() - 1;
1027    for (int i= 2; i <= liftBoundsLength && j.hasItem(); i++, j++)
1028    {
1029      earlySuccess= false;
1030      result.insert (LC (bufEval.getFirst(), 1));
1031      bufEval.append (j.getItem());
1032      liftBound= liftBounds[i];
1033      Mat= CFMatrix (liftBounds[i], result.length() - 1);
1034
1035      buf= j.getItem();
1036      if (smallFactorDeg >= liftBound)
1037        result= henselLift (bufEval, result, MOD, diophant, Pi, Mat,
1038                            liftBounds[i -  1], liftBounds[i]);
1039      else if (smallFactorDeg >= degree (buf) + 1)
1040      {
1041        result= henselLift (bufEval, result, MOD, diophant, Pi, Mat,
1042                            liftBounds[i -  1], degree (buf) + 1);
1043
1044        if (Aeval.length() == i + 1)
1045        {
1046          if (!extension)
1047            earlyFactors= earlyFactorDetect
1048                           (buf, result, adaptedLiftBound, earlySuccess,
1049                            degree (buf) + 1, MOD, liftBound);
1050          else
1051            earlyFactors= extEarlyFactorDetect
1052                           (buf, result, adaptedLiftBound, earlySuccess,
1053                            info, evaluation, degree (buf) + 1, MOD, liftBound);
1054        }
1055        else
1056        {
1057          if (!extension)
1058            adaptedLiftBound= liftBoundAdaption
1059                                (buf, result, earlySuccess, degree (buf)
1060                                 + 1,  MOD, liftBound);
1061          else
1062            adaptedLiftBound= extLiftBoundAdaption
1063                                (buf, result, earlySuccess, info, evaluation,
1064                                 degree (buf) + 1, MOD, liftBound);
1065        }
1066
1067        if (!earlySuccess)
1068        {
1069          result.insert (LC (buf, 1));
1070          liftBounds[i]= adaptedLiftBound;
1071          liftBound= adaptedLiftBound;
1072          henselLiftResume (buf, result, degree (buf) + 1, liftBound,
1073                            Pi, diophant, Mat, MOD);
1074        }
1075        else
1076        {
1077          liftBounds[i]= adaptedLiftBound;
1078        }
1079      }
1080      else if (smallFactorDeg < degree (buf) + 1)
1081      {
1082        result= henselLift (bufEval, result, MOD, diophant, Pi, Mat,
1083                            liftBounds[i -  1], smallFactorDeg);
1084
1085        if (Aeval.length() == i + 1)
1086        {
1087          if (!extension)
1088            earlyFactors= earlyFactorDetect
1089                           (buf, result, adaptedLiftBound, earlySuccess,
1090                            smallFactorDeg, MOD, liftBound);
1091          else
1092            earlyFactors= extEarlyFactorDetect
1093                           (buf, result, adaptedLiftBound, earlySuccess,
1094                            info, evaluation, smallFactorDeg, MOD, liftBound);
1095        }
1096        else
1097        {
1098          if (!extension)
1099            adaptedLiftBound= liftBoundAdaption
1100                                (buf, result, earlySuccess,
1101                                 smallFactorDeg, MOD, liftBound);
1102          else
1103            adaptedLiftBound= extLiftBoundAdaption
1104                                (buf, result, earlySuccess, info, evaluation,
1105                                 smallFactorDeg, MOD, liftBound);
1106        }
1107
1108        if (!earlySuccess)
1109        {
1110          result.insert (LC (buf, 1));
1111          henselLiftResume (buf, result, smallFactorDeg,
1112                            degree (buf) + 1, Pi, diophant, Mat, MOD);
1113          if (Aeval.length() == i + 1)
1114          {
1115            if (!extension)
1116              earlyFactors= earlyFactorDetect
1117                             (buf, result, adaptedLiftBound, earlySuccess,
1118                              degree (buf) +  1,  MOD, liftBound);
1119            else
1120              earlyFactors= extEarlyFactorDetect
1121                             (buf, result, adaptedLiftBound, earlySuccess,
1122                              info, evaluation, degree (buf) + 1, MOD,
1123                              liftBound);
1124          }
1125          else
1126          {
1127            if (!extension)
1128              adaptedLiftBound= liftBoundAdaption
1129                                  (buf, result, earlySuccess, degree
1130                                   (buf) +  1,  MOD, liftBound);
1131            else
1132              adaptedLiftBound= extLiftBoundAdaption
1133                                  (buf, result, earlySuccess, info, evaluation,
1134                                   degree (buf) + 1,  MOD, liftBound);
1135          }
1136
1137          if (!earlySuccess)
1138          {
1139            result.insert (LC (buf, 1));
1140            liftBounds[i]= adaptedLiftBound;
1141            liftBound= adaptedLiftBound;
1142            henselLiftResume (buf, result, degree (buf) + 1, liftBound,
1143                              Pi, diophant, Mat, MOD);
1144          }
1145          else
1146            liftBounds[i]= adaptedLiftBound;
1147        }
1148        else
1149          liftBounds[i]= adaptedLiftBound;
1150      }
1151      MOD.append (power (Variable (i + 2), liftBounds[i]));
1152      bufEval.removeFirst();
1153    }
1154    bufFactors= result;
1155  }
1156  else
1157    bufFactors= result;
1158
1159  if (earlySuccess)
1160    A= buf;
1161  return result;
1162}
1163
1164CFList
1165leadingCoeffReconstruction (const CanonicalForm& F, const CFList& factors,
1166                            const CFList& M)
1167{
1168  CanonicalForm quot, buf= F;
1169  CanonicalForm LCBuf= LC (buf, 1);
1170
1171  CFList result;
1172
1173  CanonicalForm tmp;
1174  for (CFListIterator i= factors; i.hasItem(); i++)
1175  {
1176    tmp= i.getItem();
1177    tmp= mulMod (tmp, LCBuf, M);
1178    tmp= tmp/content (tmp, 1);
1179    if (fdivides (tmp, buf, quot))
1180    {
1181      buf= quot;
1182      result.append (tmp);
1183      LCBuf= LC (buf, 1);
1184    }
1185    else //no one-to-one correspondence
1186      return CFList();
1187  }
1188
1189  return result;
1190}
1191
1192void
1193gcdFreeBasis (CFFList& factors1, CFFList& factors2)
1194{
1195  CanonicalForm g;
1196  int k= factors1.length();
1197  int l= factors2.length();
1198  int n= 1;
1199  int m;
1200  CFFListIterator j;
1201  for (CFFListIterator i= factors1; (n < k && i.hasItem()); i++, n++)
1202  {
1203    m= 1;
1204    for (j= factors2; (m < l && j.hasItem()); j++, m++)
1205    {
1206      g= gcd (i.getItem().factor(), j.getItem().factor());
1207      if (degree (g) > 0)
1208      {
1209        j.getItem()= CFFactor (j.getItem().factor()/g, j.getItem().exp());
1210        i.getItem()= CFFactor (i.getItem().factor()/g, i.getItem().exp());
1211        factors1.append (CFFactor (g, i.getItem().exp()));
1212        factors2.append (CFFactor (g, j.getItem().exp()));
1213      }
1214    }
1215  }
1216}
1217
1218CFList
1219distributeContent (const CFList& L, const CFList* differentSecondVarFactors,
1220                   int length
1221                  )
1222{
1223  CFList l= L;
1224  CanonicalForm content= l.getFirst();
1225
1226  if (content.inCoeffDomain())
1227    return l;
1228
1229  if (l.length() == 1)
1230  {
1231    CFList result;
1232    for (int i= 0; i < length; i++)
1233    {
1234      if (differentSecondVarFactors[i].isEmpty())
1235        continue;
1236      if (result.isEmpty())
1237      {
1238        result= differentSecondVarFactors[i];
1239        for (CFListIterator iter= result; iter.hasItem(); iter++)
1240          content /= iter.getItem();
1241      }
1242      else
1243      {
1244        CFListIterator iter1= result;
1245        for (CFListIterator iter2= differentSecondVarFactors[i]; iter2.hasItem();
1246             iter2++, iter1++)
1247        {
1248          iter1.getItem() *= iter2.getItem();
1249          content /= iter2.getItem();
1250        }
1251      }
1252    }
1253    result.insert (content);
1254    return result;
1255  }
1256
1257  Variable v;
1258  CFListIterator iter1;
1259  CanonicalForm tmp, g;
1260  for (int i= 0; i < length; i++)
1261  {
1262    if (differentSecondVarFactors[i].isEmpty())
1263      continue;
1264    iter1= l;
1265    iter1++;
1266
1267    v= Variable (i + 3);
1268    for (CFListIterator iter2= differentSecondVarFactors[i]; iter2.hasItem();
1269         iter2++, iter1++)
1270    {
1271      if (degree (iter2.getItem(),v) == degree (iter1.getItem(),v))
1272        continue;
1273      tmp= iter1.getItem();
1274      for (int j= tmp.level(); j > 1; j--)
1275      {
1276        if (j == i + 3)
1277          continue;
1278        tmp= tmp (0, j);
1279      }
1280      g= gcd (iter2.getItem(), content);
1281      if (degree (g) > 0)
1282      {
1283        iter2.getItem() /= tmp;
1284        content /= g;
1285        iter1.getItem() *= g;
1286      }
1287    }
1288  }
1289
1290  l.removeFirst();
1291  l.insert (content);
1292  return l;
1293}
1294
1295CFList evaluateAtZero (const CanonicalForm& F)
1296{
1297  CFList result;
1298  CanonicalForm buf= F;
1299  result.insert (buf);
1300  for (int i= F.level(); i > 2; i--)
1301  {
1302    buf= buf (0, i);
1303    result.insert (buf);
1304  }
1305  return result;
1306}
1307
1308int
1309testFactors (const CanonicalForm& G, const CFList& uniFactors,
1310             const Variable& alpha, CanonicalForm& sqrfPartF, CFList& factors,
1311             CFFList*& bufSqrfFactors, CFList& evalSqrfPartF)
1312{
1313  CanonicalForm tmp;
1314  CFListIterator j;
1315  for (CFListIterator i= uniFactors; i.hasItem(); i++)
1316  {
1317    tmp= i.getItem();
1318    if (i.hasItem())
1319      i++;
1320    else
1321      break;
1322    for (j= i; j.hasItem(); j++)
1323    {
1324      if (tmp == j.getItem())
1325        return 0;
1326    }
1327  }
1328
1329  CanonicalForm F= G;
1330  CFFList sqrfFactorization= squarefreeFactorization (F, alpha);
1331
1332  sqrfPartF= 1;
1333  for (CFFListIterator i= sqrfFactorization; i.hasItem(); i++)
1334    sqrfPartF *= i.getItem().factor();
1335
1336  evalSqrfPartF= evaluateAtZero (sqrfPartF);
1337
1338  CanonicalForm test= evalSqrfPartF.getFirst() (0, 2);
1339
1340  if (degree (test) != degree (sqrfPartF, 1))
1341    return 0;
1342
1343  CFFList sqrfFactors;
1344  CFList tmp2;
1345  int k= 0;
1346  factors= uniFactors;
1347  CFFListIterator iter;
1348  for (CFListIterator i= factors; i.hasItem(); i++, k++)
1349  {
1350    tmp= 1;
1351    sqrfFactors= squarefreeFactorization (i.getItem(), alpha);
1352
1353    for (iter= sqrfFactors; iter.hasItem(); iter++)
1354    {
1355      tmp2.append (iter.getItem().factor());
1356      tmp *= iter.getItem().factor();
1357    }
1358    i.getItem()= tmp/Lc(tmp);
1359    bufSqrfFactors [k]= sqrfFactors;
1360  }
1361
1362  for (int i= 0; i < factors.length() - 1; i++)
1363  {
1364    for (k= i + 1; k < factors.length(); k++)
1365    {
1366      gcdFreeBasis (bufSqrfFactors [i], bufSqrfFactors[k]);
1367    }
1368  }
1369
1370  factors= CFList();
1371  for (int i= 0; i < uniFactors.length(); i++)
1372  {
1373    if (i == 0)
1374    {
1375      for (iter= bufSqrfFactors [i]; iter.hasItem(); iter++)
1376      {
1377        if (iter.getItem().factor().inCoeffDomain())
1378          continue;
1379        iter.getItem()= CFFactor (iter.getItem().factor()/
1380                                  Lc (iter.getItem().factor()),
1381                                  iter.getItem().exp());
1382        factors.append (iter.getItem().factor());
1383      }
1384    }
1385    else
1386    {
1387      for (iter= bufSqrfFactors [i]; iter.hasItem(); iter++)
1388      {
1389        if (iter.getItem().factor().inCoeffDomain())
1390          continue;
1391        iter.getItem()= CFFactor (iter.getItem().factor()/
1392                                  Lc (iter.getItem().factor()),
1393                                  iter.getItem().exp());
1394        if (!find (factors, iter.getItem().factor()))
1395          factors.append (iter.getItem().factor());
1396      }
1397    }
1398  }
1399
1400  test= prod (factors);
1401  tmp= evalSqrfPartF.getFirst() (0,2);
1402  if (test/Lc (test) != tmp/Lc (tmp))
1403    return 0;
1404  else
1405    return 1;
1406}
1407
1408CFList
1409precomputeLeadingCoeff (const CanonicalForm& LCF, const CFList& LCFFactors,
1410                        const Variable& alpha, const CFList& evaluation,
1411                        CFList* & differentSecondVarLCs, int length,
1412                        Variable& y
1413                       )
1414{
1415  y= Variable (1);
1416  if (LCF.inCoeffDomain())
1417  {
1418    CFList result;
1419    for (int i= 1; i <= LCFFactors.length() + 1; i++)
1420      result.append (1);
1421    return result;
1422  }
1423
1424  CFMap N;
1425  CanonicalForm F= compress (LCF, N);
1426  if (LCF.isUnivariate())
1427  {
1428    CFList result;
1429    int LCFLevel= LCF.level();
1430    bool found= false;
1431    if (LCFLevel == 2)
1432    {
1433    //bivariate leading coefficients are already the true leading coefficients
1434      result= LCFFactors;
1435      Variable v= Variable (LCF.mvar());
1436      CanonicalForm bla= 1;
1437      for (CFListIterator i= result; i.hasItem(); i++)
1438      {
1439        i.getItem()= i.getItem() (v+evaluation.getLast(), v);
1440        bla *= Lc (i.getItem());
1441      }
1442      found= true;
1443    }
1444    else
1445    {
1446      CFListIterator j;
1447      for (int i= 0; i < length; i++)
1448      {
1449        for (j= differentSecondVarLCs[i]; j.hasItem(); j++)
1450        {
1451          if (j.getItem().level() == LCFLevel)
1452          {
1453            found= true;
1454            break;
1455          }
1456        }
1457        if (found)
1458        {
1459          result= differentSecondVarLCs [i];
1460          break;
1461        }
1462      }
1463      if (!found)
1464        result= LCFFactors;
1465    }
1466    if (found)
1467      result.insert (Lc (LCF));
1468    else
1469      result.append (LCF);
1470    return result;
1471  }
1472
1473  CFList factors= LCFFactors;
1474
1475  CFMap dummy;
1476  for (CFListIterator i= factors; i.hasItem(); i++)
1477  {
1478    i.getItem()= compress (i.getItem(), dummy);
1479    i.getItem()= i.getItem() (Variable (1) + evaluation.getLast(), Variable (1));
1480  }
1481
1482  CanonicalForm sqrfPartF;
1483  CFFList * bufSqrfFactors= new CFFList [factors.length()];
1484  CFList evalSqrfPartF;
1485  CFList bufFactors;
1486  int pass= testFactors (F, factors, alpha, sqrfPartF,
1487                         bufFactors, bufSqrfFactors, evalSqrfPartF);
1488
1489  bool foundDifferent= false;
1490  Variable z;
1491  Variable x= y;
1492  int j= 0;
1493  if (!pass)
1494  {
1495    int lev;
1496    // LCF is non-constant here
1497    for (int i= 1; i <= LCF.level(); i++)
1498    {
1499      if(degree (LCF, i) > 0)
1500      {
1501        lev= i - 1;
1502        break;
1503      }
1504    }
1505    CFListIterator iter;
1506    CFList bufBufFactors;
1507    CanonicalForm bufF;
1508    for (int i= 0; i < length; i++)
1509    {
1510      if (!differentSecondVarLCs [i].isEmpty())
1511      {
1512        bool allConstant= true;
1513        for (iter= differentSecondVarLCs[i]; iter.hasItem(); iter++)
1514        {
1515          if (!iter.getItem().inCoeffDomain())
1516          {
1517            allConstant= false;
1518            y= Variable (iter.getItem().level());
1519          }
1520        }
1521        if (allConstant)
1522          continue;
1523
1524        bufFactors= differentSecondVarLCs [i];
1525        for (iter= bufFactors; iter.hasItem(); iter++)
1526          iter.getItem()= swapvar (iter.getItem(), x, y);
1527        bufF= F;
1528        z= Variable (y.level() - lev);
1529        bufF= swapvar (bufF, x, z);
1530        bufBufFactors= bufFactors;
1531        pass= testFactors (bufF, bufBufFactors, alpha, sqrfPartF, bufFactors,
1532                           bufSqrfFactors, evalSqrfPartF);
1533        if (pass)
1534        {
1535          foundDifferent= true;
1536          F= bufF;
1537          CFList l= factors;
1538          for (iter= l; iter.hasItem(); iter++)
1539            iter.getItem()= swapvar (iter.getItem(), x, y);
1540          differentSecondVarLCs [i]= l;
1541          j= i;
1542          break;
1543        }
1544        if (!pass && i == length - 1)
1545        {
1546          CFList result;
1547          result.append (LCF);
1548          for (int k= 1; k <= factors.length(); k++)
1549            result.append (LCF);
1550          y= Variable (1);
1551          return result;
1552        }
1553      }
1554    }
1555  }
1556  if (!pass)
1557  {
1558    CFList result;
1559    result.append (LCF);
1560    for (int k= 1; k <= factors.length(); k++)
1561      result.append (LCF);
1562    y= Variable (1);
1563    return result;
1564  }
1565  else
1566    factors= bufFactors;
1567
1568  bufFactors= factors;
1569
1570  if (factors.length() > 1)
1571  {
1572    CanonicalForm LC1= LC (evalSqrfPartF.getFirst(), 1);
1573
1574    CFArray leadingCoeffs= CFArray (factors.length());
1575    for (int i= 0; i < factors.length(); i++)
1576      leadingCoeffs[i]= LC1;
1577    for (CFListIterator i= factors; i.hasItem(); i++)
1578      i.getItem() *= LC1 (0,2)/Lc (i.getItem());
1579    factors.insert (1);
1580
1581    CanonicalForm
1582    newSqrfPartF= evalSqrfPartF.getFirst()*power (LC1, factors.length() - 2);
1583
1584    int liftBound= degree (newSqrfPartF,2) + 1;
1585
1586    CFMatrix M= CFMatrix (liftBound, factors.length() - 1);
1587    CFArray Pi;
1588    CFList diophant;
1589    henselLift122 (newSqrfPartF, factors, liftBound, Pi, diophant, M,
1590                   leadingCoeffs, false);
1591
1592    if (sqrfPartF.level() > 2)
1593    {
1594      int* liftBounds= new int [sqrfPartF.level() - 1];
1595      liftBounds [0]= liftBound;
1596      bool noOneToOne= false;
1597      CFList *leadingCoeffs2= new CFList [sqrfPartF.level()-2];
1598      LC1= LC (evalSqrfPartF.getLast(), 1);
1599      CFList LCs;
1600      for (int i= 0; i < factors.length(); i++)
1601        LCs.append (LC1);
1602      leadingCoeffs2 [sqrfPartF.level() - 3]= LCs;
1603      for (int i= sqrfPartF.level() - 1; i > 2; i--)
1604      {
1605        for (CFListIterator j= LCs; j.hasItem(); j++)
1606          j.getItem()= j.getItem() (0, i + 1);
1607        leadingCoeffs2 [i - 3]= LCs;
1608      }
1609      sqrfPartF= sqrfPartF*power (LC1, factors.length()-1);
1610
1611      int liftBoundsLength= sqrfPartF.level() - 1;
1612      for (int i= 1; i < liftBoundsLength; i++)
1613        liftBounds [i]= degree (sqrfPartF, i + 2) + 1;
1614      evalSqrfPartF= evaluateAtZero (sqrfPartF);
1615      evalSqrfPartF.removeFirst();
1616      factors= nonMonicHenselLift (evalSqrfPartF, factors, leadingCoeffs2,
1617               diophant, Pi, liftBounds, sqrfPartF.level() - 1, noOneToOne);
1618      delete [] leadingCoeffs2;
1619      delete [] liftBounds;
1620    }
1621  }
1622  else
1623    factors= evalSqrfPartF.getLast();
1624
1625  CFList interMedResult= recoverFactors (evalSqrfPartF.getLast(), factors);
1626
1627  CFList result;
1628  CFFListIterator k;
1629  for (int i= 0; i < LCFFactors.length(); i++)
1630  {
1631    CanonicalForm tmp= 1;
1632    for (k= bufSqrfFactors[i]; k.hasItem(); k++)
1633    {
1634      int pos= findItem (bufFactors, k.getItem().factor());
1635      if (pos)
1636        tmp *= power (getItem (interMedResult, pos), k.getItem().exp());
1637    }
1638    result.append (tmp);
1639  }
1640
1641  for (CFListIterator i= result; i.hasItem(); i++)
1642  {
1643    F /= i.getItem();
1644    if (foundDifferent)
1645      i.getItem()= swapvar (i.getItem(), x, z);
1646    i.getItem()= N (i.getItem());
1647  }
1648
1649  if (foundDifferent)
1650  {
1651    CFList l= differentSecondVarLCs [j];
1652    for (CFListIterator i= l; i.hasItem(); i++)
1653      i.getItem()= swapvar (i.getItem(), y, z);
1654    differentSecondVarLCs [j]= l;
1655    F= swapvar (F, x, z);
1656  }
1657
1658  result.insert (N (F));
1659
1660  result= distributeContent (result, differentSecondVarLCs, length);
1661
1662  if (!result.getFirst().inCoeffDomain())
1663  {
1664    CFListIterator i= result;
1665    CanonicalForm tmp;
1666    if (foundDifferent)
1667      i.getItem()= swapvar (i.getItem(), Variable (2), y);
1668
1669    tmp= i.getItem();
1670
1671    i++;
1672    for (; i.hasItem(); i++)
1673    {
1674      if (foundDifferent)
1675        i.getItem()= swapvar (i.getItem(), Variable (2), y)*tmp;
1676      else
1677        i.getItem() *= tmp;
1678    }
1679  }
1680  else
1681    y= Variable (1);
1682
1683  return result;
1684}
1685
1686void
1687evaluationWRTDifferentSecondVars (CFList*& Aeval, const CFList& evaluation,
1688                                  const CanonicalForm& A)
1689{
1690  CanonicalForm tmp;
1691  CFList tmp2;
1692  CFListIterator iter;
1693  for (int i= A.level(); i > 2; i--)
1694  {
1695    tmp= A;
1696    tmp2= CFList();
1697    iter= evaluation;
1698    bool preserveDegree= true;
1699    for (int j= A.level(); j > 1; j--, iter++)
1700    {
1701      if (j == i)
1702        continue;
1703      else
1704      {
1705        tmp= tmp (iter.getItem(), j);
1706        tmp2.insert (tmp);
1707        if ((degree (tmp, i) != degree (A, i)) ||
1708            (degree (tmp, 1) != degree (A, 1)))
1709        {
1710          preserveDegree= false;
1711          break;
1712        }
1713        if (!content(tmp).inCoeffDomain() || !content(tmp,1).inCoeffDomain())
1714        {
1715          preserveDegree= false;
1716          break;
1717        }
1718      }
1719    }
1720    if (preserveDegree)
1721      Aeval [i - 3]= tmp2;
1722    else
1723      Aeval [i - 3]= CFList();
1724  }
1725}
1726
1727static inline
1728CanonicalForm prodEval (const CFList& l, const CanonicalForm& evalPoint,
1729                        const Variable& v)
1730{
1731  CanonicalForm result= 1;
1732  for (CFListIterator i= l; i.hasItem(); i++)
1733    result *= i.getItem() (evalPoint, v);
1734  return result;
1735}
1736
1737//recombine bivariate factors in case one bivariate factorization yields less
1738// factors than the other
1739CFList
1740recombination (const CFList& factors1, const CFList& factors2, int s, int thres,
1741               const CanonicalForm& evalPoint, const Variable& x)
1742{
1743  CFList T, S;
1744
1745  T= factors1;
1746  CFList result;
1747  CanonicalForm buf;
1748  int * v= new int [T.length()];
1749  for (int i= 0; i < T.length(); i++)
1750    v[i]= 0;
1751  bool nosubset= false;
1752  CFArray TT;
1753  TT= copy (factors1);
1754  while (T.length() >= 2*s && s <= thres)
1755  {
1756    while (nosubset == false) 
1757    {
1758      if (T.length() == s) 
1759      {
1760        delete [] v;
1761        result.append (prod (T));
1762        return result;
1763      }
1764      S= subset (v, s, TT, nosubset);
1765      if (nosubset) break;
1766      buf= prodEval (S, evalPoint, x);
1767      buf /= Lc (buf);
1768      if (find (factors2, buf))
1769      {
1770        T= Difference (T, S);
1771        result.append (prod (S));
1772        TT= copy (T);
1773        indexUpdate (v, s, T.length(), nosubset);
1774        if (nosubset) break;
1775      }
1776    }
1777    s++;
1778    if (T.length() < 2*s || T.length() == s) 
1779    {
1780      delete [] v;
1781      result.append (prod (T));
1782      return result;
1783    }
1784    for (int i= 0; i < T.length(); i++)
1785      v[i]= 0;
1786    nosubset= false;
1787  }
1788
1789  delete [] v;
1790  if (T.length() < 2*s)
1791  {
1792    result.append (prod (T));
1793    return result;
1794  }
1795
1796  return result;
1797}
1798
1799void
1800factorizationWRTDifferentSecondVars (const CanonicalForm& A, CFList*& Aeval,
1801                                     const ExtensionInfo& info,
1802                                     int& minFactorsLength, bool& irred)
1803{
1804  Variable x= Variable (1);
1805  minFactorsLength= 0;
1806  irred= false;
1807  CFList factors;
1808  Variable v;
1809  for (int j= 0; j < A.level() - 2; j++)
1810  {
1811    if (!Aeval[j].isEmpty())
1812    {
1813      v= Variable (Aeval[j].getFirst().level());
1814      if (CFFactory::gettype() == GaloisFieldDomain)
1815        factors= GFBiSqrfFactorize (Aeval[j].getFirst());
1816      else if (info.getAlpha().level() == 1)
1817        factors= FpBiSqrfFactorize (Aeval[j].getFirst());
1818      else
1819        factors= FqBiSqrfFactorize (Aeval[j].getFirst(), info.getAlpha());
1820
1821      factors.removeFirst();
1822      if (minFactorsLength == 0)
1823        minFactorsLength= factors.length();
1824      else
1825        minFactorsLength= tmin (minFactorsLength, factors.length());
1826
1827      if (factors.length() == 1)
1828      {
1829        irred= true;
1830        return;
1831      }
1832      sortList (factors, x);
1833      Aeval [j]= factors;
1834    }
1835  }
1836}
1837
1838void getLeadingCoeffs (const CanonicalForm& A, CFList*& Aeval,
1839                       const CFList& uniFactors, const CFList& evaluation
1840                      )
1841{
1842  CanonicalForm evalPoint;
1843  int i;
1844  CFListIterator iter, iter2;
1845  Variable v;
1846  CFList l, LCs;
1847  CanonicalForm buf;
1848  for (int j= 0; j < A.level() - 2; j++)
1849  {
1850    if (!Aeval[j].isEmpty())
1851    {
1852      i= A.level();
1853      for (iter= evaluation; iter.hasItem(); iter++, i--)
1854      {
1855        if (i == Aeval[j].getFirst().level())
1856        {
1857          evalPoint= iter.getItem();
1858          break;
1859        }
1860      }
1861
1862      v= Variable (i);
1863      if (Aeval[j].length() > uniFactors.length())
1864        Aeval[j]= recombination (Aeval[j], uniFactors, 1,
1865                                 Aeval[j].length() - uniFactors.length() + 1,
1866                                 evalPoint, v);
1867
1868      l= CFList();
1869      for (iter= uniFactors; iter.hasItem(); iter++)
1870      {
1871        for (iter2= Aeval[j]; iter2.hasItem(); iter2++)
1872        {
1873          buf= mod (iter2.getItem(), v - evalPoint);
1874          buf /= Lc (buf);
1875          if (iter.getItem() == buf)
1876          {
1877            l.append (iter2.getItem());
1878            break;
1879          }
1880        }
1881      }
1882      Aeval [j]= l;
1883
1884      LCs= CFList();
1885      for (iter= Aeval[j]; iter.hasItem(); iter++)
1886        LCs.append (LC (iter.getItem() (v + evalPoint, v), 1));
1887      normalize (LCs);
1888      Aeval[j]= LCs;
1889    }
1890  }
1891}
1892
1893CFList
1894buildUniFactors (const CFList& biFactors, const CanonicalForm& evalPoint,
1895                 const Variable& y)
1896{
1897  CFList result;
1898  CanonicalForm tmp;
1899  for (CFListIterator i= biFactors; i.hasItem(); i++)
1900  {
1901    tmp= mod (i.getItem(), y - evalPoint);
1902    tmp /= Lc (tmp);
1903    result.append (tmp);
1904  }
1905  return result;
1906}
1907
1908void refineBiFactors (const CanonicalForm& A, CFList& biFactors,
1909                      CFList* const& Aeval, const CFList& evaluation,
1910                      int minFactorsLength)
1911{
1912  CFListIterator iter;
1913  CanonicalForm evalPoint;
1914  int i;
1915  Variable v;
1916  Variable y= Variable (2);
1917  CFList list;
1918  for (int j= 0; j < A.level() - 2; j++)
1919  {
1920    if (Aeval[j].length() == minFactorsLength)
1921    {
1922      i= A.level();
1923
1924      for (iter= evaluation; iter.hasItem(); iter++, i--)
1925      {
1926        if (i == Aeval[j].getFirst().level())
1927        {
1928          evalPoint= iter.getItem();
1929          break;
1930        }
1931      }
1932
1933      v= Variable (i);
1934      list= buildUniFactors (Aeval[j], evalPoint, v);
1935
1936      biFactors= recombination (biFactors, list, 1,
1937                                biFactors.length() - list.length() + 1,
1938                                evaluation.getLast(), y);
1939      return;
1940    }
1941  }
1942}
1943
1944void prepareLeadingCoeffs (CFList*& LCs, int n, const CFList& leadingCoeffs,
1945                           const CFList& biFactors)
1946{
1947  CFList l= leadingCoeffs;
1948  LCs [n-3]= l;
1949  CFListIterator j;
1950  for (int i= n - 1; i > 2; i--)
1951  {
1952    for (j= l; j.hasItem(); j++)
1953      j.getItem()= j.getItem() (0, i + 1);
1954    LCs [i - 3]= l;
1955  }
1956  l= LCs [0];
1957  for (CFListIterator i= l; i.hasItem(); i++)
1958    i.getItem()= i.getItem() (0, 3);
1959  CFListIterator ii= biFactors;
1960  CFList normalizeFactor;
1961  for (CFListIterator i= l; i.hasItem(); i++, ii++)
1962    normalizeFactor.append (Lc (LC (ii.getItem(), 1))/Lc (i.getItem()));
1963  for (int i= 0; i < n-2; i++)
1964  {
1965    ii= normalizeFactor;
1966    for (j= LCs [i]; j.hasItem(); j++, ii++)
1967      j.getItem() *= ii.getItem();
1968  }
1969}
1970
1971CFList recoverFactors (const CanonicalForm& F, const CFList& factors)
1972{
1973  CFList result;
1974  CanonicalForm tmp, tmp2;
1975  CanonicalForm G= F;
1976  for (CFListIterator i= factors; i.hasItem(); i++)
1977  {
1978    tmp= i.getItem()/content (i.getItem(), 1);
1979    if (fdivides (tmp, G, tmp2))
1980    {
1981      G= tmp2;
1982      result.append (tmp);
1983    }
1984  }
1985  return result;
1986}
1987
1988CFList
1989extNonMonicFactorRecombination (const CFList& factors, const CanonicalForm& F,
1990                                const ExtensionInfo& info,
1991                                const CFList& evaluation)
1992{
1993  Variable alpha= info.getAlpha();
1994  Variable beta= info.getBeta();
1995  CanonicalForm gamma= info.getGamma();
1996  CanonicalForm delta= info.getDelta();
1997  int k= info.getGFDegree();
1998  CFList source, dest;
1999
2000  int degMipoBeta= 1;
2001  if (!k && beta != Variable(1))
2002    degMipoBeta= degree (getMipo (beta));
2003
2004  CFList T, S;
2005  T= factors;
2006  int s= 1;
2007  CFList result;
2008  CanonicalForm quot, buf= F;
2009
2010  CanonicalForm g;
2011  CanonicalForm buf2;
2012  int * v= new int [T.length()];
2013  for (int i= 0; i < T.length(); i++)
2014    v[i]= 0;
2015  bool noSubset= false;
2016  CFArray TT;
2017  TT= copy (factors);
2018  bool recombination= false;
2019  bool trueFactor= false;
2020  while (T.length() >= 2*s)
2021  {
2022    while (noSubset == false)
2023    {
2024      if (T.length() == s)
2025      {
2026        delete [] v;
2027        if (recombination)
2028        {
2029          g= prod (T);
2030          T.removeFirst();
2031          result.append (g/myContent (g));
2032          g= reverseShift (g, evaluation);
2033          g /= Lc (g);
2034          appendTestMapDown (result, g, info, source, dest);
2035          return result;
2036        }
2037        else
2038        {
2039          buf= reverseShift (buf, evaluation);
2040          return CFList (buf);
2041        }
2042      }
2043
2044      S= subset (v, s, TT, noSubset);
2045      if (noSubset) break;
2046
2047      g= prod (S);
2048      g /= myContent (g);
2049      if (fdivides (g, buf, quot))
2050      {
2051        buf2= reverseShift (g, evaluation);
2052        buf2 /= Lc (buf2);
2053        if (!k && beta == Variable (1))
2054        {
2055          if (degree (buf2, alpha) < degMipoBeta)
2056          {
2057            appendTestMapDown (result, buf2, info, source, dest);
2058            buf= quot;
2059            recombination= true;
2060            trueFactor= true;
2061          }
2062        }
2063        else
2064        {
2065          if (!isInExtension (buf2, gamma, k, delta, source, dest))
2066          {
2067            appendTestMapDown (result, buf2, info, source, dest);
2068            buf= quot;
2069            recombination= true;
2070            trueFactor= true;
2071          }
2072        }
2073        if (trueFactor)
2074        {
2075          T= Difference (T, S);
2076
2077          if (T.length() < 2*s || T.length() == s)
2078          {
2079            delete [] v;
2080            buf= reverseShift (buf, evaluation);
2081            buf /= Lc (buf);
2082            appendTestMapDown (result, buf, info, source, dest);
2083            return result;
2084          }
2085          trueFactor= false;
2086          TT= copy (T);
2087          indexUpdate (v, s, T.length(), noSubset);
2088          if (noSubset) break;
2089        }
2090      }
2091    }
2092    s++;
2093    if (T.length() < 2*s || T.length() == s)
2094    {
2095      delete [] v;
2096      buf= reverseShift (buf, evaluation);
2097      appendTestMapDown (result, buf, info, source, dest);
2098      return result;
2099    }
2100    for (int i= 0; i < T.length(); i++)
2101      v[i]= 0;
2102    noSubset= false;
2103  }
2104  if (T.length() < 2*s)
2105  {
2106    buf= reverseShift (F, evaluation);
2107    appendMapDown (result, buf, info, source, dest);
2108  }
2109
2110  delete [] v;
2111  return result;
2112}
2113
2114CFList
2115extFactorize (const CanonicalForm& F, const ExtensionInfo& info);
2116
2117CFList
2118multiFactorize (const CanonicalForm& F, const ExtensionInfo& info)
2119{
2120
2121  if (F.inCoeffDomain())
2122    return CFList (F);
2123
2124  // compress and find main Variable
2125  CFMap N;
2126  CanonicalForm A= myCompress (F, N);
2127
2128  A /= Lc (A); // make monic
2129
2130  Variable alpha= info.getAlpha();
2131  Variable beta= info.getBeta();
2132  CanonicalForm gamma= info.getGamma();
2133  CanonicalForm delta= info.getDelta();
2134  bool extension= info.isInExtension();
2135  bool GF= (CFFactory::gettype() == GaloisFieldDomain);
2136  //univariate case
2137  if (F.isUnivariate())
2138  {
2139    if (extension == false)
2140      return uniFactorizer (F, alpha, GF);
2141    else
2142    {
2143      CFList source, dest;
2144      A= mapDown (F, info, source, dest);
2145      return uniFactorizer (A, beta, GF);
2146    }
2147  }
2148
2149  //bivariate case
2150  if (A.level() == 2)
2151  {
2152    CFList buf= biFactorize (F, info);
2153    return buf;
2154  }
2155
2156  Variable x= Variable (1);
2157  Variable y= Variable (2);
2158
2159  // remove content
2160  CFList contentAi;
2161  CanonicalForm lcmCont= lcmContent (A, contentAi);
2162  A /= lcmCont;
2163
2164  // trivial after content removal
2165  CFList contentAFactors;
2166  if (A.inCoeffDomain())
2167  {
2168    for (CFListIterator i= contentAi; i.hasItem(); i++)
2169    {
2170      if (i.getItem().inCoeffDomain())
2171        continue;
2172      else
2173      {
2174        lcmCont /= i.getItem();
2175        contentAFactors=
2176        Union (multiFactorize (lcmCont, info),
2177               multiFactorize (i.getItem(), info));
2178        break;
2179      }
2180    }
2181    decompress (contentAFactors, N);
2182    normalize (contentAFactors);
2183    return contentAFactors;
2184  }
2185
2186  // factorize content
2187  contentAFactors= multiFactorize (lcmCont, info);
2188
2189  // univariate after content removal
2190  CFList factors;
2191  if (A.isUnivariate ())
2192  {
2193    factors= uniFactorizer (A, alpha, GF);
2194    append (factors, contentAFactors);
2195    decompress (factors, N);
2196    return factors;
2197  }
2198
2199  // check main variable
2200  int swapLevel= 0;
2201  CanonicalForm derivZ;
2202  CanonicalForm gcdDerivZ;
2203  CanonicalForm bufA= A;
2204  Variable z;
2205  for (int i= 1; i <= A.level(); i++)
2206  {
2207    z= Variable (i);
2208    derivZ= deriv (bufA, z);
2209    if (derivZ.isZero())
2210    {
2211      if (i == 1)
2212        swapLevel= 1;
2213      else
2214        continue;
2215    }
2216    else
2217    {
2218      if (swapLevel == 1)
2219      {
2220        swapLevel= i;
2221        bufA= swapvar (A, x, z);
2222      }
2223      gcdDerivZ= gcd (bufA, derivZ);
2224      if (degree (gcdDerivZ) > 0 && !derivZ.isZero())
2225      {
2226        CanonicalForm g= bufA/gcdDerivZ;
2227        CFList factorsG=
2228        Union (multiFactorize (g, info),
2229               multiFactorize (gcdDerivZ, info));
2230        appendSwapDecompress (factorsG, contentAFactors, N, swapLevel, x);
2231        normalize (factorsG);
2232        return factorsG;
2233      }
2234      else
2235      {
2236        A= bufA;
2237        break;
2238      }
2239    }
2240  }
2241
2242
2243  CFList Aeval, list, evaluation, bufEvaluation, bufAeval;
2244  bool fail= false;
2245  int swapLevel2= 0;
2246  int level;
2247  int factorNums= 3;
2248  CanonicalForm bivarEval;
2249  CFList biFactors, bufBiFactors;
2250  CanonicalForm evalPoly;
2251  int lift, bufLift;
2252  double logarithm= (double) ilog2 (totaldegree (A));
2253  logarithm /= log2exp;
2254  logarithm= ceil (logarithm);
2255  if (factorNums < (int) logarithm)
2256    factorNums= (int) logarithm;
2257  CFList* bufAeval2= new CFList [A.level() - 2];
2258  CFList* Aeval2= new CFList [A.level() - 2];
2259  int counter;
2260  int differentSecondVar= 0;
2261  // several bivariate factorizations
2262  for (int i= 0; i < factorNums; i++)
2263  {
2264    counter= 0;
2265    bufA= A;
2266    bufAeval= CFList();
2267    bufEvaluation= evalPoints (bufA, bufAeval, alpha, list, GF, fail);
2268    evalPoly= 0;
2269
2270    if (fail && (i == 0))
2271    {
2272      if (!swapLevel)
2273        level= 2;
2274      else
2275        level= swapLevel + 1;
2276
2277      CanonicalForm g;
2278      swapLevel2= newMainVariableSearch (A, Aeval, evaluation, alpha, level, g);
2279
2280      if (!swapLevel2) // need to pass to an extension
2281      {
2282        factors= extFactorize (A, info);
2283        appendSwapDecompress (factors, contentAFactors, N, swapLevel, x);
2284        normalize (factors);
2285        delete [] bufAeval2;
2286        delete [] Aeval2;
2287        return factors;
2288      }
2289      else
2290      {
2291        if (swapLevel2 == -1)
2292        {
2293          CFList factorsG=
2294          Union (multiFactorize (g, info),
2295                 multiFactorize (A/g, info));
2296          appendSwapDecompress (factorsG, contentAFactors, N, swapLevel, x);
2297          normalize (factorsG);
2298          delete [] bufAeval2;
2299          delete [] Aeval2;
2300          return factorsG;
2301        }
2302        fail= false;
2303        bufAeval= Aeval;
2304        bufA= A;
2305        bufEvaluation= evaluation;
2306      }
2307    }
2308    else if (fail && (i > 0))
2309      break;
2310
2311    bivarEval= bufEvaluation.getLast();
2312
2313    evaluationWRTDifferentSecondVars (bufAeval2, bufEvaluation, A);
2314
2315    for (int j= 0; j < A.level() - 1; j++)
2316    {
2317      if (!bufAeval2[j].isEmpty())
2318        counter++;
2319    }
2320
2321    bufLift= degree (A, y) + 1 + degree (LC(A, x), y);
2322
2323    TIMING_START (fac_bi_factorizer);
2324    if (!GF && alpha.level() == 1)
2325      bufBiFactors= FpBiSqrfFactorize (bufAeval.getFirst());
2326    else if (GF)
2327      bufBiFactors= GFBiSqrfFactorize (bufAeval.getFirst());
2328    else
2329      bufBiFactors= FqBiSqrfFactorize (bufAeval.getFirst(), alpha);
2330    TIMING_END_AND_PRINT (fac_bi_factorizer,
2331                          "time for bivariate factorization: ");
2332    bufBiFactors.removeFirst();
2333
2334    if (bufBiFactors.length() == 1)
2335    {
2336      if (extension)
2337      {
2338        CFList source, dest;
2339        A= mapDown (A, info, source, dest);
2340      }
2341      factors.append (A);
2342      appendSwapDecompress (factors, contentAFactors, N, swapLevel,
2343                            swapLevel2, x);
2344      normalize (factors);
2345      delete [] bufAeval2;
2346      delete [] Aeval2;
2347      return factors;
2348    }
2349
2350    if (i == 0)
2351    {
2352      Aeval= bufAeval;
2353      evaluation= bufEvaluation;
2354      biFactors= bufBiFactors;
2355      lift= bufLift;
2356      for (int j= 0; j < A.level() - 2; j++)
2357        Aeval2 [j]= bufAeval2 [j];
2358      differentSecondVar= counter;
2359    }
2360    else
2361    {
2362      if (bufBiFactors.length() < biFactors.length() ||
2363          ((bufLift < lift) && (bufBiFactors.length() == biFactors.length())) ||
2364          counter > differentSecondVar)
2365      {
2366        Aeval= bufAeval;
2367        evaluation= bufEvaluation;
2368        biFactors= bufBiFactors;
2369        lift= bufLift;
2370        for (int j= 0; j < A.level() - 2; j++)
2371          Aeval2 [j]= bufAeval2 [j];
2372        differentSecondVar= counter;
2373      }
2374    }
2375    int k= 0;
2376    for (CFListIterator j= bufEvaluation; j.hasItem(); j++, k++)
2377      evalPoly += j.getItem()*power (x, k);
2378    list.append (evalPoly);
2379  }
2380
2381  delete [] bufAeval2;
2382
2383  sortList (biFactors, x);
2384
2385  int minFactorsLength;
2386  bool irred= false;
2387  factorizationWRTDifferentSecondVars (A, Aeval2, info, minFactorsLength, irred);
2388
2389  if (irred)
2390  {
2391    if (extension)
2392    {
2393      CFList source, dest;
2394      A= mapDown (A, info, source, dest);
2395    }
2396    factors.append (A);
2397    appendSwapDecompress (factors, contentAFactors, N, swapLevel,
2398                          swapLevel2, x);
2399    normalize (factors);
2400    delete [] Aeval2;
2401    return factors;
2402  }
2403
2404  if (minFactorsLength == 0)
2405    minFactorsLength= biFactors.length();
2406  else if (biFactors.length() > minFactorsLength)
2407    refineBiFactors (A, biFactors, Aeval2, evaluation, minFactorsLength);
2408
2409  CFList uniFactors= buildUniFactors (biFactors, evaluation.getLast(), y);
2410
2411  CFList * oldAeval= new CFList [A.level() - 2]; //TODO use bufAeval2 for this
2412  for (int i= 0; i < A.level() - 2; i++)
2413    oldAeval[i]= Aeval2[i];
2414
2415  getLeadingCoeffs (A, Aeval2, uniFactors, evaluation);
2416
2417  CFList biFactorsLCs;
2418  for (CFListIterator i= biFactors; i.hasItem(); i++)
2419    biFactorsLCs.append (LC (i.getItem(), 1));
2420
2421
2422  //shifting to zero
2423  A= shift2Zero (A, Aeval, evaluation);
2424
2425  CanonicalForm hh= Lc (Aeval.getFirst());
2426
2427  for (CFListIterator i= Aeval; i.hasItem(); i++)
2428    i.getItem() /= hh;
2429
2430  A /= hh;
2431
2432  Variable v;
2433  CFList leadingCoeffs= precomputeLeadingCoeff (LC (A, 1), biFactorsLCs, alpha,
2434                                          evaluation, Aeval2, A.level() - 2, v);
2435
2436  if (v.level() != 1)
2437  {
2438    A= swapvar (A, y, v);
2439    for (int i= 0; i < A.level() - 2; i++)
2440    {
2441      if (oldAeval[i].isEmpty())
2442        continue;
2443      if (oldAeval[i].getFirst().level() == v.level())
2444      {
2445        biFactors= CFList();
2446        for (CFListIterator iter= oldAeval [i]; iter.hasItem(); iter++)
2447          biFactors.append (swapvar (iter.getItem(), v, y));
2448      }
2449    }
2450    int i= A.level();
2451    CanonicalForm evalPoint;
2452    for (CFListIterator iter= evaluation; iter.hasItem(); iter++, i--)
2453    {
2454      if (i == v.level())
2455      {
2456        evalPoint= iter.getItem();
2457        iter.getItem()= evaluation.getLast();
2458        evaluation.removeLast();
2459        evaluation.append (evalPoint);
2460        break;
2461      }
2462    }
2463    Aeval= evaluateAtZero (A);
2464  }
2465
2466  CFListIterator iter= biFactors;
2467  for (; iter.hasItem(); iter++)
2468    iter.getItem()= iter.getItem () (y + evaluation.getLast(), y);
2469
2470  CanonicalForm oldA= A;
2471  CFList oldBiFactors= biFactors;
2472  if (!leadingCoeffs.getFirst().inCoeffDomain())
2473  {
2474    CanonicalForm tmp= power (leadingCoeffs.getFirst(), biFactors.length() - 1);
2475    A *= tmp;
2476    Aeval= evaluateAtZero (A);
2477    tmp= leadingCoeffs.getFirst();
2478    for (int i= A.level(); i > 2; i--)
2479      tmp= tmp (0, i);
2480    if (!tmp.inCoeffDomain())
2481    {
2482      for (CFListIterator i= biFactors; i.hasItem(); i++)
2483      {
2484        i.getItem() *= tmp/LC (i.getItem(), 1);
2485        i.getItem() /= Lc (i.getItem());
2486      }
2487    }
2488    hh= Lc (Aeval.getFirst());
2489    for (CFListIterator i= Aeval; i.hasItem(); i++)
2490      i.getItem() /= hh;
2491
2492    A /= hh;
2493  }
2494
2495  leadingCoeffs.removeFirst();
2496
2497  //prepare leading coefficients
2498  CFList* leadingCoeffs2= new CFList [A.level() - 2];
2499  prepareLeadingCoeffs (leadingCoeffs2, A.level(), leadingCoeffs, biFactors);
2500
2501  CFArray Pi;
2502  CFList diophant;
2503  int* liftBounds= new int [A.level() - 1];
2504  int liftBoundsLength= A.level() - 1;
2505  for (int i= 0; i < liftBoundsLength; i++)
2506    liftBounds [i]= degree (A, i + 2) + 1;
2507
2508  Aeval.removeFirst();
2509  bool noOneToOne= false;
2510  factors= nonMonicHenselLift (Aeval, biFactors, leadingCoeffs2, diophant,
2511                               Pi, liftBounds, liftBoundsLength, noOneToOne);
2512
2513  if (!noOneToOne)
2514  {
2515    int check= factors.length();
2516    factors= recoverFactors (A, factors);
2517    if (check != factors.length())
2518      noOneToOne= true;
2519
2520    if (extension && !noOneToOne)
2521      factors= extNonMonicFactorRecombination (factors, oldA, info, evaluation);
2522  }
2523  if (noOneToOne)
2524  {
2525    A= oldA;
2526    Aeval= evaluateAtZero (A);
2527    biFactors= oldBiFactors;
2528    CanonicalForm LCA= LC (Aeval.getFirst(), 1);
2529    CanonicalForm yToLift= power (y, lift);
2530    CFListIterator i= biFactors;
2531    lift= degree (i.getItem(), 2) + degree (LC (i.getItem(), 1)) + 1;
2532    i++;
2533
2534    for (; i.hasItem(); i++)
2535      lift= tmax (lift, degree (i.getItem(), 2) + degree (LC (i.getItem(), 1)) + 1);
2536
2537    lift= tmax (degree (Aeval.getFirst() , 2) + 1, lift);
2538
2539    i= biFactors;
2540    yToLift= power (y, lift);
2541    CanonicalForm dummy;
2542    for (; i.hasItem(); i++)
2543    {
2544      LCA= LC (i.getItem(), 1);
2545      extgcd (LCA, yToLift, LCA, dummy);
2546      i.getItem()= mod (i.getItem()*LCA, yToLift);
2547    }
2548
2549    liftBoundsLength= F.level() - 1;
2550    liftBounds= liftingBounds (A, lift);
2551
2552    CFList MOD;
2553    bool earlySuccess;
2554    CFList earlyFactors;
2555    TIMING_START (fac_hensel_lift);
2556    CFList liftedFactors= henselLiftAndEarly
2557                          (A, MOD, liftBounds, earlySuccess, earlyFactors,
2558                           Aeval, biFactors, evaluation, info);
2559    TIMING_END_AND_PRINT (fac_hensel_lift, "time for hensel lifting: ");
2560
2561    if (!extension)
2562    {
2563      TIMING_START (fac_factor_recombination);
2564      factors= factorRecombination (A, liftedFactors, MOD);
2565      TIMING_END_AND_PRINT (fac_factor_recombination,
2566                            "time for factor recombination: ");
2567    }
2568    else
2569    {
2570      TIMING_START (fac_factor_recombination);
2571      factors= extFactorRecombination (liftedFactors, A, MOD, info, evaluation);
2572      TIMING_END_AND_PRINT (fac_factor_recombination,
2573                            "time for factor recombination: ");
2574    }
2575
2576    if (earlySuccess)
2577      factors= Union (factors, earlyFactors);
2578  }
2579
2580  if (!extension)
2581  {
2582    for (CFListIterator i= factors; i.hasItem(); i++)
2583    {
2584      int kk= Aeval.getLast().level();
2585      for (CFListIterator j= evaluation; j.hasItem(); j++, kk--)
2586      {
2587        if (i.getItem().level() < kk)
2588          continue;
2589        i.getItem()= i.getItem() (Variable (kk) - j.getItem(), kk);
2590      }
2591    }
2592  }
2593
2594  if (v.level() != 1)
2595  {
2596    for (CFListIterator iter= factors; iter.hasItem(); iter++)
2597      iter.getItem()= swapvar (iter.getItem(), v, y);
2598  }
2599
2600  swap (factors, swapLevel, swapLevel2, x);
2601  append (factors, contentAFactors);
2602  decompress (factors, N);
2603  normalize (factors);
2604
2605  delete[] liftBounds;
2606
2607  return factors;
2608}
2609
2610/// multivariate factorization over an extension of the initial field
2611CFList
2612extFactorize (const CanonicalForm& F, const ExtensionInfo& info)
2613{
2614  CanonicalForm A= F;
2615
2616  Variable alpha= info.getAlpha();
2617  Variable beta= info.getBeta();
2618  int k= info.getGFDegree();
2619  char cGFName= info.getGFName();
2620  CanonicalForm delta= info.getDelta();
2621  bool GF= (CFFactory::gettype() == GaloisFieldDomain);
2622  Variable w= Variable (1);
2623
2624  CFList factors;
2625  if (!GF && alpha == w)  // we are in F_p
2626  {
2627    CFList factors;
2628    bool extension= true;
2629    int p= getCharacteristic();
2630    if (p*p < (1<<16)) // pass to GF if possible
2631    {
2632      setCharacteristic (getCharacteristic(), 2, 'Z');
2633      ExtensionInfo info= ExtensionInfo (extension);
2634      A= A.mapinto();
2635      factors= multiFactorize (A, info);
2636
2637      Variable vBuf= rootOf (gf_mipo);
2638      setCharacteristic (getCharacteristic());
2639      for (CFListIterator j= factors; j.hasItem(); j++)
2640        j.getItem()= GF2FalphaRep (j.getItem(), vBuf);
2641    }
2642    else  // not able to pass to GF, pass to F_p(\alpha)
2643    {
2644      CanonicalForm mipo= randomIrredpoly (2, Variable (1));
2645      Variable v= rootOf (mipo);
2646      ExtensionInfo info= ExtensionInfo (v);
2647      factors= multiFactorize (A, info);
2648    }
2649    return factors;
2650  }
2651  else if (!GF && (alpha != w)) // we are in F_p(\alpha)
2652  {
2653    if (k == 1) // need factorization over F_p
2654    {
2655      int extDeg= degree (getMipo (alpha));
2656      extDeg++;
2657      CanonicalForm mipo= randomIrredpoly (extDeg + 1, Variable (1));
2658      Variable v= rootOf (mipo);
2659      ExtensionInfo info= ExtensionInfo (v);
2660      factors= biFactorize (A, info);
2661    }
2662    else
2663    {
2664      if (beta == Variable (1))
2665      {
2666        Variable v= chooseExtension (alpha, beta, k);
2667        CanonicalForm primElem, imPrimElem;
2668        bool primFail= false;
2669        Variable vBuf;
2670        primElem= primitiveElement (alpha, vBuf, primFail);
2671        ASSERT (!primFail, "failure in integer factorizer");
2672        if (primFail)
2673          ; //ERROR
2674        else
2675          imPrimElem= mapPrimElem (primElem, vBuf, v);
2676
2677        CFList source, dest;
2678        CanonicalForm bufA= mapUp (A, alpha, v, primElem, imPrimElem,
2679                                   source, dest);
2680        ExtensionInfo info= ExtensionInfo (v, alpha, imPrimElem, primElem);
2681        factors= biFactorize (bufA, info);
2682      }
2683      else
2684      {
2685        Variable v= chooseExtension (alpha, beta, k);
2686        CanonicalForm primElem, imPrimElem;
2687        bool primFail= false;
2688        Variable vBuf;
2689        ASSERT (!primFail, "failure in integer factorizer");
2690        if (primFail)
2691          ; //ERROR
2692        else
2693          imPrimElem= mapPrimElem (delta, beta, v);
2694
2695        CFList source, dest;
2696        CanonicalForm bufA= mapDown (A, info, source, dest);
2697        source= CFList();
2698        dest= CFList();
2699        bufA= mapUp (bufA, beta, v, delta, imPrimElem, source, dest);
2700        ExtensionInfo info= ExtensionInfo (v, beta, imPrimElem, delta);
2701        factors= biFactorize (bufA, info);
2702      }
2703    }
2704    return factors;
2705  }
2706  else // we are in GF (p^k)
2707  {
2708    int p= getCharacteristic();
2709    int extensionDeg= getGFDegree();
2710    bool extension= true;
2711    if (k == 1) // need factorization over F_p
2712    {
2713      extensionDeg++;
2714      if (pow ((double) p, (double) extensionDeg) < (1<<16))
2715      // pass to GF(p^k+1)
2716      {
2717        setCharacteristic (p);
2718        Variable vBuf= rootOf (gf_mipo);
2719        A= GF2FalphaRep (A, vBuf);
2720        setCharacteristic (p, extensionDeg, 'Z');
2721        ExtensionInfo info= ExtensionInfo (extension);
2722        factors= multiFactorize (A.mapinto(), info);
2723      }
2724      else // not able to pass to another GF, pass to F_p(\alpha)
2725      {
2726        setCharacteristic (p);
2727        Variable vBuf= rootOf (gf_mipo);
2728        A= GF2FalphaRep (A, vBuf);
2729        Variable v= chooseExtension (vBuf, beta, k);
2730        ExtensionInfo info= ExtensionInfo (v, extension);
2731        factors= multiFactorize (A, info);
2732      }
2733    }
2734    else // need factorization over GF (p^k)
2735    {
2736      if (pow ((double) p, (double) 2*extensionDeg) < (1<<16))
2737      // pass to GF(p^2k)
2738      {
2739        setCharacteristic (p, 2*extensionDeg, 'Z');
2740        ExtensionInfo info= ExtensionInfo (k, cGFName, extension);
2741        factors= multiFactorize (GFMapUp (A, extensionDeg), info);
2742        setCharacteristic (p, extensionDeg, cGFName);
2743      }
2744      else // not able to pass to GF (p^2k), pass to F_p (\alpha)
2745      {
2746        setCharacteristic (p);
2747        Variable v1= rootOf (gf_mipo);
2748        A= GF2FalphaRep (A, v1);
2749        Variable v2= chooseExtension (v1, v1, k);
2750        CanonicalForm primElem, imPrimElem;
2751        bool primFail= false;
2752        Variable vBuf;
2753        primElem= primitiveElement (v1, v1, primFail);
2754        if (primFail)
2755          ; //ERROR
2756        else
2757          imPrimElem= mapPrimElem (primElem, v1, v2);
2758        CFList source, dest;
2759        CanonicalForm bufA= mapUp (A, v1, v2, primElem, imPrimElem,
2760                                     source, dest);
2761        ExtensionInfo info= ExtensionInfo (v2, v1, imPrimElem, primElem);
2762        factors= multiFactorize (bufA, info);
2763        setCharacteristic (p, k, cGFName);
2764        for (CFListIterator i= factors; i.hasItem(); i++)
2765          i.getItem()= Falpha2GFRep (i.getItem());
2766      }
2767    }
2768    return factors;
2769  }
2770}
2771
2772#endif
2773/* HAVE_NTL */
2774
Note: See TracBrowser for help on using the repository browser.