source: git/factory/NTLconvert.cc @ 6c5d86

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