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

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