source: git/factory/NTLconvert.cc @ 19fc57b

fieker-DuValspielwiese
Last change on this file since 19fc57b was f11d7b, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: gcd via NTL git-svn-id: file:///usr/local/Singular/svn/trunk@6990 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 37.9 KB
Line 
1/* $Id: NTLconvert.cc,v 1.13 2004-01-19 11:26:19 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
421int NTLcmpCF( const CFFactor & f, const CFFactor & g )
422{
423  if (f.exp() > g.exp()) return 1;
424  if (f.exp() < g.exp()) return 0;
425  if (f.factor() > g.factor()) return 1;
426  return 0;
427}
428
429////////////////////////////////////////////////////////////////////////////////
430// NAME: convertNTLvec_pair_ZZpX_long2FacCFFList                              //
431//                                                                            //
432// DESCRIPTION:                                                               //
433// Routine for converting a vector of polynomials from ZZpX to                //
434// a CFFList of Factory. This routine will be used after a successful         //
435// factorization of NTL to convert the result back to Factory.                //
436//                                                                            //
437// Additionally a variable x and the computed multiplicity, as a type ZZp     //
438// of NTL, is needed as parameters indicating the main variable of the        //
439// computed canonicalform and the multiplicity of the original polynomial.    //
440// To guarantee the correct execution of the algorithm the characteristic     //
441// has a be an arbitrary prime number.                                        //
442//                                                                            //
443// INPUT:  A vector of polynomials over ZZp of type vec_pair_ZZ_pX_long and   //
444//         a variable x and a multiplicity of type ZZp                        //
445// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
446//         have x as their main variable                                      //
447////////////////////////////////////////////////////////////////////////////////
448
449CFFList convertNTLvec_pair_ZZpX_long2FacCFFList
450                                  (vec_pair_ZZ_pX_long e,ZZ_p multi,Variable x)
451{
452  //printf("convertNTLvec_pair_ZZpX_long2FacCFFList\n");
453  CFFList rueckgabe;
454  ZZ_pX polynom;
455  long exponent;
456  CanonicalForm bigone;
457
458  // Maybe, e may additionally be sorted with respect to increasing degree of x
459  // but this is not
460  //important for the factorization, but nevertheless would take computing time,
461  // so it is omitted
462
463
464  // Go through the vector e and compute the CFFList
465  // again bigone summarizes the result
466  for (int i=e.length()-1;i>=0;i--)
467  {
468    rueckgabe.append(CFFactor(convertNTLZZpX2CF(e[i].a,x),e[i].b));
469  }
470  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
471  // the multiplicity at pos 1
472  if (!IsOne(multi))
473    rueckgabe.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
474  return rueckgabe;
475}
476CFFList convertNTLvec_pair_zzpX_long2FacCFFList
477                                  (vec_pair_zz_pX_long e,zz_p multi,Variable x)
478{
479  //printf("convertNTLvec_pair_zzpX_long2FacCFFList\n");
480  CFFList rueckgabe;
481  zz_pX polynom;
482  long exponent;
483  CanonicalForm bigone;
484
485  // Maybe, e may additionally be sorted with respect to increasing degree of x
486  // but this is not
487  //important for the factorization, but nevertheless would take computing time,
488  // so it is omitted
489
490
491  // Go through the vector e and compute the CFFList
492  // again bigone summarizes the result
493  for (int i=e.length()-1;i>=0;i--)
494  {
495    rueckgabe.append(CFFactor(convertNTLzzpX2CF(e[i].a,x),e[i].b));
496  }
497  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
498  // the multiplicity at pos 1
499  if (!IsOne(multi))
500    rueckgabe.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
501  return rueckgabe;
502}
503
504////////////////////////////////////////////////////////////////////////////////
505// NAME: convertNTLvec_pair_GF2X_long2FacCFFList                              //
506//                                                                            //
507// DESCRIPTION:                                                               //
508// Routine for converting a vector of polynomials of type GF2X from           //
509// NTL to a list CFFList of Factory. This routine will be used after a        //
510// successful factorization of NTL to convert the result back to Factory.     //
511// As usual this is simply a special case of the more general conversion      //
512// routine but again speeded up by leaving out unnecessary steps.             //
513// Additionally a variable x and the computed multiplicity, as type           //
514// GF2 of NTL, are needed as parameters indicating the main variable of the   //
515// computed canonicalform and the multiplicity of the original polynomial.    //
516// To guarantee the correct execution of the algorithm the characteristic     //
517// has a be an arbitrary prime number.                                        //
518//                                                                            //
519// INPUT:  A vector of polynomials over GF2 of type vec_pair_GF2X_long and    //
520//         a variable x and a multiplicity of type GF2                        //
521// OUTPUT: The converted list of polynomials of type CFFList, all             //
522//         polynomials have x as their main variable                          //
523////////////////////////////////////////////////////////////////////////////////
524
525CFFList convertNTLvec_pair_GF2X_long2FacCFFList
526                               (vec_pair_GF2X_long e,GF2 multi,Variable x)
527{
528  //printf("convertNTLvec_pair_GF2X_long2FacCFFList\n");
529  CFFList rueckgabe;
530  GF2X polynom;
531  long exponent;
532  CanonicalForm bigone;
533
534  // Maybe, e may additionally be sorted with respect to increasing degree of x
535  // but this is not
536  //important for the factorization, but nevertheless would take computing time
537  // so it is omitted.
538
539  //We do not have to worry about the multiplicity in GF2 since it equals one.
540
541  // Go through the vector e and compute the CFFList
542  // bigone summarizes the result again
543  for (int i=e.length()-1;i>=0;i--)
544  {
545    bigone=0;
546
547    polynom=e[i].a;
548    exponent=e[i].b;
549    for (int j=0;j<deg(polynom)+1;j++)
550    {
551      if (coeff(polynom,j)!=0)
552        bigone += (power(x,j)*CanonicalForm(to_long(rep(coeff(polynom,j)))));
553    }
554
555    //append the converted polynomial to the CFFList
556    rueckgabe.append(CFFactor(bigone,exponent));
557  }
558  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
559  return rueckgabe;
560}
561
562////////////////////////////////////////////////////////////////////////////////
563// NAME: convertZZ2CF                                                         //
564//                                                                            //
565// DESCRIPTION:                                                               //
566// Routine for conversion of integers represented in NTL as Type ZZ to        //
567// integers in Factory represented as canonicalform.                          //
568// To guarantee the correct execution of the algorithm the characteristic     //
569// has to equal zero.                                                         //
570//                                                                            //
571// INPUT:  The value coefficient of type ZZ that has to be converted          //
572// OUTPUT: The converted Factory-integer of type canonicalform                //
573////////////////////////////////////////////////////////////////////////////////
574
575static char *cf_stringtemp=NULL;
576static char *cf_stringtemp2=NULL;
577static int cf_stringtemp_l=0;
578CanonicalForm convertZZ2CF(ZZ coefficient)
579{
580  long coeff_long;
581  //CanonicalForm tmp=0;
582  if (cf_stringtemp_l==0)
583  {
584    cf_stringtemp=(char *)Alloc(1023);
585    cf_stringtemp2=(char *)Alloc(1023);
586    cf_stringtemp[0]='\0';
587    cf_stringtemp2[0]='\0';
588    cf_stringtemp_l=1023;
589  }
590  char dummy[2];
591  int minusremainder=0;
592  char numbers[]="0123456789abcdef";
593
594  coeff_long=to_long(coefficient);
595
596  //Test whether coefficient can be represented as an immediate integer in Factory
597  if ( (NumBits(coefficient)<=NTL_ZZ_NBITS)
598  && (coeff_long>MINIMMEDIATE)
599  && (coeff_long<MAXIMMEDIATE))
600  {
601    // coefficient is immediate --> return the coefficient as canonicalform
602    return CanonicalForm(coeff_long);
603  }
604  else
605  {
606    // coefficient is not immediate (gmp-number)
607
608    // convert coefficient to char* (input for gmp)
609    dummy[1]='\0';
610
611    if (coefficient<0)
612    {
613      // negate coefficient, but store the sign in minusremainder
614      minusremainder=1;
615      coefficient=-coefficient;
616    }
617
618    int l=0;
619    while (coefficient>15)
620    {
621      ZZ quotient,remaind;
622      ZZ ten;ten=16;
623      DivRem(quotient,remaind,coefficient,ten);
624      dummy[0]=numbers[to_long(remaind)];
625      //tmp*=10; tmp+=to_long(remaind);
626
627      l++;
628      if (l>=cf_stringtemp_l-2)
629      {
630        Free(cf_stringtemp2,cf_stringtemp_l);
631        char *p=(char *)Alloc(cf_stringtemp_l*2);
632        memcpy(p,cf_stringtemp,cf_stringtemp_l);
633        Free(cf_stringtemp,cf_stringtemp_l);
634        cf_stringtemp_l*=2;
635        cf_stringtemp=p;
636        cf_stringtemp2=(char *)Alloc(cf_stringtemp_l);
637      }
638      cf_stringtemp[l-1]=dummy[0];
639      cf_stringtemp[l]='\0';
640      //strcat(stringtemp,dummy);
641
642      coefficient=quotient;
643    }
644    //built up the string in dummy[0]
645    dummy[0]=numbers[to_long(coefficient)];
646    strcat(cf_stringtemp,dummy);
647    //tmp*=10; tmp+=to_long(coefficient);
648
649    if (minusremainder==1)
650    {
651      //Check whether coefficient has been negative at the start of the procedure
652      cf_stringtemp2[0]='-';
653      //tmp*=(-1);
654    }
655
656    //reverse the list to obtain the correct string
657    int len=strlen(cf_stringtemp);
658    for (int i=len-1;i>=0;i--)
659    {
660      cf_stringtemp2[len-i-1+minusremainder]=cf_stringtemp[i];
661    }
662    cf_stringtemp2[len+minusremainder]='\0';
663  }
664
665  //convert the string to canonicalform using the char*-Constructor
666  return CanonicalForm(cf_stringtemp2,16);
667  //return tmp;
668}
669
670////////////////////////////////////////////////////////////////////////////////
671// NAME: convertFacCF2NTLZZX                                                  //
672//                                                                            //
673// DESCRIPTION:                                                               //
674// Routine for conversion of canonicalforms in Factory to polynomials         //
675// of type ZZX of NTL. To guarantee the correct execution of the              //
676// algorithm the characteristic has to equal zero.                            //
677//                                                                            //
678// INPUT:  The canonicalform that has to be converted                         //
679// OUTPUT: The converted NTL-polynom of type ZZX                              //
680////////////////////////////////////////////////////////////////////////////////
681
682ZZX convertFacCF2NTLZZX(CanonicalForm f)
683{
684    ZZX ntl_poly;
685
686    CFIterator i;
687    i=f;
688
689    int j=0;
690    int NTLcurrentExp=i.exp();
691    int largestExp=i.exp();
692    int k;
693
694    //set the length of the NTL-polynomial
695    ntl_poly.SetMaxLength(largestExp+1);
696
697    //Go through the coefficients of the canonicalform and build up the NTL-polynomial
698    for (;i.hasTerms();i++)
699    {
700      for (k=NTLcurrentExp;k>i.exp();k--)
701      {
702        SetCoeff(ntl_poly,k,0);
703      }
704      NTLcurrentExp=i.exp();
705
706      if (!i.coeff().isImm())
707      {
708        //Coefficient is a gmp-number
709        mpz_t gmp_val;
710        ZZ temp;
711        char* stringtemp;
712
713        gmp_val[0]=getmpi(i.coeff().getval());
714        int l=mpz_sizeinbase(gmp_val,10)+2;
715        stringtemp=(char*)Alloc(l);
716        stringtemp=mpz_get_str(stringtemp,10,gmp_val);
717        conv(temp,stringtemp);
718        Free(stringtemp,l);
719
720        //set the computed coefficient
721        SetCoeff(ntl_poly,NTLcurrentExp,temp);
722      }
723      else
724      {
725        //Coefficient is immediate --> use its value
726        SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval());
727      }
728
729      NTLcurrentExp--;
730    }
731    for (k=NTLcurrentExp;k>=0;k--)
732    {
733      SetCoeff(ntl_poly,k,0);
734    }
735
736    //normalize the polynomial
737    ntl_poly.normalize();
738
739    return ntl_poly;
740}
741
742////////////////////////////////////////////////////////////////////////////////
743// NAME: convertNTLvec_pair_ZZX_long2FacCFFList                               //
744//                                                                            //
745// DESCRIPTION:                                                               //
746// Routine for converting a vector of polynomials from ZZ to a list           //
747// CFFList of Factory. This routine will be used after a successful           //
748// factorization of NTL to convert the result back to Factory.                //
749// Additionally a variable x and the computed multiplicity, as a type         //
750// ZZ of NTL, is needed as parameters indicating the main variable of the     //
751// computed canonicalform and the multiplicity of the original polynomial.    //
752// To guarantee the correct execution of the algorithm the characteristic     //
753// has to equal zero.                                                         //
754//                                                                            //
755// INPUT:  A vector of polynomials over ZZ of type vec_pair_ZZX_long and      //
756//         a variable x and a multiplicity of type ZZ                         //
757// OUTPUT: The converted list of polynomials of type CFFList, all             //
758//         have x as their main variable                                      //
759////////////////////////////////////////////////////////////////////////////////
760
761CFFList convertNTLvec_pair_ZZX_long2FacCFFList(vec_pair_ZZX_long e,ZZ multi,Variable x)
762{
763  CFFList rueckgabe;
764  ZZX polynom;
765  long exponent;
766  CanonicalForm bigone;
767
768  // Go through the vector e and build up the CFFList
769  // As usual bigone summarizes the result
770  for (int i=e.length()-1;i>=0;i--)
771  {
772    ZZ coefficient;
773    polynom=e[i].a;
774    exponent=e[i].b;
775    bigone=convertNTLZZX2CF(polynom,x);
776    //append the converted polynomial to the list
777    rueckgabe.append(CFFactor(bigone,exponent));
778  }
779  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
780  // the multiplicity at pos 1
781  //if (!IsOne(multi))
782    rueckgabe.insert(CFFactor(convertZZ2CF(multi),1));
783
784  //return the converted list
785  return rueckgabe;
786}
787
788
789////////////////////////////////////////////////////////////////////////////////
790// NAME: convertNTLZZpX2CF                                                    //
791//                                                                            //
792// DESCRIPTION:                                                               //
793// Routine for conversion of elements of arbitrary extensions of ZZp,         //
794// having type ZZpE, of NTL to their corresponding values of type             //
795// canonicalform in Factory.                                                  //
796// To guarantee the correct execution of the algorithm the characteristic     //
797// has to be an arbitrary prime number and Factory has to compute in an       //
798// extension of F_p.                                                          //
799//                                                                            //
800// INPUT:  The coefficient of type ZZpE and the variable x indicating the main//
801//         variable of the computed canonicalform                             //
802// OUTPUT: The converted value of coefficient as type canonicalform           //
803////////////////////////////////////////////////////////////////////////////////
804
805CanonicalForm convertNTLZZpE2CF(ZZ_pE coefficient,Variable x)
806{
807  return convertNTLZZpX2CF(rep(coefficient),x);
808}
809CanonicalForm convertNTLzzpE2CF(zz_pE coefficient,Variable x)
810{
811  return convertNTLzzpX2CF(rep(coefficient),x);
812}
813
814////////////////////////////////////////////////////////////////////////////////
815// NAME: convertNTLvec_pair_ZZpEX_long2FacCFFList                             //
816//                                                                            //
817// DESCRIPTION:                                                               //
818// Routine for converting a vector of polynomials from ZZpEX to a CFFList     //
819// of Factory. This routine will be used after a successful factorization     //
820// of NTL to convert the result back to Factory.                              //
821// Additionally a variable x and the computed multiplicity, as a type         //
822// ZZpE of NTL, is needed as parameters indicating the main variable of the   //
823// computed canonicalform and the multiplicity of the original polynomial.    //
824// To guarantee the correct execution of the algorithm the characteristic     //
825// has a be an arbitrary prime number p and computations have to be done      //
826// in an extention of F_p.                                                    //
827//                                                                            //
828// INPUT:  A vector of polynomials over ZZpE of type vec_pair_ZZ_pEX_long and //
829//         a variable x and a multiplicity of type ZZpE                       //
830// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
831//         have x as their main variable                                      //
832////////////////////////////////////////////////////////////////////////////////
833
834CFFList convertNTLvec_pair_ZZpEX_long2FacCFFList(vec_pair_ZZ_pEX_long e,ZZ_pE multi,Variable x,Variable alpha)
835{
836  CFFList rueckgabe;
837  ZZ_pEX polynom;
838  long exponent;
839  CanonicalForm bigone;
840
841  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
842  //important for the factorization, but nevertheless would take computing time, so it is omitted
843
844  // Go through the vector e and build up the CFFList
845  // As usual bigone summarizes the result during every loop
846  for (int i=e.length()-1;i>=0;i--)
847  {
848    bigone=0;
849
850    polynom=e[i].a;
851    exponent=e[i].b;
852
853    for (int j=0;j<deg(polynom)+1;j++)
854    {
855      if (IsOne(coeff(polynom,j)))
856      {
857        bigone+=power(x,j);
858      }
859      else
860      {
861        CanonicalForm coefficient=convertNTLZZpE2CF(coeff(polynom,j),alpha);
862        if (coeff(polynom,j)!=0)
863        {
864          bigone += (power(x,j)*coefficient);
865        }
866      }
867    }
868    //append the computed polynomials together with its exponent to the CFFList
869    rueckgabe.append(CFFactor(bigone,exponent));
870  }
871  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
872  // Start by appending the multiplicity
873  if (!IsOne(multi))
874    rueckgabe.insert(CFFactor(convertNTLZZpE2CF(multi,alpha),1));
875
876  //return the computed CFFList
877  return rueckgabe;
878}
879CFFList convertNTLvec_pair_zzpEX_long2FacCFFList(vec_pair_zz_pEX_long e,zz_pE multi,Variable x,Variable alpha)
880{
881  CFFList rueckgabe;
882  zz_pEX polynom;
883  long exponent;
884  CanonicalForm bigone;
885
886  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
887  //important for the factorization, but nevertheless would take computing time, so it is omitted
888
889  // Go through the vector e and build up the CFFList
890  // As usual bigone summarizes the result during every loop
891  for (int i=e.length()-1;i>=0;i--)
892  {
893    bigone=0;
894
895    polynom=e[i].a;
896    exponent=e[i].b;
897
898    for (int j=0;j<deg(polynom)+1;j++)
899    {
900      if (IsOne(coeff(polynom,j)))
901      {
902        bigone+=power(x,j);
903      }
904      else
905      {
906        CanonicalForm coefficient=convertNTLzzpE2CF(coeff(polynom,j),alpha);
907        if (coeff(polynom,j)!=0)
908        {
909          bigone += (power(x,j)*coefficient);
910        }
911      }
912    }
913    //append the computed polynomials together with its exponent to the CFFList
914    rueckgabe.append(CFFactor(bigone,exponent));
915  }
916  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
917  // Start by appending the multiplicity
918  if (!IsOne(multi))
919    rueckgabe.insert(CFFactor(convertNTLzzpE2CF(multi,alpha),1));
920
921  //return the computed CFFList
922  return rueckgabe;
923}
924
925////////////////////////////////////////////////////////////////////////////////
926// NAME: convertNTLGF2E2CF                                                    //
927//                                                                            //
928// DESCRIPTION:                                                               //
929// Routine for conversion of elements of extensions of GF2, having type       //
930// GF2E, of NTL to their corresponding values of type canonicalform in        //
931// Factory.                                                                   //
932// To guarantee the correct execution of the algorithm, the characteristic    //
933// must equal two and Factory has to compute in an extension of F_2.          //
934// As usual this is an optimized special case of the more general conversion  //
935// routine from ZZpE to Factory.                                              //
936//                                                                            //
937// INPUT:  The coefficient of type GF2E and the variable x indicating the     //
938//         main variable of the computed canonicalform                        //
939// OUTPUT: The converted value of coefficient as type canonicalform           //
940////////////////////////////////////////////////////////////////////////////////
941
942CanonicalForm convertNTLGF2E2CF(GF2E coefficient,Variable x)
943{
944  return convertNTLGF2X2CF(rep(coefficient),x);
945}
946
947////////////////////////////////////////////////////////////////////////////////
948// NAME: convertNTLvec_pair_GF2EX_long2FacCFFList                             //
949//                                                                            //
950// DESCRIPTION:                                                               //
951// Routine for converting a vector of polynomials from GF2EX to a CFFList     //
952// of Factory. This routine will be used after a successful factorization     //
953// of NTL to convert the result back to Factory.                              //
954// This is a special, but optimized case of the more general conversion       //
955// from ZZpE to canonicalform.                                                //
956// Additionally a variable x and the computed multiplicity, as a type GF2E    //
957// of NTL, is needed as parameters indicating the main variable of the        //
958// computed canonicalform and the multiplicity of the original polynomial.    //
959// To guarantee the correct execution of the algorithm the characteristic     //
960// has to equal two and computations have to be done in an extention of F_2.  //
961//                                                                            //
962// INPUT:  A vector of polynomials over GF2E of type vec_pair_GF2EX_long and  //
963//         a variable x and a multiplicity of type GF2E                       //
964// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
965//         have x as their main variable                                      //
966////////////////////////////////////////////////////////////////////////////////
967
968CFFList convertNTLvec_pair_GF2EX_long2FacCFFList(vec_pair_GF2EX_long e,GF2E multi,Variable x,Variable alpha)
969{
970  CFFList rueckgabe;
971  GF2EX polynom;
972  long exponent;
973  CanonicalForm bigone;
974
975  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
976  //important for the factorization, but nevertheless would take computing time, so it is omitted
977
978  // multiplicity is always one, so we do not have to worry about that
979
980  // Go through the vector e and build up the CFFList
981  // As usual bigone summarizes the result during every loop
982  for (int i=e.length()-1;i>=0;i--)
983  {
984    bigone=0;
985
986    polynom=e[i].a;
987    exponent=e[i].b;
988
989    for (int j=0;j<deg(polynom)+1;j++)
990    {
991      if (IsOne(coeff(polynom,j)))
992      {
993        bigone+=power(x,j);
994      }
995      else
996      {
997        CanonicalForm coefficient=convertNTLGF2E2CF(coeff(polynom,j),alpha);
998        if (coeff(polynom,j)!=0)
999        {
1000          bigone += (power(x,j)*coefficient);
1001        }
1002      }
1003    }
1004
1005    // append the computed polynomial together with its multiplicity
1006    rueckgabe.append(CFFactor(bigone,exponent));
1007
1008  }
1009  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
1010  // return the computed CFFList
1011  return rueckgabe;
1012}
1013
1014////////////////////////////////////////////////////
1015// CanonicalForm in Z_2(a)[X] to NTL GF2EX        //
1016////////////////////////////////////////////////////
1017GF2EX convertFacCF2NTLGF2EX(CanonicalForm f,GF2X mipo)
1018{
1019  GF2E::init(mipo);
1020  GF2EX result;
1021  CFIterator i;
1022  i=f;
1023
1024  int j=0;
1025  int NTLcurrentExp=i.exp();
1026  int largestExp=i.exp();
1027  int k;
1028
1029  result.SetMaxLength(largestExp+1);
1030  for(;i.hasTerms();i++)
1031  {
1032    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1033    NTLcurrentExp=i.exp();
1034    CanonicalForm c=i.coeff();
1035    GF2X cc=convertFacCF2NTLGF2X(c);
1036    //ZZ_pE ccc;
1037    //conv(ccc,cc);
1038    SetCoeff(result,NTLcurrentExp,to_GF2E(cc));
1039    NTLcurrentExp--;
1040  }
1041  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1042  result.normalize();
1043  return result;
1044}
1045////////////////////////////////////////////////////
1046// CanonicalForm in Z_p(a)[X] to NTL ZZ_pEX       //
1047////////////////////////////////////////////////////
1048ZZ_pEX convertFacCF2NTLZZ_pEX(CanonicalForm f, ZZ_pX mipo)
1049{
1050  ZZ_pE::init(mipo);
1051  ZZ_pEX result;
1052  CFIterator i;
1053  i=f;
1054
1055  int j=0;
1056  int NTLcurrentExp=i.exp();
1057  int largestExp=i.exp();
1058  int k;
1059
1060  result.SetMaxLength(largestExp+1);
1061  for(;i.hasTerms();i++)
1062  {
1063    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1064    NTLcurrentExp=i.exp();
1065    CanonicalForm c=i.coeff();
1066    ZZ_pX cc=convertFacCF2NTLZZpX(c);
1067    //ZZ_pE ccc;
1068    //conv(ccc,cc);
1069    SetCoeff(result,NTLcurrentExp,to_ZZ_pE(cc));
1070    NTLcurrentExp--;
1071  }
1072  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1073  result.normalize();
1074  return result;
1075}
1076zz_pEX convertFacCF2NTLzz_pEX(CanonicalForm f, zz_pX mipo)
1077{
1078  zz_pE::init(mipo);
1079  zz_pEX result;
1080  CFIterator i;
1081  i=f;
1082
1083  int j=0;
1084  int NTLcurrentExp=i.exp();
1085  int largestExp=i.exp();
1086  int k;
1087
1088  result.SetMaxLength(largestExp+1);
1089  for(;i.hasTerms();i++)
1090  {
1091    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1092    NTLcurrentExp=i.exp();
1093    CanonicalForm c=i.coeff();
1094    zz_pX cc=convertFacCF2NTLzzpX(c);
1095    //ZZ_pE ccc;
1096    //conv(ccc,cc);
1097    SetCoeff(result,NTLcurrentExp,to_zz_pE(cc));
1098    NTLcurrentExp--;
1099  }
1100  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1101  result.normalize();
1102  return result;
1103}
1104#endif
Note: See TracBrowser for help on using the repository browser.