source: git/factory/FLINTconvert.cc @ fc25d7

spielwiese
Last change on this file since fc25d7 was fc25d7, checked in by Martin Lee <martinlee84@…>, 10 years ago
chg: added new conversion for polys over Fq (requires FLINT at least 2.4) Conflicts: factory/FLINTconvert.cc
  • Property mode set to 100644
File size: 13.5 KB
Line 
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#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif /* HAVE_CONFIG_H */
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_FLINT
28#ifdef HAVE_CSTDIO
29#include <cstdio>
30#else
31#include <stdio.h>
32#endif
33#ifdef __cplusplus
34extern "C"
35{
36#endif
37#ifndef __GMP_BITS_PER_MP_LIMB
38#define __GMP_BITS_PER_MP_LIMB GMP_LIMB_BITS
39#endif
40#include <flint/fmpz.h>
41#include <flint/fmpq.h>
42#include <flint/fmpz_poly.h>
43#include <flint/fmpz_mod_poly.h>
44#include <flint/nmod_poly.h>
45#include <flint/fmpq_poly.h>
46#include <flint/nmod_mat.h>
47#include <flint/fmpz_mat.h>
48#if (__FLINT_VERSION_MINOR >= 4)
49#include <flint/fq.h>
50#include <flint/fq_poly.h>
51#include <flint/fq_nmod.h>
52#include <flint/fq_nmod_poly.h>
53#include <flint/fq_nmod_mat.h>
54#endif
55#ifdef __cplusplus
56}
57#endif
58
59#include "FLINTconvert.h"
60
61void convertCF2Fmpz (fmpz_t result, const CanonicalForm& f)
62{
63  if (f.isImm())
64    fmpz_set_si (result, f.intval());
65  else
66  {
67    mpz_t gmp_val;
68    f.mpzval(gmp_val);
69    fmpz_set_mpz (result, gmp_val);
70    mpz_clear (gmp_val);
71  }
72}
73
74void convertFacCF2Fmpz_poly_t (fmpz_poly_t result, const CanonicalForm& f)
75{
76  fmpz_poly_init2 (result, degree (f)+1);
77  _fmpz_poly_set_length(result, degree(f)+1);
78  for (CFIterator i= f; i.hasTerms(); i++)
79    convertCF2Fmpz (fmpz_poly_get_coeff_ptr(result, i.exp()), i.coeff());
80}
81
82CanonicalForm convertFmpz2CF (const fmpz_t coefficient)
83{
84  if (fmpz_cmp_si (coefficient, MINIMMEDIATE) >= 0 &&
85      fmpz_cmp_si (coefficient, MAXIMMEDIATE) <= 0)
86  {
87    long coeff= fmpz_get_si (coefficient);
88    return CanonicalForm (coeff);
89  }
90  else
91  {
92    mpz_t gmp_val;
93    mpz_init (gmp_val);
94    fmpz_get_mpz (gmp_val, coefficient);
95    CanonicalForm result= CanonicalForm (CFFactory::basic (gmp_val));
96    return result;
97  }
98}
99
100CanonicalForm
101convertFmpz_poly_t2FacCF (const fmpz_poly_t poly, const Variable& x)
102{
103  CanonicalForm result= 0;
104  fmpz* coeff;
105  for (int i= 0; i < fmpz_poly_length (poly); i++)
106  {
107    coeff= fmpz_poly_get_coeff_ptr (poly, i);
108    if (!fmpz_is_zero (coeff))
109      result += convertFmpz2CF (coeff)*power (x,i);
110  }
111  return result;
112}
113
114void
115convertFacCF2nmod_poly_t (nmod_poly_t result, const CanonicalForm& f)
116{
117  bool save_sym_ff= isOn (SW_SYMMETRIC_FF);
118  if (save_sym_ff) Off (SW_SYMMETRIC_FF);
119  nmod_poly_init2 (result, getCharacteristic(), degree (f)+1);
120  for (CFIterator i= f; i.hasTerms(); i++)
121  {
122    CanonicalForm c= i.coeff();
123    if (!c.isImm()) c=c.mapinto(); //c%= getCharacteristic();
124    if (!c.isImm())
125    {  //This case will never happen if the characteristic is in fact a prime
126       // number, since all coefficients are represented as immediates
127       printf("convertCF2nmod_poly_t: coefficient not immediate!, char=%d\n",
128              getCharacteristic());
129    }
130    else
131      nmod_poly_set_coeff_ui (result, i.exp(), c.intval());
132  }
133  if (save_sym_ff) On (SW_SYMMETRIC_FF);
134}
135
136CanonicalForm
137convertnmod_poly_t2FacCF (const nmod_poly_t poly, const Variable& x)
138{
139  CanonicalForm result= 0;
140  for (int i= 0; i < nmod_poly_length (poly); i++)
141  {
142    ulong coeff= nmod_poly_get_coeff_ui (poly, i);
143    if (!coeff == 0)
144      result += CanonicalForm ((long)coeff)*power (x,i);
145  }
146  return result;
147}
148
149void convertCF2Fmpq (fmpq_t result, const CanonicalForm& f)
150{
151  ASSERT (isOn (SW_RATIONAL), "expected rational");
152  fmpz_t tmp1, tmp2;
153  fmpz_init (tmp1);
154  fmpz_init (tmp2);
155  if (f.isImm ())
156  {
157    fmpz_set_si (tmp1, f.num().intval());
158    fmpz_set_si (tmp2, f.den().intval());
159  }
160  else
161  {
162    mpz_t gmp_val;
163    gmp_numerator (f, gmp_val);
164    fmpz_set_mpz (tmp1, gmp_val);
165    mpz_clear (gmp_val);
166    gmp_denominator (f, gmp_val);
167    fmpz_set_mpz (tmp2, gmp_val);
168    mpz_clear (gmp_val);
169  }
170
171  fmpz_set (fmpq_numref (result), tmp1);
172  fmpz_set (fmpq_denref (result), tmp2);
173  fmpz_clear (tmp1);
174  fmpz_clear (tmp2);
175}
176
177CanonicalForm convertFmpq_t2CF (const fmpq_t q)
178{
179  ASSERT (isOn (SW_RATIONAL), "expected rational");
180
181  CanonicalForm num, den;
182  mpz_t nnum, nden;
183  mpz_init (nnum);
184  mpz_init (nden);
185  fmpz_get_mpz (nnum, fmpq_numref (q));
186  fmpz_get_mpz (nden, fmpq_denref (q));
187
188  if (mpz_is_imm (nnum) && mpz_is_imm (nden))
189  {
190    num= CanonicalForm (mpz_get_si(nnum));
191    den= CanonicalForm (mpz_get_si(nden));
192    mpz_clear (nnum);
193    mpz_clear (nden);
194    return num/den;
195  }
196  else
197    return make_cf (nnum, nden, false);
198}
199
200CanonicalForm
201convertFmpq_poly_t2FacCF (const fmpq_poly_t p, const Variable& x)
202{
203  CanonicalForm result= 0;
204  fmpq_t coeff;
205  long n= fmpq_poly_length (p);
206  for (long i= 0; i < n; i++)
207  {
208    fmpq_init (coeff);
209    fmpq_poly_get_coeff_fmpq (coeff, p, i);
210    if (fmpq_is_zero (coeff))
211    {
212      fmpq_clear (coeff);
213      continue;
214    }
215    result += convertFmpq_t2CF (coeff)*power (x, i);
216    fmpq_clear (coeff);
217  }
218  return result;
219}
220
221void convertFacCF2Fmpz_array (fmpz* result, const CanonicalForm& f)
222{
223  for (CFIterator i= f; i.hasTerms(); i++)
224    convertCF2Fmpz (&result[i.exp()], i.coeff());
225}
226
227void convertFacCF2Fmpq_poly_t (fmpq_poly_t result, const CanonicalForm& f)
228{
229  ASSERT (isOn (SW_RATIONAL), "expected poly over Q");
230
231  fmpq_poly_init2 (result, degree (f)+1);
232  _fmpq_poly_set_length (result, degree (f) + 1);
233  CanonicalForm den= bCommonDen (f);
234  convertFacCF2Fmpz_array (fmpq_poly_numref (result), f*den);
235  convertCF2Fmpz (fmpq_poly_denref (result), den);
236}
237
238CFFList
239convertFLINTnmod_poly_factor2FacCFFList (const nmod_poly_factor_t fac,
240                                          const mp_limb_t leadingCoeff,
241                                          const Variable& x
242                                         )
243{
244  CFFList result;
245  if (leadingCoeff != 1)
246    result.insert (CFFactor (CanonicalForm ((long) leadingCoeff), 1));
247
248  long i;
249
250  for (i = 0; i < fac->num; i++)
251    result.append (CFFactor (convertnmod_poly_t2FacCF (
252                             (nmod_poly_t &)fac->p[i],x),
253                             fac->exp[i]));
254  return result;
255}
256
257#if __FLINT_VERSION_MINOR >= 4
258CFFList
259convertFLINTFq_nmod_poly_factor2FacCFFList (const fq_nmod_poly_factor_t fac,
260                                       const Variable& x, const Variable& alpha,
261                                       const fq_nmod_ctx_t fq_con
262                                         )
263{
264  CFFList result;
265
266  long i;
267
268  for (i = 0; i < fac->num; i++)
269    result.append (CFFactor (convertFq_nmod_poly_t2FacCF (
270                             (fq_nmod_poly_t &)fac->poly[i], x, alpha, fq_con),
271                             fac->exp[i]));
272  return result;
273}
274#endif
275
276void
277convertFacCF2Fmpz_mod_poly_t (fmpz_mod_poly_t result, const CanonicalForm& f,
278                              const fmpz_t p)
279{
280  fmpz_mod_poly_init2 (result, p, degree (f) + 1);
281  fmpz_poly_t buf;
282  convertFacCF2Fmpz_poly_t (buf, f);
283  fmpz_mod_poly_set_fmpz_poly (result, buf);
284  fmpz_poly_clear (buf);
285}
286
287CanonicalForm
288convertFmpz_mod_poly_t2FacCF (const fmpz_mod_poly_t poly, const Variable& x,
289                              const modpk& b)
290{
291  fmpz_poly_t buf;
292  fmpz_poly_init (buf);
293  fmpz_mod_poly_get_fmpz_poly (buf, poly);
294  CanonicalForm result= convertFmpz_poly_t2FacCF (buf, x);
295  fmpz_poly_clear (buf);
296  return b (result);
297}
298
299#if __FLINT_VERSION_MINOR >= 4
300void
301convertFacCF2Fq_nmod_t (fq_nmod_t result, const CanonicalForm& f,
302                        const fq_nmod_ctx_t ctx)
303{
304  bool save_sym_ff= isOn (SW_SYMMETRIC_FF);
305  if (save_sym_ff) Off (SW_SYMMETRIC_FF);
306  for (CFIterator i= f; i.hasTerms(); i++)
307  {
308    CanonicalForm c= i.coeff();
309    if (!c.isImm()) c=c.mapinto(); //c%= getCharacteristic();
310    if (!c.isImm())
311    {  //This case will never happen if the characteristic is in fact a prime
312       // number, since all coefficients are represented as immediates
313       printf("convertFacCF2Fq_nmod_t: coefficient not immediate!, char=%d\n",
314              getCharacteristic());
315    }
316    else
317    {
318      STICKYASSERT (i.exp() <= fq_nmod_ctx_degree(ctx), "convertFacCF2Fq_nmod_t: element is not reduced");
319      nmod_poly_set_coeff_ui (result, i.exp(), c.intval());
320    }
321  }
322  if (save_sym_ff) On (SW_SYMMETRIC_FF);
323}
324
325CanonicalForm
326convertFq_nmod_t2FacCF (const fq_nmod_t poly, const Variable& alpha)
327{
328  return convertnmod_poly_t2FacCF (poly, alpha);
329}
330
331void
332convertFacCF2Fq_t (fq_t result, const CanonicalForm& f, const fq_ctx_t ctx)
333{
334  fmpz_poly_init2 (result, fq_ctx_degree(ctx));
335  ASSERT (degree (f) < fq_ctx_degree (ctx), "input is not reduced");
336  _fmpz_poly_set_length(result, degree(f)+1);
337  for (CFIterator i= f; i.hasTerms(); i++)
338    convertCF2Fmpz (fmpz_poly_get_coeff_ptr(result, i.exp()), i.coeff());
339  _fmpz_vec_scalar_mod_fmpz (result->coeffs, result->coeffs, degree (f) + 1,
340                             &ctx->p);
341  _fmpz_poly_normalise (result);
342}
343
344CanonicalForm
345convertFq_t2FacCF (const fq_t poly, const Variable& alpha)
346{
347  return convertFmpz_poly_t2FacCF (poly, alpha);
348}
349
350void
351convertFacCF2Fq_poly_t (fq_poly_t result, const CanonicalForm& f,
352                        const fq_ctx_t ctx)
353{
354  fq_poly_init2 (result, degree (f)+1, ctx);
355  _fq_poly_set_length (result, degree (f) + 1, ctx);
356  fmpz_poly_t buf;
357  for (CFIterator i= f; i.hasTerms(); i++)
358  {
359    convertFacCF2Fmpz_poly_t (buf, i.coeff());
360    _fmpz_vec_scalar_mod_fmpz (buf->coeffs, buf->coeffs, degree (i.coeff()) + 1,
361                               &ctx->p);
362    _fmpz_poly_normalise (buf);
363    fq_poly_set_coeff (result, i.exp(), buf, ctx);
364    fmpz_poly_clear (buf);
365  }
366}
367
368void
369convertFacCF2Fq_nmod_poly_t (fq_nmod_poly_t result, const CanonicalForm& f,
370                             const fq_nmod_ctx_t ctx)
371{
372  fq_nmod_poly_init2 (result, degree (f)+1, ctx);
373  _fq_nmod_poly_set_length (result, degree (f) + 1, ctx);
374  fq_nmod_t buf;
375  fq_nmod_init2 (buf, ctx);
376  for (CFIterator i= f; i.hasTerms(); i++)
377  {
378    convertFacCF2Fq_nmod_t (buf, i.coeff(), ctx);
379    fq_nmod_poly_set_coeff (result, i.exp(), buf, ctx);
380    fq_nmod_zero (buf, ctx);
381  }
382  fq_nmod_clear (buf, ctx);
383}
384
385CanonicalForm
386convertFq_poly_t2FacCF (const fq_poly_t p, const Variable& x,
387                        const Variable& alpha, const fq_ctx_t ctx)
388{
389  CanonicalForm result= 0;
390  fq_t coeff;
391  long n= fq_poly_length (p, ctx);
392  fq_init2 (coeff, ctx);
393  for (long i= 0; i < n; i++)
394  {
395    fq_poly_get_coeff (coeff, p, i, ctx);
396    if (fq_is_zero (coeff, ctx))
397      continue;
398    result += convertFq_t2FacCF (coeff, alpha)*power (x, i);
399    fq_zero (coeff, ctx);
400  }
401  fq_clear (coeff, ctx);
402
403  return result;
404}
405
406CanonicalForm
407convertFq_nmod_poly_t2FacCF (const fq_nmod_poly_t p, const Variable& x,
408                             const Variable& alpha, const fq_nmod_ctx_t ctx)
409{
410  CanonicalForm result= 0;
411  fq_nmod_t coeff;
412  long n= fq_nmod_poly_length (p, ctx);
413  fq_nmod_init2 (coeff, ctx);
414  for (long i= 0; i < n; i++)
415  {
416    fq_nmod_poly_get_coeff (coeff, p, i, ctx);
417    if (fq_nmod_is_zero (coeff, ctx))
418      continue;
419    result += convertFq_nmod_t2FacCF (coeff, alpha)*power (x, i);
420    fq_nmod_zero (coeff, ctx);
421  }
422  fq_nmod_clear (coeff, ctx);
423
424  return result;
425}
426#endif
427
428void convertFacCFMatrix2Fmpz_mat_t (fmpz_mat_t M, const CFMatrix &m)
429{
430  fmpz_mat_init (M, (long) m.rows(), (long) m.columns());
431
432  int i,j;
433  for(i=m.rows();i>0;i--)
434  {
435    for(j=m.columns();j>0;j--)
436    {
437      convertCF2Fmpz (fmpz_mat_entry (M,i-1,j-1), m(i,j));
438    }
439  }
440}
441CFMatrix* convertFmpz_mat_t2FacCFMatrix(const fmpz_mat_t m)
442{
443  CFMatrix *res=new CFMatrix(fmpz_mat_nrows (m),fmpz_mat_ncols (m));
444  int i,j;
445  for(i=res->rows();i>0;i--)
446  {
447    for(j=res->columns();j>0;j--)
448    {
449      (*res)(i,j)=convertFmpz2CF(fmpz_mat_entry (m,i-1,j-1));
450    }
451  }
452  return res;
453}
454
455void convertFacCFMatrix2nmod_mat_t (nmod_mat_t M, const CFMatrix &m)
456{
457  nmod_mat_init (M, (long) m.rows(), (long) m.columns(), getCharacteristic());
458
459  bool save_sym_ff= isOn (SW_SYMMETRIC_FF);
460  if (save_sym_ff) Off (SW_SYMMETRIC_FF);
461  int i,j;
462  for(i=m.rows();i>0;i--)
463  {
464    for(j=m.columns();j>0;j--)
465    {
466      if(!(m(i,j)).isImm()) printf("convertFacCFMatrix2FLINTmat_zz_p: not imm.\n");
467      nmod_mat_entry (M,i-1,j-1)= (m(i,j)).intval();
468    }
469  }
470  if (save_sym_ff) On (SW_SYMMETRIC_FF);
471}
472
473CFMatrix* convertNmod_mat_t2FacCFMatrix(const nmod_mat_t m)
474{
475  CFMatrix *res=new CFMatrix(nmod_mat_nrows (m), nmod_mat_ncols (m));
476  int i,j;
477  for(i=res->rows();i>0;i--)
478  {
479    for(j=res->columns();j>0;j--)
480    {
481      (*res)(i,j)=CanonicalForm((long) nmod_mat_entry (m, i-1, j-1));
482    }
483  }
484  return res;
485}
486
487#if __FLINT_VERSION_MINOR >= 4
488void
489convertFacCFMatrix2Fq_nmod_mat_t (fq_nmod_mat_t M,
490                                  const fq_nmod_ctx_t fq_con, const CFMatrix &m)
491{
492  fq_nmod_mat_init (M, (long) m.rows(), (long) m.columns(), fq_con);
493  int i,j;
494  for(i=m.rows();i>0;i--)
495  {
496    for(j=m.columns();j>0;j--)
497    {
498      convertFacCF2nmod_poly_t (M->rows[i-1]+j-1, m (i,j));
499    }
500  }
501}
502
503CFMatrix*
504convertFq_nmod_mat_t2FacCFMatrix(const fq_nmod_mat_t m,
505                                 const fq_nmod_ctx_t& fq_con,
506                                 const Variable& alpha)
507{
508  CFMatrix *res=new CFMatrix(fq_nmod_mat_nrows (m, fq_con),
509                             fq_nmod_mat_ncols (m, fq_con));
510  int i,j;
511  for(i=res->rows();i>0;i--)
512  {
513    for(j=res->columns();j>0;j--)
514    {
515      (*res)(i,j)=convertFq_nmod_t2FacCF (fq_nmod_mat_entry (m, i-1, j-1),
516                                          alpha);
517    }
518  }
519  return res;
520}
521#endif
522
523#endif
524
525
Note: See TracBrowser for help on using the repository browser.