source: git/factory/NTLconvert.cc @ b1326b

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