source: git/factory/NTLconvert.cc @ d67fcad

spielwiese
Last change on this file since d67fcad was 43568c, checked in by Martin Lee <martinlee84@…>, 13 years ago
deleted memory manager of factory
  • Property mode set to 100644
File size: 39.0 KB
Line 
1/* $Id$ */
2#include "config.h"
3
4#include "cf_assert.h"
5
6#include "cf_defs.h"
7#include "canonicalform.h"
8#include "cf_iter.h"
9#include "fac_berlekamp.h"
10#include "fac_cantzass.h"
11#include "fac_univar.h"
12#include "fac_multivar.h"
13#include "fac_sqrfree.h"
14#include "cf_algorithm.h"
15
16#include "cf_gmp.h"
17
18#ifdef HAVE_NTL
19#ifndef NOSTREAMIO
20#ifdef HAVE_CSTDIO
21#include <cstdio>
22#else
23#include <stdio.h>
24#endif
25#endif
26#include <string.h>
27#include <NTL/ZZXFactoring.h>
28#include <NTL/ZZ_pXFactoring.h>
29#include <NTL/lzz_pXFactoring.h>
30#include <NTL/GF2XFactoring.h>
31#include <NTL/ZZ_pEXFactoring.h>
32#include <NTL/lzz_pEXFactoring.h>
33#include <NTL/GF2EXFactoring.h>
34#include <NTL/tools.h>
35#include <NTL/mat_ZZ.h>
36#include "int_int.h"
37#include <limits.h>
38#include "NTLconvert.h"
39
40#ifdef HAVE_OMALLOC
41#define Alloc(L) omAlloc(L)
42#define Free(A,L) omFreeSize(A,L)
43#else
44#define Alloc(L) malloc(L)
45#define Free(A,L) free(A)
46#endif
47
48void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
49
50
51int fac_NTL_char=-1;            // the current characterstic for NTL calls
52                                // -1: undefined
53#ifdef NTL_CLIENT               // in <NTL/tools.h>: using of name space NTL
54NTL_CLIENT
55#endif
56
57////////////////////////////////////////////////////////////////////////////////
58// NAME: convertFacCF2NTLZZpX                                                 //
59//                                                                            //
60// DESCRIPTION:                                                               //
61// Conversion routine for Factory-type canonicalform into ZZpX of NTL,        //
62// i.e. polynomials over F_p. As a precondition for correct execution,        //
63// the characteristic has to a a prime number.                                //
64//                                                                            //
65// INPUT:  A canonicalform f                                                  //
66// OUTPUT: The converted NTL-polynomial over F_p of type ZZpX                 //
67////////////////////////////////////////////////////////////////////////////////
68
69ZZ_pX convertFacCF2NTLZZpX(CanonicalForm f)
70{
71  ZZ_pX ntl_poly;
72
73  CFIterator i;
74  i=f;
75
76  int NTLcurrentExp=i.exp();
77  int largestExp=i.exp();
78  int k;
79
80  // we now build up the NTL-polynomial
81  ntl_poly.SetMaxLength(largestExp+1);
82
83  for (;i.hasTerms();i++)
84  {
85    for (k=NTLcurrentExp;k>i.exp();k--)
86    {
87      SetCoeff(ntl_poly,k,0);
88    }
89    NTLcurrentExp=i.exp();
90
91    CanonicalForm c=i.coeff();
92    if (!c.isImm()) c=c.mapinto(); //c%= getCharacteristic();
93    if (!c.isImm())
94    {  //This case will never happen if the characteristic is in fact a prime
95       // number, since all coefficients are represented as immediates
96       #ifndef NOSTREAMIO
97       cout<<"convertFacCF2NTLZZ_pX: coefficient not immediate! : "<<f<<"\n";
98       #else
99       //NTL_SNS
100       printf("convertFacCF2NTLZZ_pX: coefficient not immediate!, char=%d\n",
101              getCharacteristic());
102       #endif
103       NTL_SNS exit(1);
104    }
105    else
106    {
107      SetCoeff(ntl_poly,NTLcurrentExp,c.intval());
108    }
109    NTLcurrentExp--;
110  }
111
112  //Set the remaining coefficients of ntl_poly to zero.
113  // This is necessary, because NTL internally
114  // also stores powers with zero coefficient,
115  // whereas factory stores tuples of degree and coefficient
116  //leaving out tuples if the coefficient equals zero
117  for (k=NTLcurrentExp;k>=0;k--)
118  {
119    SetCoeff(ntl_poly,k,0);
120  }
121
122  //normalize the polynomial and return it
123  ntl_poly.normalize();
124
125  return ntl_poly;
126}
127zz_pX convertFacCF2NTLzzpX(CanonicalForm f)
128{
129  zz_pX ntl_poly;
130
131  CFIterator i;
132  i=f;
133
134  int NTLcurrentExp=i.exp();
135  int largestExp=i.exp();
136  int k;
137
138  // we now build up the NTL-polynomial
139  ntl_poly.SetMaxLength(largestExp+1);
140
141  for (;i.hasTerms();i++)
142  {
143    for (k=NTLcurrentExp;k>i.exp();k--)
144    {
145      SetCoeff(ntl_poly,k,0);
146    }
147    NTLcurrentExp=i.exp();
148
149    CanonicalForm c=i.coeff();
150    if (!c.isImm()) c.mapinto(); //c%= getCharacteristic();
151    if (!c.isImm())
152    {  //This case will never happen if the characteristic is in fact a prime
153       // number, since all coefficients are represented as immediates
154       #ifndef NOSTREAMIO
155       cout<<"convertFacCF2NTLzz_pX: coefficient not immediate! : "<<f<<"\n";
156       #else
157       //NTL_SNS
158       printf("convertFacCF2NTLzz_pX: coefficient not immediate!, char=%d\n",
159              getCharacteristic());
160       #endif
161       NTL_SNS exit(1);
162    }
163    else
164    {
165      SetCoeff(ntl_poly,NTLcurrentExp,c.intval());
166    }
167    NTLcurrentExp--;
168  }
169
170  //Set the remaining coefficients of ntl_poly to zero.
171  // This is necessary, because NTL internally
172  // also stores powers with zero coefficient,
173  // whereas factory stores tuples of degree and coefficient
174  //leaving out tuples if the coefficient equals zero
175  for (k=NTLcurrentExp;k>=0;k--)
176  {
177    SetCoeff(ntl_poly,k,0);
178  }
179
180  //normalize the polynomial and return it
181  ntl_poly.normalize();
182
183  return ntl_poly;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187// NAME: convertFacCF2NTLGF2X                                                 //
188//                                                                            //
189// DESCRIPTION:                                                               //
190// Conversion routine for Factory-type canonicalform into GF2X of NTL,        //
191// i.e. polynomials over F_2. As precondition for correct execution,          //
192// the characteristic must equal two.                                         //
193// This is a special case of the more general conversion routine for          //
194// canonicalform to ZZpX. It is included because NTL provides additional      //
195// support and faster algorithms over F_2, moreover the conversion code       //
196// can be optimized, because certain steps are either completely obsolent     //
197// (like normalizing the polynomial) or they can be made significantly        //
198// faster (like building up the NTL-polynomial).                              //
199//                                                                            //
200// INPUT:  A canonicalform f                                                  //
201// OUTPUT: The converted NTL-polynomial over F_2 of type GF2X                 //
202////////////////////////////////////////////////////////////////////////////////
203
204GF2X convertFacCF2NTLGF2X(CanonicalForm f)
205{
206  //printf("convertFacCF2NTLGF2X\n");
207  GF2X ntl_poly;
208
209  CFIterator i;
210  i=f;
211
212  int NTLcurrentExp=i.exp();
213  int largestExp=i.exp();
214  int k;
215
216  //building the NTL-polynomial
217  ntl_poly.SetMaxLength(largestExp+1);
218
219  for (;i.hasTerms();i++)
220  {
221
222    for (k=NTLcurrentExp;k>i.exp();k--)
223    {
224      SetCoeff(ntl_poly,k,0);
225    }
226    NTLcurrentExp=i.exp();
227
228    if (!i.coeff().isImm()) i.coeff()=i.coeff().mapinto();
229    if (!i.coeff().isImm())
230    {
231      #ifndef NOSTREAMIO
232      cout<<"convertFacCF2NTLGF2X: coefficient not immidiate! : " << f << "\n";
233      #else
234      //NTL_SNS
235      printf("convertFacCF2NTLGF2X: coefficient not immidiate!");
236      #endif
237      NTL_SNS exit(1);
238    }
239    else
240    {
241      SetCoeff(ntl_poly,NTLcurrentExp,i.coeff().intval());
242    }
243    NTLcurrentExp--;
244  }
245  for (k=NTLcurrentExp;k>=0;k--)
246  {
247    SetCoeff(ntl_poly,k,0);
248  }
249  //normalization is not necessary of F_2
250
251  return ntl_poly;
252}
253
254
255////////////////////////////////////////////////////////////////////////////////
256// NAME: convertNTLZZpX2CF                                                    //
257//                                                                            //
258// DESCRIPTION:                                                               //
259// Conversion routine for NTL-Type ZZpX to Factory-Type canonicalform.        //
260// Additionally a variable x is needed as a parameter indicating the          //
261// main variable of the computed canonicalform. To guarantee the correct      //
262// execution of the algorithm, the characteristic has a be an arbitrary       //
263// prime number.                                                              //
264//                                                                            //
265// INPUT:  A canonicalform f, a variable x                                    //
266// OUTPUT: The converted Factory-polynomial of type canonicalform,            //
267//         built by the main variable x                                       //
268////////////////////////////////////////////////////////////////////////////////
269
270CanonicalForm convertNTLZZpX2CF(ZZ_pX poly,Variable x)
271{
272  //printf("convertNTLZZpX2CF\n");
273  CanonicalForm bigone;
274
275
276  if (deg(poly)>0)
277  {
278    // poly is non-constant
279    bigone=0;
280    bigone.mapinto();
281    // Compute the canonicalform coefficient by coefficient,
282    // bigone summarizes the result.
283    for (int j=0;j<=deg(poly);j++)
284    {
285      if (coeff(poly,j)!=0)
286      {
287        bigone+=(power(x,j)*CanonicalForm(to_long(rep(coeff(poly,j)))));
288      }
289    }
290  }
291  else
292  {
293    // poly is immediate
294    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
295    bigone.mapinto();
296  }
297  return bigone;
298}
299
300CanonicalForm convertNTLzzpX2CF(zz_pX poly,Variable x)
301{
302  //printf("convertNTLzzpX2CF\n");
303  CanonicalForm bigone;
304
305
306  if (deg(poly)>0)
307  {
308    // poly is non-constant
309    bigone=0;
310    bigone.mapinto();
311    // Compute the canonicalform coefficient by coefficient,
312    // bigone summarizes the result.
313    for (int j=0;j<=deg(poly);j++)
314    {
315      if (coeff(poly,j)!=0)
316      {
317        bigone+=(power(x,j)*CanonicalForm(to_long(rep(coeff(poly,j)))));
318      }
319    }
320  }
321  else
322  {
323    // poly is immediate
324    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
325    bigone.mapinto();
326  }
327  return bigone;
328}
329
330CanonicalForm convertNTLZZX2CF(ZZX polynom,Variable x)
331{
332  //printf("convertNTLZZX2CF\n");
333  CanonicalForm bigone;
334
335  // Go through the vector e and build up the CFFList
336  // As usual bigone summarizes the result
337  bigone=0;
338  ZZ coefficient;
339
340  for (int j=0;j<=deg(polynom);j++)
341  {
342    coefficient=coeff(polynom,j);
343    if (!IsZero(coefficient))
344    {
345      bigone += (power(x,j)*convertZZ2CF(coefficient));
346    }
347  }
348  return bigone;
349}
350
351////////////////////////////////////////////////////////////////////////////////
352// NAME: convertNTLGF2X2CF                                                    //
353//                                                                            //
354// DESCRIPTION:                                                               //
355// Conversion routine for NTL-Type GF2X to Factory-Type canonicalform,        //
356// the routine is again an optimized special case of the more general         //
357// conversion to ZZpX. Additionally a variable x is needed as a               //
358// parameter indicating the main variable of the computed canonicalform.      //
359// To guarantee the correct execution of the algorithm the characteristic     //
360// has a be an arbitrary prime number.                                        //
361//                                                                            //
362// INPUT:  A canonicalform f, a variable x                                    //
363// OUTPUT: The converted Factory-polynomial of type canonicalform,            //
364//         built by the main variable x                                       //
365////////////////////////////////////////////////////////////////////////////////
366
367CanonicalForm convertNTLGF2X2CF(GF2X poly,Variable x)
368{
369  //printf("convertNTLGF2X2CF\n");
370  CanonicalForm bigone;
371
372  if (deg(poly)>0)
373  {
374    // poly is non-constant
375    bigone=0;
376    bigone.mapinto();
377    // Compute the canonicalform coefficient by coefficient,
378    // bigone summarizes the result.
379    // In constrast to the more general conversion to ZZpX
380    // the only possible coefficients are zero
381    // and one yielding the following simplified loop
382    for (int j=0;j<=deg(poly);j++)
383    {
384      if (coeff(poly,j)!=0) bigone+=power(x,j);
385     // *CanonicalForm(to_long(rep(coeff(poly,j))))) is not necessary any more;
386    }
387  }
388  else
389  {
390    // poly is immediate
391    bigone=CanonicalForm(to_long(rep(coeff(poly,0))));
392    bigone.mapinto();
393  }
394
395  return bigone;
396}
397
398////////////////////////////////////////////////////////////////////////////////
399// NAME: convertNTLvec_pair_ZZpX_long2FacCFFList                              //
400//                                                                            //
401// DESCRIPTION:                                                               //
402// Routine for converting a vector of polynomials from ZZpX to                //
403// a CFFList of Factory. This routine will be used after a successful         //
404// factorization of NTL to convert the result back to Factory.                //
405//                                                                            //
406// Additionally a variable x and the computed multiplicity, as a type ZZp     //
407// of NTL, is needed as parameters indicating the main variable of the        //
408// computed canonicalform and the multiplicity of the original polynomial.    //
409// To guarantee the correct execution of the algorithm the characteristic     //
410// has a be an arbitrary prime number.                                        //
411//                                                                            //
412// INPUT:  A vector of polynomials over ZZp of type vec_pair_ZZ_pX_long and   //
413//         a variable x and a multiplicity of type ZZp                        //
414// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
415//         have x as their main variable                                      //
416////////////////////////////////////////////////////////////////////////////////
417
418CFFList convertNTLvec_pair_ZZpX_long2FacCFFList
419                                  (vec_pair_ZZ_pX_long e,ZZ_p multi,Variable x)
420{
421  //printf("convertNTLvec_pair_ZZpX_long2FacCFFList\n");
422  CFFList rueckgabe;
423  ZZ_pX polynom;
424  CanonicalForm bigone;
425
426  // Maybe, e may additionally be sorted with respect to increasing degree of x
427  // but this is not
428  //important for the factorization, but nevertheless would take computing time,
429  // so it is omitted
430
431
432  // Go through the vector e and compute the CFFList
433  // again bigone summarizes the result
434  for (int i=e.length()-1;i>=0;i--)
435  {
436    rueckgabe.append(CFFactor(convertNTLZZpX2CF(e[i].a,x),e[i].b));
437  }
438  // the multiplicity at pos 1
439  if (!IsOne(multi))
440    rueckgabe.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
441  return rueckgabe;
442}
443CFFList convertNTLvec_pair_zzpX_long2FacCFFList
444                                  (vec_pair_zz_pX_long e,zz_p multi,Variable x)
445{
446  //printf("convertNTLvec_pair_zzpX_long2FacCFFList\n");
447  CFFList rueckgabe;
448  zz_pX polynom;
449  CanonicalForm bigone;
450
451  // Maybe, e may additionally be sorted with respect to increasing degree of x
452  // but this is not
453  //important for the factorization, but nevertheless would take computing time,
454  // so it is omitted
455
456
457  // Go through the vector e and compute the CFFList
458  // again bigone summarizes the result
459  for (int i=e.length()-1;i>=0;i--)
460  {
461    rueckgabe.append(CFFactor(convertNTLzzpX2CF(e[i].a,x),e[i].b));
462  }
463  // the multiplicity at pos 1
464  if (!IsOne(multi))
465    rueckgabe.insert(CFFactor(CanonicalForm(to_long(rep(multi))),1));
466  return rueckgabe;
467}
468
469////////////////////////////////////////////////////////////////////////////////
470// NAME: convertNTLvec_pair_GF2X_long2FacCFFList                              //
471//                                                                            //
472// DESCRIPTION:                                                               //
473// Routine for converting a vector of polynomials of type GF2X from           //
474// NTL to a list CFFList of Factory. This routine will be used after a        //
475// successful factorization of NTL to convert the result back to Factory.     //
476// As usual this is simply a special case of the more general conversion      //
477// routine but again speeded up by leaving out unnecessary steps.             //
478// Additionally a variable x and the computed multiplicity, as type           //
479// GF2 of NTL, are needed as parameters indicating the main variable of the   //
480// computed canonicalform and the multiplicity of the original polynomial.    //
481// To guarantee the correct execution of the algorithm the characteristic     //
482// has a be an arbitrary prime number.                                        //
483//                                                                            //
484// INPUT:  A vector of polynomials over GF2 of type vec_pair_GF2X_long and    //
485//         a variable x and a multiplicity of type GF2                        //
486// OUTPUT: The converted list of polynomials of type CFFList, all             //
487//         polynomials have x as their main variable                          //
488////////////////////////////////////////////////////////////////////////////////
489
490CFFList convertNTLvec_pair_GF2X_long2FacCFFList
491                               (vec_pair_GF2X_long e,GF2 multi,Variable x)
492{
493  //printf("convertNTLvec_pair_GF2X_long2FacCFFList\n");
494  CFFList rueckgabe;
495  GF2X polynom;
496  long exponent;
497  CanonicalForm bigone;
498
499  // Maybe, e may additionally be sorted with respect to increasing degree of x
500  // but this is not
501  //important for the factorization, but nevertheless would take computing time
502  // so it is omitted.
503
504  //We do not have to worry about the multiplicity in GF2 since it equals one.
505
506  // Go through the vector e and compute the CFFList
507  // bigone summarizes the result again
508  for (int i=e.length()-1;i>=0;i--)
509  {
510    bigone=0;
511
512    polynom=e[i].a;
513    exponent=e[i].b;
514    for (int j=0;j<=deg(polynom);j++)
515    {
516      if (coeff(polynom,j)!=0)
517        bigone += (power(x,j)*CanonicalForm(to_long(rep(coeff(polynom,j)))));
518    }
519
520    //append the converted polynomial to the CFFList
521    rueckgabe.append(CFFactor(bigone,exponent));
522  }
523  return rueckgabe;
524}
525
526////////////////////////////////////////////////////////////////////////////////
527// NAME: convertZZ2CF                                                         //
528//                                                                            //
529// DESCRIPTION:                                                               //
530// Routine for conversion of integers represented in NTL as Type ZZ to        //
531// integers in Factory represented as canonicalform.                          //
532// To guarantee the correct execution of the algorithm the characteristic     //
533// has to equal zero.                                                         //
534//                                                                            //
535// INPUT:  The value coefficient of type ZZ that has to be converted          //
536// OUTPUT: The converted Factory-integer of type canonicalform                //
537////////////////////////////////////////////////////////////////////////////////
538
539static char *cf_stringtemp;
540static char *cf_stringtemp2;
541static int cf_stringtemp_l=0;
542CanonicalForm convertZZ2CF(ZZ coefficient)
543{
544  long coeff_long;
545  //CanonicalForm tmp=0;
546  char dummy[2];
547  int minusremainder=0;
548  char numbers[]="0123456789abcdef";
549
550  coeff_long=to_long(coefficient);
551
552  //Test whether coefficient can be represented as an immediate integer in Factory
553  if ( (NumBits(coefficient)<((long)NTL_ZZ_NBITS))
554  && (coeff_long>((long)MINIMMEDIATE))
555  && (coeff_long<((long)MAXIMMEDIATE)))
556  {
557    // coefficient is immediate --> return the coefficient as canonicalform
558    return CanonicalForm(coeff_long);
559  }
560  else
561  {
562    // coefficient is not immediate (gmp-number)
563    if (cf_stringtemp_l==0)
564    {
565      cf_stringtemp=(char *)Alloc(1023);
566      cf_stringtemp2=(char *)Alloc(1023);
567      cf_stringtemp[0]='\0';
568      cf_stringtemp2[0]='\0';
569      cf_stringtemp_l=1023;
570    }
571
572    // convert coefficient to char* (input for gmp)
573    dummy[1]='\0';
574
575    if (coefficient<0)
576    {
577      // negate coefficient, but store the sign in minusremainder
578      minusremainder=1;
579      coefficient=-coefficient;
580    }
581
582    int l=0;
583    while (coefficient>15)
584    {
585      ZZ quotient,remaind;
586      ZZ ten;ten=16;
587      DivRem(quotient,remaind,coefficient,ten);
588      dummy[0]=numbers[to_long(remaind)];
589      //tmp*=10; tmp+=to_long(remaind);
590
591      l++;
592      if (l>=cf_stringtemp_l-2)
593      {
594        Free(cf_stringtemp2,cf_stringtemp_l);
595        char *p=(char *)Alloc(cf_stringtemp_l*2);
596        //NTL_SNS
597        memcpy(p,cf_stringtemp,cf_stringtemp_l);
598        Free(cf_stringtemp,cf_stringtemp_l);
599        cf_stringtemp_l*=2;
600        cf_stringtemp=p;
601        cf_stringtemp2=(char *)Alloc(cf_stringtemp_l);
602      }
603      cf_stringtemp[l-1]=dummy[0];
604      cf_stringtemp[l]='\0';
605      //strcat(stringtemp,dummy);
606
607      coefficient=quotient;
608    }
609    //built up the string in dummy[0]
610    dummy[0]=numbers[to_long(coefficient)];
611    //NTL_SNS
612    l++;
613    cf_stringtemp[l-1]=dummy[0];
614    cf_stringtemp[l]='\0';
615    //tmp*=10; tmp+=to_long(coefficient);
616
617    if (minusremainder==1)
618    {
619      //Check whether coefficient has been negative at the start of the procedure
620      cf_stringtemp2[0]='-';
621      //tmp*=(-1);
622    }
623
624    //reverse the list to obtain the correct string
625    //NTL_SNS
626    for (int i=l-1;i>=0;i--) // l ist the position of \0
627    {
628      cf_stringtemp2[l-i-1+minusremainder]=cf_stringtemp[i];
629    }
630    cf_stringtemp2[l+minusremainder]='\0';
631  }
632
633  //convert the string to canonicalform using the char*-Constructor
634  return CanonicalForm(cf_stringtemp2,16);
635  //return tmp;
636}
637
638////////////////////////////////////////////////////////////////////////////////
639// NAME: convertFacCF2NTLZZX                                                  //
640//                                                                            //
641// DESCRIPTION:                                                               //
642// Routine for conversion of canonicalforms in Factory to polynomials         //
643// of type ZZX of NTL. To guarantee the correct execution of the              //
644// algorithm the characteristic has to equal zero.                            //
645//                                                                            //
646// INPUT:  The canonicalform that has to be converted                         //
647// OUTPUT: The converted NTL-polynom of type ZZX                              //
648////////////////////////////////////////////////////////////////////////////////
649
650ZZ convertFacCF2NTLZZ(const CanonicalForm f)
651{
652  ZZ temp;
653  if (f.isImm()) temp=f.intval();
654  else
655  {
656    //Coefficient is a gmp-number
657    mpz_t gmp_val;
658    char* stringtemp;
659
660    gmp_val[0]=getmpi(f.getval());
661    int l=mpz_sizeinbase(gmp_val,10)+2;
662    stringtemp=(char*)Alloc(l);
663    stringtemp=mpz_get_str(stringtemp,10,gmp_val);
664    mpz_clear(gmp_val);
665    conv(temp,stringtemp);
666    Free(stringtemp,l);
667  }
668  return temp;
669}
670
671ZZX convertFacCF2NTLZZX(CanonicalForm f)
672{
673    ZZX ntl_poly;
674
675    CFIterator i;
676    i=f;
677
678    int NTLcurrentExp=i.exp();
679    int largestExp=i.exp();
680    int k;
681
682    //set the length of the NTL-polynomial
683    ntl_poly.SetMaxLength(largestExp+1);
684
685    //Go through the coefficients of the canonicalform and build up the NTL-polynomial
686    for (;i.hasTerms();i++)
687    {
688      for (k=NTLcurrentExp;k>i.exp();k--)
689      {
690        SetCoeff(ntl_poly,k,0);
691      }
692      NTLcurrentExp=i.exp();
693
694      //Coefficient is a gmp-number
695      ZZ temp=convertFacCF2NTLZZ(i.coeff());
696
697      //set the computed coefficient
698      SetCoeff(ntl_poly,NTLcurrentExp,temp);
699
700      NTLcurrentExp--;
701    }
702    for (k=NTLcurrentExp;k>=0;k--)
703    {
704      SetCoeff(ntl_poly,k,0);
705    }
706
707    //normalize the polynomial
708    ntl_poly.normalize();
709
710    return ntl_poly;
711}
712
713////////////////////////////////////////////////////////////////////////////////
714// NAME: convertNTLvec_pair_ZZX_long2FacCFFList                               //
715//                                                                            //
716// DESCRIPTION:                                                               //
717// Routine for converting a vector of polynomials from ZZ to a list           //
718// CFFList of Factory. This routine will be used after a successful           //
719// factorization of NTL to convert the result back to Factory.                //
720// Additionally a variable x and the computed multiplicity, as a type         //
721// ZZ of NTL, is needed as parameters indicating the main variable of the     //
722// computed canonicalform and the multiplicity of the original polynomial.    //
723// To guarantee the correct execution of the algorithm the characteristic     //
724// has to equal zero.                                                         //
725//                                                                            //
726// INPUT:  A vector of polynomials over ZZ of type vec_pair_ZZX_long and      //
727//         a variable x and a multiplicity of type ZZ                         //
728// OUTPUT: The converted list of polynomials of type CFFList, all             //
729//         have x as their main variable                                      //
730////////////////////////////////////////////////////////////////////////////////
731
732CFFList convertNTLvec_pair_ZZX_long2FacCFFList(vec_pair_ZZX_long e,ZZ multi,Variable x)
733{
734  CFFList rueckgabe;
735  ZZX polynom;
736  long exponent;
737  CanonicalForm bigone;
738
739  // Go through the vector e and build up the CFFList
740  // As usual bigone summarizes the result
741  for (int i=e.length()-1;i>=0;i--)
742  {
743    ZZ coefficient;
744    polynom=e[i].a;
745    exponent=e[i].b;
746    bigone=convertNTLZZX2CF(polynom,x);
747    //append the converted polynomial to the list
748    rueckgabe.append(CFFactor(bigone,exponent));
749  }
750  // the multiplicity at pos 1
751  //if (!IsOne(multi))
752    rueckgabe.insert(CFFactor(convertZZ2CF(multi),1));
753
754  //return the converted list
755  return rueckgabe;
756}
757
758
759////////////////////////////////////////////////////////////////////////////////
760// NAME: convertNTLZZpX2CF                                                    //
761//                                                                            //
762// DESCRIPTION:                                                               //
763// Routine for conversion of elements of arbitrary extensions of ZZp,         //
764// having type ZZpE, of NTL to their corresponding values of type             //
765// canonicalform in Factory.                                                  //
766// To guarantee the correct execution of the algorithm the characteristic     //
767// has to be an arbitrary prime number and Factory has to compute in an       //
768// extension of F_p.                                                          //
769//                                                                            //
770// INPUT:  The coefficient of type ZZpE and the variable x indicating the main//
771//         variable of the computed canonicalform                             //
772// OUTPUT: The converted value of coefficient as type canonicalform           //
773////////////////////////////////////////////////////////////////////////////////
774
775CanonicalForm convertNTLZZpE2CF(ZZ_pE coefficient,Variable x)
776{
777  return convertNTLZZpX2CF(rep(coefficient),x);
778}
779CanonicalForm convertNTLzzpE2CF(zz_pE coefficient,Variable x)
780{
781  return convertNTLzzpX2CF(rep(coefficient),x);
782}
783
784////////////////////////////////////////////////////////////////////////////////
785// NAME: convertNTLvec_pair_ZZpEX_long2FacCFFList                             //
786//                                                                            //
787// DESCRIPTION:                                                               //
788// Routine for converting a vector of polynomials from ZZpEX to a CFFList     //
789// of Factory. This routine will be used after a successful factorization     //
790// of NTL to convert the result back to Factory.                              //
791// Additionally a variable x and the computed multiplicity, as a type         //
792// ZZpE of NTL, is needed as parameters indicating the main variable of the   //
793// computed canonicalform and the multiplicity of the original polynomial.    //
794// To guarantee the correct execution of the algorithm the characteristic     //
795// has a be an arbitrary prime number p and computations have to be done      //
796// in an extention of F_p.                                                    //
797//                                                                            //
798// INPUT:  A vector of polynomials over ZZpE of type vec_pair_ZZ_pEX_long and //
799//         a variable x and a multiplicity of type ZZpE                       //
800// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
801//         have x as their main variable                                      //
802////////////////////////////////////////////////////////////////////////////////
803
804CFFList convertNTLvec_pair_ZZpEX_long2FacCFFList(vec_pair_ZZ_pEX_long e,ZZ_pE multi,Variable x,Variable alpha)
805{
806  CFFList rueckgabe;
807  ZZ_pEX polynom;
808  long exponent;
809  CanonicalForm bigone;
810
811  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
812  //important for the factorization, but nevertheless would take computing time, so it is omitted
813
814  // Go through the vector e and build up the CFFList
815  // As usual bigone summarizes the result during every loop
816  for (int i=e.length()-1;i>=0;i--)
817  {
818    bigone=0;
819
820    polynom=e[i].a;
821    exponent=e[i].b;
822
823    for (int j=0;j<=deg(polynom);j++)
824    {
825      if (IsOne(coeff(polynom,j)))
826      {
827        bigone+=power(x,j);
828      }
829      else
830      {
831        CanonicalForm coefficient=convertNTLZZpE2CF(coeff(polynom,j),alpha);
832        if (coeff(polynom,j)!=0)
833        {
834          bigone += (power(x,j)*coefficient);
835        }
836      }
837    }
838    //append the computed polynomials together with its exponent to the CFFList
839    rueckgabe.append(CFFactor(bigone,exponent));
840  }
841  // Start by appending the multiplicity
842  if (!IsOne(multi))
843    rueckgabe.insert(CFFactor(convertNTLZZpE2CF(multi,alpha),1));
844
845  //return the computed CFFList
846  return rueckgabe;
847}
848CFFList convertNTLvec_pair_zzpEX_long2FacCFFList(vec_pair_zz_pEX_long e,zz_pE multi,Variable x,Variable alpha)
849{
850  CFFList rueckgabe;
851  zz_pEX polynom;
852  long exponent;
853  CanonicalForm bigone;
854
855  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
856  //important for the factorization, but nevertheless would take computing time, so it is omitted
857
858  // Go through the vector e and build up the CFFList
859  // As usual bigone summarizes the result during every loop
860  for (int i=e.length()-1;i>=0;i--)
861  {
862    bigone=0;
863
864    polynom=e[i].a;
865    exponent=e[i].b;
866
867    for (int j=0;j<=deg(polynom);j++)
868    {
869      if (IsOne(coeff(polynom,j)))
870      {
871        bigone+=power(x,j);
872      }
873      else
874      {
875        CanonicalForm coefficient=convertNTLzzpE2CF(coeff(polynom,j),alpha);
876        if (coeff(polynom,j)!=0)
877        {
878          bigone += (power(x,j)*coefficient);
879        }
880      }
881    }
882    //append the computed polynomials together with its exponent to the CFFList
883    rueckgabe.append(CFFactor(bigone,exponent));
884  }
885  // Start by appending the multiplicity
886  if (!IsOne(multi))
887    rueckgabe.insert(CFFactor(convertNTLzzpE2CF(multi,alpha),1));
888
889  //return the computed CFFList
890  return rueckgabe;
891}
892
893////////////////////////////////////////////////////////////////////////////////
894// NAME: convertNTLGF2E2CF                                                    //
895//                                                                            //
896// DESCRIPTION:                                                               //
897// Routine for conversion of elements of extensions of GF2, having type       //
898// GF2E, of NTL to their corresponding values of type canonicalform in        //
899// Factory.                                                                   //
900// To guarantee the correct execution of the algorithm, the characteristic    //
901// must equal two and Factory has to compute in an extension of F_2.          //
902// As usual this is an optimized special case of the more general conversion  //
903// routine from ZZpE to Factory.                                              //
904//                                                                            //
905// INPUT:  The coefficient of type GF2E and the variable x indicating the     //
906//         main variable of the computed canonicalform                        //
907// OUTPUT: The converted value of coefficient as type canonicalform           //
908////////////////////////////////////////////////////////////////////////////////
909
910CanonicalForm convertNTLGF2E2CF(GF2E coefficient,Variable x)
911{
912  return convertNTLGF2X2CF(rep(coefficient),x);
913}
914
915////////////////////////////////////////////////////////////////////////////////
916// NAME: convertNTLvec_pair_GF2EX_long2FacCFFList                             //
917//                                                                            //
918// DESCRIPTION:                                                               //
919// Routine for converting a vector of polynomials from GF2EX to a CFFList     //
920// of Factory. This routine will be used after a successful factorization     //
921// of NTL to convert the result back to Factory.                              //
922// This is a special, but optimized case of the more general conversion       //
923// from ZZpE to canonicalform.                                                //
924// Additionally a variable x and the computed multiplicity, as a type GF2E    //
925// of NTL, is needed as parameters indicating the main variable of the        //
926// computed canonicalform and the multiplicity of the original polynomial.    //
927// To guarantee the correct execution of the algorithm the characteristic     //
928// has to equal two and computations have to be done in an extention of F_2.  //
929//                                                                            //
930// INPUT:  A vector of polynomials over GF2E of type vec_pair_GF2EX_long and  //
931//         a variable x and a multiplicity of type GF2E                       //
932// OUTPUT: The converted list of polynomials of type CFFList, all polynomials //
933//         have x as their main variable                                      //
934////////////////////////////////////////////////////////////////////////////////
935
936CFFList convertNTLvec_pair_GF2EX_long2FacCFFList(vec_pair_GF2EX_long e,GF2E multi,Variable x,Variable alpha)
937{
938  CFFList rueckgabe;
939  GF2EX polynom;
940  long exponent;
941  CanonicalForm bigone;
942
943  // Maybe, e may additionally be sorted with respect to increasing degree of x, but this is not
944  //important for the factorization, but nevertheless would take computing time, so it is omitted
945
946  // multiplicity is always one, so we do not have to worry about that
947
948  // Go through the vector e and build up the CFFList
949  // As usual bigone summarizes the result during every loop
950  for (int i=e.length()-1;i>=0;i--)
951  {
952    bigone=0;
953
954    polynom=e[i].a;
955    exponent=e[i].b;
956
957    for (int j=0;j<=deg(polynom);j++)
958    {
959      if (IsOne(coeff(polynom,j)))
960      {
961        bigone+=power(x,j);
962      }
963      else
964      {
965        CanonicalForm coefficient=convertNTLGF2E2CF(coeff(polynom,j),alpha);
966        if (coeff(polynom,j)!=0)
967        {
968          bigone += (power(x,j)*coefficient);
969        }
970      }
971    }
972
973    // append the computed polynomial together with its multiplicity
974    rueckgabe.append(CFFactor(bigone,exponent));
975
976  }
977  // return the computed CFFList
978  return rueckgabe;
979}
980
981////////////////////////////////////////////////////
982// CanonicalForm in Z_2(a)[X] to NTL GF2EX        //
983////////////////////////////////////////////////////
984GF2EX convertFacCF2NTLGF2EX(CanonicalForm f,GF2X mipo)
985{
986  GF2E::init(mipo);
987  GF2EX result;
988  CFIterator i;
989  i=f;
990
991  int NTLcurrentExp=i.exp();
992  int largestExp=i.exp();
993  int k;
994
995  result.SetMaxLength(largestExp+1);
996  for(;i.hasTerms();i++)
997  {
998    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
999    NTLcurrentExp=i.exp();
1000    CanonicalForm c=i.coeff();
1001    GF2X cc=convertFacCF2NTLGF2X(c);
1002    //ZZ_pE ccc;
1003    //conv(ccc,cc);
1004    SetCoeff(result,NTLcurrentExp,to_GF2E(cc));
1005    NTLcurrentExp--;
1006  }
1007  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1008  result.normalize();
1009  return result;
1010}
1011////////////////////////////////////////////////////
1012// CanonicalForm in Z_p(a)[X] to NTL ZZ_pEX       //
1013////////////////////////////////////////////////////
1014ZZ_pEX convertFacCF2NTLZZ_pEX(CanonicalForm f, ZZ_pX mipo)
1015{
1016  ZZ_pE::init(mipo);
1017  ZZ_pEX result;
1018  CFIterator i;
1019  i=f;
1020
1021  int NTLcurrentExp=i.exp();
1022  int largestExp=i.exp();
1023  int k;
1024
1025  result.SetMaxLength(largestExp+1);
1026  for(;i.hasTerms();i++)
1027  {
1028    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1029    NTLcurrentExp=i.exp();
1030    CanonicalForm c=i.coeff();
1031    ZZ_pX cc=convertFacCF2NTLZZpX(c);
1032    //ZZ_pE ccc;
1033    //conv(ccc,cc);
1034    SetCoeff(result,NTLcurrentExp,to_ZZ_pE(cc));
1035    NTLcurrentExp--;
1036  }
1037  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1038  result.normalize();
1039  return result;
1040}
1041zz_pEX convertFacCF2NTLzz_pEX(CanonicalForm f, zz_pX mipo)
1042{
1043  zz_pE::init(mipo);
1044  zz_pEX result;
1045  CFIterator i;
1046  i=f;
1047
1048  int NTLcurrentExp=i.exp();
1049  int largestExp=i.exp();
1050  int k;
1051
1052  result.SetMaxLength(largestExp+1);
1053  for(;i.hasTerms();i++)
1054  {
1055    for(k=NTLcurrentExp;k>i.exp();k--) SetCoeff(result,k,0);
1056    NTLcurrentExp=i.exp();
1057    CanonicalForm c=i.coeff();
1058    zz_pX cc=convertFacCF2NTLzzpX(c);
1059    //ZZ_pE ccc;
1060    //conv(ccc,cc);
1061    SetCoeff(result,NTLcurrentExp,to_zz_pE(cc));
1062    NTLcurrentExp--;
1063  }
1064  for(k=NTLcurrentExp;k>=0;k--) SetCoeff(result,k,0);
1065  result.normalize();
1066  return result;
1067}
1068
1069CanonicalForm convertNTLzz_pEX2CF (zz_pEX f, Variable x, Variable alpha)
1070{
1071  CanonicalForm bigone;
1072  if (deg (f) > 0)
1073  {
1074    bigone= 0;
1075    bigone.mapinto();
1076    for (int j=0;j<deg(f)+1;j++)
1077    {
1078      if (coeff(f,j)!=0)
1079      {
1080        bigone+=(power(x,j)*convertNTLzzpE2CF(coeff(f,j),alpha));
1081      }
1082    }
1083  }
1084  else
1085  {
1086    bigone= convertNTLzzpE2CF(coeff(f,0),alpha);
1087    bigone.mapinto();
1088  }
1089  return bigone;
1090}
1091//----------------------------------------------------------------------
1092mat_ZZ* convertFacCFMatrix2NTLmat_ZZ(CFMatrix &m)
1093{
1094  mat_ZZ *res=new mat_ZZ;
1095  res->SetDims(m.rows(),m.columns());
1096
1097  int i,j;
1098  for(i=m.rows();i>0;i--)
1099  {
1100    for(j=m.columns();j>0;j--)
1101    {
1102      (*res)(i,j)=convertFacCF2NTLZZ(m(i,j));
1103    }
1104  }
1105  return res;
1106}
1107CFMatrix* convertNTLmat_ZZ2FacCFMatrix(mat_ZZ &m)
1108{
1109  CFMatrix *res=new CFMatrix(m.NumRows(),m.NumCols());
1110  int i,j;
1111  for(i=res->rows();i>0;i--)
1112  {
1113    for(j=res->columns();j>0;j--)
1114    {
1115      (*res)(i,j)=convertZZ2CF(m(i,j));
1116    }
1117  }
1118  return res;
1119}
1120
1121mat_zz_p* convertFacCFMatrix2NTLmat_zz_p(CFMatrix &m)
1122{
1123  mat_zz_p *res=new mat_zz_p;
1124  res->SetDims(m.rows(),m.columns());
1125
1126  int i,j;
1127  for(i=m.rows();i>0;i--)
1128  {
1129    for(j=m.columns();j>0;j--)
1130    {
1131      if(!(m(i,j)).isImm()) printf("convertFacCFMatrix2NTLmat_zz_p: not imm.\n");
1132      (*res)(i,j)=(m(i,j)).intval();
1133    }
1134  }
1135  return res;
1136}
1137CFMatrix* convertNTLmat_zz_p2FacCFMatrix(mat_zz_p &m)
1138{
1139  CFMatrix *res=new CFMatrix(m.NumRows(),m.NumCols());
1140  int i,j;
1141  for(i=res->rows();i>0;i--)
1142  {
1143    for(j=res->columns();j>0;j--)
1144    {
1145      (*res)(i,j)=CanonicalForm(to_long(rep(m(i,j))));
1146    }
1147  }
1148  return res;
1149}
1150mat_zz_pE* convertFacCFMatrix2NTLmat_zz_pE(CFMatrix &m)
1151{
1152  mat_zz_pE *res=new mat_zz_pE;
1153  res->SetDims(m.rows(),m.columns());
1154
1155  int i,j;
1156  for(i=m.rows();i>0;i--)
1157  {
1158    for(j=m.columns();j>0;j--)
1159    {
1160      zz_pX cc=convertFacCF2NTLzzpX(m(i,j));
1161      (*res)(i,j)=to_zz_pE(cc);
1162    }
1163  }
1164  return res;
1165}
1166CFMatrix* convertNTLmat_zz_pE2FacCFMatrix(mat_zz_pE &m, Variable alpha)
1167{
1168  CFMatrix *res=new CFMatrix(m.NumRows(),m.NumCols());
1169  int i,j;
1170  for(i=res->rows();i>0;i--)
1171  {
1172    for(j=res->columns();j>0;j--)
1173    {
1174      (*res)(i,j)=convertNTLzzpE2CF(m(i,j), alpha);
1175    }
1176  }
1177  return res;
1178}
1179#endif
Note: See TracBrowser for help on using the repository browser.