source: git/factory/fac_absfact.cc @ dea3d2

spielwiese
Last change on this file since dea3d2 was dea3d2, checked in by Martin Lee <martinlee84@…>, 11 years ago
add: rudimentary Bertone/Cheze/Galligo absolute factorization Conflicts: factory/GNUmakefile.in
  • Property mode set to 100644
File size: 9.3 KB
Line 
1/*****************************************************************************\
2 * Computer Algebra System SINGULAR
3\*****************************************************************************/
4/** @file facMul.cc
5 *
6 * This file implements functions for fast multiplication and division with
7 * remainder
8 *
9 * @author Martin Lee
10 *
11 **/
12/*****************************************************************************/
13
14#include "fac_absfact.h"
15#include "cf_reval.h"
16#include "cf_primes.h"
17#include "cf_algorithm.h"
18#ifdef HAVE_FLINT
19#include "FLINTconvert.h"
20#include <flint/fmpz_poly_factor.h>
21#endif
22#ifdef HAVE_NTL
23#include "NTLconvert.h"
24#include <NTL/LLL.h>
25#endif
26
27#ifdef HAVE_FLINT
28void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
29
30int choosePoint (const CanonicalForm& F, int tdegF, CFArray& eval)
31{
32  REvaluation E1 (1, 1, IntRandom (25));
33  REvaluation E2 (2, 2, IntRandom (25));
34  //E1.nextpoint();
35  //E2.nextpoint();
36  CanonicalForm f, Fp;
37  int i;
38  eval=CFArray (2);
39  while (1)
40  {
41    f= E1(F);
42    if (!f.isZero() && factorize (f).length() == 2)
43    {
44      Off (SW_RATIONAL);
45      f= E2(f);
46      out_cf ("f= ", f, "\n");
47      if (!f.isZero() && f > cf_getSmallPrime (cf_getNumSmallPrimes()))
48      {
49        for (i= cf_getNumPrimes()-1; i > 0; i--)
50        {
51          if (f % CanonicalForm (cf_getPrime (i)) == 0)
52          {
53            Fp= mod (F,cf_getPrime(i));
54            if (totaldegree (Fp) == tdegF)
55            {
56              eval[0]= E1[1];
57              eval[1]= E2[2];
58              return cf_getPrime(i);
59            }
60          }
61        }
62      }
63      else if (!f.isZero())
64      {
65        for (i= cf_getNumSmallPrimes()-1; i > 0; i--)
66        {
67          if (f % CanonicalForm (cf_getSmallPrime (i)) == 0)
68          {
69            Fp= mod (F,cf_getSmallPrime(i));
70            if (totaldegree (Fp) == tdegF)
71            {
72              eval[0]= E1[1];
73              eval[1]= E2[2];
74              return cf_getSmallPrime(i);
75            }
76          }
77        }
78      }
79      E2.nextpoint();
80      On (SW_RATIONAL);
81    }
82    E1.nextpoint();
83  }
84  return 0;
85}
86
87CFList absFactorize (const CanonicalForm& G)
88{
89  out_cf ("maxNorm (G)= ", maxNorm (G), "\n");
90  //F is assumed to be bivariate, irreducible over Q, primitive wrt x and y, compressed
91
92  CanonicalForm F= bCommonDen (G)*G;
93  Off (SW_RATIONAL);
94  //out_cf ("icontent (F)= ", icontent (F), "\n");
95  F /= icontent (F);
96  out_cf ("LC (F,1)= ", LC (F,1), "\n");
97  out_cf ("LC (F,2)= ", LC (F,2), "\n");
98  On (SW_RATIONAL);
99  CFArray eval;
100  int minTdeg, tdegF= totaldegree (F);
101  CanonicalForm Fp, smallestFactor;
102  int p;
103  while (1)
104  {
105    printf ("enter while\n");
106    p= choosePoint (F, tdegF, eval);
107
108    printf ("p= %d\n", p);
109    setCharacteristic (p);
110    Fp=F.mapinto();
111    CFFList factors= factorize (Fp);
112
113    printf ("after factorize\n");
114    factors.removeFirst();
115    CFFListIterator iter= factors;
116    smallestFactor= iter.getItem().factor();
117    minTdeg= totaldegree (smallestFactor);
118    iter++;
119    for (; iter.hasItem(); iter++)
120    {
121      if (totaldegree (iter.getItem().factor()) < minTdeg)
122      {
123        smallestFactor= iter.getItem().factor();
124        minTdeg= totaldegree (smallestFactor);
125      }
126    }
127    if (tdegF % minTdeg == 0)
128      break;
129    else
130      printf ("fail");
131  }
132  out_cf ("eval[0]= ", eval[0], "\n");
133  out_cf ("eval[1]= ", eval[1], "\n");
134  //out_cf ("Fp= ", Fp, "\n");
135  CanonicalForm Gp= Fp/smallestFactor;
136  printf ("Fp==Gp*smallestFactor? %d\n",Fp==Gp*smallestFactor);
137  Gp= Gp (eval[0].mapinto(), 1);
138  Fp= Fp (eval[0].mapinto(), 1);
139  CanonicalForm smallestFactorEval= smallestFactor (eval[0].mapinto(),1);
140  out_cf ("Fp= ", Fp, "\n");
141  out_cf ("Gp= ", Gp, "\n");
142  out_cf ("smallestFactorEval= ", smallestFactorEval, "\n");
143  //out_cf ("Gp*smallestFactorEval= ", Gp*smallestFactorEval, "\n");
144  printf ("Gp*smallestFactorEval==Fp? %d\n", Gp*smallestFactorEval == Fp);
145  setCharacteristic (0);
146  out_cf ("eval[0]= ", eval[0], "\n");
147  CanonicalForm F1= F(eval[0],1);
148  setCharacteristic (p);
149  printf ("F1.mapinto()==Fp? %d\n", Fp==F1.mapinto());
150  setCharacteristic (0);
151  //out_cf ("Fp= ", Fp, "\n");
152  //out_cf ("F1= ", F1, "\n");
153  int s= tdegF/minTdeg + 1;
154  CanonicalForm bound= power (maxNorm (F1), 2*(s-1));
155  bound *= power (CanonicalForm (s),s-1);
156  bound *= power (CanonicalForm (2), ((s-1)*(s-1))/2); //possible int overflow
157
158  CanonicalForm B = p;
159  long k = 1L;
160  while ( B < bound ) {
161    B *= p;
162    k++;
163  }
164
165  printf ("k= %ld\n", k);
166  //k= 73L; //reicht fÃŒr bertoneinput1
167  //k= 92L;
168  //k= 128L;
169  //k= 141L;
170  //k=256L;
171  //k= 512L;
172  //k= 1024L;
173  printf ("k= %ld\n", k);
174  k= k+1;
175  fmpz_poly_t FLINTF1;
176  convertFacCF2Fmpz_poly_t (FLINTF1, F1);
177  /*fmpz_poly_print_pretty (FLINTF1, "x");
178  printf ("\n");*/
179  setCharacteristic (p);
180  nmod_poly_t FLINTFp, FLINTGp;
181  convertFacCF2nmod_poly_t (FLINTFp, smallestFactorEval/lc (smallestFactorEval));
182  convertFacCF2nmod_poly_t (FLINTGp, Gp/lc (Gp));
183  printf ("test= %d\n", (smallestFactorEval/lc (smallestFactorEval))*(Gp/lc (Gp))*lc (Fp) == Fp);
184  nmod_poly_factor_t nmodFactors;
185  nmod_poly_factor_init (nmodFactors);
186  nmod_poly_factor_insert (nmodFactors, FLINTFp, 1L); //wird da vorne drangehangen oder hinten?
187  nmod_poly_factor_insert (nmodFactors, FLINTGp, 1L);
188
189  long * link= new long [2];
190  fmpz_poly_t *v= new fmpz_poly_t[2];
191  fmpz_poly_t *w= new fmpz_poly_t[2];
192  fmpz_poly_init(v[0]);
193  fmpz_poly_init(v[1]);
194  fmpz_poly_init(w[0]);
195  fmpz_poly_init(w[1]);
196
197  fmpz_poly_factor_t liftedFactors;
198  fmpz_poly_factor_init (liftedFactors);
199  _fmpz_poly_hensel_start_lift(liftedFactors, link, v, w, FLINTF1, nmodFactors, k); //lift factors up to p^k
200  //fmpz_poly_hensel_lift_once (liftedFactors, FLINTF1, nmodFactors, k);
201
202  nmod_poly_factor_clear (nmodFactors);
203  nmod_poly_clear (FLINTFp);
204  nmod_poly_clear (FLINTGp);
205
206  //out_cf ("smallestFactorEval= ", smallestFactorEval, "\n");
207  printf ("after lift\n");
208  setCharacteristic(0);
209  modpk pk= modpk (p,k);
210  printf ("after modpk\n");
211  /*printf ("liftedSmallestFactor= ");
212  fmpz_poly_print_pretty ((fmpz_poly_t &) liftedFactors->p[0], "x");
213  printf ("\n");*/
214  CanonicalForm liftedSmallestFactor= convertFmpz_poly_t2FacCF ((fmpz_poly_t &)liftedFactors->p[0],Variable (2));
215  out_cf ("liftedSmallestFactor= ", liftedSmallestFactor, "\n");
216  //liftedSmallestFactor *= lc (F1);
217 
218  //liftedSmallestFactor= pk (liftedSmallestFactor);
219  //out_cf ("icontent (liftedSmallestFactor)= ", icontent (liftedSmallestFactor), "\n");
220  //liftedSmallestFactor /= icontent (liftedSmallestFactor);
221  CanonicalForm otherFactor= convertFmpz_poly_t2FacCF ((fmpz_poly_t &)liftedFactors->p[1],Variable (2));
222  //otherFactor *= lc (F1);
223  //otherFactor= pk (otherFactor);
224  //out_cf ("icontent (otherFactor)= ", icontent (otherFactor), "\n");
225  printf ("isOn (SW_RATIONAL)= %d\n", isOn (SW_RATIONAL));
226  //out_cf ("F1= ", F1, "\n");
227  CanonicalForm test= pk (otherFactor*liftedSmallestFactor);
228  printf ("test == F1 %d\n", test == F1);
229  //out_cf ("otherFactor*smallestFactor= ", test, "\n");
230
231  setCharacteristic (p);
232  //out_cf ("F1.mapinto()= ", F1.mapinto(), "\n");
233  //out_cf ("test.mapinto()= ", test.mapinto(), "\n");
234  printf ("test.mapinto() == F1.mapinto() ? %d\n", test.mapinto() == F1.mapinto());
235  setCharacteristic (0);
236  //out_cf ("liftedSmallestFactor= ", liftedSmallestFactor, "\n");
237  //out_cf ("otherFactor= ", otherFactor, "\n");
238
239  Off (SW_SYMMETRIC_FF);
240  liftedSmallestFactor= pk (liftedSmallestFactor);
241  //out_cf ("pk.getpk()= ", pk.getpk(), "\n");
242  //out_cf ("liftedSmallestFactor= ", liftedSmallestFactor, "\n");
243  liftedSmallestFactor= liftedSmallestFactor (eval[1],2);
244  On (SW_SYMMETRIC_FF);
245  CFMatrix M= CFMatrix (s, s);
246  M(s,s)= power (CanonicalForm (p), k);
247  for (int i= 1; i < s; i++)
248  {
249    M (i,i)= 1;
250    M (i+1,i)= -liftedSmallestFactor;
251  }
252
253  printf ("before LLL\n");
254  /*for (int i= 1; i <= s; i++)
255  {
256    printf ("M (i)= ");
257    for (int j= 1; j <=s; j++)
258      out_cf (" ", M(i,j), "");
259    printf ("\n");
260  }*/
261
262  mat_ZZ NTLM= *convertFacCFMatrix2NTLmat_ZZ (M);
263
264  ZZ det;
265
266  transpose (NTLM, NTLM);
267  long r=LLL (det, NTLM, 1L, 1L);
268  transpose (NTLM, NTLM);
269  M= *convertNTLmat_ZZ2FacCFMatrix (NTLM);
270
271  printf ("r= %ld\n", r);
272  printf ("s= %d\n", s);
273  /*for (int i= 1; i <= s; i++)
274  {
275    printf ("M (i)= ");
276    for (int j= 1; j <=s; j++)
277      out_cf (" ", M(i,j), "");
278    printf ("\n");
279  }*/
280  CanonicalForm mipo= 0;
281  Variable x= Variable (1);
282  for (int i= 1; i <= s; i++)
283    mipo += M (i,1)*power (x,s-i);
284
285  out_cf ("/= ", F1 (eval[1],2)*lc(mipo), "\n");
286  out_cf ("mipo/= ", mipo (0,1)*lc (F1), "\n");
287  CFFList mipoFactors= factorize (mipo);
288  printf ("mipoFactors.length()= %d\n", mipoFactors.length());
289  mipoFactors.removeFirst();
290  out_cf ("mipoFactors.getFirst()= ", mipoFactors.getFirst().factor(), "\n");
291  //out_cf ("lc (F1)= ", lc (F1), "\n");
292  //out_cf ("F1eval= ", F1 (eval[1],2), "\n");
293  //out_cf ("F1= ", F1, "\n");
294  //out_cf ("icontent (mipo)= ", icontent (mipo), "\n");
295  On (SW_RATIONAL);
296  Variable alpha= rootOf (mipo);
297  CFFList QaFactors= factorize (F1, alpha);
298  for (CFFListIterator iter= QaFactors; iter.hasItem(); iter++)
299    out_cf ("iter.getItem()= ", iter.getItem().factor(), "\n");
300
301  fmpz_poly_clear (v[0]);
302  fmpz_poly_clear (v[1]);
303  fmpz_poly_clear (w[0]);
304  fmpz_poly_clear (w[1]);
305  delete [] v;
306  delete [] w;
307  delete [] link;
308  fmpz_poly_factor_clear (liftedFactors);
309  //TODO delete v, wclear
310  return CFList();
311}
312#endif
313
314
Note: See TracBrowser for help on using the repository browser.