1 | /*****************************************************************************\ |
---|
2 | * Computer Algebra System SINGULAR |
---|
3 | \*****************************************************************************/ |
---|
4 | /** @file facAbsFact.cc |
---|
5 | * |
---|
6 | * @author Martin Lee |
---|
7 | * |
---|
8 | **/ |
---|
9 | /*****************************************************************************/ |
---|
10 | |
---|
11 | #include "fac_absfact.h" |
---|
12 | #include "cf_reval.h" |
---|
13 | #include "cf_primes.h" |
---|
14 | #include "cf_algorithm.h" |
---|
15 | #ifdef HAVE_FLINT |
---|
16 | #include "FLINTconvert.h" |
---|
17 | #include <flint/fmpz_poly_factor.h> |
---|
18 | #endif |
---|
19 | #ifdef HAVE_NTL |
---|
20 | #include "NTLconvert.h" |
---|
21 | #include <NTL/LLL.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #ifdef HAVE_FLINT |
---|
25 | |
---|
26 | int choosePoint (const CanonicalForm& F, int tdegF, CFArray& eval) |
---|
27 | { |
---|
28 | REvaluation E1 (1, 1, IntRandom (25)); |
---|
29 | REvaluation E2 (2, 2, IntRandom (25)); |
---|
30 | //E1.nextpoint(); |
---|
31 | //E2.nextpoint(); |
---|
32 | CanonicalForm f, Fp; |
---|
33 | int i; |
---|
34 | eval=CFArray (2); |
---|
35 | while (1) |
---|
36 | { |
---|
37 | f= E1(F); |
---|
38 | if (!f.isZero() && factorize (f).length() == 2) |
---|
39 | { |
---|
40 | Off (SW_RATIONAL); |
---|
41 | f= E2(f); |
---|
42 | if (!f.isZero() && f > cf_getSmallPrime (cf_getNumSmallPrimes())) |
---|
43 | { |
---|
44 | for (i= cf_getNumPrimes()-1; i > 0; i--) |
---|
45 | { |
---|
46 | if (f % CanonicalForm (cf_getPrime (i)) == 0) |
---|
47 | { |
---|
48 | Fp= mod (F,cf_getPrime(i)); |
---|
49 | if (totaldegree (Fp) == tdegF) |
---|
50 | { |
---|
51 | eval[0]= E1[1]; |
---|
52 | eval[1]= E2[2]; |
---|
53 | return cf_getPrime(i); |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | else if (!f.isZero()) |
---|
59 | { |
---|
60 | for (i= cf_getNumSmallPrimes()-1; i > 0; i--) |
---|
61 | { |
---|
62 | if (f % CanonicalForm (cf_getSmallPrime (i)) == 0) |
---|
63 | { |
---|
64 | Fp= mod (F,cf_getSmallPrime(i)); |
---|
65 | if (totaldegree (Fp) == tdegF) |
---|
66 | { |
---|
67 | eval[0]= E1[1]; |
---|
68 | eval[1]= E2[2]; |
---|
69 | return cf_getSmallPrime(i); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | E2.nextpoint(); |
---|
75 | On (SW_RATIONAL); |
---|
76 | } |
---|
77 | E1.nextpoint(); |
---|
78 | } |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | CFList absFactorize (const CanonicalForm& G) |
---|
83 | { |
---|
84 | //F is assumed to be bivariate, irreducible over Q, primitive wrt x and y, compressed |
---|
85 | |
---|
86 | CanonicalForm F= bCommonDen (G)*G; |
---|
87 | Off (SW_RATIONAL); |
---|
88 | F /= icontent (F); |
---|
89 | On (SW_RATIONAL); |
---|
90 | CFArray eval; |
---|
91 | int minTdeg, tdegF= totaldegree (F); |
---|
92 | CanonicalForm Fp, smallestFactor; |
---|
93 | int p; |
---|
94 | while (1) |
---|
95 | { |
---|
96 | p= choosePoint (F, tdegF, eval); |
---|
97 | |
---|
98 | setCharacteristic (p); |
---|
99 | Fp=F.mapinto(); |
---|
100 | CFFList factors= factorize (Fp); |
---|
101 | |
---|
102 | factors.removeFirst(); |
---|
103 | CFFListIterator iter= factors; |
---|
104 | smallestFactor= iter.getItem().factor(); |
---|
105 | minTdeg= totaldegree (smallestFactor); |
---|
106 | iter++; |
---|
107 | for (; iter.hasItem(); iter++) |
---|
108 | { |
---|
109 | if (totaldegree (iter.getItem().factor()) < minTdeg) |
---|
110 | { |
---|
111 | smallestFactor= iter.getItem().factor(); |
---|
112 | minTdeg= totaldegree (smallestFactor); |
---|
113 | } |
---|
114 | } |
---|
115 | if (tdegF % minTdeg == 0) |
---|
116 | break; |
---|
117 | //TODO else |
---|
118 | } |
---|
119 | CanonicalForm Gp= Fp/smallestFactor; |
---|
120 | Gp= Gp (eval[0].mapinto(), 1); |
---|
121 | Fp= Fp (eval[0].mapinto(), 1); |
---|
122 | CanonicalForm smallestFactorEval= smallestFactor (eval[0].mapinto(),1); |
---|
123 | setCharacteristic (0); |
---|
124 | CanonicalForm F1= F(eval[0],1); |
---|
125 | setCharacteristic (p); |
---|
126 | setCharacteristic (0); |
---|
127 | int s= tdegF/minTdeg + 1; |
---|
128 | CanonicalForm bound= power (maxNorm (F1), 2*(s-1)); |
---|
129 | bound *= power (CanonicalForm (s),s-1); |
---|
130 | bound *= power (CanonicalForm (2), ((s-1)*(s-1))/2); //possible int overflow |
---|
131 | |
---|
132 | CanonicalForm B = p; |
---|
133 | long k = 1L; |
---|
134 | while ( B < bound ) { |
---|
135 | B *= p; |
---|
136 | k++; |
---|
137 | } |
---|
138 | |
---|
139 | //TODO take floor (log2(k)) |
---|
140 | k= k+1; |
---|
141 | fmpz_poly_t FLINTF1; |
---|
142 | convertFacCF2Fmpz_poly_t (FLINTF1, F1); |
---|
143 | setCharacteristic (p); |
---|
144 | nmod_poly_t FLINTFp, FLINTGp; |
---|
145 | convertFacCF2nmod_poly_t (FLINTFp, smallestFactorEval/lc (smallestFactorEval)); |
---|
146 | convertFacCF2nmod_poly_t (FLINTGp, Gp/lc (Gp)); |
---|
147 | nmod_poly_factor_t nmodFactors; |
---|
148 | nmod_poly_factor_init (nmodFactors); |
---|
149 | nmod_poly_factor_insert (nmodFactors, FLINTFp, 1L); |
---|
150 | nmod_poly_factor_insert (nmodFactors, FLINTGp, 1L); |
---|
151 | |
---|
152 | long * link= new long [2]; |
---|
153 | fmpz_poly_t *v= new fmpz_poly_t[2]; |
---|
154 | fmpz_poly_t *w= new fmpz_poly_t[2]; |
---|
155 | fmpz_poly_init(v[0]); |
---|
156 | fmpz_poly_init(v[1]); |
---|
157 | fmpz_poly_init(w[0]); |
---|
158 | fmpz_poly_init(w[1]); |
---|
159 | |
---|
160 | fmpz_poly_factor_t liftedFactors; |
---|
161 | fmpz_poly_factor_init (liftedFactors); |
---|
162 | _fmpz_poly_hensel_start_lift(liftedFactors, link, v, w, FLINTF1, nmodFactors, k); //lift factors up to p^k |
---|
163 | |
---|
164 | nmod_poly_factor_clear (nmodFactors); |
---|
165 | nmod_poly_clear (FLINTFp); |
---|
166 | nmod_poly_clear (FLINTGp); |
---|
167 | |
---|
168 | setCharacteristic(0); |
---|
169 | modpk pk= modpk (p,k); |
---|
170 | CanonicalForm liftedSmallestFactor= convertFmpz_poly_t2FacCF ((fmpz_poly_t &)liftedFactors->p[0],Variable (2)); |
---|
171 | |
---|
172 | CanonicalForm otherFactor= convertFmpz_poly_t2FacCF ((fmpz_poly_t &)liftedFactors->p[1],Variable (2)); |
---|
173 | CanonicalForm test= pk (otherFactor*liftedSmallestFactor); |
---|
174 | |
---|
175 | Off (SW_SYMMETRIC_FF); |
---|
176 | liftedSmallestFactor= pk (liftedSmallestFactor); |
---|
177 | liftedSmallestFactor= liftedSmallestFactor (eval[1],2); |
---|
178 | On (SW_SYMMETRIC_FF); |
---|
179 | CFMatrix M= CFMatrix (s, s); |
---|
180 | M(s,s)= power (CanonicalForm (p), k); |
---|
181 | for (int i= 1; i < s; i++) |
---|
182 | { |
---|
183 | M (i,i)= 1; |
---|
184 | M (i+1,i)= -liftedSmallestFactor; |
---|
185 | } |
---|
186 | |
---|
187 | mat_ZZ NTLM= *convertFacCFMatrix2NTLmat_ZZ (M); |
---|
188 | |
---|
189 | ZZ det; |
---|
190 | |
---|
191 | transpose (NTLM, NTLM); |
---|
192 | long r=LLL (det, NTLM, 1L, 1L); |
---|
193 | transpose (NTLM, NTLM); |
---|
194 | M= *convertNTLmat_ZZ2FacCFMatrix (NTLM); |
---|
195 | |
---|
196 | CanonicalForm mipo= 0; |
---|
197 | Variable x= Variable (1); |
---|
198 | for (int i= 1; i <= s; i++) |
---|
199 | mipo += M (i,1)*power (x,s-i); |
---|
200 | |
---|
201 | CFFList mipoFactors= factorize (mipo); |
---|
202 | mipoFactors.removeFirst(); |
---|
203 | On (SW_RATIONAL); |
---|
204 | Variable alpha= rootOf (mipo); |
---|
205 | CFFList QaFactors= factorize (F1, alpha); |
---|
206 | |
---|
207 | fmpz_poly_clear (v[0]); |
---|
208 | fmpz_poly_clear (v[1]); |
---|
209 | fmpz_poly_clear (w[0]); |
---|
210 | fmpz_poly_clear (w[1]); |
---|
211 | delete [] v; |
---|
212 | delete [] w; |
---|
213 | delete [] link; |
---|
214 | fmpz_poly_factor_clear (liftedFactors); |
---|
215 | return CFList(); |
---|
216 | } |
---|
217 | #endif |
---|
218 | |
---|
219 | |
---|