source: git/factory/NTLconvert.cc @ f5d2963

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