source: git/factory/NTLconvert.cc @ c6eecb

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