source: git/factory/NTLconvert.cc @ 7af0e48

fieker-DuValspielwiese
Last change on this file since 7af0e48 was d30633d, checked in by Hans Schönemann <hannes@…>, 22 years ago
*hannes: fixes and ... git-svn-id: file:///usr/local/Singular/svn/trunk@6224 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 30.9 KB
Line 
1/* $Id: NTLconvert.cc,v 1.2 2002-07-30 15:19:56 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    //for (int i=strlen(stringtemp)-1;i>=0;i--)
472    //{
473    //  stringtemp2[strlen(stringtemp)-i-1+minusremainder]=stringtemp[i];
474    //}
475    //stringtemp2[strlen(stringtemp)+minusremainder]='\0';
476  }
477
478  //convert the string to canonicalform using the char*-Constructor
479  //return CanonicalForm(stringtemp2);
480  return tmp;
481}
482
483////////////////////////////////////////////////////////////////////////////////
484// NAME: convertFacCF2NTLZZX                                                  //
485//                                                                            //
486// DESCRIPTION:                                                               //
487// Routine for conversion of canonicalforms in Factory to polynomials         //
488// of type ZZX of NTL. To guarantee the correct execution of the              //
489// algorithm the characteristic has to equal zero.                            //
490//                                                                            //
491// INPUT:  The canonicalform that has to be converted                         //
492// OUTPUT: The converted NTL-polynom of type ZZX                              //
493////////////////////////////////////////////////////////////////////////////////
494
495ZZX convertFacCF2NTLZZX(CanonicalForm f)
496{
497    ZZX ntl_poly;
498
499    CFIterator i;
500    i=f;
501
502    int j=0;
503    int NTLcurrentExp=i.exp();
504    int largestExp=i.exp();
505    int k;
506
507    //set the length of the NTL-polynomial
508    ntl_poly.SetMaxLength(largestExp+1);
509
510    //Go through the coefficients of the canonicalform and build up the NTL-polynomial
511    for (;i.hasTerms();i++)
512    {
513      for (k=NTLcurrentExp;k>i.exp();k--)
514      {
515        SetCoeff(ntl_poly,k,0);
516      }
517      NTLcurrentExp=i.exp();
518
519      if (!i.coeff().isImm())
520      {
521        //Coefficient is a gmp-number
522        mpz_t gmp_val;
523        ZZ temp;
524        char* stringtemp;
525
526        gmp_val[0]=getmpi(i.coeff().getval());
527        int l=mpz_sizeinbase(gmp_val,10)+2;
528        stringtemp=(char*)omAlloc(l);
529        stringtemp=mpz_get_str(stringtemp,10,gmp_val);
530        conv(temp,stringtemp);
531        omFreeSize(stringtemp,l);
532
533        //set the computed coefficient
534        SetCoeff(ntl_poly,NTLcurrentExp,temp);
535      }
536      else
537      {
538        //Coefficient is immediate --> use its value
539        SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval());
540      }
541
542      NTLcurrentExp--;
543    }
544    for (k=NTLcurrentExp;k>=0;k--)
545    {
546      SetCoeff(ntl_poly,k,0);
547    }
548
549    //normalize the polynomial
550    ntl_poly.normalize();
551
552    return ntl_poly;
553}
554
555////////////////////////////////////////////////////////////////////////////////
556// NAME: convertNTLvec_pair_ZZX_long2FacCFFList                               //
557//                                                                            //
558// DESCRIPTION:                                                               //
559// Routine for converting a vector of polynomials from ZZ to a list           //
560// CFFList of Factory. This routine will be used after a successful           //
561// factorization of NTL to convert the result back to Factory.                //
562// Additionally a variable x and the computed multiplicity, as a type         //
563// ZZ of NTL, is needed as parameters indicating the main variable of the     //
564// computed canonicalform and the multiplicity of the original polynomial.    //
565// To guarantee the correct execution of the algorithm the characteristic     //
566// has to equal zero.                                                         //
567//                                                                            //
568// INPUT:  A vector of polynomials over ZZ of type vec_pair_ZZX_long and      //
569//         a variable x and a multiplicity of type ZZ                         //
570// OUTPUT: The converted list of polynomials of type CFFList, all             //
571//         have x as their main variable                                      //
572////////////////////////////////////////////////////////////////////////////////
573
574CFFList convertNTLvec_pair_ZZX_long2FacCFFList(vec_pair_ZZX_long e,ZZ multi,Variable x)
575{
576  CFFList rueckgabe;
577  ZZX polynom;
578  long exponent;
579  CanonicalForm bigone;
580
581  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
582  //important for the factorization, but nevertheless would take computing time, so it is omitted
583
584
585  // Start by appending the multiplicity
586
587  if (!IsOne(multi))
588    rueckgabe.append(CFFactor(convertZZ2CF(multi),1));
589
590  // Go through the vector e and build up the CFFList
591  // As usual bigone summarizes the result
592  for (int i=e.length()-1;i>=0;i--)
593  {
594    bigone=0;
595    ZZ coefficient;
596    polynom=e[i].a;
597    exponent=e[i].b;
598    long coeff_long;
599
600    for (int j=0;j<deg(polynom)+1;j++)
601    {
602      coefficient=coeff(polynom,j);
603      if (!IsZero(coefficient))
604      {
605        bigone += (power(x,j)*convertZZ2CF(coefficient));
606      }
607    }
608
609    //append the converted polynomial to the list
610    rueckgabe.append(CFFactor(bigone,exponent));
611  }
612  //return the converted list
613  return rueckgabe;
614}
615
616
617////////////////////////////////////////////////////////////////////////////////
618// NAME: convertNTLZZpX2CF                                                    //
619//                                                                            //
620// DESCRIPTION:                                                               //
621// Routine for conversion of elements of arbitrary extensions of ZZp,         //
622// having type ZZpE, of NTL to their corresponding values of type             //
623// canonicalform in Factory.                                                  //
624// To guarantee the correct execution of the algorithm the characteristic     //
625// has to be an arbitrary prime number and Factory has to compute in an       //
626// extension of F_p.                                                          //
627//                                                                            //
628// INPUT:  The coefficient of type ZZpE and the variable x indicating the main//
629//         variable of the computed canonicalform                             //
630// OUTPUT: The converted value of coefficient as type canonicalform           //
631////////////////////////////////////////////////////////////////////////////////
632
633CanonicalForm convertNTLZZpE2CF(ZZ_pE coefficient,Variable x)
634{
635  return convertNTLZZpX2CF(rep(coefficient),x);
636}
637
638////////////////////////////////////////////////////////////////////////////////
639// NAME: convertNTLvec_pair_ZZpEX_long2FacCFFList                             //
640//                                                                            //
641// DESCRIPTION:                                                               //
642// Routine for converting a vector of polynomials from ZZpEX to a CFFList     //
643// of Factory. This routine will be used after a successful factorization     //
644// of NTL to convert the result back to Factory.                              //
645// Additionally a variable x and the computed multiplicity, as a type         //
646// ZZpE of NTL, is needed as parameters indicating the main variable of the   //
647// computed canonicalform and the multiplicity of the original polynomial.    //
648// To guarantee the correct execution of the algorithm the characteristic     //
649// has a be an arbitrary prime number p and computations have to be done      //
650// in an extention of F_p.                                                    //
651//                                                                            //
652// INPUT:  A vector of polynomials over ZZpE of type vec_pair_ZZ_pEX_long and //
653//         a variable x and a multiplicity of type ZZpE                       //
654// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
655//         have x as their main variable                                      //
656////////////////////////////////////////////////////////////////////////////////
657
658CFFList convertNTLvec_pair_ZZpEX_long2FacCFFList(vec_pair_ZZ_pEX_long e,ZZ_pE multi,Variable x,Variable alpha)
659{
660  CFFList rueckgabe;
661  ZZ_pEX polynom;
662  long exponent;
663  CanonicalForm bigone;
664
665  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
666  //important for the factorization, but nevertheless would take computing time, so it is omitted
667
668
669  // Start by appending the multiplicity
670  if (!IsOne(multi))
671    rueckgabe.append(CFFactor(convertNTLZZpE2CF(multi,x),1));
672
673
674  // Go through the vector e and build up the CFFList
675  // As usual bigone summarizes the result during every loop
676  for (int i=e.length()-1;i>=0;i--)
677  {
678    bigone=0;
679
680    polynom=e[i].a;
681    exponent=e[i].b;
682
683    for (int j=0;j<deg(polynom)+1;j++)
684    {
685      if (IsOne(coeff(polynom,j)))
686      {
687        bigone+=power(x,j);
688      }
689      else
690      {
691        CanonicalForm coefficient=convertNTLZZpE2CF(coeff(polynom,j),alpha);
692        if (coeff(polynom,j)!=0)
693        {
694          bigone += (power(x,j)*coefficient);
695        }
696      }
697    }
698
699    //append the computed polynomials together with its exponent to the CFFList
700    rueckgabe.append(CFFactor(bigone,exponent));
701
702  }
703  //return the computed CFFList
704  return rueckgabe;
705}
706
707////////////////////////////////////////////////////////////////////////////////
708// NAME: convertNTLGF2E2CF                                                    //
709//                                                                            //
710// DESCRIPTION:                                                               //
711// Routine for conversion of elements of extensions of GF2, having type       //
712// GF2E, of NTL to their corresponding values of type canonicalform in        //
713// Factory.                                                                   //
714// To guarantee the correct execution of the algorithm, the characteristic    //
715// must equal two and Factory has to compute in an extension of F_2.          //
716// As usual this is an optimized special case of the more general conversion  //
717// routine from ZZpE to Factory.                                              //
718//                                                                            //
719// INPUT:  The coefficient of type GF2E and the variable x indicating the     //
720//         main variable of the computed canonicalform                        //
721// OUTPUT: The converted value of coefficient as type canonicalform           //
722////////////////////////////////////////////////////////////////////////////////
723
724CanonicalForm convertNTLGF2E2CF(GF2E coefficient,Variable x)
725{
726  return convertNTLGF2X2CF(rep(coefficient),x);
727}
728
729////////////////////////////////////////////////////////////////////////////////
730// NAME: convertNTLvec_pair_GF2EX_long2FacCFFList                             //
731//                                                                            //
732// DESCRIPTION:                                                               //
733// Routine for converting a vector of polynomials from GF2EX to a CFFList     //
734// of Factory. This routine will be used after a successful factorization     //
735// of NTL to convert the result back to Factory.                              //
736// This is a special, but optimized case of the more general conversion       //
737// from ZZpE to canonicalform.                                                //
738// Additionally a variable x and the computed multiplicity, as a type GF2E    //
739// of NTL, is needed as parameters indicating the main variable of the        //
740// computed canonicalform and the multiplicity of the original polynomial.    //
741// To guarantee the correct execution of the algorithm the characteristic     //
742// has to equal two and computations have to be done in an extention of F_2.  //
743//                                                                            //
744// INPUT:  A vector of polynomials over GF2E of type vec_pair_GF2EX_long and  //
745//         a variable x and a multiplicity of type GF2E                       //
746// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
747//         have x as their main variable                                      //
748////////////////////////////////////////////////////////////////////////////////
749
750CFFList convertNTLvec_pair_GF2EX_long2FacCFFList(vec_pair_GF2EX_long e,GF2E multi,Variable x,Variable alpha)
751{
752  CFFList rueckgabe;
753  GF2EX polynom;
754  long exponent;
755  CanonicalForm bigone;
756
757  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
758  //important for the factorization, but nevertheless would take computing time, so it is omitted
759
760  // multiplicity is always one, so we do not have to worry about that
761
762  // Go through the vector e and build up the CFFList
763  // As usual bigone summarizes the result during every loop
764  for (int i=e.length()-1;i>=0;i--)
765  {
766     bigone=0;
767
768     polynom=e[i].a;
769     exponent=e[i].b;
770
771     for (int j=0;j<deg(polynom)+1;j++)
772     {
773       if (IsOne(coeff(polynom,j)))
774       {
775         bigone+=power(x,j);
776       }
777       else
778       {
779         CanonicalForm coefficient=convertNTLGF2E2CF(coeff(polynom,j),alpha);
780         if (coeff(polynom,j)!=0)
781         {
782           bigone += (power(x,j)*coefficient);
783         }
784       }
785     }
786
787     // append the computed polynomial together with its multiplicity
788     rueckgabe.append(CFFactor(bigone,exponent));
789
790   }
791   // return the computed CFFList
792  return rueckgabe;
793}
794
795////////////////////////////////////////////////////
796// CanonicalForm in Z_2(a)[X] to NTL GF2EX        //
797////////////////////////////////////////////////////
798GF2EX convertFacCF2NTLGF2EX(CanonicalForm f,ZZ_pX mipo) {}
799////////////////////////////////////////////////////
800// CanonicalForm in Z_p(a)[X] to NTL ZZ_pEX       //
801////////////////////////////////////////////////////
802ZZ_pEX convertFacCF2NTLZZ_pEX(CanonicalForm f, ZZ_pX mipo)
803{
804  ZZ_pE::init(mipo);
805  ZZ_pEX result;
806  CFIterator i;
807  i=f;
808
809  int j=0;
810  int NTLcurrentExp=i.exp();
811  int largestExp=i.exp();
812  int k;
813
814  result.SetMaxLength(largestExp+1);
815  for(;i.hasTerms();i++)
816  {
817    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
818    NTLcurrentExp=i.exp();
819    CanonicalForm c=i.coeff();
820    ZZ_pX cc=convertFacCF2NTLZZpX(c);
821    //ZZ_pE ccc;
822    //conv(ccc,cc);
823    SetCoeff(result,NTLcurrentExp,to_ZZ_pE(cc));
824    NTLcurrentExp--;
825  }
826  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
827  result.normalize();
828  return result;
829}
830
831
832#endif
Note: See TracBrowser for help on using the repository browser.