source: git/factory/NTLconvert.cc @ 899d4c

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