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= CanonicalForm( CFFactory::basic(nnum)); |
---|
218 | mpz_clear (nden); |
---|
219 | } |
---|
220 | else |
---|
221 | result= CanonicalForm( CFFactory::rational( nnum, nden, false)); |
---|
222 | } |
---|
223 | else |
---|
224 | { |
---|
225 | result= CanonicalForm( CFFactory::rational( 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 >= 20503 |
---|
295 | CFFList |
---|
296 | convertFLINTfmpz_poly_factor2FacCFFList ( |
---|
297 | const fmpz_poly_factor_t fac, ///< [in] a nmod_poly_factor_t |
---|
298 | const Variable& x ///< [in] variable the result should |
---|
299 | ///< have |
---|
300 | ) |
---|
301 | |
---|
302 | { |
---|
303 | CFFList result; |
---|
304 | long i; |
---|
305 | |
---|
306 | result.append (CFFactor(convertFmpz2CF(&fac->c),1)); |
---|
307 | |
---|
308 | for (i = 0; i < fac->num; i++) |
---|
309 | result.append (CFFactor (convertFmpz_poly_t2FacCF ( |
---|
310 | (fmpz_poly_t &)fac->p[i],x), |
---|
311 | fac->exp[i])); |
---|
312 | return result; |
---|
313 | } |
---|
314 | #endif |
---|
315 | |
---|
316 | #if __FLINT_RELEASE >= 20400 |
---|
317 | CFFList |
---|
318 | convertFLINTFq_nmod_poly_factor2FacCFFList (const fq_nmod_poly_factor_t fac, |
---|
319 | const Variable& x, const Variable& alpha, |
---|
320 | const fq_nmod_ctx_t fq_con |
---|
321 | ) |
---|
322 | { |
---|
323 | CFFList result; |
---|
324 | |
---|
325 | long i; |
---|
326 | |
---|
327 | for (i = 0; i < fac->num; i++) |
---|
328 | result.append (CFFactor (convertFq_nmod_poly_t2FacCF ( |
---|
329 | (fq_nmod_poly_t &)fac->poly[i], x, alpha, fq_con), |
---|
330 | fac->exp[i])); |
---|
331 | return result; |
---|
332 | } |
---|
333 | #endif |
---|
334 | |
---|
335 | void |
---|
336 | convertFacCF2Fmpz_mod_poly_t (fmpz_mod_poly_t result, const CanonicalForm& f, |
---|
337 | const fmpz_t p) |
---|
338 | { |
---|
339 | fmpz_mod_poly_init2 (result, p, degree (f) + 1); |
---|
340 | fmpz_poly_t buf; |
---|
341 | convertFacCF2Fmpz_poly_t (buf, f); |
---|
342 | fmpz_mod_poly_set_fmpz_poly (result, buf); |
---|
343 | fmpz_poly_clear (buf); |
---|
344 | } |
---|
345 | |
---|
346 | CanonicalForm |
---|
347 | convertFmpz_mod_poly_t2FacCF (const fmpz_mod_poly_t poly, const Variable& x, |
---|
348 | const modpk& b) |
---|
349 | { |
---|
350 | fmpz_poly_t buf; |
---|
351 | fmpz_poly_init (buf); |
---|
352 | fmpz_mod_poly_get_fmpz_poly (buf, poly); |
---|
353 | CanonicalForm result= convertFmpz_poly_t2FacCF (buf, x); |
---|
354 | fmpz_poly_clear (buf); |
---|
355 | return b (result); |
---|
356 | } |
---|
357 | |
---|
358 | #if __FLINT_RELEASE >= 20400 |
---|
359 | void |
---|
360 | convertFacCF2Fq_nmod_t (fq_nmod_t result, const CanonicalForm& f, |
---|
361 | const fq_nmod_ctx_t ctx) |
---|
362 | { |
---|
363 | bool save_sym_ff= isOn (SW_SYMMETRIC_FF); |
---|
364 | if (save_sym_ff) Off (SW_SYMMETRIC_FF); |
---|
365 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
366 | { |
---|
367 | CanonicalForm c= i.coeff(); |
---|
368 | if (!c.isImm()) c=c.mapinto(); //c%= getCharacteristic(); |
---|
369 | if (!c.isImm()) |
---|
370 | { //This case will never happen if the characteristic is in fact a prime |
---|
371 | // number, since all coefficients are represented as immediates |
---|
372 | printf("convertFacCF2Fq_nmod_t: coefficient not immediate!, char=%d\n", |
---|
373 | getCharacteristic()); |
---|
374 | } |
---|
375 | else |
---|
376 | { |
---|
377 | STICKYASSERT (i.exp() <= fq_nmod_ctx_degree(ctx), "convertFacCF2Fq_nmod_t: element is not reduced"); |
---|
378 | nmod_poly_set_coeff_ui (result, i.exp(), c.intval()); |
---|
379 | } |
---|
380 | } |
---|
381 | if (save_sym_ff) On (SW_SYMMETRIC_FF); |
---|
382 | } |
---|
383 | |
---|
384 | CanonicalForm |
---|
385 | convertFq_nmod_t2FacCF (const fq_nmod_t poly, const Variable& alpha) |
---|
386 | { |
---|
387 | return convertnmod_poly_t2FacCF (poly, alpha); |
---|
388 | } |
---|
389 | |
---|
390 | void |
---|
391 | convertFacCF2Fq_t (fq_t result, const CanonicalForm& f, const fq_ctx_t ctx) |
---|
392 | { |
---|
393 | fmpz_poly_init2 (result, fq_ctx_degree(ctx)); |
---|
394 | ASSERT (degree (f) < fq_ctx_degree (ctx), "input is not reduced"); |
---|
395 | _fmpz_poly_set_length(result, degree(f)+1); |
---|
396 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
397 | convertCF2Fmpz (fmpz_poly_get_coeff_ptr(result, i.exp()), i.coeff()); |
---|
398 | _fmpz_vec_scalar_mod_fmpz (result->coeffs, result->coeffs, degree (f) + 1, |
---|
399 | &ctx->p); |
---|
400 | _fmpz_poly_normalise (result); |
---|
401 | } |
---|
402 | |
---|
403 | CanonicalForm |
---|
404 | convertFq_t2FacCF (const fq_t poly, const Variable& alpha) |
---|
405 | { |
---|
406 | return convertFmpz_poly_t2FacCF (poly, alpha); |
---|
407 | } |
---|
408 | |
---|
409 | void |
---|
410 | convertFacCF2Fq_poly_t (fq_poly_t result, const CanonicalForm& f, |
---|
411 | const fq_ctx_t ctx) |
---|
412 | { |
---|
413 | fq_poly_init2 (result, degree (f)+1, ctx); |
---|
414 | _fq_poly_set_length (result, degree (f) + 1, ctx); |
---|
415 | fmpz_poly_t buf; |
---|
416 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
417 | { |
---|
418 | convertFacCF2Fmpz_poly_t (buf, i.coeff()); |
---|
419 | _fmpz_vec_scalar_mod_fmpz (buf->coeffs, buf->coeffs, degree (i.coeff()) + 1, |
---|
420 | &ctx->p); |
---|
421 | _fmpz_poly_normalise (buf); |
---|
422 | fq_poly_set_coeff (result, i.exp(), buf, ctx); |
---|
423 | fmpz_poly_clear (buf); |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | void |
---|
428 | convertFacCF2Fq_nmod_poly_t (fq_nmod_poly_t result, const CanonicalForm& f, |
---|
429 | const fq_nmod_ctx_t ctx) |
---|
430 | { |
---|
431 | fq_nmod_poly_init2 (result, degree (f)+1, ctx); |
---|
432 | _fq_nmod_poly_set_length (result, degree (f) + 1, ctx); |
---|
433 | fq_nmod_t buf; |
---|
434 | fq_nmod_init2 (buf, ctx); |
---|
435 | for (CFIterator i= f; i.hasTerms(); i++) |
---|
436 | { |
---|
437 | convertFacCF2Fq_nmod_t (buf, i.coeff(), ctx); |
---|
438 | fq_nmod_poly_set_coeff (result, i.exp(), buf, ctx); |
---|
439 | fq_nmod_zero (buf, ctx); |
---|
440 | } |
---|
441 | fq_nmod_clear (buf, ctx); |
---|
442 | } |
---|
443 | |
---|
444 | CanonicalForm |
---|
445 | convertFq_poly_t2FacCF (const fq_poly_t p, const Variable& x, |
---|
446 | const Variable& alpha, const fq_ctx_t ctx) |
---|
447 | { |
---|
448 | CanonicalForm result= 0; |
---|
449 | fq_t coeff; |
---|
450 | long n= fq_poly_length (p, ctx); |
---|
451 | fq_init2 (coeff, ctx); |
---|
452 | for (long i= 0; i < n; i++) |
---|
453 | { |
---|
454 | fq_poly_get_coeff (coeff, p, i, ctx); |
---|
455 | if (fq_is_zero (coeff, ctx)) |
---|
456 | continue; |
---|
457 | result += convertFq_t2FacCF (coeff, alpha)*power (x, i); |
---|
458 | fq_zero (coeff, ctx); |
---|
459 | } |
---|
460 | fq_clear (coeff, ctx); |
---|
461 | |
---|
462 | return result; |
---|
463 | } |
---|
464 | |
---|
465 | CanonicalForm |
---|
466 | convertFq_nmod_poly_t2FacCF (const fq_nmod_poly_t p, const Variable& x, |
---|
467 | const Variable& alpha, const fq_nmod_ctx_t ctx) |
---|
468 | { |
---|
469 | CanonicalForm result= 0; |
---|
470 | fq_nmod_t coeff; |
---|
471 | long n= fq_nmod_poly_length (p, ctx); |
---|
472 | fq_nmod_init2 (coeff, ctx); |
---|
473 | for (long i= 0; i < n; i++) |
---|
474 | { |
---|
475 | fq_nmod_poly_get_coeff (coeff, p, i, ctx); |
---|
476 | if (fq_nmod_is_zero (coeff, ctx)) |
---|
477 | continue; |
---|
478 | result += convertFq_nmod_t2FacCF (coeff, alpha)*power (x, i); |
---|
479 | fq_nmod_zero (coeff, ctx); |
---|
480 | } |
---|
481 | fq_nmod_clear (coeff, ctx); |
---|
482 | |
---|
483 | return result; |
---|
484 | } |
---|
485 | #endif |
---|
486 | |
---|
487 | void convertFacCFMatrix2Fmpz_mat_t (fmpz_mat_t M, const CFMatrix &m) |
---|
488 | { |
---|
489 | fmpz_mat_init (M, (long) m.rows(), (long) m.columns()); |
---|
490 | |
---|
491 | int i,j; |
---|
492 | for(i=m.rows();i>0;i--) |
---|
493 | { |
---|
494 | for(j=m.columns();j>0;j--) |
---|
495 | { |
---|
496 | convertCF2Fmpz (fmpz_mat_entry (M,i-1,j-1), m(i,j)); |
---|
497 | } |
---|
498 | } |
---|
499 | } |
---|
500 | CFMatrix* convertFmpz_mat_t2FacCFMatrix(const fmpz_mat_t m) |
---|
501 | { |
---|
502 | CFMatrix *res=new CFMatrix(fmpz_mat_nrows (m),fmpz_mat_ncols (m)); |
---|
503 | int i,j; |
---|
504 | for(i=res->rows();i>0;i--) |
---|
505 | { |
---|
506 | for(j=res->columns();j>0;j--) |
---|
507 | { |
---|
508 | (*res)(i,j)=convertFmpz2CF(fmpz_mat_entry (m,i-1,j-1)); |
---|
509 | } |
---|
510 | } |
---|
511 | return res; |
---|
512 | } |
---|
513 | |
---|
514 | void convertFacCFMatrix2nmod_mat_t (nmod_mat_t M, const CFMatrix &m) |
---|
515 | { |
---|
516 | nmod_mat_init (M, (long) m.rows(), (long) m.columns(), getCharacteristic()); |
---|
517 | |
---|
518 | bool save_sym_ff= isOn (SW_SYMMETRIC_FF); |
---|
519 | if (save_sym_ff) Off (SW_SYMMETRIC_FF); |
---|
520 | int i,j; |
---|
521 | for(i=m.rows();i>0;i--) |
---|
522 | { |
---|
523 | for(j=m.columns();j>0;j--) |
---|
524 | { |
---|
525 | if(!(m(i,j)).isImm()) printf("convertFacCFMatrix2FLINTmat_zz_p: not imm.\n"); |
---|
526 | nmod_mat_entry (M,i-1,j-1)= (m(i,j)).intval(); |
---|
527 | } |
---|
528 | } |
---|
529 | if (save_sym_ff) On (SW_SYMMETRIC_FF); |
---|
530 | } |
---|
531 | |
---|
532 | CFMatrix* convertNmod_mat_t2FacCFMatrix(const nmod_mat_t m) |
---|
533 | { |
---|
534 | CFMatrix *res=new CFMatrix(nmod_mat_nrows (m), nmod_mat_ncols (m)); |
---|
535 | int i,j; |
---|
536 | for(i=res->rows();i>0;i--) |
---|
537 | { |
---|
538 | for(j=res->columns();j>0;j--) |
---|
539 | { |
---|
540 | (*res)(i,j)=CanonicalForm((long) nmod_mat_entry (m, i-1, j-1)); |
---|
541 | } |
---|
542 | } |
---|
543 | return res; |
---|
544 | } |
---|
545 | |
---|
546 | #if __FLINT_RELEASE >= 20400 |
---|
547 | void |
---|
548 | convertFacCFMatrix2Fq_nmod_mat_t (fq_nmod_mat_t M, |
---|
549 | const fq_nmod_ctx_t fq_con, const CFMatrix &m) |
---|
550 | { |
---|
551 | fq_nmod_mat_init (M, (long) m.rows(), (long) m.columns(), fq_con); |
---|
552 | int i,j; |
---|
553 | for(i=m.rows();i>0;i--) |
---|
554 | { |
---|
555 | for(j=m.columns();j>0;j--) |
---|
556 | { |
---|
557 | convertFacCF2nmod_poly_t (M->rows[i-1]+j-1, m (i,j)); |
---|
558 | } |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | CFMatrix* |
---|
563 | convertFq_nmod_mat_t2FacCFMatrix(const fq_nmod_mat_t m, |
---|
564 | const fq_nmod_ctx_t& fq_con, |
---|
565 | const Variable& alpha) |
---|
566 | { |
---|
567 | CFMatrix *res=new CFMatrix(fq_nmod_mat_nrows (m, fq_con), |
---|
568 | fq_nmod_mat_ncols (m, fq_con)); |
---|
569 | int i,j; |
---|
570 | for(i=res->rows();i>0;i--) |
---|
571 | { |
---|
572 | for(j=res->columns();j>0;j--) |
---|
573 | { |
---|
574 | (*res)(i,j)=convertFq_nmod_t2FacCF (fq_nmod_mat_entry (m, i-1, j-1), |
---|
575 | alpha); |
---|
576 | } |
---|
577 | } |
---|
578 | return res; |
---|
579 | } |
---|
580 | #endif |
---|
581 | #if __FLINT_RELEASE >= 20503 |
---|
582 | static void convFlint_RecPP ( const CanonicalForm & f, ulong * exp, nmod_mpoly_t result, nmod_mpoly_ctx_t ctx, int N ) |
---|
583 | { |
---|
584 | // assume f!=0 |
---|
585 | if ( ! f.inCoeffDomain() ) |
---|
586 | { |
---|
587 | int l = f.level(); |
---|
588 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
589 | { |
---|
590 | exp[N-l] = i.exp(); |
---|
591 | convFlint_RecPP( i.coeff(), exp, result, ctx, N ); |
---|
592 | } |
---|
593 | exp[N-l] = 0; |
---|
594 | } |
---|
595 | else |
---|
596 | { |
---|
597 | int c=f.intval(); // with Off(SW_SYMMETRIC_FF): 0<=c<p |
---|
598 | nmod_mpoly_push_term_ui_ui(result,c,exp,ctx); |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | static void convFlint_RecPP ( const CanonicalForm & f, ulong * exp, fmpq_mpoly_t result, fmpq_mpoly_ctx_t ctx, int N ) |
---|
603 | { |
---|
604 | // assume f!=0 |
---|
605 | if ( ! f.inBaseDomain() ) |
---|
606 | { |
---|
607 | int l = f.level(); |
---|
608 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
609 | { |
---|
610 | exp[N-l] = i.exp(); |
---|
611 | convFlint_RecPP( i.coeff(), exp, result, ctx, N ); |
---|
612 | } |
---|
613 | exp[N-l] = 0; |
---|
614 | } |
---|
615 | else |
---|
616 | { |
---|
617 | fmpq_t c; |
---|
618 | fmpq_init(c); |
---|
619 | convertCF2Fmpq(c,f); |
---|
620 | fmpq_mpoly_push_term_fmpq_ui(result,c,exp,ctx); |
---|
621 | fmpq_clear(c); |
---|
622 | } |
---|
623 | } |
---|
624 | |
---|
625 | void convFactoryPFlintMP ( const CanonicalForm & f, nmod_mpoly_t res, nmod_mpoly_ctx_t ctx, int N ) |
---|
626 | { |
---|
627 | if (f.isZero()) return; |
---|
628 | ulong * exp = (ulong*)Alloc(N*sizeof(ulong)); |
---|
629 | memset(exp,0,N*sizeof(ulong)); |
---|
630 | bool save_sym_ff= isOn (SW_SYMMETRIC_FF); |
---|
631 | if (save_sym_ff) Off (SW_SYMMETRIC_FF); |
---|
632 | convFlint_RecPP( f, exp, res, ctx, N ); |
---|
633 | if (save_sym_ff) On(SW_SYMMETRIC_FF); |
---|
634 | Free(exp,N*sizeof(ulong)); |
---|
635 | } |
---|
636 | |
---|
637 | void convFactoryPFlintMP ( const CanonicalForm & f, fmpq_mpoly_t res, fmpq_mpoly_ctx_t ctx, int N ) |
---|
638 | { |
---|
639 | if (f.isZero()) return; |
---|
640 | ulong * exp = (ulong*)Alloc(N*sizeof(ulong)); |
---|
641 | memset(exp,0,N*sizeof(ulong)); |
---|
642 | convFlint_RecPP( f, exp, res, ctx, N ); |
---|
643 | fmpq_mpoly_reduce(res,ctx); |
---|
644 | Free(exp,N*sizeof(ulong)); |
---|
645 | } |
---|
646 | |
---|
647 | CanonicalForm convFlintMPFactoryP(nmod_mpoly_t f, nmod_mpoly_ctx_t ctx, int N) |
---|
648 | { |
---|
649 | CanonicalForm result; |
---|
650 | int d=nmod_mpoly_length(f,ctx)-1; |
---|
651 | ulong* exp=(ulong*)Alloc(N*sizeof(ulong)); |
---|
652 | for(int i=d; i>=0; i--) |
---|
653 | { |
---|
654 | ulong c=nmod_mpoly_get_term_coeff_ui(f,i,ctx); |
---|
655 | nmod_mpoly_get_term_exp_ui(exp,f,i,ctx); |
---|
656 | CanonicalForm term=(int)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 | Free(exp,N*sizeof(ulong)); |
---|
664 | return result; |
---|
665 | } |
---|
666 | |
---|
667 | CanonicalForm convFlintMPFactoryP(fmpq_mpoly_t f, fmpq_mpoly_ctx_t ctx, int N) |
---|
668 | { |
---|
669 | CanonicalForm result; |
---|
670 | int d=fmpq_mpoly_length(f,ctx)-1; |
---|
671 | ulong* exp=(ulong*)Alloc(N*sizeof(ulong)); |
---|
672 | fmpq_t c; |
---|
673 | fmpq_init(c); |
---|
674 | for(int i=d; i>=0; i--) |
---|
675 | { |
---|
676 | fmpq_mpoly_get_term_coeff_fmpq(c,f,i,ctx); |
---|
677 | fmpq_mpoly_get_term_exp_ui(exp,f,i,ctx); |
---|
678 | CanonicalForm term=convertFmpq_t2CF(c); |
---|
679 | for ( int i = 0; i <N; i++ ) |
---|
680 | { |
---|
681 | if (exp[i]!=0) term*=CanonicalForm( Variable( N-i ), exp[i] ); |
---|
682 | } |
---|
683 | result+=term; |
---|
684 | } |
---|
685 | fmpq_clear(c); |
---|
686 | Free(exp,N*sizeof(ulong)); |
---|
687 | return result; |
---|
688 | } |
---|
689 | |
---|
690 | // stolen from: |
---|
691 | // https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog |
---|
692 | static inline int SI_LOG2(int v) |
---|
693 | { |
---|
694 | const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; |
---|
695 | const unsigned int S[] = {1, 2, 4, 8, 16}; |
---|
696 | |
---|
697 | unsigned int r = 0; // result of log2(v) will go here |
---|
698 | if (v & b[4]) { v >>= S[4]; r |= S[4]; } |
---|
699 | if (v & b[3]) { v >>= S[3]; r |= S[3]; } |
---|
700 | if (v & b[2]) { v >>= S[2]; r |= S[2]; } |
---|
701 | if (v & b[1]) { v >>= S[1]; r |= S[1]; } |
---|
702 | if (v & b[0]) { v >>= S[0]; r |= S[0]; } |
---|
703 | return (int)r; |
---|
704 | } |
---|
705 | |
---|
706 | CanonicalForm mulFlintMP_Zp(const CanonicalForm& F,int lF, const CanonicalForm& G, int lG,int m) |
---|
707 | { |
---|
708 | int bits=SI_LOG2(m)+1; |
---|
709 | int N=F.level(); |
---|
710 | nmod_mpoly_ctx_t ctx; |
---|
711 | nmod_mpoly_ctx_init(ctx,N,ORD_LEX,getCharacteristic()); |
---|
712 | nmod_mpoly_t f,g,res; |
---|
713 | nmod_mpoly_init3(f,lF,bits,ctx); |
---|
714 | nmod_mpoly_init3(g,lG,bits,ctx); |
---|
715 | convFactoryPFlintMP(F,f,ctx,N); |
---|
716 | convFactoryPFlintMP(G,g,ctx,N); |
---|
717 | nmod_mpoly_init(res,ctx); |
---|
718 | nmod_mpoly_mul(res,f,g,ctx); |
---|
719 | nmod_mpoly_clear(g,ctx); |
---|
720 | nmod_mpoly_clear(f,ctx); |
---|
721 | CanonicalForm RES=convFlintMPFactoryP(res,ctx,N); |
---|
722 | nmod_mpoly_clear(res,ctx); |
---|
723 | nmod_mpoly_ctx_clear(ctx); |
---|
724 | return RES; |
---|
725 | } |
---|
726 | |
---|
727 | CanonicalForm mulFlintMP_QQ(const CanonicalForm& F,int lF, const CanonicalForm& G, int lG, int m) |
---|
728 | { |
---|
729 | int bits=SI_LOG2(m)+1; |
---|
730 | int N=F.level(); |
---|
731 | fmpq_mpoly_ctx_t ctx; |
---|
732 | fmpq_mpoly_ctx_init(ctx,N,ORD_LEX); |
---|
733 | fmpq_mpoly_t f,g,res; |
---|
734 | fmpq_mpoly_init3(f,lF,bits,ctx); |
---|
735 | fmpq_mpoly_init3(g,lG,bits,ctx); |
---|
736 | convFactoryPFlintMP(F,f,ctx,N); |
---|
737 | convFactoryPFlintMP(G,g,ctx,N); |
---|
738 | fmpq_mpoly_init(res,ctx); |
---|
739 | fmpq_mpoly_mul(res,f,g,ctx); |
---|
740 | fmpq_mpoly_clear(g,ctx); |
---|
741 | fmpq_mpoly_clear(f,ctx); |
---|
742 | CanonicalForm RES=convFlintMPFactoryP(res,ctx,N); |
---|
743 | fmpq_mpoly_clear(res,ctx); |
---|
744 | fmpq_mpoly_ctx_clear(ctx); |
---|
745 | return RES; |
---|
746 | } |
---|
747 | |
---|
748 | CanonicalForm gcdFlintMP_Zp(const CanonicalForm& F, const CanonicalForm& G) |
---|
749 | { |
---|
750 | int N=F.level(); |
---|
751 | int lf,lg,m=1<<MPOLY_MIN_BITS; |
---|
752 | lf=size_maxexp(F,m); |
---|
753 | lg=size_maxexp(G,m); |
---|
754 | int bits=SI_LOG2(m)+1; |
---|
755 | nmod_mpoly_ctx_t ctx; |
---|
756 | nmod_mpoly_ctx_init(ctx,N,ORD_LEX,getCharacteristic()); |
---|
757 | nmod_mpoly_t f,g,res; |
---|
758 | nmod_mpoly_init3(f,lf,bits,ctx); |
---|
759 | nmod_mpoly_init3(g,lg,bits,ctx); |
---|
760 | convFactoryPFlintMP(F,f,ctx,N); |
---|
761 | convFactoryPFlintMP(G,g,ctx,N); |
---|
762 | nmod_mpoly_init(res,ctx); |
---|
763 | int ok=nmod_mpoly_gcd(res,f,g,ctx); |
---|
764 | nmod_mpoly_clear(g,ctx); |
---|
765 | nmod_mpoly_clear(f,ctx); |
---|
766 | CanonicalForm RES=1; |
---|
767 | if (ok) |
---|
768 | { |
---|
769 | RES=convFlintMPFactoryP(res,ctx,N); |
---|
770 | } |
---|
771 | nmod_mpoly_clear(res,ctx); |
---|
772 | nmod_mpoly_ctx_clear(ctx); |
---|
773 | return RES; |
---|
774 | } |
---|
775 | |
---|
776 | static CanonicalForm b_content ( const CanonicalForm & f ) |
---|
777 | { |
---|
778 | if ( f.inCoeffDomain() ) |
---|
779 | return f; |
---|
780 | else |
---|
781 | { |
---|
782 | CanonicalForm result = 0; |
---|
783 | CFIterator i; |
---|
784 | for ( i = f; i.hasTerms() && (!result.isOne()); i++ ) |
---|
785 | result=bgcd( b_content(i.coeff()) , result ); |
---|
786 | return result; |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | |
---|
791 | CanonicalForm gcdFlintMP_QQ(const CanonicalForm& F, const CanonicalForm& G) |
---|
792 | { |
---|
793 | int N=F.level(); |
---|
794 | fmpq_mpoly_ctx_t ctx; |
---|
795 | fmpq_mpoly_ctx_init(ctx,N,ORD_LEX); |
---|
796 | fmpq_mpoly_t f,g,res; |
---|
797 | fmpq_mpoly_init(f,ctx); |
---|
798 | fmpq_mpoly_init(g,ctx); |
---|
799 | convFactoryPFlintMP(F,f,ctx,N); |
---|
800 | convFactoryPFlintMP(G,g,ctx,N); |
---|
801 | fmpq_mpoly_init(res,ctx); |
---|
802 | int ok=fmpq_mpoly_gcd(res,f,g,ctx); |
---|
803 | fmpq_mpoly_clear(g,ctx); |
---|
804 | fmpq_mpoly_clear(f,ctx); |
---|
805 | CanonicalForm RES=1; |
---|
806 | if (ok) |
---|
807 | { |
---|
808 | // Flint normalizes the gcd to be monic. |
---|
809 | // Singular wants a gcd defined over ZZ that is primitive and has a positive leading coeff. |
---|
810 | if (!fmpq_mpoly_is_zero(res, ctx)) |
---|
811 | { |
---|
812 | fmpq_t content; |
---|
813 | fmpq_init(content); |
---|
814 | fmpq_mpoly_content(content, res, ctx); |
---|
815 | fmpq_mpoly_scalar_div_fmpq(res, res, content, ctx); |
---|
816 | fmpq_clear(content); |
---|
817 | } |
---|
818 | RES=convFlintMPFactoryP(res,ctx,N); |
---|
819 | // gcd(2x,4x) should be 2x, so RES should also have the gcd(lc(F),lc(G)) |
---|
820 | RES*=bgcd(b_content(F),b_content(G)); |
---|
821 | } |
---|
822 | fmpq_mpoly_clear(res,ctx); |
---|
823 | fmpq_mpoly_ctx_clear(ctx); |
---|
824 | return RES; |
---|
825 | } |
---|
826 | |
---|
827 | #endif |
---|
828 | |
---|
829 | #endif |
---|
830 | |
---|
831 | |
---|