source: git/factory/NTLconvert.cc @ d45ad9

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