1 | /*****************************************************************************\ |
---|
2 | * Computer Algebra System SINGULAR |
---|
3 | \*****************************************************************************/ |
---|
4 | /** @file FLINTconvert.cc |
---|
5 | * |
---|
6 | * This file implements functions for conversion to FLINT (www.flintlib.org) |
---|
7 | * and back. |
---|
8 | * |
---|
9 | * @author Martin Lee |
---|
10 | * |
---|
11 | **/ |
---|
12 | /*****************************************************************************/ |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | #include <config.h> |
---|
17 | |
---|
18 | |
---|
19 | #include "canonicalform.h" |
---|
20 | #include "fac_util.h" |
---|
21 | #include "cf_iter.h" |
---|
22 | #include "cf_factory.h" |
---|
23 | #include "gmpext.h" |
---|
24 | #include "singext.h" |
---|
25 | #include "cf_algorithm.h" |
---|
26 | |
---|
27 | #ifdef HAVE_OMALLOC |
---|
28 | #define Alloc(L) omAlloc(L) |
---|
29 | #define Free(A,L) omFreeSize(A,L) |
---|
30 | #else |
---|
31 | #define Alloc(L) malloc(L) |
---|
32 | #define Free(A,L) free(A) |
---|
33 | #endif |
---|
34 | |
---|
35 | #ifdef HAVE_FLINT |
---|
36 | #ifdef HAVE_CSTDIO |
---|
37 | #include <cstdio> |
---|
38 | #else |
---|
39 | #include <stdio.h> |
---|
40 | #endif |
---|
41 | #ifdef __cplusplus |
---|
42 | extern "C" |
---|
43 | { |
---|
44 | #endif |
---|
45 | #ifndef __GMP_BITS_PER_MP_LIMB |
---|
46 | #define __GMP_BITS_PER_MP_LIMB GMP_LIMB_BITS |
---|
47 | #endif |
---|
48 | #include <flint/fmpz.h> |
---|
49 | #include <flint/fmpq.h> |
---|
50 | #include <flint/fmpz_poly.h> |
---|
51 | #include <flint/fmpz_mod_poly.h> |
---|
52 | #include <flint/nmod_poly.h> |
---|
53 | #include <flint/fmpq_poly.h> |
---|
54 | #include <flint/nmod_mat.h> |
---|
55 | #include <flint/fmpz_mat.h> |
---|
56 | #if ( __FLINT_RELEASE >= 20400) |
---|
57 | #include <flint/fq.h> |
---|
58 | #include <flint/fq_poly.h> |
---|
59 | #include <flint/fq_nmod.h> |
---|
60 | #include <flint/fq_nmod_poly.h> |
---|
61 | #include <flint/fq_nmod_mat.h> |
---|
62 | #endif |
---|
63 | #if ( __FLINT_RELEASE >= 20503) |
---|
64 | #include <flint/fmpq_mpoly.h> |
---|
65 | #endif |
---|
66 | #ifdef __cplusplus |
---|
67 | } |
---|
68 | #endif |
---|
69 | |
---|
70 | #include "FLINTconvert.h" |
---|
71 | |
---|
72 | void convertCF2Fmpz (fmpz_t result, const CanonicalForm& f) |
---|
73 | { |
---|
74 | if (f.isImm()) |
---|
75 | fmpz_set_si (result, f.intval()); |
---|
76 | else |
---|
77 | { |
---|
78 | mpz_t gmp_val; |
---|
79 | f.mpzval(gmp_val); |
---|
80 | fmpz_set_mpz (result, gmp_val); |
---|
81 | mpz_clear (gmp_val); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void convertFacCF2Fmpz_poly_t (fmpz_poly_t result, const CanonicalForm& f) |
---|
86 | { |
---|
87 | fmpz_poly_init2 (result, degree (f)+1); |
---|
88 | _fmpz_poly_set_length(result, degree(f)+1); |
---|
89 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
90 | convertCF2Fmpz (fmpz_poly_get_coeff_ptr(result, i.exp()), i.coeff()); |
---|
91 | } |
---|
92 | |
---|
93 | CanonicalForm convertFmpz2CF (const fmpz_t coefficient) |
---|
94 | { |
---|
95 | if (fmpz_cmp_si (coefficient, MINIMMEDIATE) >= 0 && |
---|
96 | fmpz_cmp_si (coefficient, MAXIMMEDIATE) <= 0) |
---|
97 | { |
---|
98 | long coeff= fmpz_get_si (coefficient); |
---|
99 | return CanonicalForm (coeff); |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | mpz_t gmp_val; |
---|
104 | mpz_init (gmp_val); |
---|
105 | fmpz_get_mpz (gmp_val, coefficient); |
---|
106 | CanonicalForm result= CanonicalForm (CFFactory::basic (gmp_val)); |
---|
107 | return result; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | CanonicalForm |
---|
112 | convertFmpz_poly_t2FacCF (const fmpz_poly_t poly, const Variable& x) |
---|
113 | { |
---|
114 | CanonicalForm result= 0; |
---|
115 | fmpz* coeff; |
---|
116 | for (int i= 0; i < fmpz_poly_length (poly); i++) |
---|
117 | { |
---|
118 | coeff= fmpz_poly_get_coeff_ptr (poly, i); |
---|
119 | if (!fmpz_is_zero (coeff)) |
---|
120 | result += convertFmpz2CF (coeff)*power (x,i); |
---|
121 | } |
---|
122 | return result; |
---|
123 | } |
---|
124 | |
---|
125 | void |
---|
126 | convertFacCF2nmod_poly_t (nmod_poly_t result, const CanonicalForm& f) |
---|
127 | { |
---|
128 | bool save_sym_ff= isOn (SW_SYMMETRIC_FF); |
---|
129 | if (save_sym_ff) Off (SW_SYMMETRIC_FF); |
---|
130 | nmod_poly_init2 (result, getCharacteristic(), degree (f)+1); |
---|
131 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
132 | { |
---|
133 | CanonicalForm c= i.coeff(); |
---|
134 | if (!c.isImm()) c=c.mapinto(); //c%= getCharacteristic(); |
---|
135 | if (!c.isImm()) |
---|
136 | { //This case will never happen if the characteristic is in fact a prime |
---|
137 | // number, since all coefficients are represented as immediates |
---|
138 | printf("convertCF2nmod_poly_t: coefficient not immediate!, char=%d\n", |
---|
139 | getCharacteristic()); |
---|
140 | } |
---|
141 | else |
---|
142 | nmod_poly_set_coeff_ui (result, i.exp(), c.intval()); |
---|
143 | } |
---|
144 | if (save_sym_ff) On (SW_SYMMETRIC_FF); |
---|
145 | } |
---|
146 | |
---|
147 | CanonicalForm |
---|
148 | convertnmod_poly_t2FacCF (const nmod_poly_t poly, const Variable& x) |
---|
149 | { |
---|
150 | CanonicalForm result= 0; |
---|
151 | for (int i= 0; i < nmod_poly_length (poly); i++) |
---|
152 | { |
---|
153 | ulong coeff= nmod_poly_get_coeff_ui (poly, i); |
---|
154 | if (coeff != 0) |
---|
155 | result += CanonicalForm ((long)coeff)*power (x,i); |
---|
156 | } |
---|
157 | return result; |
---|
158 | } |
---|
159 | |
---|
160 | void convertCF2Fmpq (fmpq_t result, const CanonicalForm& f) |
---|
161 | { |
---|
162 | //ASSERT (isOn (SW_RATIONAL), "expected rational"); |
---|
163 | if (f.isImm ()) |
---|
164 | { |
---|
165 | fmpq_set_si (result, f.intval(), 1); |
---|
166 | } |
---|
167 | else if(f.inQ()) |
---|
168 | { |
---|
169 | mpz_t gmp_val; |
---|
170 | gmp_numerator (f, gmp_val); |
---|
171 | fmpz_set_mpz (fmpq_numref (result), gmp_val); |
---|
172 | mpz_clear (gmp_val); |
---|
173 | gmp_denominator (f, gmp_val); |
---|
174 | fmpz_set_mpz (fmpq_denref (result), gmp_val); |
---|
175 | mpz_clear (gmp_val); |
---|
176 | } |
---|
177 | else if(f.inZ()) |
---|
178 | { |
---|
179 | mpz_t gmp_val; |
---|
180 | f.mpzval(gmp_val); |
---|
181 | fmpz_set_mpz (fmpq_numref (result), gmp_val); |
---|
182 | mpz_clear (gmp_val); |
---|
183 | fmpz_one(fmpq_denref(result)); |
---|
184 | } |
---|
185 | else |
---|
186 | { |
---|
187 | printf("wrong type\n"); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | CanonicalForm convertFmpq_t2CF (const fmpq_t q) |
---|
192 | { |
---|
193 | bool isRat= isOn (SW_RATIONAL); |
---|
194 | if (!isRat) |
---|
195 | On (SW_RATIONAL); |
---|
196 | |
---|
197 | CanonicalForm num, den; |
---|
198 | mpz_t nnum, nden; |
---|
199 | mpz_init (nnum); |
---|
200 | mpz_init (nden); |
---|
201 | fmpz_get_mpz (nnum, fmpq_numref (q)); |
---|
202 | fmpz_get_mpz (nden, fmpq_denref (q)); |
---|
203 | |
---|
204 | CanonicalForm result; |
---|
205 | if (mpz_is_imm (nden)) |
---|
206 | { |
---|
207 | if (mpz_is_imm(nnum)) |
---|
208 | { |
---|
209 | num= CanonicalForm (mpz_get_si(nnum)); |
---|
210 | den= CanonicalForm (mpz_get_si(nden)); |
---|
211 | mpz_clear (nnum); |
---|
212 | mpz_clear (nden); |
---|
213 | result= num/den; |
---|
214 | } |
---|
215 | else if (mpz_cmp_si(nden,1)==0) |
---|
216 | { |
---|
217 | result= make_cf(nnum); |
---|
218 | mpz_clear (nden); |
---|
219 | } |
---|
220 | else |
---|
221 | result= make_cf (nnum, nden, false); |
---|
222 | } |
---|
223 | else |
---|
224 | { |
---|
225 | result= make_cf (nnum, nden, false); |
---|
226 | } |
---|
227 | if (!isRat) |
---|
228 | Off (SW_RATIONAL); |
---|
229 | return result; |
---|
230 | } |
---|
231 | |
---|
232 | CanonicalForm |
---|
233 | convertFmpq_poly_t2FacCF (const fmpq_poly_t p, const Variable& x) |
---|
234 | { |
---|
235 | CanonicalForm result= 0; |
---|
236 | fmpq_t coeff; |
---|
237 | long n= p->length; |
---|
238 | for (long i= 0; i < n; i++) |
---|
239 | { |
---|
240 | fmpq_init (coeff); |
---|
241 | fmpq_poly_get_coeff_fmpq (coeff, p, i); |
---|
242 | if (fmpq_is_zero (coeff)) |
---|
243 | { |
---|
244 | fmpq_clear (coeff); |
---|
245 | continue; |
---|
246 | } |
---|
247 | result += convertFmpq_t2CF (coeff)*power (x, i); |
---|
248 | fmpq_clear (coeff); |
---|
249 | } |
---|
250 | return result; |
---|
251 | } |
---|
252 | |
---|
253 | void convertFacCF2Fmpz_array (fmpz* result, const CanonicalForm& f) |
---|
254 | { |
---|
255 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
256 | convertCF2Fmpz (&result[i.exp()], i.coeff()); |
---|
257 | } |
---|
258 | |
---|
259 | void convertFacCF2Fmpq_poly_t (fmpq_poly_t result, const CanonicalForm& f) |
---|
260 | { |
---|
261 | bool isRat= isOn (SW_RATIONAL); |
---|
262 | if (!isRat) |
---|
263 | On (SW_RATIONAL); |
---|
264 | |
---|
265 | fmpq_poly_init2 (result, degree (f)+1); |
---|
266 | _fmpq_poly_set_length (result, degree (f) + 1); |
---|
267 | CanonicalForm den= bCommonDen (f); |
---|
268 | convertFacCF2Fmpz_array (fmpq_poly_numref (result), f*den); |
---|
269 | convertCF2Fmpz (fmpq_poly_denref (result), den); |
---|
270 | |
---|
271 | if (!isRat) |
---|
272 | Off (SW_RATIONAL); |
---|
273 | } |
---|
274 | |
---|
275 | CFFList |
---|
276 | convertFLINTnmod_poly_factor2FacCFFList (const nmod_poly_factor_t fac, |
---|
277 | const mp_limb_t leadingCoeff, |
---|
278 | const Variable& x |
---|
279 | ) |
---|
280 | { |
---|
281 | CFFList result; |
---|
282 | if (leadingCoeff != 1) |
---|
283 | result.insert (CFFactor (CanonicalForm ((long) leadingCoeff), 1)); |
---|
284 | |
---|
285 | long i; |
---|
286 | |
---|
287 | for (i = 0; i < fac->num; i++) |
---|
288 | result.append (CFFactor (convertnmod_poly_t2FacCF ( |
---|
289 | (nmod_poly_t &)fac->p[i],x), |
---|
290 | fac->exp[i])); |
---|
291 | return result; |
---|
292 | } |
---|
293 | |
---|
294 | #if __FLINT_RELEASE >= 20400 |
---|
295 | CFFList |
---|
296 | convertFLINTFq_nmod_poly_factor2FacCFFList (const fq_nmod_poly_factor_t fac, |
---|
297 | const Variable& x, const Variable& alpha, |
---|
298 | const fq_nmod_ctx_t fq_con |
---|
299 | ) |
---|
300 | { |
---|
301 | CFFList result; |
---|
302 | |
---|
303 | long i; |
---|
304 | |
---|
305 | for (i = 0; i < fac->num; i++) |
---|
306 | result.append (CFFactor (convertFq_nmod_poly_t2FacCF ( |
---|
307 | (fq_nmod_poly_t &)fac->poly[i], x, alpha, fq_con), |
---|
308 | fac->exp[i])); |
---|
309 | return result; |
---|
310 | } |
---|
311 | #endif |
---|
312 | |
---|
313 | void |
---|
314 | convertFacCF2Fmpz_mod_poly_t (fmpz_mod_poly_t result, const CanonicalForm& f, |
---|
315 | const fmpz_t p) |
---|
316 | { |
---|
317 | fmpz_mod_poly_init2 (result, p, degree (f) + 1); |
---|
318 | fmpz_poly_t buf; |
---|
319 | convertFacCF2Fmpz_poly_t (buf, f); |
---|
320 | fmpz_mod_poly_set_fmpz_poly (result, buf); |
---|
321 | fmpz_poly_clear (buf); |
---|
322 | } |
---|
323 | |
---|
324 | CanonicalForm |
---|
325 | convertFmpz_mod_poly_t2FacCF (const fmpz_mod_poly_t poly, const Variable& x, |
---|
326 | const modpk& b) |
---|
327 | { |
---|
328 | fmpz_poly_t buf; |
---|
329 | fmpz_poly_init (buf); |
---|
330 | fmpz_mod_poly_get_fmpz_poly (buf, poly); |
---|
331 | CanonicalForm result= convertFmpz_poly_t2FacCF (buf, x); |
---|
332 | fmpz_poly_clear (buf); |
---|
333 | return b (result); |
---|
334 | } |
---|
335 | |
---|
336 | #if __FLINT_RELEASE >= 20400 |
---|
337 | void |
---|
338 | convertFacCF2Fq_nmod_t (fq_nmod_t result, const CanonicalForm& f, |
---|
339 | const fq_nmod_ctx_t ctx) |
---|
340 | { |
---|
341 | bool save_sym_ff= isOn (SW_SYMMETRIC_FF); |
---|
342 | if (save_sym_ff) Off (SW_SYMMETRIC_FF); |
---|
343 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
344 | { |
---|
345 | CanonicalForm c= i.coeff(); |
---|
346 | if (!c.isImm()) c=c.mapinto(); //c%= getCharacteristic(); |
---|
347 | if (!c.isImm()) |
---|
348 | { //This case will never happen if the characteristic is in fact a prime |
---|
349 | // number, since all coefficients are represented as immediates |
---|
350 | printf("convertFacCF2Fq_nmod_t: coefficient not immediate!, char=%d\n", |
---|
351 | getCharacteristic()); |
---|
352 | } |
---|
353 | else |
---|
354 | { |
---|
355 | STICKYASSERT (i.exp() <= fq_nmod_ctx_degree(ctx), "convertFacCF2Fq_nmod_t: element is not reduced"); |
---|
356 | nmod_poly_set_coeff_ui (result, i.exp(), c.intval()); |
---|
357 | } |
---|
358 | } |
---|
359 | if (save_sym_ff) On (SW_SYMMETRIC_FF); |
---|
360 | } |
---|
361 | |
---|
362 | CanonicalForm |
---|
363 | convertFq_nmod_t2FacCF (const fq_nmod_t poly, const Variable& alpha) |
---|
364 | { |
---|
365 | return convertnmod_poly_t2FacCF (poly, alpha); |
---|
366 | } |
---|
367 | |
---|
368 | void |
---|
369 | convertFacCF2Fq_t (fq_t result, const CanonicalForm& f, const fq_ctx_t ctx) |
---|
370 | { |
---|
371 | fmpz_poly_init2 (result, fq_ctx_degree(ctx)); |
---|
372 | ASSERT (degree (f) < fq_ctx_degree (ctx), "input is not reduced"); |
---|
373 | _fmpz_poly_set_length(result, degree(f)+1); |
---|
374 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
375 | convertCF2Fmpz (fmpz_poly_get_coeff_ptr(result, i.exp()), i.coeff()); |
---|
376 | _fmpz_vec_scalar_mod_fmpz (result->coeffs, result->coeffs, degree (f) + 1, |
---|
377 | &ctx->p); |
---|
378 | _fmpz_poly_normalise (result); |
---|
379 | } |
---|
380 | |
---|
381 | CanonicalForm |
---|
382 | convertFq_t2FacCF (const fq_t poly, const Variable& alpha) |
---|
383 | { |
---|
384 | return convertFmpz_poly_t2FacCF (poly, alpha); |
---|
385 | } |
---|
386 | |
---|
387 | void |
---|
388 | convertFacCF2Fq_poly_t (fq_poly_t result, const CanonicalForm& f, |
---|
389 | const fq_ctx_t ctx) |
---|
390 | { |
---|
391 | fq_poly_init2 (result, degree (f)+1, ctx); |
---|
392 | _fq_poly_set_length (result, degree (f) + 1, ctx); |
---|
393 | fmpz_poly_t buf; |
---|
394 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
395 | { |
---|
396 | convertFacCF2Fmpz_poly_t (buf, i.coeff()); |
---|
397 | _fmpz_vec_scalar_mod_fmpz (buf->coeffs, buf->coeffs, degree (i.coeff()) + 1, |
---|
398 | &ctx->p); |
---|
399 | _fmpz_poly_normalise (buf); |
---|
400 | fq_poly_set_coeff (result, i.exp(), buf, ctx); |
---|
401 | fmpz_poly_clear (buf); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | void |
---|
406 | convertFacCF2Fq_nmod_poly_t (fq_nmod_poly_t result, const CanonicalForm& f, |
---|
407 | const fq_nmod_ctx_t ctx) |
---|
408 | { |
---|
409 | fq_nmod_poly_init2 (result, degree (f)+1, ctx); |
---|
410 | _fq_nmod_poly_set_length (result, degree (f) + 1, ctx); |
---|
411 | fq_nmod_t buf; |
---|
412 | fq_nmod_init2 (buf, ctx); |
---|
413 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
414 | { |
---|
415 | convertFacCF2Fq_nmod_t (buf, i.coeff(), ctx); |
---|
416 | fq_nmod_poly_set_coeff (result, i.exp(), buf, ctx); |
---|
417 | fq_nmod_zero (buf, ctx); |
---|
418 | } |
---|
419 | fq_nmod_clear (buf, ctx); |
---|
420 | } |
---|
421 | |
---|
422 | CanonicalForm |
---|
423 | convertFq_poly_t2FacCF (const fq_poly_t p, const Variable& x, |
---|
424 | const Variable& alpha, const fq_ctx_t ctx) |
---|
425 | { |
---|
426 | CanonicalForm result= 0; |
---|
427 | fq_t coeff; |
---|
428 | long n= fq_poly_length (p, ctx); |
---|
429 | fq_init2 (coeff, ctx); |
---|
430 | for (long i= 0; i < n; i++) |
---|
431 | { |
---|
432 | fq_poly_get_coeff (coeff, p, i, ctx); |
---|
433 | if (fq_is_zero (coeff, ctx)) |
---|
434 | continue; |
---|
435 | result += convertFq_t2FacCF (coeff, alpha)*power (x, i); |
---|
436 | fq_zero (coeff, ctx); |
---|
437 | } |
---|
438 | fq_clear (coeff, ctx); |
---|
439 | |
---|
440 | return result; |
---|
441 | } |
---|
442 | |
---|
443 | CanonicalForm |
---|
444 | convertFq_nmod_poly_t2FacCF (const fq_nmod_poly_t p, const Variable& x, |
---|
445 | const Variable& alpha, const fq_nmod_ctx_t ctx) |
---|
446 | { |
---|
447 | CanonicalForm result= 0; |
---|
448 | fq_nmod_t coeff; |
---|
449 | long n= fq_nmod_poly_length (p, ctx); |
---|
450 | fq_nmod_init2 (coeff, ctx); |
---|
451 | for (long i= 0; i < n; i++) |
---|
452 | { |
---|
453 | fq_nmod_poly_get_coeff (coeff, p, i, ctx); |
---|
454 | if (fq_nmod_is_zero (coeff, ctx)) |
---|
455 | continue; |
---|
456 | result += convertFq_nmod_t2FacCF (coeff, alpha)*power (x, i); |
---|
457 | fq_nmod_zero (coeff, ctx); |
---|
458 | } |
---|
459 | fq_nmod_clear (coeff, ctx); |
---|
460 | |
---|
461 | return result; |
---|
462 | } |
---|
463 | #endif |
---|
464 | |
---|
465 | void convertFacCFMatrix2Fmpz_mat_t (fmpz_mat_t M, const CFMatrix &m) |
---|
466 | { |
---|
467 | fmpz_mat_init (M, (long) m.rows(), (long) m.columns()); |
---|
468 | |
---|
469 | int i,j; |
---|
470 | for(i=m.rows();i>0;i--) |
---|
471 | { |
---|
472 | for(j=m.columns();j>0;j--) |
---|
473 | { |
---|
474 | convertCF2Fmpz (fmpz_mat_entry (M,i-1,j-1), m(i,j)); |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | CFMatrix* convertFmpz_mat_t2FacCFMatrix(const fmpz_mat_t m) |
---|
479 | { |
---|
480 | CFMatrix *res=new CFMatrix(fmpz_mat_nrows (m),fmpz_mat_ncols (m)); |
---|
481 | int i,j; |
---|
482 | for(i=res->rows();i>0;i--) |
---|
483 | { |
---|
484 | for(j=res->columns();j>0;j--) |
---|
485 | { |
---|
486 | (*res)(i,j)=convertFmpz2CF(fmpz_mat_entry (m,i-1,j-1)); |
---|
487 | } |
---|
488 | } |
---|
489 | return res; |
---|
490 | } |
---|
491 | |
---|
492 | void convertFacCFMatrix2nmod_mat_t (nmod_mat_t M, const CFMatrix &m) |
---|
493 | { |
---|
494 | nmod_mat_init (M, (long) m.rows(), (long) m.columns(), getCharacteristic()); |
---|
495 | |
---|
496 | bool save_sym_ff= isOn (SW_SYMMETRIC_FF); |
---|
497 | if (save_sym_ff) Off (SW_SYMMETRIC_FF); |
---|
498 | int i,j; |
---|
499 | for(i=m.rows();i>0;i--) |
---|
500 | { |
---|
501 | for(j=m.columns();j>0;j--) |
---|
502 | { |
---|
503 | if(!(m(i,j)).isImm()) printf("convertFacCFMatrix2FLINTmat_zz_p: not imm.\n"); |
---|
504 | nmod_mat_entry (M,i-1,j-1)= (m(i,j)).intval(); |
---|
505 | } |
---|
506 | } |
---|
507 | if (save_sym_ff) On (SW_SYMMETRIC_FF); |
---|
508 | } |
---|
509 | |
---|
510 | CFMatrix* convertNmod_mat_t2FacCFMatrix(const nmod_mat_t m) |
---|
511 | { |
---|
512 | CFMatrix *res=new CFMatrix(nmod_mat_nrows (m), nmod_mat_ncols (m)); |
---|
513 | int i,j; |
---|
514 | for(i=res->rows();i>0;i--) |
---|
515 | { |
---|
516 | for(j=res->columns();j>0;j--) |
---|
517 | { |
---|
518 | (*res)(i,j)=CanonicalForm((long) nmod_mat_entry (m, i-1, j-1)); |
---|
519 | } |
---|
520 | } |
---|
521 | return res; |
---|
522 | } |
---|
523 | |
---|
524 | #if __FLINT_RELEASE >= 20400 |
---|
525 | void |
---|
526 | convertFacCFMatrix2Fq_nmod_mat_t (fq_nmod_mat_t M, |
---|
527 | const fq_nmod_ctx_t fq_con, const CFMatrix &m) |
---|
528 | { |
---|
529 | fq_nmod_mat_init (M, (long) m.rows(), (long) m.columns(), fq_con); |
---|
530 | int i,j; |
---|
531 | for(i=m.rows();i>0;i--) |
---|
532 | { |
---|
533 | for(j=m.columns();j>0;j--) |
---|
534 | { |
---|
535 | convertFacCF2nmod_poly_t (M->rows[i-1]+j-1, m (i,j)); |
---|
536 | } |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | CFMatrix* |
---|
541 | convertFq_nmod_mat_t2FacCFMatrix(const fq_nmod_mat_t m, |
---|
542 | const fq_nmod_ctx_t& fq_con, |
---|
543 | const Variable& alpha) |
---|
544 | { |
---|
545 | CFMatrix *res=new CFMatrix(fq_nmod_mat_nrows (m, fq_con), |
---|
546 | fq_nmod_mat_ncols (m, fq_con)); |
---|
547 | int i,j; |
---|
548 | for(i=res->rows();i>0;i--) |
---|
549 | { |
---|
550 | for(j=res->columns();j>0;j--) |
---|
551 | { |
---|
552 | (*res)(i,j)=convertFq_nmod_t2FacCF (fq_nmod_mat_entry (m, i-1, j-1), |
---|
553 | alpha); |
---|
554 | } |
---|
555 | } |
---|
556 | return res; |
---|
557 | } |
---|
558 | #endif |
---|
559 | #if __FLINT_RELEASE >= 20503 |
---|
560 | static void convFlint_RecPP ( const CanonicalForm & f, ulong * exp, nmod_mpoly_t result, nmod_mpoly_ctx_t ctx, int N ) |
---|
561 | { |
---|
562 | // assume f!=0 |
---|
563 | if ( ! f.inCoeffDomain() ) |
---|
564 | { |
---|
565 | int l = f.level(); |
---|
566 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
567 | { |
---|
568 | exp[N-l] = i.exp(); |
---|
569 | convFlint_RecPP( i.coeff(), exp, result, ctx, N ); |
---|
570 | } |
---|
571 | exp[N-l] = 0; |
---|
572 | } |
---|
573 | else |
---|
574 | { |
---|
575 | int c=f.intval(); // with Off(SW_SYMMETRIC_FF): 0<=c<p |
---|
576 | nmod_mpoly_push_term_ui_ui(result,c,exp,ctx); |
---|
577 | } |
---|
578 | } |
---|
579 | |
---|
580 | static void convFlint_RecPP ( const CanonicalForm & f, ulong * exp, fmpq_mpoly_t result, fmpq_mpoly_ctx_t ctx, int N ) |
---|
581 | { |
---|
582 | // assume f!=0 |
---|
583 | if ( ! f.inBaseDomain() ) |
---|
584 | { |
---|
585 | int l = f.level(); |
---|
586 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
587 | { |
---|
588 | exp[N-l] = i.exp(); |
---|
589 | convFlint_RecPP( i.coeff(), exp, result, ctx, N ); |
---|
590 | } |
---|
591 | exp[N-l] = 0; |
---|
592 | } |
---|
593 | else |
---|
594 | { |
---|
595 | fmpq_t c; |
---|
596 | fmpq_init(c); |
---|
597 | convertCF2Fmpq(c,f); |
---|
598 | fmpq_mpoly_push_term_fmpq_ui(result,c,exp,ctx); |
---|
599 | fmpq_clear(c); |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | void convFactoryPFlintMP ( const CanonicalForm & f, nmod_mpoly_t res, nmod_mpoly_ctx_t ctx, int N ) |
---|
604 | { |
---|
605 | if (f.isZero()) return; |
---|
606 | ulong * exp = (ulong*)Alloc(N*sizeof(ulong)); |
---|
607 | memset(exp,0,N*sizeof(ulong)); |
---|
608 | bool save_sym_ff= isOn (SW_SYMMETRIC_FF); |
---|
609 | if (save_sym_ff) Off (SW_SYMMETRIC_FF); |
---|
610 | convFlint_RecPP( f, exp, res, ctx, N ); |
---|
611 | if (save_sym_ff) On(SW_SYMMETRIC_FF); |
---|
612 | Free(exp,N*sizeof(ulong)); |
---|
613 | } |
---|
614 | |
---|
615 | void convFactoryPFlintMP ( const CanonicalForm & f, fmpq_mpoly_t res, fmpq_mpoly_ctx_t ctx, int N ) |
---|
616 | { |
---|
617 | if (f.isZero()) return; |
---|
618 | ulong * exp = (ulong*)Alloc(N*sizeof(ulong)); |
---|
619 | memset(exp,0,N*sizeof(ulong)); |
---|
620 | convFlint_RecPP( f, exp, res, ctx, N ); |
---|
621 | fmpq_mpoly_reduce(res,ctx); |
---|
622 | Free(exp,N*sizeof(ulong)); |
---|
623 | } |
---|
624 | |
---|
625 | CanonicalForm convFlintMPFactoryP(nmod_mpoly_t f, nmod_mpoly_ctx_t ctx, int N) |
---|
626 | { |
---|
627 | CanonicalForm result; |
---|
628 | int d=nmod_mpoly_length(f,ctx)-1; |
---|
629 | ulong* exp=(ulong*)Alloc(N*sizeof(ulong)); |
---|
630 | for(int i=d; i>=0; i--) |
---|
631 | { |
---|
632 | ulong c=nmod_mpoly_get_term_coeff_ui(f,i,ctx); |
---|
633 | nmod_mpoly_get_term_exp_ui(exp,f,i,ctx); |
---|
634 | CanonicalForm term=(int)c; |
---|
635 | for ( int i = 0; i <N; i++ ) |
---|
636 | { |
---|
637 | if (exp[i]!=0) term*=CanonicalForm( Variable( N-i ), exp[i] ); |
---|
638 | } |
---|
639 | result+=term; |
---|
640 | } |
---|
641 | Free(exp,N*sizeof(ulong)); |
---|
642 | return result; |
---|
643 | } |
---|
644 | |
---|
645 | CanonicalForm convFlintMPFactoryP(fmpq_mpoly_t f, fmpq_mpoly_ctx_t ctx, int N) |
---|
646 | { |
---|
647 | CanonicalForm result; |
---|
648 | int d=fmpq_mpoly_length(f,ctx)-1; |
---|
649 | ulong* exp=(ulong*)Alloc(N*sizeof(ulong)); |
---|
650 | fmpq_t c; |
---|
651 | fmpq_init(c); |
---|
652 | for(int i=d; i>=0; i--) |
---|
653 | { |
---|
654 | fmpq_mpoly_get_term_coeff_fmpq(c,f,i,ctx); |
---|
655 | fmpq_mpoly_get_term_exp_ui(exp,f,i,ctx); |
---|
656 | CanonicalForm term=convertFmpq_t2CF(c); |
---|
657 | for ( int i = 0; i <N; i++ ) |
---|
658 | { |
---|
659 | if (exp[i]!=0) term*=CanonicalForm( Variable( N-i ), exp[i] ); |
---|
660 | } |
---|
661 | result+=term; |
---|
662 | } |
---|
663 | fmpq_clear(c); |
---|
664 | Free(exp,N*sizeof(ulong)); |
---|
665 | return result; |
---|
666 | } |
---|
667 | |
---|
668 | // stolen from: |
---|
669 | // https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog |
---|
670 | static inline int SI_LOG2(int v) |
---|
671 | { |
---|
672 | const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; |
---|
673 | const unsigned int S[] = {1, 2, 4, 8, 16}; |
---|
674 | |
---|
675 | unsigned int r = 0; // result of log2(v) will go here |
---|
676 | if (v & b[4]) { v >>= S[4]; r |= S[4]; } |
---|
677 | if (v & b[3]) { v >>= S[3]; r |= S[3]; } |
---|
678 | if (v & b[2]) { v >>= S[2]; r |= S[2]; } |
---|
679 | if (v & b[1]) { v >>= S[1]; r |= S[1]; } |
---|
680 | if (v & b[0]) { v >>= S[0]; r |= S[0]; } |
---|
681 | return (int)r; |
---|
682 | } |
---|
683 | |
---|
684 | CanonicalForm mulFlintMP_Zp(const CanonicalForm& F,int lF, const CanonicalForm& G, int lG,int m) |
---|
685 | { |
---|
686 | int bits=SI_LOG2(m)+1; |
---|
687 | int N=F.level(); |
---|
688 | nmod_mpoly_ctx_t ctx; |
---|
689 | nmod_mpoly_ctx_init(ctx,N,ORD_LEX,getCharacteristic()); |
---|
690 | nmod_mpoly_t f,g,res; |
---|
691 | nmod_mpoly_init3(f,lF,bits,ctx); |
---|
692 | nmod_mpoly_init3(g,lG,bits,ctx); |
---|
693 | convFactoryPFlintMP(F,f,ctx,N); |
---|
694 | convFactoryPFlintMP(G,g,ctx,N); |
---|
695 | nmod_mpoly_init(res,ctx); |
---|
696 | nmod_mpoly_mul(res,f,g,ctx); |
---|
697 | nmod_mpoly_clear(g,ctx); |
---|
698 | nmod_mpoly_clear(f,ctx); |
---|
699 | CanonicalForm RES=convFlintMPFactoryP(res,ctx,N); |
---|
700 | nmod_mpoly_clear(res,ctx); |
---|
701 | nmod_mpoly_ctx_clear(ctx); |
---|
702 | return RES; |
---|
703 | } |
---|
704 | |
---|
705 | CanonicalForm mulFlintMP_QQ(const CanonicalForm& F,int lF, const CanonicalForm& G, int lG, int m) |
---|
706 | { |
---|
707 | int bits=SI_LOG2(m)+1; |
---|
708 | int N=F.level(); |
---|
709 | fmpq_mpoly_ctx_t ctx; |
---|
710 | fmpq_mpoly_ctx_init(ctx,N,ORD_LEX); |
---|
711 | fmpq_mpoly_t f,g,res; |
---|
712 | fmpq_mpoly_init3(f,lF,bits,ctx); |
---|
713 | fmpq_mpoly_init3(g,lG,bits,ctx); |
---|
714 | convFactoryPFlintMP(F,f,ctx,N); |
---|
715 | convFactoryPFlintMP(G,g,ctx,N); |
---|
716 | fmpq_mpoly_init(res,ctx); |
---|
717 | fmpq_mpoly_mul(res,f,g,ctx); |
---|
718 | fmpq_mpoly_clear(g,ctx); |
---|
719 | fmpq_mpoly_clear(f,ctx); |
---|
720 | CanonicalForm RES=convFlintMPFactoryP(res,ctx,N); |
---|
721 | fmpq_mpoly_clear(res,ctx); |
---|
722 | fmpq_mpoly_ctx_clear(ctx); |
---|
723 | return RES; |
---|
724 | } |
---|
725 | |
---|
726 | CanonicalForm gcdFlintMP_Zp(const CanonicalForm& F, const CanonicalForm& G) |
---|
727 | { |
---|
728 | int N=F.level(); |
---|
729 | int lf,lg,m=1<<MPOLY_MIN_BITS; |
---|
730 | lf=size_maxexp(F,m); |
---|
731 | lg=size_maxexp(G,m); |
---|
732 | int bits=SI_LOG2(m)+1; |
---|
733 | nmod_mpoly_ctx_t ctx; |
---|
734 | nmod_mpoly_ctx_init(ctx,N,ORD_LEX,getCharacteristic()); |
---|
735 | nmod_mpoly_t f,g,res; |
---|
736 | nmod_mpoly_init3(f,lf,bits,ctx); |
---|
737 | nmod_mpoly_init3(g,lg,bits,ctx); |
---|
738 | convFactoryPFlintMP(F,f,ctx,N); |
---|
739 | convFactoryPFlintMP(G,g,ctx,N); |
---|
740 | nmod_mpoly_init(res,ctx); |
---|
741 | int ok=nmod_mpoly_gcd(res,f,g,ctx); |
---|
742 | nmod_mpoly_clear(g,ctx); |
---|
743 | nmod_mpoly_clear(f,ctx); |
---|
744 | CanonicalForm RES=1; |
---|
745 | if (ok) |
---|
746 | { |
---|
747 | RES=convFlintMPFactoryP(res,ctx,N); |
---|
748 | } |
---|
749 | nmod_mpoly_clear(res,ctx); |
---|
750 | nmod_mpoly_ctx_clear(ctx); |
---|
751 | return RES; |
---|
752 | } |
---|
753 | |
---|
754 | CanonicalForm gcdFlintMP_QQ(const CanonicalForm& F, const CanonicalForm& G) |
---|
755 | { |
---|
756 | int N=F.level(); |
---|
757 | fmpq_mpoly_ctx_t ctx; |
---|
758 | fmpq_mpoly_ctx_init(ctx,N,ORD_LEX); |
---|
759 | fmpq_mpoly_t f,g,res; |
---|
760 | fmpq_mpoly_init(f,ctx); |
---|
761 | fmpq_mpoly_init(g,ctx); |
---|
762 | convFactoryPFlintMP(F,f,ctx,N); |
---|
763 | convFactoryPFlintMP(G,g,ctx,N); |
---|
764 | fmpq_mpoly_init(res,ctx); |
---|
765 | int ok=fmpq_mpoly_gcd(res,f,g,ctx); |
---|
766 | fmpq_mpoly_clear(g,ctx); |
---|
767 | fmpq_mpoly_clear(f,ctx); |
---|
768 | CanonicalForm RES=1; |
---|
769 | if (ok) |
---|
770 | { |
---|
771 | // Flint normalizes the gcd to be monic. |
---|
772 | // Singular wants a gcd defined over ZZ that is primitive and has a positive leading coeff. |
---|
773 | if (!fmpq_mpoly_is_zero(res, ctx)) |
---|
774 | { |
---|
775 | fmpq_t content; |
---|
776 | fmpq_init(content); |
---|
777 | fmpq_mpoly_content(content, res, ctx); |
---|
778 | fmpq_mpoly_scalar_div_fmpq(res, res, content, ctx); |
---|
779 | fmpq_clear(content); |
---|
780 | } |
---|
781 | RES=convFlintMPFactoryP(res,ctx,N); |
---|
782 | // gcd(2x,4x) should be 2x, so RES should also have the gcd(lc(F),lc(G)) |
---|
783 | RES*=gcd(F.lc(),G.lc()); |
---|
784 | } |
---|
785 | fmpq_mpoly_clear(res,ctx); |
---|
786 | fmpq_mpoly_ctx_clear(ctx); |
---|
787 | return RES; |
---|
788 | } |
---|
789 | |
---|
790 | #endif |
---|
791 | |
---|
792 | #endif |
---|
793 | |
---|
794 | |
---|