source: git/libpolys/polys/flintconv.cc @ 9e2e97

spielwiese
Last change on this file since 9e2e97 was 9e2e97, checked in by Hans Schoenemann <hannes@…>, 2 years ago
system("rref",M): move to flintconv.cc, add Q as cf
  • Property mode set to 100644
File size: 11.3 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/*
6* ABSTRACT: convert data between Singular and Flint
7*/
8
9
10
11#include "misc/auxiliary.h"
12#include "flintconv.h"
13
14#ifdef HAVE_FLINT
15#if __FLINT_RELEASE >= 20500
16
17#include "coeffs/coeffs.h"
18#include "coeffs/longrat.h"
19#include "polys/monomials/p_polys.h"
20
21#include "polys/sbuckets.h"
22#include "polys/clapconv.h"
23
24#include "simpleideals.h"
25
26
27int convFlintISingI (fmpz_t f)
28{
29  //return fmpz_get_si(f);
30  return (int)*f;
31}
32
33void convSingIFlintI(fmpz_t f, int p)
34{
35  fmpz_init(f);
36  *f=p;
37  //fmpz_set_si(f,p);
38  return;
39}
40
41void convFlintNSingN (mpz_t z, fmpz_t f)
42{
43  mpz_init(z);
44  fmpz_get_mpz(z,f);
45}
46
47number convFlintNSingN (fmpz_t f)
48{
49  number n;
50  if(COEFF_IS_MPZ(*f))
51    nlMPZ(COEFF_TO_PTR(*f),n,NULL);
52  else
53  {
54    mpz_t z;
55    mpz_init(z);
56    fmpz_get_mpz(z,f);
57    nlMPZ(z,n,NULL);
58    mpz_clear(z);
59  }
60  return n;
61}
62
63number convFlintNSingN (fmpq_t f, const coeffs cf)
64{
65#if __FLINT_RELEASE > 20502
66  number z;
67  if (nCoeff_is_Q(cf))
68  {
69    z=ALLOC_RNUMBER();
70    #if defined(LDEBUG)
71    z->debug=123456;
72    #endif
73    z->s=0;
74    mpz_init(z->z);
75    mpz_init(z->n);
76    fmpq_get_mpz_frac(z->z,z->n,f);
77  }
78  else
79  {
80    mpz_t a,b;
81    mpz_init(a);
82    mpz_init(b);
83    fmpq_get_mpz_frac(a,b,f);
84    number na=n_InitMPZ(a,cf);
85    number nb=n_InitMPZ(b,cf);
86    z=n_Div(na,nb,cf);
87    n_Delete(&na,cf);
88    n_Delete(&nb,cf);
89    mpz_clear(a);
90    mpz_clear(b);
91  }
92  n_Normalize(z,cf);
93  n_Test(z,cf);
94  return z;
95#else
96  WerrorS("not implemented");
97  return NULL;
98#endif
99}
100
101number convFlintNSingN (fmpz_t f, const coeffs cf)
102{
103#if __FLINT_RELEASE > 20502
104  number z;
105  mpz_t a;
106  mpz_init(a);
107  fmpz_get_mpz(a,f);
108  z=n_InitMPZ(a,cf);
109  mpz_clear(a);
110  n_Normalize(z,cf);
111  n_Test(z,cf);
112  return z;
113#else
114  WerrorS("not implemented");
115  return NULL;
116#endif
117}
118
119number convFlintNSingN_QQ (fmpq_t f, const coeffs cf)
120{
121#if __FLINT_RELEASE > 20502
122  if (fmpz_is_one(fmpq_denref(f)))
123  {
124    if (fmpz_fits_si(fmpq_numref(f)))
125    {
126      long i=fmpz_get_si(fmpq_numref(f));
127      return n_Init(i,cf);
128    }
129  }
130  number z=ALLOC_RNUMBER();
131  #if defined(LDEBUG)
132  z->debug=123456;
133  #endif
134  mpz_init(z->z);
135  if (fmpz_is_one(fmpq_denref(f)))
136  {
137    z->s=3;
138    fmpz_get_mpz(z->z,fmpq_numref(f));
139  }
140  else
141  {
142    z->s=0;
143    mpz_init(z->n);
144    fmpq_get_mpz_frac(z->z,z->n,f);
145  }
146  n_Test(z,cf);
147  return z;
148#else
149  WerrorS("not implemented");
150  return NULL;
151#endif
152}
153
154void convSingNFlintN(fmpz_t f, mpz_t n)
155{
156  fmpz_init(f);
157  fmpz_set_mpz(f,n);
158}
159
160void convSingNFlintN(fmpz_t f, number n)
161{
162  fmpz_init(f);
163  fmpz_set_mpz(f,(mpz_ptr)n);
164}
165
166void convSingNFlintN(fmpq_t f, number n, const coeffs cf)
167{
168  if (nCoeff_is_Q(cf))
169  {
170    fmpq_init(f);
171    if (SR_HDL(n)&SR_INT)
172      fmpq_set_si(f,SR_TO_INT(n),1);
173    else if (n->s<3)
174    {
175      fmpz_set_mpz(fmpq_numref(f), n->z);
176      fmpz_set_mpz(fmpq_denref(f), n->n);
177    }
178    else
179    {
180      mpz_t one;
181      mpz_init_set_si(one,1);
182      fmpz_set_mpz(fmpq_numref(f), n->z);
183      fmpz_set_mpz(fmpq_denref(f), one);
184      mpz_clear(one);
185    }
186  }
187  else
188  {
189    coeffs QQ=nInitChar(n_Q,NULL);
190    nMapFunc nMap=n_SetMap(cf,QQ);
191    if (nMap!=NULL)
192    {
193      number nn=nMap(n,cf,QQ);
194      convSingNFlintN(f,nn,QQ);
195    }
196    nKillChar(QQ);
197  }
198}
199
200void convSingNFlintN_QQ(fmpq_t f, number n)
201{
202  fmpq_init(f);
203  if (SR_HDL(n)&SR_INT)
204    fmpq_set_si(f,SR_TO_INT(n),1);
205  else if (n->s<3)
206  {
207    fmpz_set_mpz(fmpq_numref(f), n->z);
208    fmpz_set_mpz(fmpq_denref(f), n->n);
209  }
210  else
211  {
212    mpz_t one;
213    mpz_init_set_si(one,1);
214    fmpz_set_mpz(fmpq_numref(f), n->z);
215    fmpz_set_mpz(fmpq_denref(f), one);
216    mpz_clear(one);
217  }
218}
219
220void convSingNFlintNN(fmpq_t re, fmpq_t im, number n, const coeffs cf)
221{
222  number n_2=n_RePart(n,cf);
223  convSingNFlintN(re,n_2,cf);
224  n_Delete(&n_2,cf);
225  n_2=n_ImPart(n,cf);
226  convSingNFlintN(im,n_2,cf);
227  n_Delete(&n_2,cf);
228}
229
230void convSingPFlintP(fmpq_poly_t res, poly p, const ring r)
231{
232  int d=p_GetExp(p,1,r);
233  fmpq_poly_init2(res,d+1);
234  _fmpq_poly_set_length (res, d + 1);
235  while(p!=NULL)
236  {
237    number n=pGetCoeff(p);
238    fmpq_t c;
239    convSingNFlintN(c,n,r->cf);
240    fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c);
241    fmpq_clear(c);
242    pIter(p);
243  }
244}
245
246void convSingImPFlintP(fmpq_poly_t res, poly p, const ring r)
247{
248  int d=p_GetExp(p,1,r);
249  fmpq_poly_init2(res,d+1);
250  _fmpq_poly_set_length (res, d + 1);
251  while(p!=NULL)
252  {
253    number n=n_ImPart(pGetCoeff(p),r->cf);
254    fmpq_t c;
255    convSingNFlintN(c,n,r->cf);
256    fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c);
257    fmpq_clear(c);
258    n_Delete(&n,r->cf);
259    pIter(p);
260  }
261}
262
263poly convFlintPSingP(fmpq_poly_t f, const ring r)
264{
265  int d=fmpq_poly_length(f);
266  poly p=NULL;
267  fmpq_t c;
268  fmpq_init(c);
269  for(int i=0; i<=d; i++)
270  {
271    fmpq_poly_get_coeff_fmpq(c,f,i);
272    number n=convFlintNSingN(c,r->cf);
273    poly pp=p_Init(r);
274    pSetCoeff0(pp,n);
275    p_SetExp(pp,1,i,r);
276    p_Setm(pp,r);
277    p=p_Add_q(p,pp,r);
278  }
279  fmpq_clear(c);
280  p_Test(p,r);
281  return p;
282}
283
284void convSingPFlintnmod_poly_t(nmod_poly_t result, const poly p, const ring r)
285{
286  // assume univariate, r->cf=Z/p
287  nmod_poly_init2 (result,rChar(r),p_Deg(p,r));
288  poly h=p;
289  while(h!=NULL)
290  {
291    if (h==NULL)
292      nmod_poly_set_coeff_ui(result,0,0);
293    else
294      nmod_poly_set_coeff_ui(result,p_GetExp(h,1,r),n_Int(pGetCoeff(h),r->cf)+rChar(r));
295    pIter(h);
296  }
297}
298
299void convSingMFlintFq_nmod_mat(matrix m, fq_nmod_mat_t M, const fq_nmod_ctx_t fq_con, const ring r)
300{
301  fq_nmod_mat_init (M, (long)MATROWS(m), (long) MATCOLS(m), fq_con);
302  int i,j;
303  for(i=MATROWS(m);i>0;i--)
304  {
305    for(j=MATCOLS(m);j>0;j--)
306    {
307      convSingPFlintnmod_poly_t (M->rows[i-1]+j-1, MATELEM(m,i,j),r);
308    }
309  }
310}
311
312poly convFlintFq_nmodSingP(const fq_nmod_t Fp, const fq_nmod_ctx_t ctx, const ring r)
313{
314  poly p=NULL;
315  poly h;
316  for (int i= 0; i < nmod_poly_length (Fp); i++)
317  {
318    ulong coeff= nmod_poly_get_coeff_ui (Fp, i);
319    if (coeff != 0)
320    h=p_NSet(n_Init(coeff,r->cf),r);
321    if (h!=NULL)
322    {
323      p_SetExp(h,1,i,r);
324      p_Setm(h,r);
325      p=p_Add_q(p,h,r);
326    }
327  }
328  return p;
329}
330
331matrix convFlintFq_nmod_matSingM(fq_nmod_mat_t m, const fq_nmod_ctx_t fq_con, const ring r)
332{
333  matrix M=mpNew(fq_nmod_mat_nrows (m, fq_con),fq_nmod_mat_ncols (m, fq_con));
334   int i,j;
335  for(i=MATROWS(M);i>0;i--)
336  {
337    for(j=MATCOLS(M);j>0;j--)
338    {
339      MATELEM(M,i,j)=convFlintFq_nmodSingP(fq_nmod_mat_entry (m, i-1, j-1),
340                                          fq_con, r);
341    }
342  }
343  return M;
344}
345
346void convSingMFlintNmod_mat(matrix m, nmod_mat_t M, const ring r)
347{
348  nmod_mat_init (M, (long)MATROWS(m), (long) MATCOLS(m), rChar(r));
349  int i,j;
350  for(i=MATROWS(m);i>0;i--)
351  {
352    for(j=MATCOLS(m);j>0;j--)
353    {
354      poly h=MATELEM(m,i,j);
355      if (h==NULL)
356        nmod_mat_entry(M,i-1,j-1)=0;
357      else
358        nmod_mat_entry(M,i-1,j-1)=(long)pGetCoeff(h);
359    }
360  }
361}
362
363matrix convFlintNmod_matSingM(nmod_mat_t m, const ring r)
364{
365  matrix M=mpNew(nmod_mat_nrows (m),nmod_mat_ncols (m));
366   int i,j;
367  for(i=MATROWS(M);i>0;i--)
368  {
369    for(j=MATCOLS(M);j>0;j--)
370    {
371      MATELEM(M,i,j)=p_ISet(nmod_mat_entry (m, i-1, j-1),r);
372    }
373  }
374  return M;
375}
376
377matrix singflint_rref(matrix  m, const ring R)
378{
379  int r=m->rows();
380  int c=m->cols();
381  int i,j;
382  matrix M=mpNew(r,c);
383  if (rField_is_Q(R))
384  {
385    fmpq_mat_t FLINTM;
386    fmpq_mat_init(FLINTM,r,c);
387    number n=n_Init(0,R->cf);
388    for(i=r;i>0;i--)
389    {
390      for(j=c;j>0;j--)
391      {
392        poly h=MATELEM(m,i,j);
393        if (h==NULL)
394          convSingNFlintN(fmpq_mat_entry(FLINTM,i-1,j-1),n,R->cf);
395        else
396          convSingNFlintN(fmpq_mat_entry(FLINTM,i-1,j-1),pGetCoeff(h),R->cf);
397      }
398    }
399    n_Delete(&n,R->cf);
400    fmpq_mat_rref(FLINTM,FLINTM);
401    for(i=r;i>0;i--)
402    {
403      for(j=c;j>0;j--)
404      {
405        n=convFlintNSingN(fmpq_mat_entry(FLINTM,i-1,j-1),R->cf);
406        MATELEM(M,i,j)=p_NSet(n,R);
407      }
408    }
409    fmpq_mat_clear(FLINTM);
410  }
411  else if (rField_is_Zp(R))
412  {
413    nmod_mat_t FLINTM;
414    // convert matrix
415    convSingMFlintNmod_mat(M,FLINTM,R);
416    // rank
417    long rk= nmod_mat_rref (FLINTM);
418    M=convFlintNmod_matSingM(FLINTM,R);
419    // clean up
420    nmod_mat_clear(FLINTM);
421  }
422  else
423  {
424    #if 0
425    fmpz_t p;
426    convSingIFlintI(p,rChar(currRing));
427    fq_nmod_ctx_init(ctx,p,1,"t");
428    fq_nmod_mat_t FLINTM;
429    // convert matrix
430    convSingMFlintFq_nmod_mat(M,FLINTM,ctx,currRing);
431    // rank
432    long rk= fq_nmod_mat_rref (FLINTM,ctx);
433    res->data=(void*)convFlintFq_nmod_matSingM(FLINTM,ctx,currRing);
434    // clean up
435    fq_nmod_mat_clear (FLINTM,ctx);
436    fq_nmod_ctx_clear(ctx);
437    fmpz_clear(p);
438    #endif
439    WerrorS("not implmented for these coefficients");
440  }
441  return M;
442}
443
444bigintmat* singflint_LLL(bigintmat*  m, bigintmat* T)
445{
446  int r=m->rows();
447  int c=m->cols();
448  bigintmat* res=new bigintmat(r,c,m->basecoeffs());
449  fmpz_mat_t M, Transf;
450  fmpz_mat_init(M, r, c);
451  if(T != NULL)
452  {
453    fmpz_mat_init(Transf, T->rows(), T->rows());
454  }
455  fmpz_t dummy;
456  mpz_t n;
457  int i,j;
458  for(i=r;i>0;i--)
459  {
460    for(j=c;j>0;j--)
461    {
462      n_MPZ(n, BIMATELEM(*m, i, j),m->basecoeffs());
463      convSingNFlintN(dummy,n);
464      mpz_clear(n);
465      fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy);
466      fmpz_clear(dummy);
467    }
468  }
469  if(T != NULL)
470  {
471    for(i=T->rows();i>0;i--)
472    {
473      for(j=T->rows();j>0;j--)
474      {
475        n_MPZ(n, BIMATELEM(*T, i, j),T->basecoeffs());
476        convSingNFlintN(dummy,n);
477        mpz_clear(n);
478        fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy);
479        fmpz_clear(dummy);
480      }
481    }
482  }
483  fmpz_lll_t fl;
484  fmpz_lll_context_init_default(fl);
485  if(T != NULL)
486    fmpz_lll(M, Transf, fl);
487  else
488    fmpz_lll(M, NULL, fl);
489  for(i=r;i>0;i--)
490  {
491    for(j=c;j>0;j--)
492    {
493      convFlintNSingN(n, fmpz_mat_entry(M, i-1, j-1));
494      n_Delete(&(BIMATELEM(*res,i,j)),res->basecoeffs());
495      BIMATELEM(*res,i,j)=n_InitMPZ(n,res->basecoeffs());
496      mpz_clear(n);
497    }
498  }
499  if(T != NULL)
500  {
501    for(i=T->rows();i>0;i--)
502    {
503      for(j=T->cols();j>0;j--)
504      {
505        convFlintNSingN(n, fmpz_mat_entry(Transf, i-1, j-1));
506        n_Delete(&(BIMATELEM(*T,i,j)),T->basecoeffs());
507        BIMATELEM(*T,i,j)=n_InitMPZ(n,T->basecoeffs());
508        mpz_clear(n);
509      }
510    }
511  }
512  return res;
513}
514
515intvec* singflint_LLL(intvec*  m, intvec* T)
516{
517  int r=m->rows();
518  int c=m->cols();
519  intvec* res = new intvec(r,c,(int)0);
520  fmpz_mat_t M,Transf;
521  fmpz_mat_init(M, r, c);
522  if(T != NULL)
523    fmpz_mat_init(Transf, r, r);
524  fmpz_t dummy;
525  int i,j;
526  for(i=r;i>0;i--)
527  {
528    for(j=c;j>0;j--)
529    {
530      convSingIFlintI(dummy,IMATELEM(*m,i,j));
531      fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy);
532      fmpz_clear(dummy);
533    }
534  }
535  if(T != NULL)
536  {
537    for(i=T->rows();i>0;i--)
538    {
539      for(j=T->rows();j>0;j--)
540      {
541        convSingIFlintI(dummy,IMATELEM(*T,i,j));
542        fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy);
543        fmpz_clear(dummy);
544      }
545    }
546  }
547  fmpz_lll_t fl;
548  fmpz_lll_context_init_default(fl);
549  if(T != NULL)
550    fmpz_lll(M, Transf, fl);
551  else
552    fmpz_lll(M, NULL, fl);
553  for(i=r;i>0;i--)
554  {
555    for(j=c;j>0;j--)
556    {
557      IMATELEM(*res,i,j)=convFlintISingI(fmpz_mat_entry(M, i-1, j-1));
558    }
559  }
560  if(T != NULL)
561  {
562    for(i=Transf->r;i>0;i--)
563    {
564      for(j=Transf->r;j>0;j--)
565      {
566        IMATELEM(*T,i,j)=convFlintISingI(fmpz_mat_entry(Transf, i-1, j-1));
567      }
568    }
569  }
570  return res;
571}
572#endif
573#endif
Note: See TracBrowser for help on using the repository browser.