source: git/factory/facFqFactorize.cc @ 21b8f4c

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