source: git/factory/NTLconvert.cc @ 4d50d8c

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