source: git/factory/NTLconvert.cc @ 958fc4

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