source: git/factory/NTLconvert.cc @ 521be7

spielwiese
Last change on this file since 521be7 was 521be7, checked in by Hans Schönemann <hannes@…>, 18 years ago
*hannes: fixed conversion of imm. ints from NTL git-svn-id: file:///usr/local/Singular/svn/trunk@9080 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 37.3 KB
Line 
1/* $Id: NTLconvert.cc,v 1.15 2006-05-02 08:50:43 Singular Exp $ */
2#include <config.h>
3
4#include "cf_gmp.h"
5
6#include "assert.h"
7
8#include "cf_defs.h"
9#include "canonicalform.h"
10#include "cf_iter.h"
11#include "fac_berlekamp.h"
12#include "fac_cantzass.h"
13#include "fac_univar.h"
14#include "fac_multivar.h"
15#include "fac_sqrfree.h"
16#include "cf_algorithm.h"
17
18#ifdef HAVE_NTL
19#include <string.h>
20#include <NTL/ZZXFactoring.h>
21#include <NTL/ZZ_pXFactoring.h>
22#include <NTL/lzz_pXFactoring.h>
23#include <NTL/GF2XFactoring.h>
24#include <NTL/ZZ_pEXFactoring.h>
25#include <NTL/lzz_pEXFactoring.h>
26#include <NTL/GF2EXFactoring.h>
27#include <NTL/tools.h>
28#include "int_int.h"
29#include <limits.h>
30#include "NTLconvert.h"
31
32#ifdef HAVE_OMALLOC
33#define Alloc(L) omAlloc(L)
34#define Free(A,L) omFreeSize(A,L)
35#elif defined(USE_MEMUTIL)
36#include "memutil.h"
37#define Alloc(L) getBlock(L)
38#define Free(A,L) freeBlock(A,L)
39#else
40#define Alloc(L) malloc(L)
41#define Free(A,L) free(A)
42#endif
43
44#ifdef NTL_CLIENT               // in <NTL/tools.h>: using of name space NTL
45NTL_CLIENT
46#endif
47
48////////////////////////////////////////////////////////////////////////////////
49// NAME: convertFacCF2NTLZZpX                                                 //
50//                                                                            //
51// DESCRIPTION:                                                               //
52// Conversion routine for Factory-type canonicalform into ZZpX of NTL,        //
53// i.e. polynomials over F_p. As a precondition for correct execution,        //
54// the characteristic has to a a prime number.                                //
55//                                                                            //
56// INPUT:  A canonicalform f                                                  //
57// OUTPUT: The converted NTL-polynomial over F_p of type ZZpX                 //
58////////////////////////////////////////////////////////////////////////////////
59
60#if 0
61void out_cf(char *s1,const CanonicalForm &f,char *s2)
62{
63  printf("%s",s1);
64  if (f==0) printf("+0");
65  else if (! f.inCoeffDomain() )
66  {
67    int l = f.level();
68    for ( CFIterator i = f; i.hasTerms(); i++ )
69    {
70      int e=i.exp();
71      printf("+(");out_cf("+(",i.coeff(),")*v(");printf("%d)^%d",l,e);
72    }
73  }
74  else
75  {
76    if ( f.isImm() )
77    {
78      printf("+%d(",f.intval());
79    }
80    else printf("+...(");
81    if (f.inZ()) printf("Z)");
82    else if (f.inQ()) printf("Q)");
83    else if (f.inFF()) printf("FF)");
84    else if (f.inPP()) printf("PP)");
85    else if (f.inGF()) printf("PP)");
86    else if (f.inExtension()) printf("E(%d))",f.level());
87  }
88  printf("%s",s2);
89}
90#endif
91
92ZZ_pX convertFacCF2NTLZZpX(CanonicalForm f)
93{
94  ZZ_pX ntl_poly;
95
96  CFIterator i;
97  i=f;
98
99  int j=0;
100  int NTLcurrentExp=i.exp();
101  int largestExp=i.exp();
102  int k;
103
104  // we now build up the NTL-polynomial
105  ntl_poly.SetMaxLength(largestExp+1);
106
107  for (;i.hasTerms();i++)
108  {
109    for (k=NTLcurrentExp;k>i.exp();k--)
110    {
111      SetCoeff(ntl_poly,k,0);
112    }
113    NTLcurrentExp=i.exp();
114
115    CanonicalForm c=i.coeff();
116    if (!c.isImm()) c.mapinto(); //c%= getCharacteristic();
117    if (!c.isImm())
118    {  //This case will never happen if the characteristic is in fact a prime
119       // number, since all coefficients are represented as immediates
120       #ifndef NOSTREAMIO
121       cout<<"convertFacCF2NTLZZ_pX: coefficient not immediate! : "<<f<<"\n";
122       #else
123       printf("convertFacCF2NTLZZ_pX: coefficient not immediate!, char=%d\n",
124              getCharacteristic());
125       #endif
126       exit(1);
127    }
128    else
129    {
130      SetCoeff(ntl_poly,NTLcurrentExp,c.intval());
131    }
132    NTLcurrentExp--;
133  }
134
135  //Set the remaining coefficients of ntl_poly to zero.
136  // This is necessary, because NTL internally
137  // also stores powers with zero coefficient,
138  // whereas factory stores tuples of degree and coefficient
139  //leaving out tuples if the coefficient equals zero
140  for (k=NTLcurrentExp;k>=0;k--)
141  {
142    SetCoeff(ntl_poly,k,0);
143  }
144
145  //normalize the polynomial and return it
146  ntl_poly.normalize();
147
148  return ntl_poly;
149}
150zz_pX convertFacCF2NTLzzpX(CanonicalForm f)
151{
152  zz_pX ntl_poly;
153
154  CFIterator i;
155  i=f;
156
157  int j=0;
158  int NTLcurrentExp=i.exp();
159  int largestExp=i.exp();
160  int k;
161
162  // we now build up the NTL-polynomial
163  ntl_poly.SetMaxLength(largestExp+1);
164
165  for (;i.hasTerms();i++)
166  {
167    for (k=NTLcurrentExp;k>i.exp();k--)
168    {
169      SetCoeff(ntl_poly,k,0);
170    }
171    NTLcurrentExp=i.exp();
172
173    CanonicalForm c=i.coeff();
174    if (!c.isImm()) c.mapinto(); //c%= getCharacteristic();
175    if (!c.isImm())
176    {  //This case will never happen if the characteristic is in fact a prime
177       // number, since all coefficients are represented as immediates
178       #ifndef NOSTREAMIO
179       cout<<"convertFacCF2NTLzz_pX: coefficient not immediate! : "<<f<<"\n";
180       #else
181       printf("convertFacCF2NTLzz_pX: coefficient not immediate!, char=%d\n",
182              getCharacteristic());
183       #endif
184       exit(1);
185    }
186    else
187    {
188      SetCoeff(ntl_poly,NTLcurrentExp,c.intval());
189    }
190    NTLcurrentExp--;
191  }
192
193  //Set the remaining coefficients of ntl_poly to zero.
194  // This is necessary, because NTL internally
195  // also stores powers with zero coefficient,
196  // whereas factory stores tuples of degree and coefficient
197  //leaving out tuples if the coefficient equals zero
198  for (k=NTLcurrentExp;k>=0;k--)
199  {
200    SetCoeff(ntl_poly,k,0);
201  }
202
203  //normalize the polynomial and return it
204  ntl_poly.normalize();
205
206  return ntl_poly;
207}
208
209////////////////////////////////////////////////////////////////////////////////
210// NAME: convertFacCF2NTLGF2X                                                 //
211//                                                                            //
212// DESCRIPTION:                                                               //
213// Conversion routine for Factory-type canonicalform into GF2X of NTL,        //
214// i.e. polynomials over F_2. As precondition for correct execution,          //
215// the characteristic must equal two.                                         //
216// This is a special case of the more general conversion routine for          //
217// canonicalform to ZZpX. It is included because NTL provides additional      //
218// support and faster algorithms over F_2, moreover the conversion code       //
219// can be optimized, because certain steps are either completely obsolent     //
220// (like normalizing the polynomial) or they can be made significantly        //
221// faster (like building up the NTL-polynomial).                              //
222//                                                                            //
223// INPUT:  A canonicalform f                                                  //
224// OUTPUT: The converted NTL-polynomial over F_2 of type GF2X                 //
225////////////////////////////////////////////////////////////////////////////////
226
227GF2X convertFacCF2NTLGF2X(CanonicalForm f)
228{
229  //printf("convertFacCF2NTLGF2X\n");
230  GF2X ntl_poly;
231
232  CFIterator i;
233  i=f;
234
235  int j=0;
236  int NTLcurrentExp=i.exp();
237  int largestExp=i.exp();
238  int k;
239
240  //building the NTL-polynomial
241  ntl_poly.SetMaxLength(largestExp+1);
242
243  for (;i.hasTerms();i++)
244  {
245
246    for (k=NTLcurrentExp;k>i.exp();k--)
247    {
248      SetCoeff(ntl_poly,k,0);
249    }
250    NTLcurrentExp=i.exp();
251
252    if (!i.coeff().isImm()) i.coeff()=i.coeff().mapinto();
253    if (!i.coeff().isImm())
254    {
255      #ifndef NOSTREAMIO
256      cout<<"convertFacCF2NTLGF2X: coefficient not immidiate! : " << f << "\n";
257      #else
258      printf("convertFacCF2NTLGF2X: coefficient not immidiate!");
259      #endif
260      exit(1);
261    }
262    else
263    {
264      SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval());
265    }
266    NTLcurrentExp--;
267  }
268  for (k=NTLcurrentExp;k>=0;k--)
269  {
270    SetCoeff(ntl_poly,k,0);
271  }
272  //normalization is not necessary of F_2
273
274  return ntl_poly;
275}
276
277
278////////////////////////////////////////////////////////////////////////////////
279// NAME: convertNTLZZpX2CF                                                    //
280//                                                                            //
281// DESCRIPTION:                                                               //
282// Conversion routine for NTL-Type ZZpX to Factory-Type canonicalform.        //
283// Additionally a variable x is needed as a parameter indicating the          //
284// main variable of the computed canonicalform. To guarantee the correct      //
285// execution of the algorithm, the characteristic has a be an arbitrary       //
286// prime number.                                                              //
287//                                                                            //
288// INPUT:  A canonicalform f, a variable x                                    //
289// OUTPUT: The converted Factory-polynomial of type canonicalform,            //
290//         built by the main variable x                                       //
291////////////////////////////////////////////////////////////////////////////////
292
293CanonicalForm convertNTLZZpX2CF(ZZ_pX poly,Variable x)
294{
295  //printf("convertNTLZZpX2CF\n");
296  CanonicalForm bigone;
297
298
299  if (deg(poly)>0)
300  {
301    // poly is non-constant
302    bigone=0;
303    bigone.mapinto();
304    // Compute the canonicalform coefficient by coefficient,
305    // bigone summarizes the result.
306    for (int j=0;j<deg(poly)+1;j++)
307    {
308      if (coeff(poly,j)!=0)
309      {
310        bigone+=(power(x,j)*CanonicalForm(to_long(rep(coeff(poly,j)))));
311      }
312    }
313  }
314  else
315  {
316    // poly is immediate
317    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
318    bigone.mapinto();
319  }
320  return bigone;
321}
322
323CanonicalForm convertNTLzzpX2CF(zz_pX poly,Variable x)
324{
325  //printf("convertNTLzzpX2CF\n");
326  CanonicalForm bigone;
327
328
329  if (deg(poly)>0)
330  {
331    // poly is non-constant
332    bigone=0;
333    bigone.mapinto();
334    // Compute the canonicalform coefficient by coefficient,
335    // bigone summarizes the result.
336    for (int j=0;j<deg(poly)+1;j++)
337    {
338      if (coeff(poly,j)!=0)
339      {
340        bigone+=(power(x,j)*CanonicalForm(to_long(rep(coeff(poly,j)))));
341      }
342    }
343  }
344  else
345  {
346    // poly is immediate
347    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
348    bigone.mapinto();
349  }
350  return bigone;
351}
352
353CanonicalForm convertNTLZZX2CF(ZZX polynom,Variable x)
354{
355  //printf("convertNTLZZX2CF\n");
356  CanonicalForm bigone;
357
358  // Go through the vector e and build up the CFFList
359  // As usual bigone summarizes the result
360  bigone=0;
361  ZZ coefficient;
362
363  for (int j=0;j<=deg(polynom);j++)
364  {
365    coefficient=coeff(polynom,j);
366    if (!IsZero(coefficient))
367    {
368      bigone += (power(x,j)*convertZZ2CF(coefficient));
369    }
370  }
371  return bigone;
372}
373
374////////////////////////////////////////////////////////////////////////////////
375// NAME: convertNTLGF2X2CF                                                    //
376//                                                                            //
377// DESCRIPTION:                                                               //
378// Conversion routine for NTL-Type GF2X to Factory-Type canonicalform,        //
379// the routine is again an optimized special case of the more general         //
380// conversion to ZZpX. Additionally a variable x is needed as a               //
381// parameter indicating the main variable of the computed canonicalform.      //
382// To guarantee the correct execution of the algorithm the characteristic     //
383// has a be an arbitrary prime number.                                        //
384//                                                                            //
385// INPUT:  A canonicalform f, a variable x                                    //
386// OUTPUT: The converted Factory-polynomial of type canonicalform,            //
387//         built by the main variable x                                       //
388////////////////////////////////////////////////////////////////////////////////
389
390CanonicalForm convertNTLGF2X2CF(GF2X poly,Variable x)
391{
392  //printf("convertNTLGF2X2CF\n");
393  CanonicalForm bigone;
394
395  if (deg(poly)>0)
396  {
397    // poly is non-constant
398    bigone=0;
399    bigone.mapinto();
400    // Compute the canonicalform coefficient by coefficient,
401    // bigone summarizes the result.
402    // In constrast to the more general conversion to ZZpX
403    // the only possible coefficients are zero
404    // and one yielding the following simplified loop
405    for (int j=0;j<deg(poly)+1;j++)
406    {
407      if (coeff(poly,j)!=0) bigone+=power(x,j);
408     // *CanonicalForm(to_long(rep(coeff(poly,j))))) is not necessary any more;
409    }
410  }
411  else
412  {
413    // poly is immediate
414    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
415    bigone.mapinto();
416  }
417
418  return bigone;
419}
420
421////////////////////////////////////////////////////////////////////////////////
422// NAME: convertNTLvec_pair_ZZpX_long2FacCFFList                              //
423//                                                                            //
424// DESCRIPTION:                                                               //
425// Routine for converting a vector of polynomials from ZZpX to                //
426// a CFFList of Factory. This routine will be used after a successful         //
427// factorization of NTL to convert the result back to Factory.                //
428//                                                                            //
429// Additionally a variable x and the computed multiplicity, as a type ZZp     //
430// of NTL, is needed as parameters indicating the main variable of the        //
431// computed canonicalform and the multiplicity of the original polynomial.    //
432// To guarantee the correct execution of the algorithm the characteristic     //
433// has a be an arbitrary prime number.                                        //
434//                                                                            //
435// INPUT:  A vector of polynomials over ZZp of type vec_pair_ZZ_pX_long and   //
436//         a variable x and a multiplicity of type ZZp                        //
437// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
438//         have x as their main variable                                      //
439////////////////////////////////////////////////////////////////////////////////
440
441CFFList convertNTLvec_pair_ZZpX_long2FacCFFList
442                                  (vec_pair_ZZ_pX_long e,ZZ_p multi,Variable x)
443{
444  //printf("convertNTLvec_pair_ZZpX_long2FacCFFList\n");
445  CFFList rueckgabe;
446  ZZ_pX polynom;
447  long exponent;
448  CanonicalForm bigone;
449
450  // Maybe, e may additionally be sorted with respect to increasing degree of x
451  // but this is not
452  //important for the factorization, but nevertheless would take computing time,
453  // so it is omitted
454
455
456  // Go through the vector e and compute the CFFList
457  // again bigone summarizes the result
458  for (int i=e.length()-1;i>=0;i--)
459  {
460    rueckgabe.append(CFFactor(convertNTLZZpX2CF(e[i].a,x),e[i].b));
461  }
462  // the multiplicity at pos 1
463  if (!IsOne(multi))
464    rueckgabe.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
465  return rueckgabe;
466}
467CFFList convertNTLvec_pair_zzpX_long2FacCFFList
468                                  (vec_pair_zz_pX_long e,zz_p multi,Variable x)
469{
470  //printf("convertNTLvec_pair_zzpX_long2FacCFFList\n");
471  CFFList rueckgabe;
472  zz_pX polynom;
473  long exponent;
474  CanonicalForm bigone;
475
476  // Maybe, e may additionally be sorted with respect to increasing degree of x
477  // but this is not
478  //important for the factorization, but nevertheless would take computing time,
479  // so it is omitted
480
481
482  // Go through the vector e and compute the CFFList
483  // again bigone summarizes the result
484  for (int i=e.length()-1;i>=0;i--)
485  {
486    rueckgabe.append(CFFactor(convertNTLzzpX2CF(e[i].a,x),e[i].b));
487  }
488  // the multiplicity at pos 1
489  if (!IsOne(multi))
490    rueckgabe.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
491  return rueckgabe;
492}
493
494////////////////////////////////////////////////////////////////////////////////
495// NAME: convertNTLvec_pair_GF2X_long2FacCFFList                              //
496//                                                                            //
497// DESCRIPTION:                                                               //
498// Routine for converting a vector of polynomials of type GF2X from           //
499// NTL to a list CFFList of Factory. This routine will be used after a        //
500// successful factorization of NTL to convert the result back to Factory.     //
501// As usual this is simply a special case of the more general conversion      //
502// routine but again speeded up by leaving out unnecessary steps.             //
503// Additionally a variable x and the computed multiplicity, as type           //
504// GF2 of NTL, are needed as parameters indicating the main variable of the   //
505// computed canonicalform and the multiplicity of the original polynomial.    //
506// To guarantee the correct execution of the algorithm the characteristic     //
507// has a be an arbitrary prime number.                                        //
508//                                                                            //
509// INPUT:  A vector of polynomials over GF2 of type vec_pair_GF2X_long and    //
510//         a variable x and a multiplicity of type GF2                        //
511// OUTPUT: The converted list of polynomials of type CFFList, all             //
512//         polynomials have x as their main variable                          //
513////////////////////////////////////////////////////////////////////////////////
514
515CFFList convertNTLvec_pair_GF2X_long2FacCFFList
516                               (vec_pair_GF2X_long e,GF2 multi,Variable x)
517{
518  //printf("convertNTLvec_pair_GF2X_long2FacCFFList\n");
519  CFFList rueckgabe;
520  GF2X polynom;
521  long exponent;
522  CanonicalForm bigone;
523
524  // Maybe, e may additionally be sorted with respect to increasing degree of x
525  // but this is not
526  //important for the factorization, but nevertheless would take computing time
527  // so it is omitted.
528
529  //We do not have to worry about the multiplicity in GF2 since it equals one.
530
531  // Go through the vector e and compute the CFFList
532  // bigone summarizes the result again
533  for (int i=e.length()-1;i>=0;i--)
534  {
535    bigone=0;
536
537    polynom=e[i].a;
538    exponent=e[i].b;
539    for (int j=0;j<deg(polynom)+1;j++)
540    {
541      if (coeff(polynom,j)!=0)
542        bigone += (power(x,j)*CanonicalForm(to_long(rep(coeff(polynom,j)))));
543    }
544
545    //append the converted polynomial to the CFFList
546    rueckgabe.append(CFFactor(bigone,exponent));
547  }
548  return rueckgabe;
549}
550
551////////////////////////////////////////////////////////////////////////////////
552// NAME: convertZZ2CF                                                         //
553//                                                                            //
554// DESCRIPTION:                                                               //
555// Routine for conversion of integers represented in NTL as Type ZZ to        //
556// integers in Factory represented as canonicalform.                          //
557// To guarantee the correct execution of the algorithm the characteristic     //
558// has to equal zero.                                                         //
559//                                                                            //
560// INPUT:  The value coefficient of type ZZ that has to be converted          //
561// OUTPUT: The converted Factory-integer of type canonicalform                //
562////////////////////////////////////////////////////////////////////////////////
563
564static char *cf_stringtemp=NULL;
565static char *cf_stringtemp2=NULL;
566static int cf_stringtemp_l=0;
567CanonicalForm convertZZ2CF(ZZ coefficient)
568{
569  long coeff_long;
570  //CanonicalForm tmp=0;
571  if (cf_stringtemp_l==0)
572  {
573    cf_stringtemp=(char *)Alloc(1023);
574    cf_stringtemp2=(char *)Alloc(1023);
575    cf_stringtemp[0]='\0';
576    cf_stringtemp2[0]='\0';
577    cf_stringtemp_l=1023;
578  }
579  char dummy[2];
580  int minusremainder=0;
581  char numbers[]="0123456789abcdef";
582
583  coeff_long=to_long(coefficient);
584
585  //Test whether coefficient can be represented as an immediate integer in Factory
586  if ( (NumBits(coefficient)<NTL_ZZ_NBITS)
587  && (coeff_long>MINIMMEDIATE)
588  && (coeff_long<MAXIMMEDIATE))
589  {
590    // coefficient is immediate --> return the coefficient as canonicalform
591    return CanonicalForm(coeff_long);
592  }
593  else
594  {
595    // coefficient is not immediate (gmp-number)
596
597    // convert coefficient to char* (input for gmp)
598    dummy[1]='\0';
599
600    if (coefficient<0)
601    {
602      // negate coefficient, but store the sign in minusremainder
603      minusremainder=1;
604      coefficient=-coefficient;
605    }
606
607    int l=0;
608    while (coefficient>15)
609    {
610      ZZ quotient,remaind;
611      ZZ ten;ten=16;
612      DivRem(quotient,remaind,coefficient,ten);
613      dummy[0]=numbers[to_long(remaind)];
614      //tmp*=10; tmp+=to_long(remaind);
615
616      l++;
617      if (l>=cf_stringtemp_l-2)
618      {
619        Free(cf_stringtemp2,cf_stringtemp_l);
620        char *p=(char *)Alloc(cf_stringtemp_l*2);
621        memcpy(p,cf_stringtemp,cf_stringtemp_l);
622        Free(cf_stringtemp,cf_stringtemp_l);
623        cf_stringtemp_l*=2;
624        cf_stringtemp=p;
625        cf_stringtemp2=(char *)Alloc(cf_stringtemp_l);
626      }
627      cf_stringtemp[l-1]=dummy[0];
628      cf_stringtemp[l]='\0';
629      //strcat(stringtemp,dummy);
630
631      coefficient=quotient;
632    }
633    //built up the string in dummy[0]
634    dummy[0]=numbers[to_long(coefficient)];
635    strcat(cf_stringtemp,dummy);
636    //tmp*=10; tmp+=to_long(coefficient);
637
638    if (minusremainder==1)
639    {
640      //Check whether coefficient has been negative at the start of the procedure
641      cf_stringtemp2[0]='-';
642      //tmp*=(-1);
643    }
644
645    //reverse the list to obtain the correct string
646    int len=strlen(cf_stringtemp);
647    for (int i=len-1;i>=0;i--)
648    {
649      cf_stringtemp2[len-i-1+minusremainder]=cf_stringtemp[i];
650    }
651    cf_stringtemp2[len+minusremainder]='\0';
652  }
653
654  //convert the string to canonicalform using the char*-Constructor
655  return CanonicalForm(cf_stringtemp2,16);
656  //return tmp;
657}
658
659////////////////////////////////////////////////////////////////////////////////
660// NAME: convertFacCF2NTLZZX                                                  //
661//                                                                            //
662// DESCRIPTION:                                                               //
663// Routine for conversion of canonicalforms in Factory to polynomials         //
664// of type ZZX of NTL. To guarantee the correct execution of the              //
665// algorithm the characteristic has to equal zero.                            //
666//                                                                            //
667// INPUT:  The canonicalform that has to be converted                         //
668// OUTPUT: The converted NTL-polynom of type ZZX                              //
669////////////////////////////////////////////////////////////////////////////////
670
671ZZX convertFacCF2NTLZZX(CanonicalForm f)
672{
673    ZZX ntl_poly;
674
675    CFIterator i;
676    i=f;
677
678    int j=0;
679    int NTLcurrentExp=i.exp();
680    int largestExp=i.exp();
681    int k;
682
683    //set the length of the NTL-polynomial
684    ntl_poly.SetMaxLength(largestExp+1);
685
686    //Go through the coefficients of the canonicalform and build up the NTL-polynomial
687    for (;i.hasTerms();i++)
688    {
689      for (k=NTLcurrentExp;k>i.exp();k--)
690      {
691        SetCoeff(ntl_poly,k,0);
692      }
693      NTLcurrentExp=i.exp();
694
695      if (!i.coeff().isImm())
696      {
697        //Coefficient is a gmp-number
698        mpz_t gmp_val;
699        ZZ temp;
700        char* stringtemp;
701
702        gmp_val[0]=getmpi(i.coeff().getval());
703        int l=mpz_sizeinbase(gmp_val,10)+2;
704        stringtemp=(char*)Alloc(l);
705        stringtemp=mpz_get_str(stringtemp,10,gmp_val);
706        conv(temp,stringtemp);
707        Free(stringtemp,l);
708
709        //set the computed coefficient
710        SetCoeff(ntl_poly,NTLcurrentExp,temp);
711      }
712      else
713      {
714        //Coefficient is immediate --> use its value
715        SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval());
716      }
717
718      NTLcurrentExp--;
719    }
720    for (k=NTLcurrentExp;k>=0;k--)
721    {
722      SetCoeff(ntl_poly,k,0);
723    }
724
725    //normalize the polynomial
726    ntl_poly.normalize();
727
728    return ntl_poly;
729}
730
731////////////////////////////////////////////////////////////////////////////////
732// NAME: convertNTLvec_pair_ZZX_long2FacCFFList                               //
733//                                                                            //
734// DESCRIPTION:                                                               //
735// Routine for converting a vector of polynomials from ZZ to a list           //
736// CFFList of Factory. This routine will be used after a successful           //
737// factorization of NTL to convert the result back to Factory.                //
738// Additionally a variable x and the computed multiplicity, as a type         //
739// ZZ of NTL, is needed as parameters indicating the main variable of the     //
740// computed canonicalform and the multiplicity of the original polynomial.    //
741// To guarantee the correct execution of the algorithm the characteristic     //
742// has to equal zero.                                                         //
743//                                                                            //
744// INPUT:  A vector of polynomials over ZZ of type vec_pair_ZZX_long and      //
745//         a variable x and a multiplicity of type ZZ                         //
746// OUTPUT: The converted list of polynomials of type CFFList, all             //
747//         have x as their main variable                                      //
748////////////////////////////////////////////////////////////////////////////////
749
750CFFList convertNTLvec_pair_ZZX_long2FacCFFList(vec_pair_ZZX_long e,ZZ multi,Variable x)
751{
752  CFFList rueckgabe;
753  ZZX polynom;
754  long exponent;
755  CanonicalForm bigone;
756
757  // Go through the vector e and build up the CFFList
758  // As usual bigone summarizes the result
759  for (int i=e.length()-1;i>=0;i--)
760  {
761    ZZ coefficient;
762    polynom=e[i].a;
763    exponent=e[i].b;
764    bigone=convertNTLZZX2CF(polynom,x);
765    //append the converted polynomial to the list
766    rueckgabe.append(CFFactor(bigone,exponent));
767  }
768  // the multiplicity at pos 1
769  //if (!IsOne(multi))
770    rueckgabe.insert(CFFactor(convertZZ2CF(multi),1));
771
772  //return the converted list
773  return rueckgabe;
774}
775
776
777////////////////////////////////////////////////////////////////////////////////
778// NAME: convertNTLZZpX2CF                                                    //
779//                                                                            //
780// DESCRIPTION:                                                               //
781// Routine for conversion of elements of arbitrary extensions of ZZp,         //
782// having type ZZpE, of NTL to their corresponding values of type             //
783// canonicalform in Factory.                                                  //
784// To guarantee the correct execution of the algorithm the characteristic     //
785// has to be an arbitrary prime number and Factory has to compute in an       //
786// extension of F_p.                                                          //
787//                                                                            //
788// INPUT:  The coefficient of type ZZpE and the variable x indicating the main//
789//         variable of the computed canonicalform                             //
790// OUTPUT: The converted value of coefficient as type canonicalform           //
791////////////////////////////////////////////////////////////////////////////////
792
793CanonicalForm convertNTLZZpE2CF(ZZ_pE coefficient,Variable x)
794{
795  return convertNTLZZpX2CF(rep(coefficient),x);
796}
797CanonicalForm convertNTLzzpE2CF(zz_pE coefficient,Variable x)
798{
799  return convertNTLzzpX2CF(rep(coefficient),x);
800}
801
802////////////////////////////////////////////////////////////////////////////////
803// NAME: convertNTLvec_pair_ZZpEX_long2FacCFFList                             //
804//                                                                            //
805// DESCRIPTION:                                                               //
806// Routine for converting a vector of polynomials from ZZpEX to a CFFList     //
807// of Factory. This routine will be used after a successful factorization     //
808// of NTL to convert the result back to Factory.                              //
809// Additionally a variable x and the computed multiplicity, as a type         //
810// ZZpE of NTL, is needed as parameters indicating the main variable of the   //
811// computed canonicalform and the multiplicity of the original polynomial.    //
812// To guarantee the correct execution of the algorithm the characteristic     //
813// has a be an arbitrary prime number p and computations have to be done      //
814// in an extention of F_p.                                                    //
815//                                                                            //
816// INPUT:  A vector of polynomials over ZZpE of type vec_pair_ZZ_pEX_long and //
817//         a variable x and a multiplicity of type ZZpE                       //
818// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
819//         have x as their main variable                                      //
820////////////////////////////////////////////////////////////////////////////////
821
822CFFList convertNTLvec_pair_ZZpEX_long2FacCFFList(vec_pair_ZZ_pEX_long e,ZZ_pE multi,Variable x,Variable alpha)
823{
824  CFFList rueckgabe;
825  ZZ_pEX polynom;
826  long exponent;
827  CanonicalForm bigone;
828
829  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
830  //important for the factorization, but nevertheless would take computing time, so it is omitted
831
832  // Go through the vector e and build up the CFFList
833  // As usual bigone summarizes the result during every loop
834  for (int i=e.length()-1;i>=0;i--)
835  {
836    bigone=0;
837
838    polynom=e[i].a;
839    exponent=e[i].b;
840
841    for (int j=0;j<deg(polynom)+1;j++)
842    {
843      if (IsOne(coeff(polynom,j)))
844      {
845        bigone+=power(x,j);
846      }
847      else
848      {
849        CanonicalForm coefficient=convertNTLZZpE2CF(coeff(polynom,j),alpha);
850        if (coeff(polynom,j)!=0)
851        {
852          bigone += (power(x,j)*coefficient);
853        }
854      }
855    }
856    //append the computed polynomials together with its exponent to the CFFList
857    rueckgabe.append(CFFactor(bigone,exponent));
858  }
859  // Start by appending the multiplicity
860  if (!IsOne(multi))
861    rueckgabe.insert(CFFactor(convertNTLZZpE2CF(multi,alpha),1));
862
863  //return the computed CFFList
864  return rueckgabe;
865}
866CFFList convertNTLvec_pair_zzpEX_long2FacCFFList(vec_pair_zz_pEX_long e,zz_pE multi,Variable x,Variable alpha)
867{
868  CFFList rueckgabe;
869  zz_pEX polynom;
870  long exponent;
871  CanonicalForm bigone;
872
873  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
874  //important for the factorization, but nevertheless would take computing time, so it is omitted
875
876  // Go through the vector e and build up the CFFList
877  // As usual bigone summarizes the result during every loop
878  for (int i=e.length()-1;i>=0;i--)
879  {
880    bigone=0;
881
882    polynom=e[i].a;
883    exponent=e[i].b;
884
885    for (int j=0;j<deg(polynom)+1;j++)
886    {
887      if (IsOne(coeff(polynom,j)))
888      {
889        bigone+=power(x,j);
890      }
891      else
892      {
893        CanonicalForm coefficient=convertNTLzzpE2CF(coeff(polynom,j),alpha);
894        if (coeff(polynom,j)!=0)
895        {
896          bigone += (power(x,j)*coefficient);
897        }
898      }
899    }
900    //append the computed polynomials together with its exponent to the CFFList
901    rueckgabe.append(CFFactor(bigone,exponent));
902  }
903  // Start by appending the multiplicity
904  if (!IsOne(multi))
905    rueckgabe.insert(CFFactor(convertNTLzzpE2CF(multi,alpha),1));
906
907  //return the computed CFFList
908  return rueckgabe;
909}
910
911////////////////////////////////////////////////////////////////////////////////
912// NAME: convertNTLGF2E2CF                                                    //
913//                                                                            //
914// DESCRIPTION:                                                               //
915// Routine for conversion of elements of extensions of GF2, having type       //
916// GF2E, of NTL to their corresponding values of type canonicalform in        //
917// Factory.                                                                   //
918// To guarantee the correct execution of the algorithm, the characteristic    //
919// must equal two and Factory has to compute in an extension of F_2.          //
920// As usual this is an optimized special case of the more general conversion  //
921// routine from ZZpE to Factory.                                              //
922//                                                                            //
923// INPUT:  The coefficient of type GF2E and the variable x indicating the     //
924//         main variable of the computed canonicalform                        //
925// OUTPUT: The converted value of coefficient as type canonicalform           //
926////////////////////////////////////////////////////////////////////////////////
927
928CanonicalForm convertNTLGF2E2CF(GF2E coefficient,Variable x)
929{
930  return convertNTLGF2X2CF(rep(coefficient),x);
931}
932
933////////////////////////////////////////////////////////////////////////////////
934// NAME: convertNTLvec_pair_GF2EX_long2FacCFFList                             //
935//                                                                            //
936// DESCRIPTION:                                                               //
937// Routine for converting a vector of polynomials from GF2EX to a CFFList     //
938// of Factory. This routine will be used after a successful factorization     //
939// of NTL to convert the result back to Factory.                              //
940// This is a special, but optimized case of the more general conversion       //
941// from ZZpE to canonicalform.                                                //
942// Additionally a variable x and the computed multiplicity, as a type GF2E    //
943// of NTL, is needed as parameters indicating the main variable of the        //
944// computed canonicalform and the multiplicity of the original polynomial.    //
945// To guarantee the correct execution of the algorithm the characteristic     //
946// has to equal two and computations have to be done in an extention of F_2.  //
947//                                                                            //
948// INPUT:  A vector of polynomials over GF2E of type vec_pair_GF2EX_long and  //
949//         a variable x and a multiplicity of type GF2E                       //
950// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
951//         have x as their main variable                                      //
952////////////////////////////////////////////////////////////////////////////////
953
954CFFList convertNTLvec_pair_GF2EX_long2FacCFFList(vec_pair_GF2EX_long e,GF2E multi,Variable x,Variable alpha)
955{
956  CFFList rueckgabe;
957  GF2EX polynom;
958  long exponent;
959  CanonicalForm bigone;
960
961  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
962  //important for the factorization, but nevertheless would take computing time, so it is omitted
963
964  // multiplicity is always one, so we do not have to worry about that
965
966  // Go through the vector e and build up the CFFList
967  // As usual bigone summarizes the result during every loop
968  for (int i=e.length()-1;i>=0;i--)
969  {
970    bigone=0;
971
972    polynom=e[i].a;
973    exponent=e[i].b;
974
975    for (int j=0;j<deg(polynom)+1;j++)
976    {
977      if (IsOne(coeff(polynom,j)))
978      {
979        bigone+=power(x,j);
980      }
981      else
982      {
983        CanonicalForm coefficient=convertNTLGF2E2CF(coeff(polynom,j),alpha);
984        if (coeff(polynom,j)!=0)
985        {
986          bigone += (power(x,j)*coefficient);
987        }
988      }
989    }
990
991    // append the computed polynomial together with its multiplicity
992    rueckgabe.append(CFFactor(bigone,exponent));
993
994  }
995  // return the computed CFFList
996  return rueckgabe;
997}
998
999////////////////////////////////////////////////////
1000// CanonicalForm in Z_2(a)[X] to NTL GF2EX        //
1001////////////////////////////////////////////////////
1002GF2EX convertFacCF2NTLGF2EX(CanonicalForm f,GF2X mipo)
1003{
1004  GF2E::init(mipo);
1005  GF2EX result;
1006  CFIterator i;
1007  i=f;
1008
1009  int j=0;
1010  int NTLcurrentExp=i.exp();
1011  int largestExp=i.exp();
1012  int k;
1013
1014  result.SetMaxLength(largestExp+1);
1015  for(;i.hasTerms();i++)
1016  {
1017    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1018    NTLcurrentExp=i.exp();
1019    CanonicalForm c=i.coeff();
1020    GF2X cc=convertFacCF2NTLGF2X(c);
1021    //ZZ_pE ccc;
1022    //conv(ccc,cc);
1023    SetCoeff(result,NTLcurrentExp,to_GF2E(cc));
1024    NTLcurrentExp--;
1025  }
1026  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1027  result.normalize();
1028  return result;
1029}
1030////////////////////////////////////////////////////
1031// CanonicalForm in Z_p(a)[X] to NTL ZZ_pEX       //
1032////////////////////////////////////////////////////
1033ZZ_pEX convertFacCF2NTLZZ_pEX(CanonicalForm f, ZZ_pX mipo)
1034{
1035  ZZ_pE::init(mipo);
1036  ZZ_pEX result;
1037  CFIterator i;
1038  i=f;
1039
1040  int j=0;
1041  int NTLcurrentExp=i.exp();
1042  int largestExp=i.exp();
1043  int k;
1044
1045  result.SetMaxLength(largestExp+1);
1046  for(;i.hasTerms();i++)
1047  {
1048    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1049    NTLcurrentExp=i.exp();
1050    CanonicalForm c=i.coeff();
1051    ZZ_pX cc=convertFacCF2NTLZZpX(c);
1052    //ZZ_pE ccc;
1053    //conv(ccc,cc);
1054    SetCoeff(result,NTLcurrentExp,to_ZZ_pE(cc));
1055    NTLcurrentExp--;
1056  }
1057  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1058  result.normalize();
1059  return result;
1060}
1061zz_pEX convertFacCF2NTLzz_pEX(CanonicalForm f, zz_pX mipo)
1062{
1063  zz_pE::init(mipo);
1064  zz_pEX result;
1065  CFIterator i;
1066  i=f;
1067
1068  int j=0;
1069  int NTLcurrentExp=i.exp();
1070  int largestExp=i.exp();
1071  int k;
1072
1073  result.SetMaxLength(largestExp+1);
1074  for(;i.hasTerms();i++)
1075  {
1076    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1077    NTLcurrentExp=i.exp();
1078    CanonicalForm c=i.coeff();
1079    zz_pX cc=convertFacCF2NTLzzpX(c);
1080    //ZZ_pE ccc;
1081    //conv(ccc,cc);
1082    SetCoeff(result,NTLcurrentExp,to_zz_pE(cc));
1083    NTLcurrentExp--;
1084  }
1085  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1086  result.normalize();
1087  return result;
1088}
1089#endif
Note: See TracBrowser for help on using the repository browser.