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