source: git/libpolys/coeffs/ffields.cc @ dc4782

spielwiese
Last change on this file since dc4782 was dc4782, checked in by Hans Schoenemann <hannes@…>, 10 years ago
chg: factory/libfac is not optional, removing HAVE_FACTORY/HAVE_LIBFAC
  • Property mode set to 100644
File size: 19.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: finite fields with a none-prime number of elements (via tables)
6*/
7
8#ifdef HAVE_CONFIG_H
9#include "libpolysconfig.h"
10#endif /* HAVE_CONFIG_H */
11
12#include <omalloc/omalloc.h>
13
14#include <misc/auxiliary.h>
15#include <misc/mylimits.h>
16
17#include <reporter/reporter.h>
18
19#include <coeffs/coeffs.h>
20#include <coeffs/numbers.h>
21#include <coeffs/ffields.h>
22#include <coeffs/longrat.h>
23
24#include <string.h>
25#include <math.h>
26#include <errno.h>
27
28BOOLEAN nfGreaterZero (number k, const coeffs r);
29number  nfMult        (number a, number b, const coeffs r);
30number  nfInit        (long i, const coeffs r);
31number  nfParameter   (int i, const coeffs r);
32int     nfInt         (number &n, const coeffs r);
33number  nfAdd         (number a, number b, const coeffs r);
34number  nfSub         (number a, number b, const coeffs r);
35void    nfPower       (number a, int i, number * result, const coeffs r);
36BOOLEAN nfIsZero      (number a, const coeffs r);
37BOOLEAN nfIsOne       (number a, const coeffs r);
38BOOLEAN nfIsMOne      (number a, const coeffs r);
39number  nfDiv         (number a, number b, const coeffs r);
40number  nfNeg         (number c, const coeffs r);
41number  nfInvers      (number c, const coeffs r);
42BOOLEAN nfGreater     (number a, number b, const coeffs r);
43BOOLEAN nfEqual       (number a, number b, const coeffs r);
44const char *  nfRead  (const char *s, number *a, const coeffs r);
45#ifdef LDEBUG
46BOOLEAN nfDBTest      (number a, const char *f, const int l, const coeffs r);
47#endif
48//void    nfSetChar     (const coeffs r);
49
50nMapFunc nfSetMap     (const coeffs src, const coeffs dst);
51char *  nfName        (number n, const coeffs r);
52void    nfReadTable   (const int c, const coeffs r);
53
54void    nfCoeffWrite(const coeffs r, BOOLEAN details);
55void    nfShowMipo(const coeffs r);
56
57
58
59/// Our Type!
60static const n_coeffType ID = n_GF;
61
62//unsigned short *nfPlus1Table=NULL; /* the table i=log(z^i) -> log(z^i+1) */
63
64const double sixteenlog2= 11.09035489;
65/* the q's from the table 'fftable' */
66unsigned short fftable[]={
67    4,  8, 16, 32, 64, 128, 256, 512,1024,2048,4096,8192,16384, 32768,
68/*2^2 2^3 2^4 2^5 2^6  2^7  2^8  2^9 2^10 2^11 2^12 2^13  2^14  2^15*/
69    9, 27, 81,243,729,2187, 6561,19683,59049,
70/*3^2 3^3 3^4 3^5 3^6  3^7  3^8   3^9  3^10*/
71   25,125,625,3125,15625,
72/*5^2 5^3 5^4 5^5  5^6*/
73   49,343,2401,16807,
74/*7^2 7^3  7^4 7^5*/
75   121,1331, 14641,
76/*11^2 11^3  11^4*/
77  169, 2197, 28561,
78/*13^2 13^3  13^4*/
79  289, 4913,
80/*17^2 17^3*/
81  361, 6859,
82/*19^2 19^3*/
83  529, 12167,
84/*23^2 23^3*/
85  841, 24389,
86/*29^2 29^3*/
87  961, 29791,
88/*31^2 31^3*/
89  1369, 50653,
90/*37^2  37^3*/
91  1681, /*41^2*/
92  1849, /*43^2*/
93  2209, /*47^2*/
94  2809, /*53^2*/
95  3481, /*59^2*/
96  3721, /*61^2*/
97  4489, /*67^2*/
98  5041, /*71^2*/
99  5329, /*73^2*/
100  6241, /*79^2*/
101  6889, /*83^2*/
102  7921, /*89^2*/
103  9409, /*97^2*/
104  10201, /*101^2*/
105  10609, /*103^2*/
106  11449, /*107^2*/
107  11881, /*109^2*/
108  12769, /*113^2*/
109  16129, /*127^2*/
110  17161, /*131^2*/
111  18769, /*137^2*/
112  19321, /*139^2*/
113  22201, /*149^2*/
114  22801, /*151^2*/
115  24649, /*157^2*/
116  26569, /*163^2*/
117  27889, /*167^2*/
118  29929, /*173^2*/
119  32041, /*179^2*/
120  32761, /*181^2*/
121  36481, /*191^2*/
122  37249, /*193^2*/
123  38809, /*197^2*/
124  39601, /*199^2*/
125  49729, /*223^2*/
126  44521, /*211^2*/
127  51529, /*227^2*/
128  52441, /*229^2*/
129  54289, /*233^2*/
130  57121, /*239^2*/
131  58081, /*241^2*/
132  63001, /*251^2*/
133  0 };
134
135/*1
136* numbers in GF(p^n):
137* let nfCharQ=q=nfCharP^n=p^n
138* GF(q)\{0} will be generated by powers of an element Z
139* Z^i will be represented by the int i, 1 by the int 0, 0 by the int q=nfChar
140*/
141
142#ifdef LDEBUG
143/*2
144* debugging: is a a valid representation of a number ?
145*/
146BOOLEAN nfDBTest (number a, const char *f, const int l, const coeffs r)
147{
148  assume( r->m_nfPlus1Table != NULL );
149  if (((long)a<0L) || ((long)a>(long)r->m_nfCharQ))
150  {
151    Print("wrong %d in %s:%d\n",(int)((long)a),f,l);
152    return FALSE;
153  }
154  int i=0;
155  do
156  {
157    if (r->m_nfPlus1Table[i]>r->m_nfCharQ)
158    {
159      Print("wrong table %d=%d in %s:%d\n",i,r->m_nfPlus1Table[i],f,l);
160      return FALSE;
161    }
162    i++;
163  } while (i<r->m_nfCharQ);
164  return TRUE;
165}
166#define nfTest(N, R) nfDBTest(N,__FILE__,__LINE__, R)
167#endif
168
169/*2
170* k >= 0 ?
171*/
172BOOLEAN nfGreaterZero (number k, const coeffs r)
173{
174#ifdef LDEBUG
175  nfTest(k, r);
176#endif
177  return !nfIsZero(k, r) && !nfIsMOne(k, r);
178}
179
180/*2
181* a*b
182*/
183number nfMult (number a,number b, const coeffs r)
184{
185#ifdef LDEBUG
186  nfTest(a, r);
187  nfTest(b, r);
188#endif
189  if (((long)a == (long)r->m_nfCharQ) || ((long)b == (long)r->m_nfCharQ))
190    return (number)(long)r->m_nfCharQ;
191  /*else*/
192  int i=(int)((long)a+(long)b);
193  if (i>=r->m_nfCharQ1) i-=r->m_nfCharQ1;
194#ifdef LDEBUG
195  nfTest((number)(long)i, r);
196#endif
197  return (number)(long)i;
198}
199
200/*2
201* int -> number
202*/
203number nfInit (long i, const coeffs r)
204{
205  assume( r->m_nfPlus1Table != NULL );
206  // Hmm .. this is just to prevent initialization
207  // from nfInitChar to go into an infinite loop
208  if (i==0) return (number)(long)r->m_nfCharQ;
209  while (i <  0)    i += r->m_nfCharP;
210  while (i >= r->m_nfCharP) i -= r->m_nfCharP;
211  if (i==0) return (number)(long)r->m_nfCharQ;
212  unsigned short c=0;
213  while (i>1)
214  {
215    c=r->m_nfPlus1Table[c];
216    i--;
217  }
218#ifdef LDEBUG
219  nfTest((number)(long)c, r);
220#endif
221  return (number)(long)c;
222}
223
224/*
225* the generating element `z`
226*/
227number nfParameter (int i, const coeffs)
228{
229  assume(i==1);
230
231  if( i == 1 )
232    return (number)1;
233
234  return NULL;
235}
236
237/*2
238* the degree of the "alg. number"
239*/
240static int nfParDeg(number n, const coeffs r)
241{
242#ifdef LDEBUG
243  nfTest(n, r);
244#endif
245  if((long)r->m_nfCharQ == (long)n) return -1;
246  return (int)((long)n);
247}
248
249/*2
250* number -> int
251*/
252int nfInt (number &, const coeffs )
253{
254  return 0;
255}
256
257/*2
258* a + b
259*/
260number nfAdd (number a, number b, const coeffs R)
261{
262/*4 z^a+z^b=z^b*(z^(a-b)+1), if a>=b; *
263*          =z^a*(z^(b-a)+1)  if a<b  */
264#ifdef LDEBUG
265  nfTest(a, R);
266  nfTest(b, R);
267#endif
268  if ((long)R->m_nfCharQ == (long)a) return b;
269  if ((long)R->m_nfCharQ == (long)b) return a;
270  long zb,zab,r;
271  if ((long)a >= (long)b)
272  {
273    zb = (long)b;
274    zab = (long)a-(long)b;
275  }
276  else
277  {
278    zb = (long)a;
279    zab = (long)b-(long)a;
280  }
281#ifdef LDEBUG
282  nfTest((number)zab, R);
283#endif
284  if (R->m_nfPlus1Table[zab]==R->m_nfCharQ) r=(long)R->m_nfCharQ; /*if z^(a-b)+1 =0*/
285  else
286  {
287    r= zb+(long)R->m_nfPlus1Table[zab];
288    if(r>=(long)R->m_nfCharQ1) r-=(long)R->m_nfCharQ1;
289  }
290#ifdef LDEBUG
291  nfTest((number)r, R);
292#endif
293  return (number)r;
294}
295
296/*2
297* a - b
298*/
299number nfSub (number a, number b, const coeffs r)
300{
301  number mb = nfNeg(b, r);
302  return nfAdd(a,mb,r);
303}
304
305/*2
306* a == 0 ?
307*/
308BOOLEAN nfIsZero (number  a, const coeffs r)
309{
310#ifdef LDEBUG
311  nfTest(a, r);
312#endif
313  return (long)r->m_nfCharQ == (long)a;
314}
315
316/*2
317* a == 1 ?
318*/
319BOOLEAN nfIsOne (number a, const coeffs r)
320{
321#ifdef LDEBUG
322  nfTest(a, r);
323#endif
324  return 0L == (long)a;
325}
326
327/*2
328* a == -1 ?
329*/
330BOOLEAN nfIsMOne (number a, const coeffs r)
331{
332#ifdef LDEBUG
333  nfTest(a, r);
334#endif
335  if (0L == (long)a) return FALSE; /* special handling of char 2*/
336  return (long)r->m_nfM1 == (long)a;
337}
338
339/*2
340* a / b
341*/
342number nfDiv (number a,number b, const coeffs r)
343{
344#ifdef LDEBUG
345  nfTest(b, r);
346#endif
347  if ((long)b==(long)r->m_nfCharQ)
348  {
349    WerrorS(nDivBy0);
350    return (number)((long)r->m_nfCharQ);
351  }
352#ifdef LDEBUG
353  nfTest(a, r);
354#endif
355  if ((long)a==(long)r->m_nfCharQ)
356    return (number)((long)r->m_nfCharQ);
357  /*else*/
358  long s = (long)a - (long)b;
359  if (s < 0L)
360    s += (long)r->m_nfCharQ1;
361#ifdef LDEBUG
362  nfTest((number)s, r);
363#endif
364  return (number)s;
365}
366
367/*2
368* 1 / c
369*/
370number  nfInvers (number c, const coeffs r)
371{
372#ifdef LDEBUG
373  nfTest(c, r);
374#endif
375  if ((long)c==(long)r->m_nfCharQ)
376  {
377    WerrorS(nDivBy0);
378    return (number)((long)r->m_nfCharQ);
379  }
380#ifdef LDEBUG
381  nfTest(((number)((long)r->m_nfCharQ1-(long)c)), r);
382#endif
383  return (number)((long)r->m_nfCharQ1-(long)c);
384}
385
386/*2
387* -c
388*/
389number nfNeg (number c, const coeffs r)
390{
391/*4 -z^c=z^c*(-1)=z^c*nfM1*/
392#ifdef LDEBUG
393  nfTest(c, r);
394#endif
395  if ((long)r->m_nfCharQ == (long)c) return c;
396  long i=(long)c+(long)r->m_nfM1;
397  if (i>=(long)r->m_nfCharQ1) i-=(long)r->m_nfCharQ1;
398#ifdef LDEBUG
399  nfTest((number)i, r);
400#endif
401  return (number)i;
402}
403
404/*2
405* a > b ?
406*/
407BOOLEAN nfGreater (number a,number b, const coeffs r)
408{
409#ifdef LDEBUG
410  nfTest(a, r);
411  nfTest(b, r);
412#endif
413  return (long)a != (long)b;
414}
415
416/*2
417* a == b ?
418*/
419BOOLEAN nfEqual (number a,number b, const coeffs r)
420{
421#ifdef LDEBUG
422  nfTest(a, r);
423  nfTest(b, r);
424#endif
425  return (long)a == (long)b;
426}
427
428/*2
429* write via StringAppend
430*/
431static void nfWriteLong (number &a, const coeffs r)
432{
433#ifdef LDEBUG
434  nfTest(a, r);
435#endif
436  if ((long)a==(long)r->m_nfCharQ)  StringAppendS("0");
437  else if ((long)a==0L)   StringAppendS("1");
438  else if (nfIsMOne(a, r))   StringAppendS("-1");
439  else
440  {
441    StringAppendS(n_ParameterNames(r)[0]);
442    if ((long)a!=1L)
443    {
444      StringAppend("^%d",(int)((long)a)); // long output!
445    }
446  }
447}
448
449
450/*2
451* write (shortert output) via StringAppend
452*/
453static void nfWriteShort (number &a, const coeffs r)
454{
455#ifdef LDEBUG
456  nfTest(a, r);
457#endif
458  if ((long)a==(long)r->m_nfCharQ)  StringAppendS("0");
459  else if ((long)a==0L)   StringAppendS("1");
460  else if (nfIsMOne(a, r))   StringAppendS("-1");
461  else
462  {
463    StringAppendS(n_ParameterNames(r)[0]);
464    if ((long)a!=1L)
465    {
466      StringAppend("%d",(int)((long)a));
467    }
468  }
469}
470
471/*2
472*
473*/
474char * nfName(number a, const coeffs r)
475{
476#ifdef LDEBUG
477  nfTest(a, r);
478#endif
479  char *s;
480  const char * const nf_Parameter=n_ParameterNames(r)[0];
481  if (((long)a==(long)r->m_nfCharQ) || ((long)a==0L)) return NULL;
482  else if ((long)a==1L)
483  {
484    return omStrDup(nf_Parameter);
485  }
486  else
487  {
488    s=(char *)omAlloc(4+strlen(nf_Parameter));
489    sprintf(s,"%s%d",nf_Parameter,(int)((long)a));
490  }
491  return s;
492}
493/*2
494* c ^ i with i>=0
495*/
496void nfPower (number a, int i, number * result, const coeffs r)
497{
498#ifdef LDEBUG
499  nfTest(a, r);
500#endif
501  if (i==0)
502  {
503    //*result=nfInit(1);
504    *result = (number)0L;
505  }
506  else if (i==1)
507  {
508    *result = a;
509  }
510  else
511  {
512    nfPower(a,i-1,result, r);
513    *result = nfMult(a,*result, r);
514  }
515#ifdef LDEBUG
516  nfTest(*result, r);
517#endif
518}
519
520/*4
521* read an integer (with reduction mod p)
522*/
523static const char* nfEati(const char *s, int *i, const coeffs r)
524{
525  if (*s >= '0' && *s <= '9')
526  {
527    *i = 0;
528    do
529    {
530      *i *= 10;
531      *i += *s++ - '0';
532      if (*i > (MAX_INT_VAL / 10)) *i = *i % r->m_nfCharP;
533    }
534    while (*s >= '0' && *s <= '9');
535    if (*i >= r->m_nfCharP) *i = *i % r->m_nfCharP;
536  }
537  else *i = 1;
538  return s;
539}
540
541/*2
542* read a number
543*/
544const char * nfRead (const char *s, number *a, const coeffs r)
545{
546  int i;
547  number z;
548  number n;
549
550  s = nfEati(s, &i, r);
551  z=nfInit(i, r);
552  *a=z;
553  if (*s == '/')
554  {
555    s++;
556    s = nfEati(s, &i, r);
557    n=nfInit(i, r);
558    *a = nfDiv(z,n,r);
559  }
560  const char * const nf_Parameter = n_ParameterNames(r)[0];
561  const int N = strlen(nf_Parameter);
562  if (strncmp(s,nf_Parameter, N)==0)
563  {
564    s += N;
565    if ((*s >= '0') && (*s <= '9'))
566    {
567      s=eati(s,&i);
568      while (i>=r->m_nfCharQ1) i-=r->m_nfCharQ1;
569    }
570    else
571      i=1;
572    z=(number)(long)i;
573    *a=nfMult(*a,z,r);
574  }
575#ifdef LDEBUG
576  nfTest(*a, r);
577#endif
578  return s;
579}
580
581int gf_tab_numdigits62 ( int q );
582int convertback62 ( char * p, int n );
583
584int nfMinPoly[16];
585
586void nfShowMipo(const coeffs r)
587{
588  int i=nfMinPoly[0];
589  int j=0;
590  loop
591  {
592    j++;
593    if (nfMinPoly[j]!=0)
594      StringAppend("%d*%s^%d",nfMinPoly[j],n_ParameterNames(r)[0],i);
595    i--;
596    if(i<0) break;
597    if (nfMinPoly[j]!=0)
598      StringAppendS("+");
599  }
600}
601
602static void nfReadMipo(char *s)
603{
604  const char *l=strchr(s,';')+1;
605  char *n;
606  int i=strtol(l,&n,10);
607  l=n;
608  int j=1;
609  nfMinPoly[0]=i;
610  while(i>=0)
611  {
612    nfMinPoly[j]=strtol(l,&n,10);
613    if (l==n) break;
614    l=n;
615    j++;
616    i--;
617  }
618  if (i>=0)
619  {
620    WerrorS("error in reading minpoly from gftables");
621  }
622}
623
624/*2
625* init global variables from files 'gftables/%d'
626*/
627void nfReadTable(const int c, const coeffs r)
628{
629  //Print("GF(%d)\n",c);
630  if ((c==r->m_nfCharQ)||(c==-r->m_nfCharQ))
631    /*this field is already set*/  return;
632  int i=0;
633
634  while ((fftable[i]!=c) && (fftable[i]!=0))
635    i++;
636
637  if (fftable[i]==0)
638  {
639#ifndef NDEBUG
640    Warn("illegal GF-table size: %d", c);
641#endif
642    return;
643  }
644
645  if (r->m_nfCharQ > 1)
646  {
647    omFreeSize( (ADDRESS)r->m_nfPlus1Table,r->m_nfCharQ*sizeof(unsigned short) );
648    r->m_nfPlus1Table=NULL;
649  }
650  if ((c>1) || (c<0))
651  {
652    if (c>1) r->m_nfCharQ = c;
653    else     r->m_nfCharQ = -c;
654    char buf[100];
655    sprintf(buf,"gftables/%d",r->m_nfCharQ);
656    FILE * fp = feFopen(buf,"r",NULL,TRUE);
657    if (fp==NULL)
658    {
659      return;
660    }
661    if(!fgets( buf, sizeof(buf), fp)) return;
662    if(strcmp(buf,"@@ factory GF(q) table @@\n")!=0)
663    {
664      goto err;
665    }
666    if(!fgets( buf, sizeof(buf), fp))
667    {
668      goto err;
669    }
670    int q;
671    int res = -1;
672    do
673    {
674      res = sscanf(buf,"%d %d",&r->m_nfCharP,&q);
675    }
676    while((res < 0) and (errno == EINTR));
677
678    nfReadMipo(buf);
679    r->m_nfCharQ1=r->m_nfCharQ-1;
680    //Print("nfCharQ=%d,nfCharQ1=%d,mipo=>>%s<<\n",nfCharQ,nfCharQ1,buf);
681    r->m_nfPlus1Table= (unsigned short *)omAlloc( (r->m_nfCharQ)*sizeof(unsigned short) );
682    int digs = gf_tab_numdigits62( r->m_nfCharQ );
683    char * bufptr;
684    int i = 1;
685    int k;
686    while ( i < r->m_nfCharQ )
687    {
688      fgets( buf, sizeof(buf), fp);
689      //( strlen( buffer ) == (size_t)digs * 30, "illegal table" );
690      bufptr = buf;
691      k = 0;
692      while ( (i < r->m_nfCharQ) && (k < 30) )
693      {
694        r->m_nfPlus1Table[i] = convertback62( bufptr, digs );
695        if(r->m_nfPlus1Table[i]>r->m_nfCharQ)
696        {
697          Print("wrong entry %d: %d(%c%c%c)\n",i,r->m_nfPlus1Table[i],bufptr[0],bufptr[1],bufptr[2]);
698        }
699        bufptr += digs;
700        if (r->m_nfPlus1Table[i]==r->m_nfCharQ)
701        {
702          if(i==r->m_nfCharQ1)
703          {
704            r->m_nfM1=0;
705          }
706          else
707          {
708            r->m_nfM1=i;
709          }
710        }
711        i++; k++;
712      }
713    }
714    r->m_nfPlus1Table[0]=r->m_nfPlus1Table[r->m_nfCharQ1];
715  }
716  else
717    r->m_nfCharQ=0;
718#ifdef LDEBUG
719  nfTest((number)0, r);
720#endif
721  return;
722err:
723  Werror("illegal GF-table %d",r->m_nfCharQ);
724}
725
726/*2
727* map Z/p -> GF(p,n)
728*/
729number nfMapP(number c, const coeffs, const coeffs dst)
730{
731  return nfInit((int)((long)c), dst);
732}
733
734/*2
735* map GF(p,n1) -> GF(p,n2), n1 < n2, n1 | n2
736*/
737int nfMapGG_factor;
738number nfMapGG(number c, const coeffs src, const coeffs)
739{
740  int i=(long)c;
741  i*= nfMapGG_factor;
742  while (i >src->m_nfCharQ1) i-=src->m_nfCharQ1;
743  return (number)((long)i);
744}
745/*2
746* map GF(p,n1) -> GF(p,n2), n1 > n2, n2 | n1
747*/
748number nfMapGGrev(number c, const coeffs src, const coeffs)
749{
750  int ex=(int)((long)c);
751  if ((ex % nfMapGG_factor)==0)
752    return (number)(((long)ex) / ((long)nfMapGG_factor));
753  else
754    return (number)(long)src->m_nfCharQ; /* 0 */
755}
756
757/*2
758* set map function nMap ... -> GF(p,n)
759*/
760nMapFunc nfSetMap(const coeffs src, const coeffs dst)
761{
762  if (nCoeff_is_GF(src,src->m_nfCharQ))
763  {
764    return ndCopyMap;   /* GF(p,n) -> GF(p,n) */
765  }
766  if (nCoeff_is_GF(src))
767  {
768    const coeffs r = dst;
769    int q=src->ch;
770    if ((src->m_nfCharQ % q)==0) /* GF(p,n1) -> GF(p,n2), n2 > n1 */
771    {
772      // check if n2 is a multiple of n1
773      int n1=1;
774      int qq=r->m_nfCharP;
775      while(qq!=q) { qq *= r->m_nfCharP; n1++; }
776      int n2=1;
777      qq=r->m_nfCharP;
778      while(qq!=src->m_nfCharQ) { qq *= r->m_nfCharP; n2++; }
779      //Print("map %d^%d -> %d^%d\n",r->m_nfCharP,n1,r->m_nfCharP,n2);
780      if ((n2 % n1)==0)
781      {
782        int save_ch=r->m_nfCharQ;
783        nfReadTable(src->m_nfCharQ, r);
784        int nn=r->m_nfPlus1Table[0];
785        nfReadTable(save_ch, r);
786        nfMapGG_factor= r->m_nfPlus1Table[0] / nn;
787        //Print("nfMapGG_factor=%d (%d / %d)\n",nfMapGG_factor, r->m_nfPlus1Table[0], nn);
788        return nfMapGG;
789      }
790      else if ((n1 % n2)==0)
791      {
792        nfMapGG_factor= (n1/n2);
793        return nfMapGGrev;
794      }
795      else
796        return NULL;
797    }
798  }
799  if (nCoeff_is_Zp(src,dst->m_nfCharP))
800  {
801    return nfMapP;    /* Z/p -> GF(p,n) */
802  }
803  return NULL;     /* default */
804}
805
806static BOOLEAN nfCoeffIsEqual(const coeffs, n_coeffType, void*);
807
808static void nfKillChar(coeffs r)
809{
810  char** p = (char**)n_ParameterNames(r);
811
812  const int P = n_NumberOfParameters(r);
813
814  for( int i = 1; i <= P; i++ )
815    if (p[i-1] != NULL)
816      omFree( (ADDRESS)p[i-1] );
817
818  omFreeSize((ADDRESS)p, P * sizeof(char*));
819}
820
821BOOLEAN nfInitChar(coeffs r,  void * parameter)
822{
823  //r->cfInitChar=npInitChar;
824  r->cfKillChar=nfKillChar;
825  r->nCoeffIsEqual=nfCoeffIsEqual;
826
827  r->cfMult  = nfMult;
828  r->cfSub   = nfSub;
829  r->cfAdd   = nfAdd;
830  r->cfDiv   = nfDiv;
831  r->cfIntDiv= nfDiv;
832  //r->cfIntMod= ndIntMod;
833  r->cfExactDiv= nfDiv;
834  r->cfInit = nfInit;
835  //r->cfSize  = ndSize;
836  r->cfInt  = nfInt;
837  #ifdef HAVE_RINGS
838  //r->cfDivComp = NULL; // only for ring stuff
839  //r->cfIsUnit = NULL; // only for ring stuff
840  //r->cfGetUnit = NULL; // only for ring stuff
841  //r->cfExtGcd = NULL; // only for ring stuff
842  // r->cfDivBy = NULL; // only for ring stuff
843  #endif
844  r->cfNeg   = nfNeg;
845  r->cfInvers= nfInvers;
846  //r->cfCopy  = ndCopy;
847  //r->cfRePart = ndCopy;
848  //r->cfImPart = ndReturn0;
849
850  r->cfWriteLong = nfWriteLong;
851  r->cfInit_bigint = nlModP;
852  r->cfRead = nfRead;
853  //r->cfNormalize=ndNormalize;
854  r->cfGreater = nfGreater;
855  r->cfEqual = nfEqual;
856  r->cfIsZero = nfIsZero;
857  r->cfIsOne = nfIsOne;
858  r->cfIsMOne = nfIsMOne;
859  r->cfGreaterZero = nfGreaterZero;
860  r->cfPower = nfPower;
861  //r->cfGcd  = ndGcd;
862  //r->cfLcm  = ndGcd;
863  //r->cfDelete= ndDelete;
864  r->cfSetMap = nfSetMap;
865  //r->cfName = ndName;
866  // debug stuff
867  r->cfCoeffWrite=nfCoeffWrite;
868
869  r->cfParDeg = nfParDeg;
870
871#ifdef LDEBUG
872  r->cfDBTest=nfDBTest;
873#endif
874
875  // the variables:
876  r->nNULL = (number)0;
877  assume( getCoeffType(r) == n_GF );
878
879  GFInfo* p = (GFInfo *)(parameter);
880  assume (p->GFChar > 0);
881  assume (p->GFDegree > 0);
882
883  const char * name = p->GFPar_name;
884
885  r->m_nfCharQ = 0;
886  r->m_nfCharP = p->GFChar;
887  r->m_nfCharQ1 = 0;
888
889  r->iNumberOfParameters = 1;
890  r->cfParameter = nfParameter;
891
892  char ** pParameterNames = (char **) omAlloc0(sizeof(char *));
893  pParameterNames[0] = omStrDup(name); //TODO use omAlloc for allocating memory and use strcpy?
894
895  assume( pParameterNames != NULL );
896  assume( pParameterNames[0] != NULL );
897
898  r->pParameterNames = pParameterNames;
899  // NOTE: r->m_nfParameter was replaced by n_ParameterNames(r)[0]
900
901  // TODO: nfKillChar MUST destroy r->pParameterNames[0] (0-term. string) && r->pParameterNames (array of size 1)
902
903  r->m_nfPlus1Table= NULL;
904
905  if (strlen(name) > 1)
906    r->cfWriteShort = nfWriteLong;
907  else
908    r->cfWriteShort = nfWriteShort;
909
910  r->has_simple_Alloc=TRUE;
911  r->has_simple_Inverse=TRUE;
912
913  if(p->GFChar > (2<<15))
914  {
915#ifndef NDEBUG
916    Warn("illegal characteristic");
917#endif
918    return TRUE;
919  }
920
921  const double check= log ((double) (p->GFChar));
922
923  if( (p->GFDegree * check) > sixteenlog2 )
924  {
925#ifndef NDEBUG
926    Warn("Sorry: illegal size: %u ^ %u", p->GFChar, p->GFDegree );
927#endif
928    return TRUE;
929  }
930
931  int c = pow (p->GFChar, p->GFDegree);
932
933  nfReadTable(c, r);
934
935  if( r->m_nfPlus1Table == NULL )
936  {
937#ifndef NDEBUG
938    Warn("Sorry: cannot init lookup table!");
939#endif
940    return TRUE;
941  }
942
943
944  assume (r -> m_nfCharQ > 0);
945
946  r->ch = r->m_nfCharP;
947  assume( r->m_nfPlus1Table != NULL );
948
949  return FALSE;
950
951}
952
953void    nfCoeffWrite  (const coeffs r, BOOLEAN details)
954{
955  // m_nfCharQ = p^k where p is the characteristic (r->CharP) and k is GFDegree
956  Print("//   # ground field : %d\n",r->m_nfCharQ);
957  Print("//   primitive element : %s\n", n_ParameterNames(r)[0]);
958  if ( details )
959  {
960    StringSetS("//   minpoly        : ");
961    nfShowMipo(r);
962    StringAppendS("\n");
963    char *s=StringEndS(); PrintS(s); omFree(s);
964  }
965  else PrintS("//   minpoly        : ...\n");
966}
967
968static BOOLEAN nfCoeffIsEqual (const coeffs r, n_coeffType n, void * parameter)
969{
970  if (n==n_GF) {
971    GFInfo* p = (GFInfo *)(parameter);
972    int c = pow (p->GFChar, p->GFDegree);
973    if ((c == r->m_nfCharQ) && (strcmp(n_ParameterNames(r)[0], p->GFPar_name) == 0))
974      return TRUE;
975  }
976  return FALSE;
977}
Note: See TracBrowser for help on using the repository browser.