source: git/factory/facFqFactorize.cc @ c729f2

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