source: git/factory/NTLconvert.cc @ d07137

spielwiese
Last change on this file since d07137 was d07137, checked in by Hans Schönemann <hannes@…>, 21 years ago
*hannes: faster conversions git-svn-id: file:///usr/local/Singular/svn/trunk@6895 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 32.9 KB
Line 
1/* $Id: NTLconvert.cc,v 1.12 2003-10-15 17:19:38 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  char numbers[]="0123456789abcdef";
456
457  coeff_long=to_long(coefficient);
458
459  //Test whether coefficient can be represented as an immediate integer in Factory
460  if ( (NumBits(coefficient)<=NTL_ZZ_NBITS)
461  && (coeff_long>MINIMMEDIATE)
462  && (coeff_long<MAXIMMEDIATE))
463  {
464    // coefficient is immediate --> return the coefficient as canonicalform
465    return CanonicalForm(coeff_long);
466  }
467  else
468  {
469    // coefficient is not immediate (gmp-number)
470
471    // convert coefficient to char* (input for gmp)
472    dummy[1]='\0';
473
474    if (coefficient<0)
475    {
476      // negate coefficient, but store the sign in minusremainder
477      minusremainder=1;
478      coefficient=-coefficient;
479    }
480
481    int l=0;
482    while (coefficient>15)
483    {
484      ZZ quotient,remaind;
485      ZZ ten;ten=16;
486      DivRem(quotient,remaind,coefficient,ten);
487      dummy[0]=numbers[to_long(remaind)];
488      //tmp*=10; tmp+=to_long(remaind);
489
490      l++;
491      if (l>=cf_stringtemp_l-2)
492      {
493        Free(cf_stringtemp2,cf_stringtemp_l);
494        char *p=(char *)Alloc(cf_stringtemp_l*2);
495        memcpy(p,cf_stringtemp,cf_stringtemp_l);
496        Free(cf_stringtemp,cf_stringtemp_l);
497        cf_stringtemp_l*=2;
498        cf_stringtemp=p;
499        cf_stringtemp2=(char *)Alloc(cf_stringtemp_l);
500      }
501      cf_stringtemp[l-1]=dummy[0];
502      cf_stringtemp[l]='\0';
503      //strcat(stringtemp,dummy);
504
505      coefficient=quotient;
506    }
507    //built up the string in dummy[0]
508    dummy[0]=numbers[to_long(coefficient)];
509    strcat(cf_stringtemp,dummy);
510    //tmp*=10; tmp+=to_long(coefficient);
511
512    if (minusremainder==1)
513    {
514      //Check whether coefficient has been negative at the start of the procedure
515      cf_stringtemp2[0]='-';
516      //tmp*=(-1);
517    }
518
519    //reverse the list to obtain the correct string
520    int len=strlen(cf_stringtemp);
521    for (int i=len-1;i>=0;i--)
522    {
523      cf_stringtemp2[len-i-1+minusremainder]=cf_stringtemp[i];
524    }
525    cf_stringtemp2[len+minusremainder]='\0';
526  }
527
528  //convert the string to canonicalform using the char*-Constructor
529  return CanonicalForm(cf_stringtemp2,16);
530  //return tmp;
531}
532
533////////////////////////////////////////////////////////////////////////////////
534// NAME: convertFacCF2NTLZZX                                                  //
535//                                                                            //
536// DESCRIPTION:                                                               //
537// Routine for conversion of canonicalforms in Factory to polynomials         //
538// of type ZZX of NTL. To guarantee the correct execution of the              //
539// algorithm the characteristic has to equal zero.                            //
540//                                                                            //
541// INPUT:  The canonicalform that has to be converted                         //
542// OUTPUT: The converted NTL-polynom of type ZZX                              //
543////////////////////////////////////////////////////////////////////////////////
544
545ZZX convertFacCF2NTLZZX(CanonicalForm f)
546{
547    ZZX ntl_poly;
548
549    CFIterator i;
550    i=f;
551
552    int j=0;
553    int NTLcurrentExp=i.exp();
554    int largestExp=i.exp();
555    int k;
556
557    //set the length of the NTL-polynomial
558    ntl_poly.SetMaxLength(largestExp+1);
559
560    //Go through the coefficients of the canonicalform and build up the NTL-polynomial
561    for (;i.hasTerms();i++)
562    {
563      for (k=NTLcurrentExp;k>i.exp();k--)
564      {
565        SetCoeff(ntl_poly,k,0);
566      }
567      NTLcurrentExp=i.exp();
568
569      if (!i.coeff().isImm())
570      {
571        //Coefficient is a gmp-number
572        mpz_t gmp_val;
573        ZZ temp;
574        char* stringtemp;
575
576        gmp_val[0]=getmpi(i.coeff().getval());
577        int l=mpz_sizeinbase(gmp_val,10)+2;
578        stringtemp=(char*)Alloc(l);
579        stringtemp=mpz_get_str(stringtemp,10,gmp_val);
580        conv(temp,stringtemp);
581        Free(stringtemp,l);
582
583        //set the computed coefficient
584        SetCoeff(ntl_poly,NTLcurrentExp,temp);
585      }
586      else
587      {
588        //Coefficient is immediate --> use its value
589        SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval());
590      }
591
592      NTLcurrentExp--;
593    }
594    for (k=NTLcurrentExp;k>=0;k--)
595    {
596      SetCoeff(ntl_poly,k,0);
597    }
598
599    //normalize the polynomial
600    ntl_poly.normalize();
601
602    return ntl_poly;
603}
604
605////////////////////////////////////////////////////////////////////////////////
606// NAME: convertNTLvec_pair_ZZX_long2FacCFFList                               //
607//                                                                            //
608// DESCRIPTION:                                                               //
609// Routine for converting a vector of polynomials from ZZ to a list           //
610// CFFList of Factory. This routine will be used after a successful           //
611// factorization of NTL to convert the result back to Factory.                //
612// Additionally a variable x and the computed multiplicity, as a type         //
613// ZZ of NTL, is needed as parameters indicating the main variable of the     //
614// computed canonicalform and the multiplicity of the original polynomial.    //
615// To guarantee the correct execution of the algorithm the characteristic     //
616// has to equal zero.                                                         //
617//                                                                            //
618// INPUT:  A vector of polynomials over ZZ of type vec_pair_ZZX_long and      //
619//         a variable x and a multiplicity of type ZZ                         //
620// OUTPUT: The converted list of polynomials of type CFFList, all             //
621//         have x as their main variable                                      //
622////////////////////////////////////////////////////////////////////////////////
623
624CFFList convertNTLvec_pair_ZZX_long2FacCFFList(vec_pair_ZZX_long e,ZZ multi,Variable x)
625{
626  CFFList rueckgabe;
627  ZZX polynom;
628  long exponent;
629  CanonicalForm bigone;
630
631  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
632  //important for the factorization, but nevertheless would take computing time, so it is omitted
633
634
635  // Go through the vector e and build up the CFFList
636  // As usual bigone summarizes the result
637  for (int i=e.length()-1;i>=0;i--)
638  {
639    bigone=0;
640    ZZ coefficient;
641    polynom=e[i].a;
642    exponent=e[i].b;
643    long coeff_long;
644
645    for (int j=0;j<deg(polynom)+1;j++)
646    {
647      coefficient=coeff(polynom,j);
648      if (!IsZero(coefficient))
649      {
650        bigone += (power(x,j)*convertZZ2CF(coefficient));
651      }
652    }
653
654    //append the converted polynomial to the list
655    rueckgabe.append(CFFactor(bigone,exponent));
656  }
657  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
658  // the multiplicity at pos 1
659  //if (!IsOne(multi))
660    rueckgabe.insert(CFFactor(convertZZ2CF(multi),1));
661
662  //return the converted list
663  return rueckgabe;
664}
665
666
667////////////////////////////////////////////////////////////////////////////////
668// NAME: convertNTLZZpX2CF                                                    //
669//                                                                            //
670// DESCRIPTION:                                                               //
671// Routine for conversion of elements of arbitrary extensions of ZZp,         //
672// having type ZZpE, of NTL to their corresponding values of type             //
673// canonicalform in Factory.                                                  //
674// To guarantee the correct execution of the algorithm the characteristic     //
675// has to be an arbitrary prime number and Factory has to compute in an       //
676// extension of F_p.                                                          //
677//                                                                            //
678// INPUT:  The coefficient of type ZZpE and the variable x indicating the main//
679//         variable of the computed canonicalform                             //
680// OUTPUT: The converted value of coefficient as type canonicalform           //
681////////////////////////////////////////////////////////////////////////////////
682
683CanonicalForm convertNTLZZpE2CF(ZZ_pE coefficient,Variable x)
684{
685  return convertNTLZZpX2CF(rep(coefficient),x);
686}
687
688////////////////////////////////////////////////////////////////////////////////
689// NAME: convertNTLvec_pair_ZZpEX_long2FacCFFList                             //
690//                                                                            //
691// DESCRIPTION:                                                               //
692// Routine for converting a vector of polynomials from ZZpEX to a CFFList     //
693// of Factory. This routine will be used after a successful factorization     //
694// of NTL to convert the result back to Factory.                              //
695// Additionally a variable x and the computed multiplicity, as a type         //
696// ZZpE of NTL, is needed as parameters indicating the main variable of the   //
697// computed canonicalform and the multiplicity of the original polynomial.    //
698// To guarantee the correct execution of the algorithm the characteristic     //
699// has a be an arbitrary prime number p and computations have to be done      //
700// in an extention of F_p.                                                    //
701//                                                                            //
702// INPUT:  A vector of polynomials over ZZpE of type vec_pair_ZZ_pEX_long and //
703//         a variable x and a multiplicity of type ZZpE                       //
704// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
705//         have x as their main variable                                      //
706////////////////////////////////////////////////////////////////////////////////
707
708CFFList convertNTLvec_pair_ZZpEX_long2FacCFFList(vec_pair_ZZ_pEX_long e,ZZ_pE multi,Variable x,Variable alpha)
709{
710  CFFList rueckgabe;
711  ZZ_pEX polynom;
712  long exponent;
713  CanonicalForm bigone;
714
715  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
716  //important for the factorization, but nevertheless would take computing time, so it is omitted
717
718  // Go through the vector e and build up the CFFList
719  // As usual bigone summarizes the result during every loop
720  for (int i=e.length()-1;i>=0;i--)
721  {
722    bigone=0;
723
724    polynom=e[i].a;
725    exponent=e[i].b;
726
727    for (int j=0;j<deg(polynom)+1;j++)
728    {
729      if (IsOne(coeff(polynom,j)))
730      {
731        bigone+=power(x,j);
732      }
733      else
734      {
735        CanonicalForm coefficient=convertNTLZZpE2CF(coeff(polynom,j),alpha);
736        if (coeff(polynom,j)!=0)
737        {
738          bigone += (power(x,j)*coefficient);
739        }
740      }
741    }
742    //append the computed polynomials together with its exponent to the CFFList
743    rueckgabe.append(CFFactor(bigone,exponent));
744  }
745  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
746  // Start by appending the multiplicity
747  if (!IsOne(multi))
748    rueckgabe.insert(CFFactor(convertNTLZZpE2CF(multi,alpha),1));
749
750  //return the computed CFFList
751  return rueckgabe;
752}
753
754////////////////////////////////////////////////////////////////////////////////
755// NAME: convertNTLGF2E2CF                                                    //
756//                                                                            //
757// DESCRIPTION:                                                               //
758// Routine for conversion of elements of extensions of GF2, having type       //
759// GF2E, of NTL to their corresponding values of type canonicalform in        //
760// Factory.                                                                   //
761// To guarantee the correct execution of the algorithm, the characteristic    //
762// must equal two and Factory has to compute in an extension of F_2.          //
763// As usual this is an optimized special case of the more general conversion  //
764// routine from ZZpE to Factory.                                              //
765//                                                                            //
766// INPUT:  The coefficient of type GF2E and the variable x indicating the     //
767//         main variable of the computed canonicalform                        //
768// OUTPUT: The converted value of coefficient as type canonicalform           //
769////////////////////////////////////////////////////////////////////////////////
770
771CanonicalForm convertNTLGF2E2CF(GF2E coefficient,Variable x)
772{
773  return convertNTLGF2X2CF(rep(coefficient),x);
774}
775
776////////////////////////////////////////////////////////////////////////////////
777// NAME: convertNTLvec_pair_GF2EX_long2FacCFFList                             //
778//                                                                            //
779// DESCRIPTION:                                                               //
780// Routine for converting a vector of polynomials from GF2EX to a CFFList     //
781// of Factory. This routine will be used after a successful factorization     //
782// of NTL to convert the result back to Factory.                              //
783// This is a special, but optimized case of the more general conversion       //
784// from ZZpE to canonicalform.                                                //
785// Additionally a variable x and the computed multiplicity, as a type GF2E    //
786// of NTL, is needed as parameters indicating the main variable of the        //
787// computed canonicalform and the multiplicity of the original polynomial.    //
788// To guarantee the correct execution of the algorithm the characteristic     //
789// has to equal two and computations have to be done in an extention of F_2.  //
790//                                                                            //
791// INPUT:  A vector of polynomials over GF2E of type vec_pair_GF2EX_long and  //
792//         a variable x and a multiplicity of type GF2E                       //
793// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
794//         have x as their main variable                                      //
795////////////////////////////////////////////////////////////////////////////////
796
797CFFList convertNTLvec_pair_GF2EX_long2FacCFFList(vec_pair_GF2EX_long e,GF2E multi,Variable x,Variable alpha)
798{
799  CFFList rueckgabe;
800  GF2EX polynom;
801  long exponent;
802  CanonicalForm bigone;
803
804  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
805  //important for the factorization, but nevertheless would take computing time, so it is omitted
806
807  // multiplicity is always one, so we do not have to worry about that
808
809  // Go through the vector e and build up the CFFList
810  // As usual bigone summarizes the result during every loop
811  for (int i=e.length()-1;i>=0;i--)
812  {
813    bigone=0;
814
815    polynom=e[i].a;
816    exponent=e[i].b;
817
818    for (int j=0;j<deg(polynom)+1;j++)
819    {
820      if (IsOne(coeff(polynom,j)))
821      {
822        bigone+=power(x,j);
823      }
824      else
825      {
826        CanonicalForm coefficient=convertNTLGF2E2CF(coeff(polynom,j),alpha);
827        if (coeff(polynom,j)!=0)
828        {
829          bigone += (power(x,j)*coefficient);
830        }
831      }
832    }
833
834    // append the computed polynomial together with its multiplicity
835    rueckgabe.append(CFFactor(bigone,exponent));
836
837  }
838  if(isOn(SW_USE_NTL_SORT)) rueckgabe.sort(NTLcmpCF);
839  // return the computed CFFList
840  return rueckgabe;
841}
842
843////////////////////////////////////////////////////
844// CanonicalForm in Z_2(a)[X] to NTL GF2EX        //
845////////////////////////////////////////////////////
846GF2EX convertFacCF2NTLGF2EX(CanonicalForm f,GF2X mipo)
847{
848  GF2E::init(mipo);
849  GF2EX result;
850  CFIterator i;
851  i=f;
852
853  int j=0;
854  int NTLcurrentExp=i.exp();
855  int largestExp=i.exp();
856  int k;
857
858  result.SetMaxLength(largestExp+1);
859  for(;i.hasTerms();i++)
860  {
861    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
862    NTLcurrentExp=i.exp();
863    CanonicalForm c=i.coeff();
864    GF2X cc=convertFacCF2NTLGF2X(c);
865    //ZZ_pE ccc;
866    //conv(ccc,cc);
867    SetCoeff(result,NTLcurrentExp,to_GF2E(cc));
868    NTLcurrentExp--;
869  }
870  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
871  result.normalize();
872  return result;
873}
874////////////////////////////////////////////////////
875// CanonicalForm in Z_p(a)[X] to NTL ZZ_pEX       //
876////////////////////////////////////////////////////
877ZZ_pEX convertFacCF2NTLZZ_pEX(CanonicalForm f, ZZ_pX mipo)
878{
879  ZZ_pE::init(mipo);
880  ZZ_pEX result;
881  CFIterator i;
882  i=f;
883
884  int j=0;
885  int NTLcurrentExp=i.exp();
886  int largestExp=i.exp();
887  int k;
888
889  result.SetMaxLength(largestExp+1);
890  for(;i.hasTerms();i++)
891  {
892    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
893    NTLcurrentExp=i.exp();
894    CanonicalForm c=i.coeff();
895    ZZ_pX cc=convertFacCF2NTLZZpX(c);
896    //ZZ_pE ccc;
897    //conv(ccc,cc);
898    SetCoeff(result,NTLcurrentExp,to_ZZ_pE(cc));
899    NTLcurrentExp--;
900  }
901  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
902  result.normalize();
903  return result;
904}
905#endif
Note: See TracBrowser for help on using the repository browser.