source: git/factory/NTLconvert.cc @ 56216b

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