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

fieker-DuValspielwiese
Last change on this file since 9a4aed was 2a869f, checked in by Hans Schoenemann <hannes@…>, 23 months ago
fix: cov to/from flint for bigint
  • Property mode set to 100644
File size: 14.5 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 (getCoeffType(cf)==n_Q) /* QQ, bigint */
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 (getCoeffType(cf)==n_Q) /* QQ, bigint */ 
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  if (p==NULL)
233  {
234    fmpq_poly_init(res);
235    return;
236  }
237  int d=p_GetExp(p,1,r);
238  fmpq_poly_init2(res,d+1);
239  _fmpq_poly_set_length (res, d + 1);
240  while(p!=NULL)
241  {
242    number n=pGetCoeff(p);
243    fmpq_t c;
244    convSingNFlintN(c,n,r->cf);
245    fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c);
246    fmpq_clear(c);
247    pIter(p);
248  }
249}
250
251void convSingImPFlintP(fmpq_poly_t res, poly p, const ring r)
252{
253  int d=p_GetExp(p,1,r);
254  fmpq_poly_init2(res,d+1);
255  _fmpq_poly_set_length (res, d + 1);
256  while(p!=NULL)
257  {
258    number n=n_ImPart(pGetCoeff(p),r->cf);
259    fmpq_t c;
260    convSingNFlintN(c,n,r->cf);
261    fmpq_poly_set_coeff_fmpq(res,p_GetExp(p,1,r),c);
262    fmpq_clear(c);
263    n_Delete(&n,r->cf);
264    pIter(p);
265  }
266}
267
268poly convFlintPSingP(fmpq_poly_t f, const ring r)
269{
270  if (fmpq_poly_is_zero(f)) return NULL;
271  int d=fmpq_poly_length(f);
272  poly p=NULL;
273  fmpq_t c;
274  fmpq_init(c);
275  for(int i=0; i<=d; i++)
276  {
277    fmpq_poly_get_coeff_fmpq(c,f,i);
278    number n=convFlintNSingN(c,r->cf);
279    if(!n_IsZero(n,r->cf))
280    {
281      poly pp=p_Init(r);
282      pSetCoeff0(pp,n);
283      p_SetExp(pp,1,i,r);
284      p_Setm(pp,r);
285      p=p_Add_q(p,pp,r);
286    }
287  }
288  fmpq_clear(c);
289  p_Test(p,r);
290  return p;
291}
292
293void convSingPFlintnmod_poly_t(nmod_poly_t result, const poly p, const ring r)
294{
295  // assume univariate, r->cf=Z/p
296  nmod_poly_init2 (result,rChar(r),p_Deg(p,r));
297  poly h=p;
298  while(h!=NULL)
299  {
300    if (h==NULL)
301      nmod_poly_set_coeff_ui(result,0,0);
302    else
303      nmod_poly_set_coeff_ui(result,p_GetExp(h,1,r),n_Int(pGetCoeff(h),r->cf)+rChar(r));
304    pIter(h);
305  }
306}
307
308void convSingMFlintFq_nmod_mat(matrix m, fq_nmod_mat_t M, const fq_nmod_ctx_t fq_con, const ring r)
309{
310  fq_nmod_mat_init (M, (long)MATROWS(m), (long) MATCOLS(m), fq_con);
311  int i,j;
312  for(i=MATROWS(m);i>0;i--)
313  {
314    for(j=MATCOLS(m);j>0;j--)
315    {
316      convSingPFlintnmod_poly_t (M->rows[i-1]+j-1, MATELEM(m,i,j),r);
317    }
318  }
319}
320
321poly convFlintFq_nmodSingP(const fq_nmod_t Fp, const fq_nmod_ctx_t ctx, const ring r)
322{
323  poly p=NULL;
324  poly h;
325  for (int i= 0; i < nmod_poly_length (Fp); i++)
326  {
327    ulong coeff= nmod_poly_get_coeff_ui (Fp, i);
328    if (coeff != 0)
329    h=p_NSet(n_Init(coeff,r->cf),r);
330    if (h!=NULL)
331    {
332      p_SetExp(h,1,i,r);
333      p_Setm(h,r);
334      p=p_Add_q(p,h,r);
335    }
336  }
337  return p;
338}
339
340matrix convFlintFq_nmod_matSingM(fq_nmod_mat_t m, const fq_nmod_ctx_t fq_con, const ring r)
341{
342  matrix M=mpNew(fq_nmod_mat_nrows (m, fq_con),fq_nmod_mat_ncols (m, fq_con));
343   int i,j;
344  for(i=MATROWS(M);i>0;i--)
345  {
346    for(j=MATCOLS(M);j>0;j--)
347    {
348      MATELEM(M,i,j)=convFlintFq_nmodSingP(fq_nmod_mat_entry (m, i-1, j-1),
349                                          fq_con, r);
350    }
351  }
352  return M;
353}
354
355void convSingMFlintNmod_mat(matrix m, nmod_mat_t M, const ring r)
356{
357  nmod_mat_init (M, (long)MATROWS(m), (long) MATCOLS(m), rChar(r));
358  int i,j;
359  for(i=MATROWS(m);i>0;i--)
360  {
361    for(j=MATCOLS(m);j>0;j--)
362    {
363      poly h=MATELEM(m,i,j);
364      if (h!=NULL)
365        nmod_mat_entry(M,i-1,j-1)=(long)pGetCoeff(h);
366    }
367  }
368}
369
370matrix convFlintNmod_matSingM(nmod_mat_t m, const ring r)
371{
372  matrix M=mpNew(nmod_mat_nrows (m),nmod_mat_ncols (m));
373   int i,j;
374  for(i=MATROWS(M);i>0;i--)
375  {
376    for(j=MATCOLS(M);j>0;j--)
377    {
378      MATELEM(M,i,j)=p_ISet(nmod_mat_entry (m, i-1, j-1),r);
379    }
380  }
381  return M;
382}
383
384matrix singflint_rref(matrix  m, const ring R)
385{
386  int r=m->rows();
387  int c=m->cols();
388  int i,j;
389  matrix M=NULL;
390  if (rField_is_Q(R))
391  {
392    fmpq_mat_t FLINTM;
393    fmpq_mat_init(FLINTM,r,c);
394    M=mpNew(r,c);
395    for(i=r;i>0;i--)
396    {
397      for(j=c;j>0;j--)
398      {
399        poly h=MATELEM(m,i,j);
400        if (h!=NULL)
401        {
402          if (p_Totaldegree(h,R)==0)
403            convSingNFlintN(fmpq_mat_entry(FLINTM,i-1,j-1),pGetCoeff(h),R->cf);
404          else
405          {
406            WerrorS("matrix for rref is not constant");
407            return M;
408          }
409        }
410      }
411    }
412    fmpq_mat_rref(FLINTM,FLINTM);
413    for(i=r;i>0;i--)
414    {
415      for(j=c;j>0;j--)
416      {
417        number n=convFlintNSingN(fmpq_mat_entry(FLINTM,i-1,j-1),R->cf);
418        MATELEM(M,i,j)=p_NSet(n,R);
419      }
420    }
421    fmpq_mat_clear(FLINTM);
422  }
423  else if (rField_is_Zp(R))
424  {
425    nmod_mat_t FLINTM;
426    // convert matrix
427    convSingMFlintNmod_mat(m,FLINTM,R);
428    // rank
429    long rk= nmod_mat_rref (FLINTM);
430    M=convFlintNmod_matSingM(FLINTM,R);
431    // clean up
432    nmod_mat_clear(FLINTM);
433  }
434  else
435  {
436    WerrorS("not implemented for these coefficients");
437  }
438  return M;
439}
440
441ideal singflint_rref(ideal  m, const ring R) /*assume smatrix m*/
442{
443  int r=m->rank;
444  int c=m->ncols;
445  int i,j;
446  ideal M=idInit(c,r);
447  if (rField_is_Q(R))
448  {
449    fmpq_mat_t FLINTM;
450    fmpq_mat_init(FLINTM,r,c);
451    for(j=c-1;j>=0;j--)
452    {
453      poly h=m->m[j];
454      while(h!=NULL)
455      {
456        i=p_GetComp(h,R);
457        if (p_Totaldegree(h,R)==0)
458          convSingNFlintN(fmpq_mat_entry(FLINTM,i-1,j),p_GetCoeff(h,R),R->cf);
459        else
460        {
461          WerrorS("smatrix for rref is not constant");
462          return M;
463        }
464        pIter(h);
465      }
466    }
467    fmpq_mat_rref(FLINTM,FLINTM);
468    for(i=r;i>0;i--)
469    {
470      for(j=c-1;j>=0;j--)
471      {
472        number n=convFlintNSingN(fmpq_mat_entry(FLINTM,i-1,j),R->cf);
473        if(!n_IsZero(n,R->cf))
474        {
475          poly p=p_NSet(n,R);
476          p_SetComp(p,i,R);
477          M->m[j]=p_Add_q(M->m[j],p,R);
478        }
479      }
480    }
481    fmpq_mat_clear(FLINTM);
482  }
483  else if (rField_is_Zp(R))
484  {
485    nmod_mat_t FLINTM;
486    nmod_mat_init(FLINTM,r,c,rChar(R));
487    for(j=c-1;j>=0;j--)
488    {
489      poly h=m->m[j];
490      while(h!=NULL)
491      {
492        i=p_GetComp(h,R);
493        if (p_Totaldegree(h,R)==0)
494          nmod_mat_entry(FLINTM,i-1,j)=(long)p_GetCoeff(h,R);
495        else
496        {
497          WerrorS("smatrix for rref is not constant");
498          return M;
499        }
500        pIter(h);
501      }
502    }
503    nmod_mat_rref(FLINTM);
504    for(i=r;i>0;i--)
505    {
506      for(j=c-1;j>=0;j--)
507      {
508        number n=n_Init(nmod_mat_entry(FLINTM,i-1,j),R->cf);
509        if(!n_IsZero(n,R->cf))
510        {
511          poly p=p_NSet(n,R);
512          p_SetComp(p,i,R);
513          M->m[j]=p_Add_q(M->m[j],p,R);
514        }
515      }
516    }
517    nmod_mat_clear(FLINTM);
518  }
519  else
520  {
521    WerrorS("not implemented for these coefficients");
522  }
523  return M;
524}
525
526matrix singflint_kernel(matrix  m, const ring R)
527{
528  matrix M=NULL;
529  if (rField_is_Zp(R))
530  {
531    nmod_mat_t FLINTM;
532    nmod_mat_t FLINTX;
533    nmod_mat_init (FLINTX, (long)MATROWS(m), (long) MATCOLS(m), rChar(R));
534    // convert matrix
535    convSingMFlintNmod_mat(m,FLINTM,R);
536    // rank
537    long rk= nmod_mat_nullspace(FLINTX,FLINTM);
538    nmod_mat_clear(FLINTM);
539    M=convFlintNmod_matSingM(FLINTX,R);
540    // clean up
541    nmod_mat_clear(FLINTX);
542  }
543  else
544  {
545    WerrorS("not implemented for these coefficients");
546  }
547  return M;
548}
549
550ideal singflint_kernel(ideal  m, const ring R) /*assume smatrix m*/
551{
552  int r=m->rank;
553  int c=m->ncols;
554  int i,j;
555  ideal M=idInit(c,r);
556  if (rField_is_Zp(R))
557  {
558    nmod_mat_t FLINTM;
559    nmod_mat_t FLINTX;
560    nmod_mat_init(FLINTM,r,c,rChar(R));
561    nmod_mat_init(FLINTX,r,c,rChar(R));
562    for(j=c-1;j>=0;j--)
563    {
564      poly h=m->m[j];
565      while(h!=NULL)
566      {
567        i=p_GetComp(h,R);
568        if (p_Totaldegree(h,R)==0)
569          nmod_mat_entry(FLINTM,i-1,j)=(long)p_GetCoeff(h,R);
570        else
571        {
572          WerrorS("smatrix for rref is not constant");
573          return M;
574        }
575        pIter(h);
576      }
577    }
578    nmod_mat_nullspace(FLINTX,FLINTM);
579    nmod_mat_clear(FLINTM);
580    for(i=r;i>0;i--)
581    {
582      for(j=c-1;j>=0;j--)
583      {
584        number n=n_Init(nmod_mat_entry(FLINTX,i-1,j),R->cf);
585        if(!n_IsZero(n,R->cf))
586        {
587          poly p=p_NSet(n,R);
588          p_SetComp(p,i,R);
589          M->m[j]=p_Add_q(M->m[j],p,R);
590        }
591      }
592    }
593    nmod_mat_clear(FLINTX);
594  }
595  else
596  {
597    WerrorS("not implemented for these coefficients");
598  }
599  return M;
600}
601
602bigintmat* singflint_LLL(bigintmat*  m, bigintmat* T)
603{
604  int r=m->rows();
605  int c=m->cols();
606  bigintmat* res=new bigintmat(r,c,m->basecoeffs());
607  fmpz_mat_t M, Transf;
608  fmpz_mat_init(M, r, c);
609  if(T != NULL)
610  {
611    fmpz_mat_init(Transf, T->rows(), T->rows());
612  }
613  fmpz_t dummy;
614  mpz_t n;
615  int i,j;
616  for(i=r;i>0;i--)
617  {
618    for(j=c;j>0;j--)
619    {
620      n_MPZ(n, BIMATELEM(*m, i, j),m->basecoeffs());
621      convSingNFlintN(dummy,n);
622      mpz_clear(n);
623      fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy);
624      fmpz_clear(dummy);
625    }
626  }
627  if(T != NULL)
628  {
629    for(i=T->rows();i>0;i--)
630    {
631      for(j=T->rows();j>0;j--)
632      {
633        n_MPZ(n, BIMATELEM(*T, i, j),T->basecoeffs());
634        convSingNFlintN(dummy,n);
635        mpz_clear(n);
636        fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy);
637        fmpz_clear(dummy);
638      }
639    }
640  }
641  fmpz_lll_t fl;
642  fmpz_lll_context_init_default(fl);
643  if(T != NULL)
644    fmpz_lll(M, Transf, fl);
645  else
646    fmpz_lll(M, NULL, fl);
647  for(i=r;i>0;i--)
648  {
649    for(j=c;j>0;j--)
650    {
651      convFlintNSingN(n, fmpz_mat_entry(M, i-1, j-1));
652      n_Delete(&(BIMATELEM(*res,i,j)),res->basecoeffs());
653      BIMATELEM(*res,i,j)=n_InitMPZ(n,res->basecoeffs());
654      mpz_clear(n);
655    }
656  }
657  if(T != NULL)
658  {
659    for(i=T->rows();i>0;i--)
660    {
661      for(j=T->cols();j>0;j--)
662      {
663        convFlintNSingN(n, fmpz_mat_entry(Transf, i-1, j-1));
664        n_Delete(&(BIMATELEM(*T,i,j)),T->basecoeffs());
665        BIMATELEM(*T,i,j)=n_InitMPZ(n,T->basecoeffs());
666        mpz_clear(n);
667      }
668    }
669  }
670  return res;
671}
672
673intvec* singflint_LLL(intvec*  m, intvec* T)
674{
675  int r=m->rows();
676  int c=m->cols();
677  intvec* res = new intvec(r,c,(int)0);
678  fmpz_mat_t M,Transf;
679  fmpz_mat_init(M, r, c);
680  if(T != NULL)
681    fmpz_mat_init(Transf, r, r);
682  fmpz_t dummy;
683  int i,j;
684  for(i=r;i>0;i--)
685  {
686    for(j=c;j>0;j--)
687    {
688      convSingIFlintI(dummy,IMATELEM(*m,i,j));
689      fmpz_set(fmpz_mat_entry(M, i-1, j-1), dummy);
690      fmpz_clear(dummy);
691    }
692  }
693  if(T != NULL)
694  {
695    for(i=T->rows();i>0;i--)
696    {
697      for(j=T->rows();j>0;j--)
698      {
699        convSingIFlintI(dummy,IMATELEM(*T,i,j));
700        fmpz_set(fmpz_mat_entry(Transf, i-1, j-1), dummy);
701        fmpz_clear(dummy);
702      }
703    }
704  }
705  fmpz_lll_t fl;
706  fmpz_lll_context_init_default(fl);
707  if(T != NULL)
708    fmpz_lll(M, Transf, fl);
709  else
710    fmpz_lll(M, NULL, fl);
711  for(i=r;i>0;i--)
712  {
713    for(j=c;j>0;j--)
714    {
715      IMATELEM(*res,i,j)=convFlintISingI(fmpz_mat_entry(M, i-1, j-1));
716    }
717  }
718  if(T != NULL)
719  {
720    for(i=Transf->r;i>0;i--)
721    {
722      for(j=Transf->r;j>0;j--)
723      {
724        IMATELEM(*T,i,j)=convFlintISingI(fmpz_mat_entry(Transf, i-1, j-1));
725      }
726    }
727  }
728  return res;
729}
730#endif
731#endif
Note: See TracBrowser for help on using the repository browser.