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

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