source: git/factory/facFqFactorize.cc @ b1d287

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