source: git/factory/facFqFactorize.cc @ 81d96c

spielwiese
Last change on this file since 81d96c was 81d96c, checked in by Martin Lee <martinlee84@…>, 12 years ago
chg: more reorganization
  • Property mode set to 100644
File size: 74.9 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    if (LucksWangSparseHeuristic (oldSqrfPartF*power (LC1, factors.length()-1),
1631                                  oldFactors, 1, leadingCoeffs, factors))
1632    {
1633      interMedResult= recoverFactors (oldSqrfPartF, factors);
1634      if (oldFactors.length() == interMedResult.length())
1635        success= true;
1636    }
1637    if (!success)
1638    {
1639      LC1= LC (evalSqrfPartF.getFirst(), 1);
1640
1641      CFArray leadingCoeffs= CFArray (factors.length());
1642      for (int i= 0; i < factors.length(); i++)
1643        leadingCoeffs[i]= LC1;
1644
1645      for (CFListIterator i= factors; i.hasItem(); i++)
1646      {
1647        i.getItem()= i.getItem() (x + evaluation2.getLast(), x);
1648        i.getItem() *= LC1 (0,2)/Lc (i.getItem());
1649      }
1650      factors.insert (1);
1651
1652      CanonicalForm
1653      newSqrfPartF= evalSqrfPartF.getFirst()*power (LC1, factors.length() - 2);
1654
1655      int liftBound= degree (newSqrfPartF,2) + 1;
1656
1657      CFMatrix M= CFMatrix (liftBound, factors.length() - 1);
1658      CFArray Pi;
1659      CFList diophant;
1660      nonMonicHenselLift12 (newSqrfPartF, factors, liftBound, Pi, diophant, M,
1661                            leadingCoeffs, false);
1662
1663      if (sqrfPartF.level() > 2)
1664      {
1665        int* liftBounds= new int [sqrfPartF.level() - 1];
1666        liftBounds [0]= liftBound;
1667        bool noOneToOne= false;
1668        CFList *leadingCoeffs2= new CFList [sqrfPartF.level()-2];
1669        LC1= LC (evalSqrfPartF.getLast(), 1);
1670        CFList LCs;
1671        for (int i= 0; i < factors.length(); i++)
1672          LCs.append (LC1);
1673        leadingCoeffs2 [sqrfPartF.level() - 3]= LCs;
1674        for (int i= sqrfPartF.level() - 1; i > 2; i--)
1675        {
1676          for (CFListIterator j= LCs; j.hasItem(); j++)
1677            j.getItem()= j.getItem() (0, i + 1);
1678          leadingCoeffs2 [i - 3]= LCs;
1679        }
1680        sqrfPartF *= power (LC1, factors.length()-1);
1681
1682        int liftBoundsLength= sqrfPartF.level() - 1;
1683        for (int i= 1; i < liftBoundsLength; i++)
1684          liftBounds [i]= degree (sqrfPartF, i + 2) + 1;
1685        evalSqrfPartF= evaluateAtZero (sqrfPartF);
1686        evalSqrfPartF.removeFirst();
1687        factors= nonMonicHenselLift (evalSqrfPartF, factors, leadingCoeffs2,
1688                 diophant, Pi, liftBounds, sqrfPartF.level() - 1, noOneToOne);
1689        delete [] leadingCoeffs2;
1690        delete [] liftBounds;
1691      }
1692      for (CFListIterator iter= factors; iter.hasItem(); iter++)
1693        iter.getItem()= reverseShift (iter.getItem(), evaluation2, 1);
1694
1695      interMedResult=
1696      recoverFactors (reverseShift(evalSqrfPartF.getLast(),evaluation2,1),
1697                      factors);
1698    }
1699  }
1700  else
1701  {
1702    factors= CFList (oldSqrfPartF);
1703    interMedResult= recoverFactors (oldSqrfPartF, factors);
1704  }
1705
1706  CFList result;
1707  CFFListIterator k;
1708  for (int i= 0; i < LCFFactors.length(); i++)
1709  {
1710    CanonicalForm tmp= 1;
1711    for (k= bufSqrfFactors[i]; k.hasItem(); k++)
1712    {
1713      int pos= findItem (bufFactors, k.getItem().factor());
1714      if (pos)
1715        tmp *= power (getItem (interMedResult, pos), k.getItem().exp());
1716    }
1717    result.append (tmp);
1718  }
1719
1720  for (CFListIterator i= result; i.hasItem(); i++)
1721  {
1722    F /= i.getItem();
1723    if (foundDifferent)
1724      i.getItem()= swapvar (i.getItem(), x, z);
1725    i.getItem()= N (i.getItem());
1726  }
1727
1728  if (foundDifferent)
1729  {
1730    CFList l= differentSecondVarLCs [j];
1731    for (CFListIterator i= l; i.hasItem(); i++)
1732      i.getItem()= swapvar (i.getItem(), y, z);
1733    differentSecondVarLCs [j]= l;
1734    F= swapvar (F, x, z);
1735  }
1736
1737  result.insert (N (F));
1738
1739  result= distributeContent (result, differentSecondVarLCs, lSecondVarLCs);
1740
1741  if (!result.getFirst().inCoeffDomain())
1742  {
1743    CFListIterator i= result;
1744    CanonicalForm tmp;
1745    if (foundDifferent)
1746      i.getItem()= swapvar (i.getItem(), Variable (2), y);
1747
1748    tmp= i.getItem();
1749
1750    i++;
1751    for (; i.hasItem(); i++)
1752    {
1753      if (foundDifferent)
1754        i.getItem()= swapvar (i.getItem(), Variable (2), y)*tmp;
1755      else
1756        i.getItem() *= tmp;
1757    }
1758  }
1759  else
1760    y= Variable (1);
1761
1762  delete [] bufSqrfFactors;
1763
1764  return result;
1765}
1766
1767void
1768evaluationWRTDifferentSecondVars (CFList*& Aeval, const CFList& evaluation,
1769                                  const CanonicalForm& A)
1770{
1771  CanonicalForm tmp;
1772  CFList tmp2;
1773  CFListIterator iter;
1774  for (int i= A.level(); i > 2; i--)
1775  {
1776    tmp= A;
1777    tmp2= CFList();
1778    iter= evaluation;
1779    bool preserveDegree= true;
1780    for (int j= A.level(); j > 1; j--, iter++)
1781    {
1782      if (j == i)
1783        continue;
1784      else
1785      {
1786        tmp= tmp (iter.getItem(), j);
1787        tmp2.insert (tmp);
1788        if ((degree (tmp, i) != degree (A, i)) ||
1789            (degree (tmp, 1) != degree (A, 1)))
1790        {
1791          preserveDegree= false;
1792          break;
1793        }
1794        if (!content(tmp).inCoeffDomain() || !content(tmp,1).inCoeffDomain())
1795        {
1796          preserveDegree= false;
1797          break;
1798        }
1799      }
1800    }
1801    if (preserveDegree)
1802      Aeval [i - 3]= tmp2;
1803    else
1804      Aeval [i - 3]= CFList();
1805  }
1806}
1807
1808#endif
1809
1810static inline
1811CanonicalForm prodEval (const CFList& l, const CanonicalForm& evalPoint,
1812                        const Variable& v)
1813{
1814  CanonicalForm result= 1;
1815  for (CFListIterator i= l; i.hasItem(); i++)
1816    result *= i.getItem() (evalPoint, v);
1817  return result;
1818}
1819
1820//recombine bivariate factors in case one bivariate factorization yields less
1821// factors than the other
1822CFList
1823recombination (const CFList& factors1, const CFList& factors2, int s, int thres,
1824               const CanonicalForm& evalPoint, const Variable& x)
1825{
1826  CFList T, S;
1827
1828  T= factors1;
1829  CFList result;
1830  CanonicalForm buf;
1831  int * v= new int [T.length()];
1832  for (int i= 0; i < T.length(); i++)
1833    v[i]= 0;
1834  bool nosubset= false;
1835  CFArray TT;
1836  TT= copy (factors1);
1837  while (T.length() >= 2*s && s <= thres)
1838  {
1839    while (nosubset == false) 
1840    {
1841      if (T.length() == s) 
1842      {
1843        delete [] v;
1844        result.append (prod (T));
1845        return result;
1846      }
1847      S= subset (v, s, TT, nosubset);
1848      if (nosubset) break;
1849      buf= prodEval (S, evalPoint, x);
1850      buf /= Lc (buf);
1851      if (find (factors2, buf))
1852      {
1853        T= Difference (T, S);
1854        result.append (prod (S));
1855        TT= copy (T);
1856        indexUpdate (v, s, T.length(), nosubset);
1857        if (nosubset) break;
1858      }
1859    }
1860    s++;
1861    if (T.length() < 2*s || T.length() == s) 
1862    {
1863      delete [] v;
1864      result.append (prod (T));
1865      return result;
1866    }
1867    for (int i= 0; i < T.length(); i++)
1868      v[i]= 0;
1869    nosubset= false;
1870  }
1871
1872  delete [] v;
1873  if (T.length() < 2*s)
1874  {
1875    result.append (prod (T));
1876    return result;
1877  }
1878
1879  return result;
1880}
1881
1882#ifdef HAVE_NTL
1883void
1884factorizationWRTDifferentSecondVars (const CanonicalForm& A, CFList*& Aeval,
1885                                     const ExtensionInfo& info,
1886                                     int& minFactorsLength, bool& irred)
1887{
1888  Variable x= Variable (1);
1889  minFactorsLength= 0;
1890  irred= false;
1891  CFList factors;
1892  Variable v;
1893  for (int j= 0; j < A.level() - 2; j++)
1894  {
1895    if (!Aeval[j].isEmpty())
1896    {
1897      v= Variable (Aeval[j].getFirst().level());
1898      if (CFFactory::gettype() == GaloisFieldDomain)
1899        factors= GFBiSqrfFactorize (Aeval[j].getFirst());
1900      else if (info.getAlpha().level() == 1)
1901        factors= FpBiSqrfFactorize (Aeval[j].getFirst());
1902      else
1903        factors= FqBiSqrfFactorize (Aeval[j].getFirst(), info.getAlpha());
1904
1905      factors.removeFirst();
1906      if (minFactorsLength == 0)
1907        minFactorsLength= factors.length();
1908      else
1909        minFactorsLength= tmin (minFactorsLength, factors.length());
1910
1911      if (factors.length() == 1)
1912      {
1913        irred= true;
1914        return;
1915      }
1916      sortList (factors, x);
1917      Aeval [j]= factors;
1918    }
1919  }
1920}
1921
1922void getLeadingCoeffs (const CanonicalForm& A, CFList*& Aeval,
1923                       const CFList& uniFactors, const CFList& evaluation
1924                      )
1925{
1926  CanonicalForm evalPoint;
1927  int i;
1928  CFListIterator iter, iter2;
1929  Variable v;
1930  CFList l, LCs, buf;
1931  int pos;
1932  for (int j= 0; j < A.level() - 2; j++)
1933  {
1934    if (!Aeval[j].isEmpty())
1935    {
1936      i= A.level();
1937      for (iter= evaluation; iter.hasItem(); iter++, i--)
1938      {
1939        if (i == Aeval[j].getFirst().level())
1940        {
1941          evalPoint= iter.getItem();
1942          break;
1943        }
1944      }
1945
1946      v= Variable (i);
1947      if (Aeval[j].length() > uniFactors.length())
1948        Aeval[j]= recombination (Aeval[j], uniFactors, 1,
1949                                 Aeval[j].length() - uniFactors.length() + 1,
1950                                 evalPoint, v);
1951
1952      l= CFList();
1953      buf= buildUniFactors (Aeval[j], evalPoint, v);
1954      for (iter= uniFactors; iter.hasItem(); iter++)
1955      {
1956        pos= findItem (buf, iter.getItem());
1957        if (pos)
1958          l.append (getItem (Aeval[j], pos));
1959      }
1960      Aeval [j]= l;
1961
1962      LCs= CFList();
1963      for (iter= Aeval[j]; iter.hasItem(); iter++)
1964        LCs.append (LC (iter.getItem(), 1));
1965      normalize (LCs);
1966      Aeval[j]= LCs;
1967    }
1968  }
1969}
1970
1971CFList
1972buildUniFactors (const CFList& biFactors, const CanonicalForm& evalPoint,
1973                 const Variable& y)
1974{
1975  CFList result;
1976  CanonicalForm tmp;
1977  for (CFListIterator i= biFactors; i.hasItem(); i++)
1978  {
1979    tmp= mod (i.getItem(), y - evalPoint);
1980    tmp /= Lc (tmp);
1981    result.append (tmp);
1982  }
1983  return result;
1984}
1985
1986void refineBiFactors (const CanonicalForm& A, CFList& biFactors,
1987                      CFList* const& Aeval, const CFList& evaluation,
1988                      int minFactorsLength)
1989{
1990  CFListIterator iter;
1991  CanonicalForm evalPoint;
1992  int i;
1993  Variable v;
1994  Variable y= Variable (2);
1995  CFList list;
1996  for (int j= 0; j < A.level() - 2; j++)
1997  {
1998    if (Aeval[j].length() == minFactorsLength)
1999    {
2000      i= A.level();
2001
2002      for (iter= evaluation; iter.hasItem(); iter++, i--)
2003      {
2004        if (i == Aeval[j].getFirst().level())
2005        {
2006          evalPoint= iter.getItem();
2007          break;
2008        }
2009      }
2010
2011      v= Variable (i);
2012      list= buildUniFactors (Aeval[j], evalPoint, v);
2013
2014      biFactors= recombination (biFactors, list, 1,
2015                                biFactors.length() - list.length() + 1,
2016                                evaluation.getLast(), y);
2017      return;
2018    }
2019  }
2020}
2021
2022void prepareLeadingCoeffs (CFList*& LCs, int n, const CFList& leadingCoeffs,
2023                           const CFList& biFactors, const CFList& evaluation)
2024{
2025  CFList l= leadingCoeffs;
2026  LCs [n-3]= l;
2027  CFListIterator j;
2028  CFListIterator iter= evaluation;
2029  for (int i= n - 1; i > 2; i--, iter++)
2030  {
2031    for (j= l; j.hasItem(); j++)
2032      j.getItem()= j.getItem() (iter.getItem(), i + 1);
2033    LCs [i - 3]= l;
2034  }
2035  l= LCs [0];
2036  for (CFListIterator i= l; i.hasItem(); i++)
2037    i.getItem()= i.getItem() (iter.getItem(), 3);
2038  CFListIterator ii= biFactors;
2039  CFList normalizeFactor;
2040  for (CFListIterator i= l; i.hasItem(); i++, ii++)
2041    normalizeFactor.append (Lc (LC (ii.getItem(), 1))/Lc (i.getItem()));
2042  for (int i= 0; i < n-2; i++)
2043  {
2044    ii= normalizeFactor;
2045    for (j= LCs [i]; j.hasItem(); j++, ii++)
2046      j.getItem() *= ii.getItem();
2047  }
2048}
2049
2050CFList recoverFactors (const CanonicalForm& F, const CFList& factors)
2051{
2052  CFList result;
2053  CanonicalForm tmp, tmp2;
2054  CanonicalForm G= F;
2055  for (CFListIterator i= factors; i.hasItem(); i++)
2056  {
2057    tmp= i.getItem()/content (i.getItem(), 1);
2058    if (fdivides (tmp, G, tmp2))
2059    {
2060      G= tmp2;
2061      result.append (tmp);
2062    }
2063  }
2064  return result;
2065}
2066
2067CFList recoverFactors (const CanonicalForm& F, const CFList& factors,
2068                       const CFList& evaluation)
2069{
2070  CFList result;
2071  CanonicalForm tmp, tmp2;
2072  CanonicalForm G= F;
2073  for (CFListIterator i= factors; i.hasItem(); i++)
2074  {
2075    tmp= reverseShift (i.getItem(), evaluation);
2076    tmp /= content (tmp, 1);
2077    if (fdivides (tmp, G, tmp2))
2078    {
2079      G= tmp2;
2080      result.append (tmp);
2081    }
2082  }
2083  return result;
2084}
2085
2086CFList
2087extNonMonicFactorRecombination (const CFList& factors, const CanonicalForm& F,
2088                                const ExtensionInfo& info)
2089{
2090  Variable alpha= info.getAlpha();
2091  Variable beta= info.getBeta();
2092  CanonicalForm gamma= info.getGamma();
2093  CanonicalForm delta= info.getDelta();
2094  int k= info.getGFDegree();
2095  CFList source, dest;
2096
2097  int degMipoBeta= 1;
2098  if (!k && beta != Variable(1))
2099    degMipoBeta= degree (getMipo (beta));
2100
2101  CFList T, S;
2102  T= factors;
2103  int s= 1;
2104  CFList result;
2105  CanonicalForm quot, buf= F;
2106
2107  CanonicalForm g;
2108  CanonicalForm buf2;
2109  int * v= new int [T.length()];
2110  for (int i= 0; i < T.length(); i++)
2111    v[i]= 0;
2112  bool noSubset= false;
2113  CFArray TT;
2114  TT= copy (factors);
2115  bool recombination= false;
2116  bool trueFactor= false;
2117  while (T.length() >= 2*s)
2118  {
2119    while (noSubset == false)
2120    {
2121      if (T.length() == s)
2122      {
2123        delete [] v;
2124        if (recombination)
2125        {
2126          g= prod (T);
2127          T.removeFirst();
2128          result.append (g/myContent (g));
2129          g /= Lc (g);
2130          appendTestMapDown (result, g, info, source, dest);
2131          return result;
2132        }
2133        else
2134          return CFList (buf);
2135      }
2136
2137      S= subset (v, s, TT, noSubset);
2138      if (noSubset) break;
2139
2140      g= prod (S);
2141      g /= myContent (g);
2142      if (fdivides (g, buf, quot))
2143      {
2144        buf2= g;
2145        buf2 /= Lc (buf2);
2146        if (!k && beta.level() == 1)
2147        {
2148          if (degree (buf2, alpha) < degMipoBeta)
2149          {
2150            appendTestMapDown (result, buf2, info, source, dest);
2151            buf= quot;
2152            recombination= true;
2153            trueFactor= true;
2154          }
2155        }
2156        else
2157        {
2158          if (!isInExtension (buf2, gamma, k, delta, source, dest))
2159          {
2160            appendTestMapDown (result, buf2, info, source, dest);
2161            buf= quot;
2162            recombination= true;
2163            trueFactor= true;
2164          }
2165        }
2166        if (trueFactor)
2167        {
2168          T= Difference (T, S);
2169
2170          if (T.length() < 2*s || T.length() == s)
2171          {
2172            delete [] v;
2173            buf /= Lc (buf);
2174            appendTestMapDown (result, buf, info, source, dest);
2175            return result;
2176          }
2177          trueFactor= false;
2178          TT= copy (T);
2179          indexUpdate (v, s, T.length(), noSubset);
2180          if (noSubset) break;
2181        }
2182      }
2183    }
2184    s++;
2185    if (T.length() < 2*s || T.length() == s)
2186    {
2187      delete [] v;
2188      appendTestMapDown (result, buf, info, source, dest);
2189      return result;
2190    }
2191    for (int i= 0; i < T.length(); i++)
2192      v[i]= 0;
2193    noSubset= false;
2194  }
2195  if (T.length() < 2*s)
2196    appendMapDown (result, F, info, source, dest);
2197
2198  delete [] v;
2199  return result;
2200}
2201
2202CFList
2203extFactorize (const CanonicalForm& F, const ExtensionInfo& info);
2204
2205CFList
2206multiFactorize (const CanonicalForm& F, const ExtensionInfo& info)
2207{
2208
2209  if (F.inCoeffDomain())
2210    return CFList (F);
2211
2212  // compress and find main Variable
2213  CFMap N;
2214  CanonicalForm A= myCompress (F, N);
2215
2216  A /= Lc (A); // make monic
2217
2218  Variable alpha= info.getAlpha();
2219  Variable beta= info.getBeta();
2220  CanonicalForm gamma= info.getGamma();
2221  CanonicalForm delta= info.getDelta();
2222  bool extension= info.isInExtension();
2223  bool GF= (CFFactory::gettype() == GaloisFieldDomain);
2224  //univariate case
2225  if (F.isUnivariate())
2226  {
2227    if (extension == false)
2228      return uniFactorizer (F, alpha, GF);
2229    else
2230    {
2231      CFList source, dest;
2232      A= mapDown (F, info, source, dest);
2233      return uniFactorizer (A, beta, GF);
2234    }
2235  }
2236
2237  //bivariate case
2238  if (A.level() == 2)
2239  {
2240    CFList buf= biFactorize (F, info);
2241    return buf;
2242  }
2243
2244  Variable x= Variable (1);
2245  Variable y= Variable (2);
2246
2247  // remove content
2248  CFList contentAi;
2249  CanonicalForm lcmCont= lcmContent (A, contentAi);
2250  A /= lcmCont;
2251
2252  // trivial after content removal
2253  CFList contentAFactors;
2254  if (A.inCoeffDomain())
2255  {
2256    for (CFListIterator i= contentAi; i.hasItem(); i++)
2257    {
2258      if (i.getItem().inCoeffDomain())
2259        continue;
2260      else
2261      {
2262        lcmCont /= i.getItem();
2263        contentAFactors=
2264        Union (multiFactorize (lcmCont, info),
2265               multiFactorize (i.getItem(), info));
2266        break;
2267      }
2268    }
2269    decompress (contentAFactors, N);
2270    normalize (contentAFactors);
2271    return contentAFactors;
2272  }
2273
2274  // factorize content
2275  contentAFactors= multiFactorize (lcmCont, info);
2276
2277  // univariate after content removal
2278  CFList factors;
2279  if (A.isUnivariate ())
2280  {
2281    factors= uniFactorizer (A, alpha, GF);
2282    append (factors, contentAFactors);
2283    decompress (factors, N);
2284    return factors;
2285  }
2286
2287  // check main variable
2288  int swapLevel= 0;
2289  CanonicalForm derivZ;
2290  CanonicalForm gcdDerivZ;
2291  CanonicalForm bufA= A;
2292  Variable z;
2293  for (int i= 1; i <= A.level(); i++)
2294  {
2295    z= Variable (i);
2296    derivZ= deriv (bufA, z);
2297    if (derivZ.isZero())
2298    {
2299      if (i == 1)
2300        swapLevel= 1;
2301      else
2302        continue;
2303    }
2304    else
2305    {
2306      if (swapLevel == 1)
2307      {
2308        swapLevel= i;
2309        bufA= swapvar (A, x, z);
2310      }
2311      gcdDerivZ= gcd (bufA, derivZ);
2312      if (degree (gcdDerivZ) > 0 && !derivZ.isZero())
2313      {
2314        CanonicalForm g= bufA/gcdDerivZ;
2315        CFList factorsG=
2316        Union (multiFactorize (g, info),
2317               multiFactorize (gcdDerivZ, info));
2318        appendSwapDecompress (factorsG, contentAFactors, N, swapLevel, x);
2319        normalize (factorsG);
2320        return factorsG;
2321      }
2322      else
2323      {
2324        A= bufA;
2325        break;
2326      }
2327    }
2328  }
2329
2330
2331  CFList Aeval, list, evaluation, bufEvaluation, bufAeval;
2332  bool fail= false;
2333  int swapLevel2= 0;
2334  int level;
2335  int factorNums= 3;
2336  CanonicalForm bivarEval;
2337  CFList biFactors, bufBiFactors;
2338  CanonicalForm evalPoly;
2339  int lift, bufLift;
2340  double logarithm= (double) ilog2 (totaldegree (A));
2341  logarithm /= log2exp;
2342  logarithm= ceil (logarithm);
2343  if (factorNums < (int) logarithm)
2344    factorNums= (int) logarithm;
2345  CFList* bufAeval2= new CFList [A.level() - 2];
2346  CFList* Aeval2= new CFList [A.level() - 2];
2347  int counter;
2348  int differentSecondVar= 0;
2349  // several bivariate factorizations
2350  for (int i= 0; i < factorNums; i++)
2351  {
2352    counter= 0;
2353    bufA= A;
2354    bufAeval= CFList();
2355    bufEvaluation= evalPoints (bufA, bufAeval, alpha, list, GF, fail);
2356    evalPoly= 0;
2357
2358    if (fail && (i == 0))
2359    {
2360      if (!swapLevel)
2361        level= 2;
2362      else
2363        level= swapLevel + 1;
2364
2365      CanonicalForm g;
2366      swapLevel2= newMainVariableSearch (A, Aeval, evaluation, alpha, level, g);
2367
2368      if (!swapLevel2) // need to pass to an extension
2369      {
2370        factors= extFactorize (A, info);
2371        appendSwapDecompress (factors, contentAFactors, N, swapLevel, x);
2372        normalize (factors);
2373        delete [] bufAeval2;
2374        delete [] Aeval2;
2375        return factors;
2376      }
2377      else
2378      {
2379        if (swapLevel2 == -1)
2380        {
2381          CFList factorsG=
2382          Union (multiFactorize (g, info),
2383                 multiFactorize (A/g, info));
2384          appendSwapDecompress (factorsG, contentAFactors, N, swapLevel, x);
2385          normalize (factorsG);
2386          delete [] bufAeval2;
2387          delete [] Aeval2;
2388          return factorsG;
2389        }
2390        fail= false;
2391        bufAeval= Aeval;
2392        bufA= A;
2393        bufEvaluation= evaluation;
2394      }
2395    }
2396    else if (fail && (i > 0))
2397      break;
2398
2399    bivarEval= bufEvaluation.getLast();
2400
2401    evaluationWRTDifferentSecondVars (bufAeval2, bufEvaluation, A);
2402
2403    for (int j= 0; j < A.level() - 2; j++)
2404    {
2405      if (!bufAeval2[j].isEmpty())
2406        counter++;
2407    }
2408
2409    bufLift= degree (A, y) + 1 + degree (LC(A, x), y);
2410
2411    TIMING_START (fac_bi_factorizer);
2412    if (!GF && alpha.level() == 1)
2413      bufBiFactors= FpBiSqrfFactorize (bufAeval.getFirst());
2414    else if (GF)
2415      bufBiFactors= GFBiSqrfFactorize (bufAeval.getFirst());
2416    else
2417      bufBiFactors= FqBiSqrfFactorize (bufAeval.getFirst(), alpha);
2418    TIMING_END_AND_PRINT (fac_bi_factorizer,
2419                          "time for bivariate factorization: ");
2420    bufBiFactors.removeFirst();
2421
2422    if (bufBiFactors.length() == 1)
2423    {
2424      if (extension)
2425      {
2426        CFList source, dest;
2427        A= mapDown (A, info, source, dest);
2428      }
2429      factors.append (A);
2430      appendSwapDecompress (factors, contentAFactors, N, swapLevel,
2431                            swapLevel2, x);
2432      normalize (factors);
2433      delete [] bufAeval2;
2434      delete [] Aeval2;
2435      return factors;
2436    }
2437
2438    if (i == 0)
2439    {
2440      Aeval= bufAeval;
2441      evaluation= bufEvaluation;
2442      biFactors= bufBiFactors;
2443      lift= bufLift;
2444      for (int j= 0; j < A.level() - 2; j++)
2445        Aeval2 [j]= bufAeval2 [j];
2446      differentSecondVar= counter;
2447    }
2448    else
2449    {
2450      if (bufBiFactors.length() < biFactors.length() ||
2451          ((bufLift < lift) && (bufBiFactors.length() == biFactors.length())) ||
2452          counter > differentSecondVar)
2453      {
2454        Aeval= bufAeval;
2455        evaluation= bufEvaluation;
2456        biFactors= bufBiFactors;
2457        lift= bufLift;
2458        for (int j= 0; j < A.level() - 2; j++)
2459          Aeval2 [j]= bufAeval2 [j];
2460        differentSecondVar= counter;
2461      }
2462    }
2463    int k= 0;
2464    for (CFListIterator j= bufEvaluation; j.hasItem(); j++, k++)
2465      evalPoly += j.getItem()*power (x, k);
2466    list.append (evalPoly);
2467  }
2468
2469  delete [] bufAeval2;
2470
2471  sortList (biFactors, x);
2472
2473  int minFactorsLength;
2474  bool irred= false;
2475  factorizationWRTDifferentSecondVars (A, Aeval2, info, minFactorsLength, irred);
2476
2477  if (irred)
2478  {
2479    if (extension)
2480    {
2481      CFList source, dest;
2482      A= mapDown (A, info, source, dest);
2483    }
2484    factors.append (A);
2485    appendSwapDecompress (factors, contentAFactors, N, swapLevel,
2486                          swapLevel2, x);
2487    normalize (factors);
2488    delete [] Aeval2;
2489    return factors;
2490  }
2491
2492  if (minFactorsLength == 0)
2493    minFactorsLength= biFactors.length();
2494  else if (biFactors.length() > minFactorsLength)
2495    refineBiFactors (A, biFactors, Aeval2, evaluation, minFactorsLength);
2496  minFactorsLength= tmin (minFactorsLength, biFactors.length());
2497
2498  if (differentSecondVar == A.level() - 2)
2499  {
2500    bool zeroOccured= false;
2501    for (CFListIterator iter= evaluation; iter.hasItem(); iter++)
2502    {
2503      if (iter.getItem().isZero())
2504      {
2505        zeroOccured= true;
2506        break;
2507      }
2508    }
2509    if (!zeroOccured)
2510    {
2511      factors= sparseHeuristic (A, biFactors, Aeval2, evaluation, minFactorsLength);
2512      if (factors.length() == biFactors.length())
2513      {
2514        if (extension)
2515          factors= extNonMonicFactorRecombination (factors, A, info);
2516
2517        appendSwapDecompress (factors, contentAFactors, N, swapLevel,
2518                              swapLevel2, x);
2519        normalize (factors);
2520        delete [] Aeval2;
2521        return factors;
2522      }
2523      else
2524        factors= CFList();
2525      //TODO case where factors.length() > 0
2526    }
2527  }
2528
2529  CFList uniFactors= buildUniFactors (biFactors, evaluation.getLast(), y);
2530
2531  CFList * oldAeval= new CFList [A.level() - 2]; //TODO use bufAeval2 for this
2532  for (int i= 0; i < A.level() - 2; i++)
2533    oldAeval[i]= Aeval2[i];
2534
2535  getLeadingCoeffs (A, Aeval2, uniFactors, evaluation);
2536
2537  CFList biFactorsLCs;
2538  for (CFListIterator i= biFactors; i.hasItem(); i++)
2539    biFactorsLCs.append (LC (i.getItem(), 1));
2540
2541  Variable v;
2542  CFList leadingCoeffs= precomputeLeadingCoeff (LC (A, 1), biFactorsLCs, alpha,
2543                                          evaluation, Aeval2, A.level() - 2, v);
2544
2545  if (v.level() != 1)
2546  {
2547    A= swapvar (A, y, v);
2548    for (int i= 0; i < A.level() - 2; i++)
2549    {
2550      if (oldAeval[i].isEmpty())
2551        continue;
2552      if (oldAeval[i].getFirst().level() == v.level())
2553      {
2554        biFactors= CFList();
2555        for (CFListIterator iter= oldAeval [i]; iter.hasItem(); iter++)
2556          biFactors.append (swapvar (iter.getItem(), v, y));
2557      }
2558    }
2559    int i= A.level();
2560    CanonicalForm evalPoint;
2561    for (CFListIterator iter= evaluation; iter.hasItem(); iter++, i--)
2562    {
2563      if (i == v.level())
2564      {
2565        evalPoint= iter.getItem();
2566        iter.getItem()= evaluation.getLast();
2567        evaluation.removeLast();
2568        evaluation.append (evalPoint);
2569        break;
2570      }
2571    }
2572  }
2573
2574  CFListIterator iter;
2575  CanonicalForm oldA= A;
2576  CFList oldBiFactors= biFactors;
2577  if (!leadingCoeffs.getFirst().inCoeffDomain())
2578  {
2579    CanonicalForm tmp= power (leadingCoeffs.getFirst(), biFactors.length() - 1);
2580    A *= tmp;
2581    tmp= leadingCoeffs.getFirst();
2582    iter= evaluation;
2583    for (int i= A.level(); i > 2; i--, iter++)
2584      tmp= tmp (iter.getItem(), i);
2585    if (!tmp.inCoeffDomain())
2586    {
2587      for (CFListIterator i= biFactors; i.hasItem(); i++)
2588      {
2589        i.getItem() *= tmp/LC (i.getItem(), 1);
2590        i.getItem() /= Lc (i.getItem());
2591      }
2592    }
2593  }
2594
2595  leadingCoeffs.removeFirst();
2596
2597  //prepare leading coefficients
2598  CFList* leadingCoeffs2= new CFList [A.level() - 2];
2599  prepareLeadingCoeffs (leadingCoeffs2, A.level(), leadingCoeffs, biFactors,
2600                        evaluation);
2601
2602  Aeval= evaluateAtEval (A, evaluation, 2);
2603  CanonicalForm hh= Lc (Aeval.getFirst());
2604  for (iter= Aeval; iter.hasItem(); iter++)
2605    iter.getItem() /= hh;
2606
2607  A /= hh;
2608
2609  if (LucksWangSparseHeuristic (A, biFactors, 2, leadingCoeffs2 [A.level() - 3],
2610      factors))
2611  {
2612    int check= factors.length();
2613    factors= recoverFactors (A, factors);
2614
2615    if (check == factors.length())
2616    {
2617      if (extension)
2618        factors= extNonMonicFactorRecombination (factors, A, info);
2619
2620      appendSwapDecompress (factors, contentAFactors, N, swapLevel,
2621                            swapLevel2, x);
2622      normalize (factors);
2623      delete [] Aeval2;
2624      return factors;
2625    }
2626    else
2627      factors= CFList();
2628    //TODO handle this case
2629  }
2630
2631  A= shift2Zero (A, Aeval, evaluation);
2632
2633  for (iter= biFactors; iter.hasItem(); iter++)
2634    iter.getItem()= iter.getItem () (y + evaluation.getLast(), y);
2635
2636  for (int i= 0; i < A.level() - 2; i++)
2637  {
2638    if (i != A.level() - 3)
2639      leadingCoeffs2[i]= CFList();
2640  }
2641  for (iter= leadingCoeffs2[A.level() - 3]; iter.hasItem(); iter++)
2642  {
2643    iter.getItem()= shift2Zero (iter.getItem(), list, evaluation);
2644    for (int i= A.level() - 4; i > -1; i--)
2645    {
2646      if (i + 1 == A.level() - 3)
2647        leadingCoeffs2[i].append (iter.getItem() (0, i + 4));
2648      else
2649        leadingCoeffs2[i].append (leadingCoeffs2[i+1].getLast() (0, i + 4));
2650    }
2651  }
2652
2653  CFArray Pi;
2654  CFList diophant;
2655  int* liftBounds= new int [A.level() - 1];
2656  int liftBoundsLength= A.level() - 1;
2657  for (int i= 0; i < liftBoundsLength; i++)
2658    liftBounds [i]= degree (A, i + 2) + 1;
2659
2660  Aeval.removeFirst();
2661  bool noOneToOne= false;
2662  factors= nonMonicHenselLift (Aeval, biFactors, leadingCoeffs2, diophant,
2663                               Pi, liftBounds, liftBoundsLength, noOneToOne);
2664
2665  if (!noOneToOne)
2666  {
2667    int check= factors.length();
2668    A= oldA;
2669    factors= recoverFactors (A, factors, evaluation);
2670    if (check != factors.length())
2671      noOneToOne= true;
2672
2673    if (extension && !noOneToOne)
2674      factors= extNonMonicFactorRecombination (factors, A, info);
2675  }
2676  if (noOneToOne)
2677  {
2678    A= shift2Zero (oldA, Aeval, evaluation);
2679    biFactors= oldBiFactors;
2680    for (iter= biFactors; iter.hasItem(); iter++)
2681      iter.getItem()= iter.getItem () (y + evaluation.getLast(), y);
2682    CanonicalForm LCA= LC (Aeval.getFirst(), 1);
2683    CanonicalForm yToLift= power (y, lift);
2684    CFListIterator i= biFactors;
2685    lift= degree (i.getItem(), 2) + degree (LC (i.getItem(), 1)) + 1;
2686    i++;
2687
2688    for (; i.hasItem(); i++)
2689      lift= tmax (lift, degree (i.getItem(), 2) + degree (LC (i.getItem(), 1)) + 1);
2690
2691    lift= tmax (degree (Aeval.getFirst() , 2) + 1, lift);
2692
2693    i= biFactors;
2694    yToLift= power (y, lift);
2695    CanonicalForm dummy;
2696    for (; i.hasItem(); i++)
2697    {
2698      LCA= LC (i.getItem(), 1);
2699      extgcd (LCA, yToLift, LCA, dummy);
2700      i.getItem()= mod (i.getItem()*LCA, yToLift);
2701    }
2702
2703    liftBoundsLength= F.level() - 1;
2704    liftBounds= liftingBounds (A, lift);
2705
2706    CFList MOD;
2707    bool earlySuccess;
2708    CFList earlyFactors, liftedFactors;
2709    TIMING_START (fac_hensel_lift);
2710    liftedFactors= henselLiftAndEarly
2711                   (A, MOD, liftBounds, earlySuccess, earlyFactors,
2712                    Aeval, biFactors, evaluation, info);
2713    TIMING_END_AND_PRINT (fac_hensel_lift, "time for hensel lifting: ");
2714
2715    if (!extension)
2716    {
2717      TIMING_START (fac_factor_recombination);
2718      factors= factorRecombination (A, liftedFactors, MOD);
2719      TIMING_END_AND_PRINT (fac_factor_recombination,
2720                            "time for factor recombination: ");
2721    }
2722    else
2723    {
2724      TIMING_START (fac_factor_recombination);
2725      factors= extFactorRecombination (liftedFactors, A, MOD, info, evaluation);
2726      TIMING_END_AND_PRINT (fac_factor_recombination,
2727                            "time for factor recombination: ");
2728    }
2729
2730    if (earlySuccess)
2731      factors= Union (factors, earlyFactors);
2732    if (!extension)
2733    {
2734      for (CFListIterator i= factors; i.hasItem(); i++)
2735      {
2736        int kk= Aeval.getLast().level();
2737        for (CFListIterator j= evaluation; j.hasItem(); j++, kk--)
2738        {
2739          if (i.getItem().level() < kk)
2740            continue;
2741          i.getItem()= i.getItem() (Variable (kk) - j.getItem(), kk);
2742        }
2743      }
2744    }
2745  }
2746
2747  if (v.level() != 1)
2748  {
2749    for (CFListIterator iter= factors; iter.hasItem(); iter++)
2750      iter.getItem()= swapvar (iter.getItem(), v, y);
2751  }
2752
2753  swap (factors, swapLevel, swapLevel2, x);
2754  append (factors, contentAFactors);
2755  decompress (factors, N);
2756  normalize (factors);
2757
2758  delete[] liftBounds;
2759
2760  return factors;
2761}
2762
2763/// multivariate factorization over an extension of the initial field
2764CFList
2765extFactorize (const CanonicalForm& F, const ExtensionInfo& info)
2766{
2767  CanonicalForm A= F;
2768
2769  Variable alpha= info.getAlpha();
2770  Variable beta= info.getBeta();
2771  int k= info.getGFDegree();
2772  char cGFName= info.getGFName();
2773  CanonicalForm delta= info.getDelta();
2774  bool GF= (CFFactory::gettype() == GaloisFieldDomain);
2775  Variable w= Variable (1);
2776
2777  CFList factors;
2778  if (!GF && alpha == w)  // we are in F_p
2779  {
2780    CFList factors;
2781    bool extension= true;
2782    int p= getCharacteristic();
2783    if (p*p < (1<<16)) // pass to GF if possible
2784    {
2785      setCharacteristic (getCharacteristic(), 2, 'Z');
2786      ExtensionInfo info= ExtensionInfo (extension);
2787      A= A.mapinto();
2788      factors= multiFactorize (A, info);
2789
2790      Variable vBuf= rootOf (gf_mipo);
2791      setCharacteristic (getCharacteristic());
2792      for (CFListIterator j= factors; j.hasItem(); j++)
2793        j.getItem()= GF2FalphaRep (j.getItem(), vBuf);
2794    }
2795    else  // not able to pass to GF, pass to F_p(\alpha)
2796    {
2797      CanonicalForm mipo= randomIrredpoly (2, w);
2798      Variable v= rootOf (mipo);
2799      ExtensionInfo info= ExtensionInfo (v);
2800      factors= multiFactorize (A, info);
2801    }
2802    return factors;
2803  }
2804  else if (!GF && (alpha != w)) // we are in F_p(\alpha)
2805  {
2806    if (k == 1) // need factorization over F_p
2807    {
2808      int extDeg= degree (getMipo (alpha));
2809      extDeg++;
2810      CanonicalForm mipo= randomIrredpoly (extDeg + 1, w);
2811      Variable v= rootOf (mipo);
2812      ExtensionInfo info= ExtensionInfo (v);
2813      factors= biFactorize (A, info);
2814    }
2815    else
2816    {
2817      if (beta == w)
2818      {
2819        Variable v= chooseExtension (alpha, beta, k);
2820        CanonicalForm primElem, imPrimElem;
2821        bool primFail= false;
2822        Variable vBuf;
2823        primElem= primitiveElement (alpha, vBuf, primFail);
2824        ASSERT (!primFail, "failure in integer factorizer");
2825        if (primFail)
2826          ; //ERROR
2827        else
2828          imPrimElem= mapPrimElem (primElem, vBuf, v);
2829
2830        CFList source, dest;
2831        CanonicalForm bufA= mapUp (A, alpha, v, primElem, imPrimElem,
2832                                   source, dest);
2833        ExtensionInfo info= ExtensionInfo (v, alpha, imPrimElem, primElem);
2834        factors= biFactorize (bufA, info);
2835      }
2836      else
2837      {
2838        Variable v= chooseExtension (alpha, beta, k);
2839        CanonicalForm primElem, imPrimElem;
2840        bool primFail= false;
2841        Variable vBuf;
2842        ASSERT (!primFail, "failure in integer factorizer");
2843        if (primFail)
2844          ; //ERROR
2845        else
2846          imPrimElem= mapPrimElem (delta, beta, v);
2847
2848        CFList source, dest;
2849        CanonicalForm bufA= mapDown (A, info, source, dest);
2850        source= CFList();
2851        dest= CFList();
2852        bufA= mapUp (bufA, beta, v, delta, imPrimElem, source, dest);
2853        ExtensionInfo info= ExtensionInfo (v, beta, imPrimElem, delta);
2854        factors= biFactorize (bufA, info);
2855      }
2856    }
2857    return factors;
2858  }
2859  else // we are in GF (p^k)
2860  {
2861    int p= getCharacteristic();
2862    int extensionDeg= getGFDegree();
2863    bool extension= true;
2864    if (k == 1) // need factorization over F_p
2865    {
2866      extensionDeg++;
2867      if (pow ((double) p, (double) extensionDeg) < (1<<16))
2868      // pass to GF(p^k+1)
2869      {
2870        setCharacteristic (p);
2871        Variable vBuf= rootOf (gf_mipo);
2872        A= GF2FalphaRep (A, vBuf);
2873        setCharacteristic (p, extensionDeg, 'Z');
2874        ExtensionInfo info= ExtensionInfo (extension);
2875        factors= multiFactorize (A.mapinto(), info);
2876      }
2877      else // not able to pass to another GF, pass to F_p(\alpha)
2878      {
2879        setCharacteristic (p);
2880        Variable vBuf= rootOf (gf_mipo);
2881        A= GF2FalphaRep (A, vBuf);
2882        Variable v= chooseExtension (vBuf, beta, k);
2883        ExtensionInfo info= ExtensionInfo (v, extension);
2884        factors= multiFactorize (A, info);
2885      }
2886    }
2887    else // need factorization over GF (p^k)
2888    {
2889      if (pow ((double) p, (double) 2*extensionDeg) < (1<<16))
2890      // pass to GF(p^2k)
2891      {
2892        setCharacteristic (p, 2*extensionDeg, 'Z');
2893        ExtensionInfo info= ExtensionInfo (k, cGFName, extension);
2894        factors= multiFactorize (GFMapUp (A, extensionDeg), info);
2895        setCharacteristic (p, extensionDeg, cGFName);
2896      }
2897      else // not able to pass to GF (p^2k), pass to F_p (\alpha)
2898      {
2899        setCharacteristic (p);
2900        Variable v1= rootOf (gf_mipo);
2901        A= GF2FalphaRep (A, v1);
2902        Variable v2= chooseExtension (v1, v1, k);
2903        CanonicalForm primElem, imPrimElem;
2904        bool primFail= false;
2905        Variable vBuf;
2906        primElem= primitiveElement (v1, v1, primFail);
2907        if (primFail)
2908          ; //ERROR
2909        else
2910          imPrimElem= mapPrimElem (primElem, v1, v2);
2911        CFList source, dest;
2912        CanonicalForm bufA= mapUp (A, v1, v2, primElem, imPrimElem,
2913                                     source, dest);
2914        ExtensionInfo info= ExtensionInfo (v2, v1, imPrimElem, primElem);
2915        factors= multiFactorize (bufA, info);
2916        setCharacteristic (p, k, cGFName);
2917        for (CFListIterator i= factors; i.hasItem(); i++)
2918          i.getItem()= Falpha2GFRep (i.getItem());
2919      }
2920    }
2921    return factors;
2922  }
2923}
2924
2925#endif
2926/* HAVE_NTL */
2927
Note: See TracBrowser for help on using the repository browser.